]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/platform/x86/thinkpad_acpi.c
Merge branch 'for-4.11-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/tj...
[mirror_ubuntu-artful-kernel.git] / drivers / platform / x86 / thinkpad_acpi.c
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
3 *
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 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
22 */
23
24 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25
26 #define TPACPI_VERSION "0.25"
27 #define TPACPI_SYSFS_VERSION 0x020700
28
29 /*
30 * Changelog:
31 * 2007-10-20 changelog trimmed down
32 *
33 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * drivers/misc.
35 *
36 * 2006-11-22 0.13 new maintainer
37 * changelog now lives in git commit history, and will
38 * not be updated further in-file.
39 *
40 * 2005-03-17 0.11 support for 600e, 770x
41 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
42 *
43 * 2005-01-16 0.9 use MODULE_VERSION
44 * thanks to Henrik Brix Andersen <brix@gentoo.org>
45 * fix parameter passing on module loading
46 * thanks to Rusty Russell <rusty@rustcorp.com.au>
47 * thanks to Jim Radford <radford@blackbean.org>
48 * 2004-11-08 0.8 fix init error case, don't return from a macro
49 * thanks to Chris Wright <chrisw@osdl.org>
50 */
51
52 #include <linux/kernel.h>
53 #include <linux/module.h>
54 #include <linux/init.h>
55 #include <linux/types.h>
56 #include <linux/string.h>
57 #include <linux/list.h>
58 #include <linux/mutex.h>
59 #include <linux/sched.h>
60 #include <linux/kthread.h>
61 #include <linux/freezer.h>
62 #include <linux/delay.h>
63 #include <linux/slab.h>
64 #include <linux/nvram.h>
65 #include <linux/proc_fs.h>
66 #include <linux/seq_file.h>
67 #include <linux/sysfs.h>
68 #include <linux/backlight.h>
69 #include <linux/fb.h>
70 #include <linux/platform_device.h>
71 #include <linux/hwmon.h>
72 #include <linux/hwmon-sysfs.h>
73 #include <linux/input.h>
74 #include <linux/leds.h>
75 #include <linux/rfkill.h>
76 #include <linux/dmi.h>
77 #include <linux/jiffies.h>
78 #include <linux/workqueue.h>
79 #include <linux/acpi.h>
80 #include <linux/pci_ids.h>
81 #include <linux/thinkpad_acpi.h>
82 #include <sound/core.h>
83 #include <sound/control.h>
84 #include <sound/initval.h>
85 #include <linux/uaccess.h>
86 #include <acpi/video.h>
87
88 /* ThinkPad CMOS commands */
89 #define TP_CMOS_VOLUME_DOWN 0
90 #define TP_CMOS_VOLUME_UP 1
91 #define TP_CMOS_VOLUME_MUTE 2
92 #define TP_CMOS_BRIGHTNESS_UP 4
93 #define TP_CMOS_BRIGHTNESS_DOWN 5
94 #define TP_CMOS_THINKLIGHT_ON 12
95 #define TP_CMOS_THINKLIGHT_OFF 13
96
97 /* NVRAM Addresses */
98 enum tp_nvram_addr {
99 TP_NVRAM_ADDR_HK2 = 0x57,
100 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
101 TP_NVRAM_ADDR_VIDEO = 0x59,
102 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
103 TP_NVRAM_ADDR_MIXER = 0x60,
104 };
105
106 /* NVRAM bit masks */
107 enum {
108 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
109 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
110 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
111 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
112 TP_NVRAM_MASK_THINKLIGHT = 0x10,
113 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
114 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
115 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
116 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
117 TP_NVRAM_MASK_MUTE = 0x40,
118 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
119 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
120 TP_NVRAM_POS_LEVEL_VOLUME = 0,
121 };
122
123 /* Misc NVRAM-related */
124 enum {
125 TP_NVRAM_LEVEL_VOLUME_MAX = 14,
126 };
127
128 /* ACPI HIDs */
129 #define TPACPI_ACPI_IBM_HKEY_HID "IBM0068"
130 #define TPACPI_ACPI_LENOVO_HKEY_HID "LEN0068"
131 #define TPACPI_ACPI_LENOVO_HKEY_V2_HID "LEN0268"
132 #define TPACPI_ACPI_EC_HID "PNP0C09"
133
134 /* Input IDs */
135 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
136 #define TPACPI_HKEY_INPUT_VERSION 0x4101
137
138 /* ACPI \WGSV commands */
139 enum {
140 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
141 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
142 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
143 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
144 };
145
146 /* TP_ACPI_WGSV_GET_STATE bits */
147 enum {
148 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
149 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
150 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
151 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
152 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
153 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
154 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
155 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
156 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
157 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
158 };
159
160 /* HKEY events */
161 enum tpacpi_hkey_event_t {
162 /* Hotkey-related */
163 TP_HKEY_EV_HOTKEY_BASE = 0x1001, /* first hotkey (FN+F1) */
164 TP_HKEY_EV_BRGHT_UP = 0x1010, /* Brightness up */
165 TP_HKEY_EV_BRGHT_DOWN = 0x1011, /* Brightness down */
166 TP_HKEY_EV_KBD_LIGHT = 0x1012, /* Thinklight/kbd backlight */
167 TP_HKEY_EV_VOL_UP = 0x1015, /* Volume up or unmute */
168 TP_HKEY_EV_VOL_DOWN = 0x1016, /* Volume down or unmute */
169 TP_HKEY_EV_VOL_MUTE = 0x1017, /* Mixer output mute */
170
171 /* Reasons for waking up from S3/S4 */
172 TP_HKEY_EV_WKUP_S3_UNDOCK = 0x2304, /* undock requested, S3 */
173 TP_HKEY_EV_WKUP_S4_UNDOCK = 0x2404, /* undock requested, S4 */
174 TP_HKEY_EV_WKUP_S3_BAYEJ = 0x2305, /* bay ejection req, S3 */
175 TP_HKEY_EV_WKUP_S4_BAYEJ = 0x2405, /* bay ejection req, S4 */
176 TP_HKEY_EV_WKUP_S3_BATLOW = 0x2313, /* battery empty, S3 */
177 TP_HKEY_EV_WKUP_S4_BATLOW = 0x2413, /* battery empty, S4 */
178
179 /* Auto-sleep after eject request */
180 TP_HKEY_EV_BAYEJ_ACK = 0x3003, /* bay ejection complete */
181 TP_HKEY_EV_UNDOCK_ACK = 0x4003, /* undock complete */
182
183 /* Misc bay events */
184 TP_HKEY_EV_OPTDRV_EJ = 0x3006, /* opt. drive tray ejected */
185 TP_HKEY_EV_HOTPLUG_DOCK = 0x4010, /* docked into hotplug dock
186 or port replicator */
187 TP_HKEY_EV_HOTPLUG_UNDOCK = 0x4011, /* undocked from hotplug
188 dock or port replicator */
189
190 /* User-interface events */
191 TP_HKEY_EV_LID_CLOSE = 0x5001, /* laptop lid closed */
192 TP_HKEY_EV_LID_OPEN = 0x5002, /* laptop lid opened */
193 TP_HKEY_EV_TABLET_TABLET = 0x5009, /* tablet swivel up */
194 TP_HKEY_EV_TABLET_NOTEBOOK = 0x500a, /* tablet swivel down */
195 TP_HKEY_EV_TABLET_CHANGED = 0x60c0, /* X1 Yoga (2016):
196 * enter/leave tablet mode
197 */
198 TP_HKEY_EV_PEN_INSERTED = 0x500b, /* tablet pen inserted */
199 TP_HKEY_EV_PEN_REMOVED = 0x500c, /* tablet pen removed */
200 TP_HKEY_EV_BRGHT_CHANGED = 0x5010, /* backlight control event */
201
202 /* Key-related user-interface events */
203 TP_HKEY_EV_KEY_NUMLOCK = 0x6000, /* NumLock key pressed */
204 TP_HKEY_EV_KEY_FN = 0x6005, /* Fn key pressed? E420 */
205 TP_HKEY_EV_KEY_FN_ESC = 0x6060, /* Fn+Esc key pressed X240 */
206
207 /* Thermal events */
208 TP_HKEY_EV_ALARM_BAT_HOT = 0x6011, /* battery too hot */
209 TP_HKEY_EV_ALARM_BAT_XHOT = 0x6012, /* battery critically hot */
210 TP_HKEY_EV_ALARM_SENSOR_HOT = 0x6021, /* sensor too hot */
211 TP_HKEY_EV_ALARM_SENSOR_XHOT = 0x6022, /* sensor critically hot */
212 TP_HKEY_EV_THM_TABLE_CHANGED = 0x6030, /* thermal table changed */
213
214 /* AC-related events */
215 TP_HKEY_EV_AC_CHANGED = 0x6040, /* AC status changed */
216
217 /* Misc */
218 TP_HKEY_EV_RFKILL_CHANGED = 0x7000, /* rfkill switch changed */
219 };
220
221 /****************************************************************************
222 * Main driver
223 */
224
225 #define TPACPI_NAME "thinkpad"
226 #define TPACPI_DESC "ThinkPad ACPI Extras"
227 #define TPACPI_FILE TPACPI_NAME "_acpi"
228 #define TPACPI_URL "http://ibm-acpi.sf.net/"
229 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
230
231 #define TPACPI_PROC_DIR "ibm"
232 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
233 #define TPACPI_DRVR_NAME TPACPI_FILE
234 #define TPACPI_DRVR_SHORTNAME "tpacpi"
235 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
236
237 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
238 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
239
240 #define TPACPI_MAX_ACPI_ARGS 3
241
242 /* Debugging printk groups */
243 #define TPACPI_DBG_ALL 0xffff
244 #define TPACPI_DBG_DISCLOSETASK 0x8000
245 #define TPACPI_DBG_INIT 0x0001
246 #define TPACPI_DBG_EXIT 0x0002
247 #define TPACPI_DBG_RFKILL 0x0004
248 #define TPACPI_DBG_HKEY 0x0008
249 #define TPACPI_DBG_FAN 0x0010
250 #define TPACPI_DBG_BRGHT 0x0020
251 #define TPACPI_DBG_MIXER 0x0040
252
253 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
254 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
255 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
256
257
258 /****************************************************************************
259 * Driver-wide structs and misc. variables
260 */
261
262 struct ibm_struct;
263
264 struct tp_acpi_drv_struct {
265 const struct acpi_device_id *hid;
266 struct acpi_driver *driver;
267
268 void (*notify) (struct ibm_struct *, u32);
269 acpi_handle *handle;
270 u32 type;
271 struct acpi_device *device;
272 };
273
274 struct ibm_struct {
275 char *name;
276
277 int (*read) (struct seq_file *);
278 int (*write) (char *);
279 void (*exit) (void);
280 void (*resume) (void);
281 void (*suspend) (void);
282 void (*shutdown) (void);
283
284 struct list_head all_drivers;
285
286 struct tp_acpi_drv_struct *acpi;
287
288 struct {
289 u8 acpi_driver_registered:1;
290 u8 acpi_notify_installed:1;
291 u8 proc_created:1;
292 u8 init_called:1;
293 u8 experimental:1;
294 } flags;
295 };
296
297 struct ibm_init_struct {
298 char param[32];
299
300 int (*init) (struct ibm_init_struct *);
301 umode_t base_procfs_mode;
302 struct ibm_struct *data;
303 };
304
305 static struct {
306 u32 bluetooth:1;
307 u32 hotkey:1;
308 u32 hotkey_mask:1;
309 u32 hotkey_wlsw:1;
310 enum {
311 TP_HOTKEY_TABLET_NONE = 0,
312 TP_HOTKEY_TABLET_USES_MHKG,
313 /* X1 Yoga 2016, seen on BIOS N1FET44W */
314 TP_HOTKEY_TABLET_USES_CMMD,
315 } hotkey_tablet;
316 u32 kbdlight:1;
317 u32 light:1;
318 u32 light_status:1;
319 u32 bright_acpimode:1;
320 u32 bright_unkfw:1;
321 u32 wan:1;
322 u32 uwb:1;
323 u32 fan_ctrl_status_undef:1;
324 u32 second_fan:1;
325 u32 beep_needs_two_args:1;
326 u32 mixer_no_level_control:1;
327 u32 input_device_registered:1;
328 u32 platform_drv_registered:1;
329 u32 platform_drv_attrs_registered:1;
330 u32 sensors_pdrv_registered:1;
331 u32 sensors_pdrv_attrs_registered:1;
332 u32 sensors_pdev_attrs_registered:1;
333 u32 hotkey_poll_active:1;
334 u32 has_adaptive_kbd:1;
335 } tp_features;
336
337 static struct {
338 u16 hotkey_mask_ff:1;
339 u16 volume_ctrl_forbidden:1;
340 } tp_warned;
341
342 struct thinkpad_id_data {
343 unsigned int vendor; /* ThinkPad vendor:
344 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
345
346 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
347 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
348
349 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
350 u16 ec_model;
351 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
352 u16 ec_release;
353
354 char *model_str; /* ThinkPad T43 */
355 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
356 };
357 static struct thinkpad_id_data thinkpad_id;
358
359 static enum {
360 TPACPI_LIFE_INIT = 0,
361 TPACPI_LIFE_RUNNING,
362 TPACPI_LIFE_EXITING,
363 } tpacpi_lifecycle;
364
365 static int experimental;
366 static u32 dbg_level;
367
368 static struct workqueue_struct *tpacpi_wq;
369
370 enum led_status_t {
371 TPACPI_LED_OFF = 0,
372 TPACPI_LED_ON,
373 TPACPI_LED_BLINK,
374 };
375
376 /* tpacpi LED class */
377 struct tpacpi_led_classdev {
378 struct led_classdev led_classdev;
379 int led;
380 };
381
382 /* brightness level capabilities */
383 static unsigned int bright_maxlvl; /* 0 = unknown */
384
385 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
386 static int dbg_wlswemul;
387 static bool tpacpi_wlsw_emulstate;
388 static int dbg_bluetoothemul;
389 static bool tpacpi_bluetooth_emulstate;
390 static int dbg_wwanemul;
391 static bool tpacpi_wwan_emulstate;
392 static int dbg_uwbemul;
393 static bool tpacpi_uwb_emulstate;
394 #endif
395
396
397 /*************************************************************************
398 * Debugging helpers
399 */
400
401 #define dbg_printk(a_dbg_level, format, arg...) \
402 do { \
403 if (dbg_level & (a_dbg_level)) \
404 printk(KERN_DEBUG pr_fmt("%s: " format), \
405 __func__, ##arg); \
406 } while (0)
407
408 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
409 #define vdbg_printk dbg_printk
410 static const char *str_supported(int is_supported);
411 #else
412 static inline const char *str_supported(int is_supported) { return ""; }
413 #define vdbg_printk(a_dbg_level, format, arg...) \
414 do { if (0) no_printk(format, ##arg); } while (0)
415 #endif
416
417 static void tpacpi_log_usertask(const char * const what)
418 {
419 printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
420 what, task_tgid_vnr(current));
421 }
422
423 #define tpacpi_disclose_usertask(what, format, arg...) \
424 do { \
425 if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) && \
426 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
427 printk(KERN_DEBUG pr_fmt("%s: PID %d: " format), \
428 what, task_tgid_vnr(current), ## arg); \
429 } \
430 } while (0)
431
432 /*
433 * Quirk handling helpers
434 *
435 * ThinkPad IDs and versions seen in the field so far
436 * are two-characters from the set [0-9A-Z], i.e. base 36.
437 *
438 * We use values well outside that range as specials.
439 */
440
441 #define TPACPI_MATCH_ANY 0xffffU
442 #define TPACPI_MATCH_UNKNOWN 0U
443
444 /* TPID('1', 'Y') == 0x5931 */
445 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
446
447 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
448 { .vendor = PCI_VENDOR_ID_IBM, \
449 .bios = TPID(__id1, __id2), \
450 .ec = TPACPI_MATCH_ANY, \
451 .quirks = (__quirk) }
452
453 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
454 { .vendor = PCI_VENDOR_ID_LENOVO, \
455 .bios = TPID(__id1, __id2), \
456 .ec = TPACPI_MATCH_ANY, \
457 .quirks = (__quirk) }
458
459 #define TPACPI_QEC_LNV(__id1, __id2, __quirk) \
460 { .vendor = PCI_VENDOR_ID_LENOVO, \
461 .bios = TPACPI_MATCH_ANY, \
462 .ec = TPID(__id1, __id2), \
463 .quirks = (__quirk) }
464
465 struct tpacpi_quirk {
466 unsigned int vendor;
467 u16 bios;
468 u16 ec;
469 unsigned long quirks;
470 };
471
472 /**
473 * tpacpi_check_quirks() - search BIOS/EC version on a list
474 * @qlist: array of &struct tpacpi_quirk
475 * @qlist_size: number of elements in @qlist
476 *
477 * Iterates over a quirks list until one is found that matches the
478 * ThinkPad's vendor, BIOS and EC model.
479 *
480 * Returns 0 if nothing matches, otherwise returns the quirks field of
481 * the matching &struct tpacpi_quirk entry.
482 *
483 * The match criteria is: vendor, ec and bios much match.
484 */
485 static unsigned long __init tpacpi_check_quirks(
486 const struct tpacpi_quirk *qlist,
487 unsigned int qlist_size)
488 {
489 while (qlist_size) {
490 if ((qlist->vendor == thinkpad_id.vendor ||
491 qlist->vendor == TPACPI_MATCH_ANY) &&
492 (qlist->bios == thinkpad_id.bios_model ||
493 qlist->bios == TPACPI_MATCH_ANY) &&
494 (qlist->ec == thinkpad_id.ec_model ||
495 qlist->ec == TPACPI_MATCH_ANY))
496 return qlist->quirks;
497
498 qlist_size--;
499 qlist++;
500 }
501 return 0;
502 }
503
504 static inline bool __pure __init tpacpi_is_lenovo(void)
505 {
506 return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
507 }
508
509 static inline bool __pure __init tpacpi_is_ibm(void)
510 {
511 return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
512 }
513
514 /****************************************************************************
515 ****************************************************************************
516 *
517 * ACPI Helpers and device model
518 *
519 ****************************************************************************
520 ****************************************************************************/
521
522 /*************************************************************************
523 * ACPI basic handles
524 */
525
526 static acpi_handle root_handle;
527 static acpi_handle ec_handle;
528
529 #define TPACPI_HANDLE(object, parent, paths...) \
530 static acpi_handle object##_handle; \
531 static const acpi_handle * const object##_parent __initconst = \
532 &parent##_handle; \
533 static char *object##_paths[] __initdata = { paths }
534
535 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
536 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
537
538 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
539 /* T4x, X31, X40 */
540 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
541 "\\CMS", /* R40, R40e */
542 ); /* all others */
543
544 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
545 "^HKEY", /* R30, R31 */
546 "HKEY", /* all others */
547 ); /* 570 */
548
549 /*************************************************************************
550 * ACPI helpers
551 */
552
553 static int acpi_evalf(acpi_handle handle,
554 int *res, char *method, char *fmt, ...)
555 {
556 char *fmt0 = fmt;
557 struct acpi_object_list params;
558 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
559 struct acpi_buffer result, *resultp;
560 union acpi_object out_obj;
561 acpi_status status;
562 va_list ap;
563 char res_type;
564 int success;
565 int quiet;
566
567 if (!*fmt) {
568 pr_err("acpi_evalf() called with empty format\n");
569 return 0;
570 }
571
572 if (*fmt == 'q') {
573 quiet = 1;
574 fmt++;
575 } else
576 quiet = 0;
577
578 res_type = *(fmt++);
579
580 params.count = 0;
581 params.pointer = &in_objs[0];
582
583 va_start(ap, fmt);
584 while (*fmt) {
585 char c = *(fmt++);
586 switch (c) {
587 case 'd': /* int */
588 in_objs[params.count].integer.value = va_arg(ap, int);
589 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
590 break;
591 /* add more types as needed */
592 default:
593 pr_err("acpi_evalf() called "
594 "with invalid format character '%c'\n", c);
595 va_end(ap);
596 return 0;
597 }
598 }
599 va_end(ap);
600
601 if (res_type != 'v') {
602 result.length = sizeof(out_obj);
603 result.pointer = &out_obj;
604 resultp = &result;
605 } else
606 resultp = NULL;
607
608 status = acpi_evaluate_object(handle, method, &params, resultp);
609
610 switch (res_type) {
611 case 'd': /* int */
612 success = (status == AE_OK &&
613 out_obj.type == ACPI_TYPE_INTEGER);
614 if (success && res)
615 *res = out_obj.integer.value;
616 break;
617 case 'v': /* void */
618 success = status == AE_OK;
619 break;
620 /* add more types as needed */
621 default:
622 pr_err("acpi_evalf() called "
623 "with invalid format character '%c'\n", res_type);
624 return 0;
625 }
626
627 if (!success && !quiet)
628 pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
629 method, fmt0, acpi_format_exception(status));
630
631 return success;
632 }
633
634 static int acpi_ec_read(int i, u8 *p)
635 {
636 int v;
637
638 if (ecrd_handle) {
639 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
640 return 0;
641 *p = v;
642 } else {
643 if (ec_read(i, p) < 0)
644 return 0;
645 }
646
647 return 1;
648 }
649
650 static int acpi_ec_write(int i, u8 v)
651 {
652 if (ecwr_handle) {
653 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
654 return 0;
655 } else {
656 if (ec_write(i, v) < 0)
657 return 0;
658 }
659
660 return 1;
661 }
662
663 static int issue_thinkpad_cmos_command(int cmos_cmd)
664 {
665 if (!cmos_handle)
666 return -ENXIO;
667
668 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
669 return -EIO;
670
671 return 0;
672 }
673
674 /*************************************************************************
675 * ACPI device model
676 */
677
678 #define TPACPI_ACPIHANDLE_INIT(object) \
679 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
680 object##_paths, ARRAY_SIZE(object##_paths))
681
682 static void __init drv_acpi_handle_init(const char *name,
683 acpi_handle *handle, const acpi_handle parent,
684 char **paths, const int num_paths)
685 {
686 int i;
687 acpi_status status;
688
689 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
690 name);
691
692 for (i = 0; i < num_paths; i++) {
693 status = acpi_get_handle(parent, paths[i], handle);
694 if (ACPI_SUCCESS(status)) {
695 dbg_printk(TPACPI_DBG_INIT,
696 "Found ACPI handle %s for %s\n",
697 paths[i], name);
698 return;
699 }
700 }
701
702 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
703 name);
704 *handle = NULL;
705 }
706
707 static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
708 u32 level, void *context, void **return_value)
709 {
710 struct acpi_device *dev;
711 if (!strcmp(context, "video")) {
712 if (acpi_bus_get_device(handle, &dev))
713 return AE_OK;
714 if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
715 return AE_OK;
716 }
717
718 *(acpi_handle *)return_value = handle;
719
720 return AE_CTRL_TERMINATE;
721 }
722
723 static void __init tpacpi_acpi_handle_locate(const char *name,
724 const char *hid,
725 acpi_handle *handle)
726 {
727 acpi_status status;
728 acpi_handle device_found;
729
730 BUG_ON(!name || !handle);
731 vdbg_printk(TPACPI_DBG_INIT,
732 "trying to locate ACPI handle for %s, using HID %s\n",
733 name, hid ? hid : "NULL");
734
735 memset(&device_found, 0, sizeof(device_found));
736 status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
737 (void *)name, &device_found);
738
739 *handle = NULL;
740
741 if (ACPI_SUCCESS(status)) {
742 *handle = device_found;
743 dbg_printk(TPACPI_DBG_INIT,
744 "Found ACPI handle for %s\n", name);
745 } else {
746 vdbg_printk(TPACPI_DBG_INIT,
747 "Could not locate an ACPI handle for %s: %s\n",
748 name, acpi_format_exception(status));
749 }
750 }
751
752 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
753 {
754 struct ibm_struct *ibm = data;
755
756 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
757 return;
758
759 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
760 return;
761
762 ibm->acpi->notify(ibm, event);
763 }
764
765 static int __init setup_acpi_notify(struct ibm_struct *ibm)
766 {
767 acpi_status status;
768 int rc;
769
770 BUG_ON(!ibm->acpi);
771
772 if (!*ibm->acpi->handle)
773 return 0;
774
775 vdbg_printk(TPACPI_DBG_INIT,
776 "setting up ACPI notify for %s\n", ibm->name);
777
778 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
779 if (rc < 0) {
780 pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
781 return -ENODEV;
782 }
783
784 ibm->acpi->device->driver_data = ibm;
785 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
786 TPACPI_ACPI_EVENT_PREFIX,
787 ibm->name);
788
789 status = acpi_install_notify_handler(*ibm->acpi->handle,
790 ibm->acpi->type, dispatch_acpi_notify, ibm);
791 if (ACPI_FAILURE(status)) {
792 if (status == AE_ALREADY_EXISTS) {
793 pr_notice("another device driver is already "
794 "handling %s events\n", ibm->name);
795 } else {
796 pr_err("acpi_install_notify_handler(%s) failed: %s\n",
797 ibm->name, acpi_format_exception(status));
798 }
799 return -ENODEV;
800 }
801 ibm->flags.acpi_notify_installed = 1;
802 return 0;
803 }
804
805 static int __init tpacpi_device_add(struct acpi_device *device)
806 {
807 return 0;
808 }
809
810 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
811 {
812 int rc;
813
814 dbg_printk(TPACPI_DBG_INIT,
815 "registering %s as an ACPI driver\n", ibm->name);
816
817 BUG_ON(!ibm->acpi);
818
819 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
820 if (!ibm->acpi->driver) {
821 pr_err("failed to allocate memory for ibm->acpi->driver\n");
822 return -ENOMEM;
823 }
824
825 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
826 ibm->acpi->driver->ids = ibm->acpi->hid;
827
828 ibm->acpi->driver->ops.add = &tpacpi_device_add;
829
830 rc = acpi_bus_register_driver(ibm->acpi->driver);
831 if (rc < 0) {
832 pr_err("acpi_bus_register_driver(%s) failed: %d\n",
833 ibm->name, rc);
834 kfree(ibm->acpi->driver);
835 ibm->acpi->driver = NULL;
836 } else if (!rc)
837 ibm->flags.acpi_driver_registered = 1;
838
839 return rc;
840 }
841
842
843 /****************************************************************************
844 ****************************************************************************
845 *
846 * Procfs Helpers
847 *
848 ****************************************************************************
849 ****************************************************************************/
850
851 static int dispatch_proc_show(struct seq_file *m, void *v)
852 {
853 struct ibm_struct *ibm = m->private;
854
855 if (!ibm || !ibm->read)
856 return -EINVAL;
857 return ibm->read(m);
858 }
859
860 static int dispatch_proc_open(struct inode *inode, struct file *file)
861 {
862 return single_open(file, dispatch_proc_show, PDE_DATA(inode));
863 }
864
865 static ssize_t dispatch_proc_write(struct file *file,
866 const char __user *userbuf,
867 size_t count, loff_t *pos)
868 {
869 struct ibm_struct *ibm = PDE_DATA(file_inode(file));
870 char *kernbuf;
871 int ret;
872
873 if (!ibm || !ibm->write)
874 return -EINVAL;
875 if (count > PAGE_SIZE - 2)
876 return -EINVAL;
877
878 kernbuf = kmalloc(count + 2, GFP_KERNEL);
879 if (!kernbuf)
880 return -ENOMEM;
881
882 if (copy_from_user(kernbuf, userbuf, count)) {
883 kfree(kernbuf);
884 return -EFAULT;
885 }
886
887 kernbuf[count] = 0;
888 strcat(kernbuf, ",");
889 ret = ibm->write(kernbuf);
890 if (ret == 0)
891 ret = count;
892
893 kfree(kernbuf);
894
895 return ret;
896 }
897
898 static const struct file_operations dispatch_proc_fops = {
899 .owner = THIS_MODULE,
900 .open = dispatch_proc_open,
901 .read = seq_read,
902 .llseek = seq_lseek,
903 .release = single_release,
904 .write = dispatch_proc_write,
905 };
906
907 static char *next_cmd(char **cmds)
908 {
909 char *start = *cmds;
910 char *end;
911
912 while ((end = strchr(start, ',')) && end == start)
913 start = end + 1;
914
915 if (!end)
916 return NULL;
917
918 *end = 0;
919 *cmds = end + 1;
920 return start;
921 }
922
923
924 /****************************************************************************
925 ****************************************************************************
926 *
927 * Device model: input, hwmon and platform
928 *
929 ****************************************************************************
930 ****************************************************************************/
931
932 static struct platform_device *tpacpi_pdev;
933 static struct platform_device *tpacpi_sensors_pdev;
934 static struct device *tpacpi_hwmon;
935 static struct input_dev *tpacpi_inputdev;
936 static struct mutex tpacpi_inputdev_send_mutex;
937 static LIST_HEAD(tpacpi_all_drivers);
938
939 #ifdef CONFIG_PM_SLEEP
940 static int tpacpi_suspend_handler(struct device *dev)
941 {
942 struct ibm_struct *ibm, *itmp;
943
944 list_for_each_entry_safe(ibm, itmp,
945 &tpacpi_all_drivers,
946 all_drivers) {
947 if (ibm->suspend)
948 (ibm->suspend)();
949 }
950
951 return 0;
952 }
953
954 static int tpacpi_resume_handler(struct device *dev)
955 {
956 struct ibm_struct *ibm, *itmp;
957
958 list_for_each_entry_safe(ibm, itmp,
959 &tpacpi_all_drivers,
960 all_drivers) {
961 if (ibm->resume)
962 (ibm->resume)();
963 }
964
965 return 0;
966 }
967 #endif
968
969 static SIMPLE_DEV_PM_OPS(tpacpi_pm,
970 tpacpi_suspend_handler, tpacpi_resume_handler);
971
972 static void tpacpi_shutdown_handler(struct platform_device *pdev)
973 {
974 struct ibm_struct *ibm, *itmp;
975
976 list_for_each_entry_safe(ibm, itmp,
977 &tpacpi_all_drivers,
978 all_drivers) {
979 if (ibm->shutdown)
980 (ibm->shutdown)();
981 }
982 }
983
984 static struct platform_driver tpacpi_pdriver = {
985 .driver = {
986 .name = TPACPI_DRVR_NAME,
987 .pm = &tpacpi_pm,
988 },
989 .shutdown = tpacpi_shutdown_handler,
990 };
991
992 static struct platform_driver tpacpi_hwmon_pdriver = {
993 .driver = {
994 .name = TPACPI_HWMON_DRVR_NAME,
995 },
996 };
997
998 /*************************************************************************
999 * sysfs support helpers
1000 */
1001
1002 struct attribute_set {
1003 unsigned int members, max_members;
1004 struct attribute_group group;
1005 };
1006
1007 struct attribute_set_obj {
1008 struct attribute_set s;
1009 struct attribute *a;
1010 } __attribute__((packed));
1011
1012 static struct attribute_set *create_attr_set(unsigned int max_members,
1013 const char *name)
1014 {
1015 struct attribute_set_obj *sobj;
1016
1017 if (max_members == 0)
1018 return NULL;
1019
1020 /* Allocates space for implicit NULL at the end too */
1021 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1022 max_members * sizeof(struct attribute *),
1023 GFP_KERNEL);
1024 if (!sobj)
1025 return NULL;
1026 sobj->s.max_members = max_members;
1027 sobj->s.group.attrs = &sobj->a;
1028 sobj->s.group.name = name;
1029
1030 return &sobj->s;
1031 }
1032
1033 #define destroy_attr_set(_set) \
1034 kfree(_set);
1035
1036 /* not multi-threaded safe, use it in a single thread per set */
1037 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
1038 {
1039 if (!s || !attr)
1040 return -EINVAL;
1041
1042 if (s->members >= s->max_members)
1043 return -ENOMEM;
1044
1045 s->group.attrs[s->members] = attr;
1046 s->members++;
1047
1048 return 0;
1049 }
1050
1051 static int add_many_to_attr_set(struct attribute_set *s,
1052 struct attribute **attr,
1053 unsigned int count)
1054 {
1055 int i, res;
1056
1057 for (i = 0; i < count; i++) {
1058 res = add_to_attr_set(s, attr[i]);
1059 if (res)
1060 return res;
1061 }
1062
1063 return 0;
1064 }
1065
1066 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
1067 {
1068 sysfs_remove_group(kobj, &s->group);
1069 destroy_attr_set(s);
1070 }
1071
1072 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
1073 sysfs_create_group(_kobj, &_attr_set->group)
1074
1075 static int parse_strtoul(const char *buf,
1076 unsigned long max, unsigned long *value)
1077 {
1078 char *endp;
1079
1080 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
1081 endp = skip_spaces(endp);
1082 if (*endp || *value > max)
1083 return -EINVAL;
1084
1085 return 0;
1086 }
1087
1088 static void tpacpi_disable_brightness_delay(void)
1089 {
1090 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
1091 pr_notice("ACPI backlight control delay disabled\n");
1092 }
1093
1094 static void printk_deprecated_attribute(const char * const what,
1095 const char * const details)
1096 {
1097 tpacpi_log_usertask("deprecated sysfs attribute");
1098 pr_warn("WARNING: sysfs attribute %s is deprecated and "
1099 "will be removed. %s\n",
1100 what, details);
1101 }
1102
1103 /*************************************************************************
1104 * rfkill and radio control support helpers
1105 */
1106
1107 /*
1108 * ThinkPad-ACPI firmware handling model:
1109 *
1110 * WLSW (master wireless switch) is event-driven, and is common to all
1111 * firmware-controlled radios. It cannot be controlled, just monitored,
1112 * as expected. It overrides all radio state in firmware
1113 *
1114 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1115 * (TODO: verify how WLSW interacts with the returned radio state).
1116 *
1117 * The only time there are shadow radio state changes, is when
1118 * masked-off hotkeys are used.
1119 */
1120
1121 /*
1122 * Internal driver API for radio state:
1123 *
1124 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1125 * bool: true means radio blocked (off)
1126 */
1127 enum tpacpi_rfkill_state {
1128 TPACPI_RFK_RADIO_OFF = 0,
1129 TPACPI_RFK_RADIO_ON
1130 };
1131
1132 /* rfkill switches */
1133 enum tpacpi_rfk_id {
1134 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1135 TPACPI_RFK_WWAN_SW_ID,
1136 TPACPI_RFK_UWB_SW_ID,
1137 TPACPI_RFK_SW_MAX
1138 };
1139
1140 static const char *tpacpi_rfkill_names[] = {
1141 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1142 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1143 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1144 [TPACPI_RFK_SW_MAX] = NULL
1145 };
1146
1147 /* ThinkPad-ACPI rfkill subdriver */
1148 struct tpacpi_rfk {
1149 struct rfkill *rfkill;
1150 enum tpacpi_rfk_id id;
1151 const struct tpacpi_rfk_ops *ops;
1152 };
1153
1154 struct tpacpi_rfk_ops {
1155 /* firmware interface */
1156 int (*get_status)(void);
1157 int (*set_status)(const enum tpacpi_rfkill_state);
1158 };
1159
1160 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1161
1162 /* Query FW and update rfkill sw state for a given rfkill switch */
1163 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1164 {
1165 int status;
1166
1167 if (!tp_rfk)
1168 return -ENODEV;
1169
1170 status = (tp_rfk->ops->get_status)();
1171 if (status < 0)
1172 return status;
1173
1174 rfkill_set_sw_state(tp_rfk->rfkill,
1175 (status == TPACPI_RFK_RADIO_OFF));
1176
1177 return status;
1178 }
1179
1180 /* Query FW and update rfkill sw state for all rfkill switches */
1181 static void tpacpi_rfk_update_swstate_all(void)
1182 {
1183 unsigned int i;
1184
1185 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1186 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1187 }
1188
1189 /*
1190 * Sync the HW-blocking state of all rfkill switches,
1191 * do notice it causes the rfkill core to schedule uevents
1192 */
1193 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1194 {
1195 unsigned int i;
1196 struct tpacpi_rfk *tp_rfk;
1197
1198 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1199 tp_rfk = tpacpi_rfkill_switches[i];
1200 if (tp_rfk) {
1201 if (rfkill_set_hw_state(tp_rfk->rfkill,
1202 blocked)) {
1203 /* ignore -- we track sw block */
1204 }
1205 }
1206 }
1207 }
1208
1209 /* Call to get the WLSW state from the firmware */
1210 static int hotkey_get_wlsw(void);
1211
1212 /* Call to query WLSW state and update all rfkill switches */
1213 static bool tpacpi_rfk_check_hwblock_state(void)
1214 {
1215 int res = hotkey_get_wlsw();
1216 int hw_blocked;
1217
1218 /* When unknown or unsupported, we have to assume it is unblocked */
1219 if (res < 0)
1220 return false;
1221
1222 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1223 tpacpi_rfk_update_hwblock_state(hw_blocked);
1224
1225 return hw_blocked;
1226 }
1227
1228 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1229 {
1230 struct tpacpi_rfk *tp_rfk = data;
1231 int res;
1232
1233 dbg_printk(TPACPI_DBG_RFKILL,
1234 "request to change radio state to %s\n",
1235 blocked ? "blocked" : "unblocked");
1236
1237 /* try to set radio state */
1238 res = (tp_rfk->ops->set_status)(blocked ?
1239 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1240
1241 /* and update the rfkill core with whatever the FW really did */
1242 tpacpi_rfk_update_swstate(tp_rfk);
1243
1244 return (res < 0) ? res : 0;
1245 }
1246
1247 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1248 .set_block = tpacpi_rfk_hook_set_block,
1249 };
1250
1251 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1252 const struct tpacpi_rfk_ops *tp_rfkops,
1253 const enum rfkill_type rfktype,
1254 const char *name,
1255 const bool set_default)
1256 {
1257 struct tpacpi_rfk *atp_rfk;
1258 int res;
1259 bool sw_state = false;
1260 bool hw_state;
1261 int sw_status;
1262
1263 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1264
1265 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1266 if (atp_rfk)
1267 atp_rfk->rfkill = rfkill_alloc(name,
1268 &tpacpi_pdev->dev,
1269 rfktype,
1270 &tpacpi_rfk_rfkill_ops,
1271 atp_rfk);
1272 if (!atp_rfk || !atp_rfk->rfkill) {
1273 pr_err("failed to allocate memory for rfkill class\n");
1274 kfree(atp_rfk);
1275 return -ENOMEM;
1276 }
1277
1278 atp_rfk->id = id;
1279 atp_rfk->ops = tp_rfkops;
1280
1281 sw_status = (tp_rfkops->get_status)();
1282 if (sw_status < 0) {
1283 pr_err("failed to read initial state for %s, error %d\n",
1284 name, sw_status);
1285 } else {
1286 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1287 if (set_default) {
1288 /* try to keep the initial state, since we ask the
1289 * firmware to preserve it across S5 in NVRAM */
1290 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1291 }
1292 }
1293 hw_state = tpacpi_rfk_check_hwblock_state();
1294 rfkill_set_hw_state(atp_rfk->rfkill, hw_state);
1295
1296 res = rfkill_register(atp_rfk->rfkill);
1297 if (res < 0) {
1298 pr_err("failed to register %s rfkill switch: %d\n", name, res);
1299 rfkill_destroy(atp_rfk->rfkill);
1300 kfree(atp_rfk);
1301 return res;
1302 }
1303
1304 tpacpi_rfkill_switches[id] = atp_rfk;
1305
1306 pr_info("rfkill switch %s: radio is %sblocked\n",
1307 name, (sw_state || hw_state) ? "" : "un");
1308 return 0;
1309 }
1310
1311 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1312 {
1313 struct tpacpi_rfk *tp_rfk;
1314
1315 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1316
1317 tp_rfk = tpacpi_rfkill_switches[id];
1318 if (tp_rfk) {
1319 rfkill_unregister(tp_rfk->rfkill);
1320 rfkill_destroy(tp_rfk->rfkill);
1321 tpacpi_rfkill_switches[id] = NULL;
1322 kfree(tp_rfk);
1323 }
1324 }
1325
1326 static void printk_deprecated_rfkill_attribute(const char * const what)
1327 {
1328 printk_deprecated_attribute(what,
1329 "Please switch to generic rfkill before year 2010");
1330 }
1331
1332 /* sysfs <radio> enable ------------------------------------------------ */
1333 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1334 struct device_attribute *attr,
1335 char *buf)
1336 {
1337 int status;
1338
1339 printk_deprecated_rfkill_attribute(attr->attr.name);
1340
1341 /* This is in the ABI... */
1342 if (tpacpi_rfk_check_hwblock_state()) {
1343 status = TPACPI_RFK_RADIO_OFF;
1344 } else {
1345 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1346 if (status < 0)
1347 return status;
1348 }
1349
1350 return snprintf(buf, PAGE_SIZE, "%d\n",
1351 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1352 }
1353
1354 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1355 struct device_attribute *attr,
1356 const char *buf, size_t count)
1357 {
1358 unsigned long t;
1359 int res;
1360
1361 printk_deprecated_rfkill_attribute(attr->attr.name);
1362
1363 if (parse_strtoul(buf, 1, &t))
1364 return -EINVAL;
1365
1366 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1367
1368 /* This is in the ABI... */
1369 if (tpacpi_rfk_check_hwblock_state() && !!t)
1370 return -EPERM;
1371
1372 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1373 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1374 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1375
1376 return (res < 0) ? res : count;
1377 }
1378
1379 /* procfs -------------------------------------------------------------- */
1380 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, struct seq_file *m)
1381 {
1382 if (id >= TPACPI_RFK_SW_MAX)
1383 seq_printf(m, "status:\t\tnot supported\n");
1384 else {
1385 int status;
1386
1387 /* This is in the ABI... */
1388 if (tpacpi_rfk_check_hwblock_state()) {
1389 status = TPACPI_RFK_RADIO_OFF;
1390 } else {
1391 status = tpacpi_rfk_update_swstate(
1392 tpacpi_rfkill_switches[id]);
1393 if (status < 0)
1394 return status;
1395 }
1396
1397 seq_printf(m, "status:\t\t%s\n",
1398 (status == TPACPI_RFK_RADIO_ON) ?
1399 "enabled" : "disabled");
1400 seq_printf(m, "commands:\tenable, disable\n");
1401 }
1402
1403 return 0;
1404 }
1405
1406 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1407 {
1408 char *cmd;
1409 int status = -1;
1410 int res = 0;
1411
1412 if (id >= TPACPI_RFK_SW_MAX)
1413 return -ENODEV;
1414
1415 while ((cmd = next_cmd(&buf))) {
1416 if (strlencmp(cmd, "enable") == 0)
1417 status = TPACPI_RFK_RADIO_ON;
1418 else if (strlencmp(cmd, "disable") == 0)
1419 status = TPACPI_RFK_RADIO_OFF;
1420 else
1421 return -EINVAL;
1422 }
1423
1424 if (status != -1) {
1425 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1426 (status == TPACPI_RFK_RADIO_ON) ?
1427 "enable" : "disable",
1428 tpacpi_rfkill_names[id]);
1429 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1430 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1431 }
1432
1433 return res;
1434 }
1435
1436 /*************************************************************************
1437 * thinkpad-acpi driver attributes
1438 */
1439
1440 /* interface_version --------------------------------------------------- */
1441 static ssize_t tpacpi_driver_interface_version_show(
1442 struct device_driver *drv,
1443 char *buf)
1444 {
1445 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1446 }
1447
1448 static DRIVER_ATTR(interface_version, S_IRUGO,
1449 tpacpi_driver_interface_version_show, NULL);
1450
1451 /* debug_level --------------------------------------------------------- */
1452 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1453 char *buf)
1454 {
1455 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1456 }
1457
1458 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1460 {
1461 unsigned long t;
1462
1463 if (parse_strtoul(buf, 0xffff, &t))
1464 return -EINVAL;
1465
1466 dbg_level = t;
1467
1468 return count;
1469 }
1470
1471 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1472 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1473
1474 /* version ------------------------------------------------------------- */
1475 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1476 char *buf)
1477 {
1478 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1479 TPACPI_DESC, TPACPI_VERSION);
1480 }
1481
1482 static DRIVER_ATTR(version, S_IRUGO,
1483 tpacpi_driver_version_show, NULL);
1484
1485 /* --------------------------------------------------------------------- */
1486
1487 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1488
1489 /* wlsw_emulstate ------------------------------------------------------ */
1490 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1491 char *buf)
1492 {
1493 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1494 }
1495
1496 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1497 const char *buf, size_t count)
1498 {
1499 unsigned long t;
1500
1501 if (parse_strtoul(buf, 1, &t))
1502 return -EINVAL;
1503
1504 if (tpacpi_wlsw_emulstate != !!t) {
1505 tpacpi_wlsw_emulstate = !!t;
1506 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1507 }
1508
1509 return count;
1510 }
1511
1512 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1513 tpacpi_driver_wlsw_emulstate_show,
1514 tpacpi_driver_wlsw_emulstate_store);
1515
1516 /* bluetooth_emulstate ------------------------------------------------- */
1517 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1518 struct device_driver *drv,
1519 char *buf)
1520 {
1521 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1522 }
1523
1524 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1525 struct device_driver *drv,
1526 const char *buf, size_t count)
1527 {
1528 unsigned long t;
1529
1530 if (parse_strtoul(buf, 1, &t))
1531 return -EINVAL;
1532
1533 tpacpi_bluetooth_emulstate = !!t;
1534
1535 return count;
1536 }
1537
1538 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1539 tpacpi_driver_bluetooth_emulstate_show,
1540 tpacpi_driver_bluetooth_emulstate_store);
1541
1542 /* wwan_emulstate ------------------------------------------------- */
1543 static ssize_t tpacpi_driver_wwan_emulstate_show(
1544 struct device_driver *drv,
1545 char *buf)
1546 {
1547 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1548 }
1549
1550 static ssize_t tpacpi_driver_wwan_emulstate_store(
1551 struct device_driver *drv,
1552 const char *buf, size_t count)
1553 {
1554 unsigned long t;
1555
1556 if (parse_strtoul(buf, 1, &t))
1557 return -EINVAL;
1558
1559 tpacpi_wwan_emulstate = !!t;
1560
1561 return count;
1562 }
1563
1564 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1565 tpacpi_driver_wwan_emulstate_show,
1566 tpacpi_driver_wwan_emulstate_store);
1567
1568 /* uwb_emulstate ------------------------------------------------- */
1569 static ssize_t tpacpi_driver_uwb_emulstate_show(
1570 struct device_driver *drv,
1571 char *buf)
1572 {
1573 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1574 }
1575
1576 static ssize_t tpacpi_driver_uwb_emulstate_store(
1577 struct device_driver *drv,
1578 const char *buf, size_t count)
1579 {
1580 unsigned long t;
1581
1582 if (parse_strtoul(buf, 1, &t))
1583 return -EINVAL;
1584
1585 tpacpi_uwb_emulstate = !!t;
1586
1587 return count;
1588 }
1589
1590 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1591 tpacpi_driver_uwb_emulstate_show,
1592 tpacpi_driver_uwb_emulstate_store);
1593 #endif
1594
1595 /* --------------------------------------------------------------------- */
1596
1597 static struct driver_attribute *tpacpi_driver_attributes[] = {
1598 &driver_attr_debug_level, &driver_attr_version,
1599 &driver_attr_interface_version,
1600 };
1601
1602 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1603 {
1604 int i, res;
1605
1606 i = 0;
1607 res = 0;
1608 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1609 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1610 i++;
1611 }
1612
1613 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1614 if (!res && dbg_wlswemul)
1615 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1616 if (!res && dbg_bluetoothemul)
1617 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1618 if (!res && dbg_wwanemul)
1619 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1620 if (!res && dbg_uwbemul)
1621 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1622 #endif
1623
1624 return res;
1625 }
1626
1627 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1628 {
1629 int i;
1630
1631 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1632 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1633
1634 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1635 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1636 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1637 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1638 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1639 #endif
1640 }
1641
1642 /*************************************************************************
1643 * Firmware Data
1644 */
1645
1646 /*
1647 * Table of recommended minimum BIOS versions
1648 *
1649 * Reasons for listing:
1650 * 1. Stable BIOS, listed because the unknown amount of
1651 * bugs and bad ACPI behaviour on older versions
1652 *
1653 * 2. BIOS or EC fw with known bugs that trigger on Linux
1654 *
1655 * 3. BIOS with known reduced functionality in older versions
1656 *
1657 * We recommend the latest BIOS and EC version.
1658 * We only support the latest BIOS and EC fw version as a rule.
1659 *
1660 * Sources: IBM ThinkPad Public Web Documents (update changelogs),
1661 * Information from users in ThinkWiki
1662 *
1663 * WARNING: we use this table also to detect that the machine is
1664 * a ThinkPad in some cases, so don't remove entries lightly.
1665 */
1666
1667 #define TPV_Q(__v, __id1, __id2, __bv1, __bv2) \
1668 { .vendor = (__v), \
1669 .bios = TPID(__id1, __id2), \
1670 .ec = TPACPI_MATCH_ANY, \
1671 .quirks = TPACPI_MATCH_ANY << 16 \
1672 | (__bv1) << 8 | (__bv2) }
1673
1674 #define TPV_Q_X(__v, __bid1, __bid2, __bv1, __bv2, \
1675 __eid, __ev1, __ev2) \
1676 { .vendor = (__v), \
1677 .bios = TPID(__bid1, __bid2), \
1678 .ec = __eid, \
1679 .quirks = (__ev1) << 24 | (__ev2) << 16 \
1680 | (__bv1) << 8 | (__bv2) }
1681
1682 #define TPV_QI0(__id1, __id2, __bv1, __bv2) \
1683 TPV_Q(PCI_VENDOR_ID_IBM, __id1, __id2, __bv1, __bv2)
1684
1685 /* Outdated IBM BIOSes often lack the EC id string */
1686 #define TPV_QI1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1687 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1688 __bv1, __bv2, TPID(__id1, __id2), \
1689 __ev1, __ev2), \
1690 TPV_Q_X(PCI_VENDOR_ID_IBM, __id1, __id2, \
1691 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1692 __ev1, __ev2)
1693
1694 /* Outdated IBM BIOSes often lack the EC id string */
1695 #define TPV_QI2(__bid1, __bid2, __bv1, __bv2, \
1696 __eid1, __eid2, __ev1, __ev2) \
1697 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1698 __bv1, __bv2, TPID(__eid1, __eid2), \
1699 __ev1, __ev2), \
1700 TPV_Q_X(PCI_VENDOR_ID_IBM, __bid1, __bid2, \
1701 __bv1, __bv2, TPACPI_MATCH_UNKNOWN, \
1702 __ev1, __ev2)
1703
1704 #define TPV_QL0(__id1, __id2, __bv1, __bv2) \
1705 TPV_Q(PCI_VENDOR_ID_LENOVO, __id1, __id2, __bv1, __bv2)
1706
1707 #define TPV_QL1(__id1, __id2, __bv1, __bv2, __ev1, __ev2) \
1708 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __id1, __id2, \
1709 __bv1, __bv2, TPID(__id1, __id2), \
1710 __ev1, __ev2)
1711
1712 #define TPV_QL2(__bid1, __bid2, __bv1, __bv2, \
1713 __eid1, __eid2, __ev1, __ev2) \
1714 TPV_Q_X(PCI_VENDOR_ID_LENOVO, __bid1, __bid2, \
1715 __bv1, __bv2, TPID(__eid1, __eid2), \
1716 __ev1, __ev2)
1717
1718 static const struct tpacpi_quirk tpacpi_bios_version_qtable[] __initconst = {
1719 /* Numeric models ------------------ */
1720 /* FW MODEL BIOS VERS */
1721 TPV_QI0('I', 'M', '6', '5'), /* 570 */
1722 TPV_QI0('I', 'U', '2', '6'), /* 570E */
1723 TPV_QI0('I', 'B', '5', '4'), /* 600 */
1724 TPV_QI0('I', 'H', '4', '7'), /* 600E */
1725 TPV_QI0('I', 'N', '3', '6'), /* 600E */
1726 TPV_QI0('I', 'T', '5', '5'), /* 600X */
1727 TPV_QI0('I', 'D', '4', '8'), /* 770, 770E, 770ED */
1728 TPV_QI0('I', 'I', '4', '2'), /* 770X */
1729 TPV_QI0('I', 'O', '2', '3'), /* 770Z */
1730
1731 /* A-series ------------------------- */
1732 /* FW MODEL BIOS VERS EC VERS */
1733 TPV_QI0('I', 'W', '5', '9'), /* A20m */
1734 TPV_QI0('I', 'V', '6', '9'), /* A20p */
1735 TPV_QI0('1', '0', '2', '6'), /* A21e, A22e */
1736 TPV_QI0('K', 'U', '3', '6'), /* A21e */
1737 TPV_QI0('K', 'X', '3', '6'), /* A21m, A22m */
1738 TPV_QI0('K', 'Y', '3', '8'), /* A21p, A22p */
1739 TPV_QI0('1', 'B', '1', '7'), /* A22e */
1740 TPV_QI0('1', '3', '2', '0'), /* A22m */
1741 TPV_QI0('1', 'E', '7', '3'), /* A30/p (0) */
1742 TPV_QI1('1', 'G', '4', '1', '1', '7'), /* A31/p (0) */
1743 TPV_QI1('1', 'N', '1', '6', '0', '7'), /* A31/p (0) */
1744
1745 /* G-series ------------------------- */
1746 /* FW MODEL BIOS VERS */
1747 TPV_QI0('1', 'T', 'A', '6'), /* G40 */
1748 TPV_QI0('1', 'X', '5', '7'), /* G41 */
1749
1750 /* R-series, T-series --------------- */
1751 /* FW MODEL BIOS VERS EC VERS */
1752 TPV_QI0('1', 'C', 'F', '0'), /* R30 */
1753 TPV_QI0('1', 'F', 'F', '1'), /* R31 */
1754 TPV_QI0('1', 'M', '9', '7'), /* R32 */
1755 TPV_QI0('1', 'O', '6', '1'), /* R40 */
1756 TPV_QI0('1', 'P', '6', '5'), /* R40 */
1757 TPV_QI0('1', 'S', '7', '0'), /* R40e */
1758 TPV_QI1('1', 'R', 'D', 'R', '7', '1'), /* R50/p, R51,
1759 T40/p, T41/p, T42/p (1) */
1760 TPV_QI1('1', 'V', '7', '1', '2', '8'), /* R50e, R51 (1) */
1761 TPV_QI1('7', '8', '7', '1', '0', '6'), /* R51e (1) */
1762 TPV_QI1('7', '6', '6', '9', '1', '6'), /* R52 (1) */
1763 TPV_QI1('7', '0', '6', '9', '2', '8'), /* R52, T43 (1) */
1764
1765 TPV_QI0('I', 'Y', '6', '1'), /* T20 */
1766 TPV_QI0('K', 'Z', '3', '4'), /* T21 */
1767 TPV_QI0('1', '6', '3', '2'), /* T22 */
1768 TPV_QI1('1', 'A', '6', '4', '2', '3'), /* T23 (0) */
1769 TPV_QI1('1', 'I', '7', '1', '2', '0'), /* T30 (0) */
1770 TPV_QI1('1', 'Y', '6', '5', '2', '9'), /* T43/p (1) */
1771
1772 TPV_QL1('7', '9', 'E', '3', '5', '0'), /* T60/p */
1773 TPV_QL1('7', 'C', 'D', '2', '2', '2'), /* R60, R60i */
1774 TPV_QL1('7', 'E', 'D', '0', '1', '5'), /* R60e, R60i */
1775
1776 /* BIOS FW BIOS VERS EC FW EC VERS */
1777 TPV_QI2('1', 'W', '9', '0', '1', 'V', '2', '8'), /* R50e (1) */
1778 TPV_QL2('7', 'I', '3', '4', '7', '9', '5', '0'), /* T60/p wide */
1779
1780 /* X-series ------------------------- */
1781 /* FW MODEL BIOS VERS EC VERS */
1782 TPV_QI0('I', 'Z', '9', 'D'), /* X20, X21 */
1783 TPV_QI0('1', 'D', '7', '0'), /* X22, X23, X24 */
1784 TPV_QI1('1', 'K', '4', '8', '1', '8'), /* X30 (0) */
1785 TPV_QI1('1', 'Q', '9', '7', '2', '3'), /* X31, X32 (0) */
1786 TPV_QI1('1', 'U', 'D', '3', 'B', '2'), /* X40 (0) */
1787 TPV_QI1('7', '4', '6', '4', '2', '7'), /* X41 (0) */
1788 TPV_QI1('7', '5', '6', '0', '2', '0'), /* X41t (0) */
1789
1790 TPV_QL1('7', 'B', 'D', '7', '4', '0'), /* X60/s */
1791 TPV_QL1('7', 'J', '3', '0', '1', '3'), /* X60t */
1792
1793 /* (0) - older versions lack DMI EC fw string and functionality */
1794 /* (1) - older versions known to lack functionality */
1795 };
1796
1797 #undef TPV_QL1
1798 #undef TPV_QL0
1799 #undef TPV_QI2
1800 #undef TPV_QI1
1801 #undef TPV_QI0
1802 #undef TPV_Q_X
1803 #undef TPV_Q
1804
1805 static void __init tpacpi_check_outdated_fw(void)
1806 {
1807 unsigned long fwvers;
1808 u16 ec_version, bios_version;
1809
1810 fwvers = tpacpi_check_quirks(tpacpi_bios_version_qtable,
1811 ARRAY_SIZE(tpacpi_bios_version_qtable));
1812
1813 if (!fwvers)
1814 return;
1815
1816 bios_version = fwvers & 0xffffU;
1817 ec_version = (fwvers >> 16) & 0xffffU;
1818
1819 /* note that unknown versions are set to 0x0000 and we use that */
1820 if ((bios_version > thinkpad_id.bios_release) ||
1821 (ec_version > thinkpad_id.ec_release &&
1822 ec_version != TPACPI_MATCH_ANY)) {
1823 /*
1824 * The changelogs would let us track down the exact
1825 * reason, but it is just too much of a pain to track
1826 * it. We only list BIOSes that are either really
1827 * broken, or really stable to begin with, so it is
1828 * best if the user upgrades the firmware anyway.
1829 */
1830 pr_warn("WARNING: Outdated ThinkPad BIOS/EC firmware\n");
1831 pr_warn("WARNING: This firmware may be missing critical bug "
1832 "fixes and/or important features\n");
1833 }
1834 }
1835
1836 static bool __init tpacpi_is_fw_known(void)
1837 {
1838 return tpacpi_check_quirks(tpacpi_bios_version_qtable,
1839 ARRAY_SIZE(tpacpi_bios_version_qtable)) != 0;
1840 }
1841
1842 /****************************************************************************
1843 ****************************************************************************
1844 *
1845 * Subdrivers
1846 *
1847 ****************************************************************************
1848 ****************************************************************************/
1849
1850 /*************************************************************************
1851 * thinkpad-acpi metadata subdriver
1852 */
1853
1854 static int thinkpad_acpi_driver_read(struct seq_file *m)
1855 {
1856 seq_printf(m, "driver:\t\t%s\n", TPACPI_DESC);
1857 seq_printf(m, "version:\t%s\n", TPACPI_VERSION);
1858 return 0;
1859 }
1860
1861 static struct ibm_struct thinkpad_acpi_driver_data = {
1862 .name = "driver",
1863 .read = thinkpad_acpi_driver_read,
1864 };
1865
1866 /*************************************************************************
1867 * Hotkey subdriver
1868 */
1869
1870 /*
1871 * ThinkPad firmware event model
1872 *
1873 * The ThinkPad firmware has two main event interfaces: normal ACPI
1874 * notifications (which follow the ACPI standard), and a private event
1875 * interface.
1876 *
1877 * The private event interface also issues events for the hotkeys. As
1878 * the driver gained features, the event handling code ended up being
1879 * built around the hotkey subdriver. This will need to be refactored
1880 * to a more formal event API eventually.
1881 *
1882 * Some "hotkeys" are actually supposed to be used as event reports,
1883 * such as "brightness has changed", "volume has changed", depending on
1884 * the ThinkPad model and how the firmware is operating.
1885 *
1886 * Unlike other classes, hotkey-class events have mask/unmask control on
1887 * non-ancient firmware. However, how it behaves changes a lot with the
1888 * firmware model and version.
1889 */
1890
1891 enum { /* hot key scan codes (derived from ACPI DSDT) */
1892 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1893 TP_ACPI_HOTKEYSCAN_FNF2,
1894 TP_ACPI_HOTKEYSCAN_FNF3,
1895 TP_ACPI_HOTKEYSCAN_FNF4,
1896 TP_ACPI_HOTKEYSCAN_FNF5,
1897 TP_ACPI_HOTKEYSCAN_FNF6,
1898 TP_ACPI_HOTKEYSCAN_FNF7,
1899 TP_ACPI_HOTKEYSCAN_FNF8,
1900 TP_ACPI_HOTKEYSCAN_FNF9,
1901 TP_ACPI_HOTKEYSCAN_FNF10,
1902 TP_ACPI_HOTKEYSCAN_FNF11,
1903 TP_ACPI_HOTKEYSCAN_FNF12,
1904 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1905 TP_ACPI_HOTKEYSCAN_FNINSERT,
1906 TP_ACPI_HOTKEYSCAN_FNDELETE,
1907 TP_ACPI_HOTKEYSCAN_FNHOME,
1908 TP_ACPI_HOTKEYSCAN_FNEND,
1909 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1910 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1911 TP_ACPI_HOTKEYSCAN_FNSPACE,
1912 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1913 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1914 TP_ACPI_HOTKEYSCAN_MUTE,
1915 TP_ACPI_HOTKEYSCAN_THINKPAD,
1916 TP_ACPI_HOTKEYSCAN_UNK1,
1917 TP_ACPI_HOTKEYSCAN_UNK2,
1918 TP_ACPI_HOTKEYSCAN_UNK3,
1919 TP_ACPI_HOTKEYSCAN_UNK4,
1920 TP_ACPI_HOTKEYSCAN_UNK5,
1921 TP_ACPI_HOTKEYSCAN_UNK6,
1922 TP_ACPI_HOTKEYSCAN_UNK7,
1923 TP_ACPI_HOTKEYSCAN_UNK8,
1924
1925 TP_ACPI_HOTKEYSCAN_MUTE2,
1926 TP_ACPI_HOTKEYSCAN_BRIGHTNESS_ZERO,
1927 TP_ACPI_HOTKEYSCAN_CLIPPING_TOOL,
1928 TP_ACPI_HOTKEYSCAN_CLOUD,
1929 TP_ACPI_HOTKEYSCAN_UNK9,
1930 TP_ACPI_HOTKEYSCAN_VOICE,
1931 TP_ACPI_HOTKEYSCAN_UNK10,
1932 TP_ACPI_HOTKEYSCAN_GESTURES,
1933 TP_ACPI_HOTKEYSCAN_UNK11,
1934 TP_ACPI_HOTKEYSCAN_UNK12,
1935 TP_ACPI_HOTKEYSCAN_UNK13,
1936 TP_ACPI_HOTKEYSCAN_CONFIG,
1937 TP_ACPI_HOTKEYSCAN_NEW_TAB,
1938 TP_ACPI_HOTKEYSCAN_RELOAD,
1939 TP_ACPI_HOTKEYSCAN_BACK,
1940 TP_ACPI_HOTKEYSCAN_MIC_DOWN,
1941 TP_ACPI_HOTKEYSCAN_MIC_UP,
1942 TP_ACPI_HOTKEYSCAN_MIC_CANCELLATION,
1943 TP_ACPI_HOTKEYSCAN_CAMERA_MODE,
1944 TP_ACPI_HOTKEYSCAN_ROTATE_DISPLAY,
1945
1946 /* Hotkey keymap size */
1947 TPACPI_HOTKEY_MAP_LEN
1948 };
1949
1950 enum { /* Keys/events available through NVRAM polling */
1951 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1952 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1953 };
1954
1955 enum { /* Positions of some of the keys in hotkey masks */
1956 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1957 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1958 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1959 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1960 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1961 TP_ACPI_HKEY_KBD_LIGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1962 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1963 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1964 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1965 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1966 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1967 };
1968
1969 enum { /* NVRAM to ACPI HKEY group map */
1970 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1971 TP_ACPI_HKEY_ZOOM_MASK |
1972 TP_ACPI_HKEY_DISPSWTCH_MASK |
1973 TP_ACPI_HKEY_HIBERNATE_MASK,
1974 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1975 TP_ACPI_HKEY_BRGHTDWN_MASK,
1976 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1977 TP_ACPI_HKEY_VOLDWN_MASK |
1978 TP_ACPI_HKEY_MUTE_MASK,
1979 };
1980
1981 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1982 struct tp_nvram_state {
1983 u16 thinkpad_toggle:1;
1984 u16 zoom_toggle:1;
1985 u16 display_toggle:1;
1986 u16 thinklight_toggle:1;
1987 u16 hibernate_toggle:1;
1988 u16 displayexp_toggle:1;
1989 u16 display_state:1;
1990 u16 brightness_toggle:1;
1991 u16 volume_toggle:1;
1992 u16 mute:1;
1993
1994 u8 brightness_level;
1995 u8 volume_level;
1996 };
1997
1998 /* kthread for the hotkey poller */
1999 static struct task_struct *tpacpi_hotkey_task;
2000
2001 /*
2002 * Acquire mutex to write poller control variables as an
2003 * atomic block.
2004 *
2005 * Increment hotkey_config_change when changing them if you
2006 * want the kthread to forget old state.
2007 *
2008 * See HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2009 */
2010 static struct mutex hotkey_thread_data_mutex;
2011 static unsigned int hotkey_config_change;
2012
2013 /*
2014 * hotkey poller control variables
2015 *
2016 * Must be atomic or readers will also need to acquire mutex
2017 *
2018 * HOTKEY_CONFIG_CRITICAL_START/HOTKEY_CONFIG_CRITICAL_END
2019 * should be used only when the changes need to be taken as
2020 * a block, OR when one needs to force the kthread to forget
2021 * old state.
2022 */
2023 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
2024 static unsigned int hotkey_poll_freq = 10; /* Hz */
2025
2026 #define HOTKEY_CONFIG_CRITICAL_START \
2027 do { \
2028 mutex_lock(&hotkey_thread_data_mutex); \
2029 hotkey_config_change++; \
2030 } while (0);
2031 #define HOTKEY_CONFIG_CRITICAL_END \
2032 mutex_unlock(&hotkey_thread_data_mutex);
2033
2034 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2035
2036 #define hotkey_source_mask 0U
2037 #define HOTKEY_CONFIG_CRITICAL_START
2038 #define HOTKEY_CONFIG_CRITICAL_END
2039
2040 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2041
2042 static struct mutex hotkey_mutex;
2043
2044 static enum { /* Reasons for waking up */
2045 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
2046 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
2047 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
2048 } hotkey_wakeup_reason;
2049
2050 static int hotkey_autosleep_ack;
2051
2052 static u32 hotkey_orig_mask; /* events the BIOS had enabled */
2053 static u32 hotkey_all_mask; /* all events supported in fw */
2054 static u32 hotkey_adaptive_all_mask; /* all adaptive events supported in fw */
2055 static u32 hotkey_reserved_mask; /* events better left disabled */
2056 static u32 hotkey_driver_mask; /* events needed by the driver */
2057 static u32 hotkey_user_mask; /* events visible to userspace */
2058 static u32 hotkey_acpi_mask; /* events enabled in firmware */
2059
2060 static u16 *hotkey_keycode_map;
2061
2062 static struct attribute_set *hotkey_dev_attributes;
2063
2064 static void tpacpi_driver_event(const unsigned int hkey_event);
2065 static void hotkey_driver_event(const unsigned int scancode);
2066 static void hotkey_poll_setup(const bool may_warn);
2067
2068 /* HKEY.MHKG() return bits */
2069 #define TP_HOTKEY_TABLET_MASK (1 << 3)
2070 /* ThinkPad X1 Yoga (2016) */
2071 #define TP_EC_CMMD_TABLET_MODE 0x6
2072
2073 static int hotkey_get_wlsw(void)
2074 {
2075 int status;
2076
2077 if (!tp_features.hotkey_wlsw)
2078 return -ENODEV;
2079
2080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2081 if (dbg_wlswemul)
2082 return (tpacpi_wlsw_emulstate) ?
2083 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2084 #endif
2085
2086 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
2087 return -EIO;
2088
2089 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
2090 }
2091
2092 static int hotkey_get_tablet_mode(int *status)
2093 {
2094 int s;
2095
2096 switch (tp_features.hotkey_tablet) {
2097 case TP_HOTKEY_TABLET_USES_MHKG:
2098 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
2099 return -EIO;
2100
2101 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
2102 break;
2103 case TP_HOTKEY_TABLET_USES_CMMD:
2104 if (!acpi_evalf(ec_handle, &s, "CMMD", "d"))
2105 return -EIO;
2106
2107 *status = (s == TP_EC_CMMD_TABLET_MODE);
2108 break;
2109 default:
2110 break;
2111 }
2112
2113 return 0;
2114 }
2115
2116 /*
2117 * Reads current event mask from firmware, and updates
2118 * hotkey_acpi_mask accordingly. Also resets any bits
2119 * from hotkey_user_mask that are unavailable to be
2120 * delivered (shadow requirement of the userspace ABI).
2121 *
2122 * Call with hotkey_mutex held
2123 */
2124 static int hotkey_mask_get(void)
2125 {
2126 if (tp_features.hotkey_mask) {
2127 u32 m = 0;
2128
2129 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
2130 return -EIO;
2131
2132 hotkey_acpi_mask = m;
2133 } else {
2134 /* no mask support doesn't mean no event support... */
2135 hotkey_acpi_mask = hotkey_all_mask;
2136 }
2137
2138 /* sync userspace-visible mask */
2139 hotkey_user_mask &= (hotkey_acpi_mask | hotkey_source_mask);
2140
2141 return 0;
2142 }
2143
2144 static void hotkey_mask_warn_incomplete_mask(void)
2145 {
2146 /* log only what the user can fix... */
2147 const u32 wantedmask = hotkey_driver_mask &
2148 ~(hotkey_acpi_mask | hotkey_source_mask) &
2149 (hotkey_all_mask | TPACPI_HKEY_NVRAM_KNOWN_MASK);
2150
2151 if (wantedmask)
2152 pr_notice("required events 0x%08x not enabled!\n", wantedmask);
2153 }
2154
2155 /*
2156 * Set the firmware mask when supported
2157 *
2158 * Also calls hotkey_mask_get to update hotkey_acpi_mask.
2159 *
2160 * NOTE: does not set bits in hotkey_user_mask, but may reset them.
2161 *
2162 * Call with hotkey_mutex held
2163 */
2164 static int hotkey_mask_set(u32 mask)
2165 {
2166 int i;
2167 int rc = 0;
2168
2169 const u32 fwmask = mask & ~hotkey_source_mask;
2170
2171 if (tp_features.hotkey_mask) {
2172 for (i = 0; i < 32; i++) {
2173 if (!acpi_evalf(hkey_handle,
2174 NULL, "MHKM", "vdd", i + 1,
2175 !!(mask & (1 << i)))) {
2176 rc = -EIO;
2177 break;
2178 }
2179 }
2180 }
2181
2182 /*
2183 * We *must* make an inconditional call to hotkey_mask_get to
2184 * refresh hotkey_acpi_mask and update hotkey_user_mask
2185 *
2186 * Take the opportunity to also log when we cannot _enable_
2187 * a given event.
2188 */
2189 if (!hotkey_mask_get() && !rc && (fwmask & ~hotkey_acpi_mask)) {
2190 pr_notice("asked for hotkey mask 0x%08x, but "
2191 "firmware forced it to 0x%08x\n",
2192 fwmask, hotkey_acpi_mask);
2193 }
2194
2195 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING)
2196 hotkey_mask_warn_incomplete_mask();
2197
2198 return rc;
2199 }
2200
2201 /*
2202 * Sets hotkey_user_mask and tries to set the firmware mask
2203 *
2204 * Call with hotkey_mutex held
2205 */
2206 static int hotkey_user_mask_set(const u32 mask)
2207 {
2208 int rc;
2209
2210 /* Give people a chance to notice they are doing something that
2211 * is bound to go boom on their users sooner or later */
2212 if (!tp_warned.hotkey_mask_ff &&
2213 (mask == 0xffff || mask == 0xffffff ||
2214 mask == 0xffffffff)) {
2215 tp_warned.hotkey_mask_ff = 1;
2216 pr_notice("setting the hotkey mask to 0x%08x is likely "
2217 "not the best way to go about it\n", mask);
2218 pr_notice("please consider using the driver defaults, "
2219 "and refer to up-to-date thinkpad-acpi "
2220 "documentation\n");
2221 }
2222
2223 /* Try to enable what the user asked for, plus whatever we need.
2224 * this syncs everything but won't enable bits in hotkey_user_mask */
2225 rc = hotkey_mask_set((mask | hotkey_driver_mask) & ~hotkey_source_mask);
2226
2227 /* Enable the available bits in hotkey_user_mask */
2228 hotkey_user_mask = mask & (hotkey_acpi_mask | hotkey_source_mask);
2229
2230 return rc;
2231 }
2232
2233 /*
2234 * Sets the driver hotkey mask.
2235 *
2236 * Can be called even if the hotkey subdriver is inactive
2237 */
2238 static int tpacpi_hotkey_driver_mask_set(const u32 mask)
2239 {
2240 int rc;
2241
2242 /* Do the right thing if hotkey_init has not been called yet */
2243 if (!tp_features.hotkey) {
2244 hotkey_driver_mask = mask;
2245 return 0;
2246 }
2247
2248 mutex_lock(&hotkey_mutex);
2249
2250 HOTKEY_CONFIG_CRITICAL_START
2251 hotkey_driver_mask = mask;
2252 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2253 hotkey_source_mask |= (mask & ~hotkey_all_mask);
2254 #endif
2255 HOTKEY_CONFIG_CRITICAL_END
2256
2257 rc = hotkey_mask_set((hotkey_acpi_mask | hotkey_driver_mask) &
2258 ~hotkey_source_mask);
2259 hotkey_poll_setup(true);
2260
2261 mutex_unlock(&hotkey_mutex);
2262
2263 return rc;
2264 }
2265
2266 static int hotkey_status_get(int *status)
2267 {
2268 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
2269 return -EIO;
2270
2271 return 0;
2272 }
2273
2274 static int hotkey_status_set(bool enable)
2275 {
2276 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
2277 return -EIO;
2278
2279 return 0;
2280 }
2281
2282 static void tpacpi_input_send_tabletsw(void)
2283 {
2284 int state;
2285
2286 if (tp_features.hotkey_tablet &&
2287 !hotkey_get_tablet_mode(&state)) {
2288 mutex_lock(&tpacpi_inputdev_send_mutex);
2289
2290 input_report_switch(tpacpi_inputdev,
2291 SW_TABLET_MODE, !!state);
2292 input_sync(tpacpi_inputdev);
2293
2294 mutex_unlock(&tpacpi_inputdev_send_mutex);
2295 }
2296 }
2297
2298 /* Do NOT call without validating scancode first */
2299 static void tpacpi_input_send_key(const unsigned int scancode)
2300 {
2301 const unsigned int keycode = hotkey_keycode_map[scancode];
2302
2303 if (keycode != KEY_RESERVED) {
2304 mutex_lock(&tpacpi_inputdev_send_mutex);
2305
2306 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2307 input_report_key(tpacpi_inputdev, keycode, 1);
2308 input_sync(tpacpi_inputdev);
2309
2310 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN, scancode);
2311 input_report_key(tpacpi_inputdev, keycode, 0);
2312 input_sync(tpacpi_inputdev);
2313
2314 mutex_unlock(&tpacpi_inputdev_send_mutex);
2315 }
2316 }
2317
2318 /* Do NOT call without validating scancode first */
2319 static void tpacpi_input_send_key_masked(const unsigned int scancode)
2320 {
2321 hotkey_driver_event(scancode);
2322 if (hotkey_user_mask & (1 << scancode))
2323 tpacpi_input_send_key(scancode);
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 */
2330 static void tpacpi_hotkey_send_key(unsigned int scancode)
2331 {
2332 tpacpi_input_send_key_masked(scancode);
2333 }
2334
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
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
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
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 */
2500 static int hotkey_kthread(void *data)
2501 {
2502 struct tp_nvram_state s[2];
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
2572 /* call with hotkey_mutex held */
2573 static void hotkey_poll_stop_sync(void)
2574 {
2575 if (tpacpi_hotkey_task) {
2576 kthread_stop(tpacpi_hotkey_task);
2577 tpacpi_hotkey_task = NULL;
2578 }
2579 }
2580
2581 /* call with hotkey_mutex held */
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 if (hotkey_poll_freq > 0 &&
2588 (poll_driver_mask ||
2589 (poll_user_mask && tpacpi_inputdev->users > 0))) {
2590 if (!tpacpi_hotkey_task) {
2591 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2592 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2593 if (IS_ERR(tpacpi_hotkey_task)) {
2594 tpacpi_hotkey_task = NULL;
2595 pr_err("could not create kernel thread "
2596 "for hotkey polling\n");
2597 }
2598 }
2599 } else {
2600 hotkey_poll_stop_sync();
2601 if (may_warn && (poll_driver_mask || poll_user_mask) &&
2602 hotkey_poll_freq == 0) {
2603 pr_notice("hot keys 0x%08x and/or events 0x%08x "
2604 "require polling, which is currently "
2605 "disabled\n",
2606 poll_user_mask, poll_driver_mask);
2607 }
2608 }
2609 }
2610
2611 static void hotkey_poll_setup_safe(const bool may_warn)
2612 {
2613 mutex_lock(&hotkey_mutex);
2614 hotkey_poll_setup(may_warn);
2615 mutex_unlock(&hotkey_mutex);
2616 }
2617
2618 /* call with hotkey_mutex held */
2619 static void hotkey_poll_set_freq(unsigned int freq)
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
2629 static void hotkey_poll_setup(const bool __unused)
2630 {
2631 }
2632
2633 static void hotkey_poll_setup_safe(const bool __unused)
2634 {
2635 }
2636
2637 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2638
2639 static int hotkey_inputdev_open(struct input_dev *dev)
2640 {
2641 switch (tpacpi_lifecycle) {
2642 case TPACPI_LIFE_INIT:
2643 case TPACPI_LIFE_RUNNING:
2644 hotkey_poll_setup_safe(false);
2645 return 0;
2646 case TPACPI_LIFE_EXITING:
2647 return -EBUSY;
2648 }
2649
2650 /* Should only happen if tpacpi_lifecycle is corrupt */
2651 BUG();
2652 return -EBUSY;
2653 }
2654
2655 static void hotkey_inputdev_close(struct input_dev *dev)
2656 {
2657 /* disable hotkey polling when possible */
2658 if (tpacpi_lifecycle != TPACPI_LIFE_EXITING &&
2659 !(hotkey_source_mask & hotkey_driver_mask))
2660 hotkey_poll_setup_safe(false);
2661 }
2662
2663 /* sysfs hotkey enable ------------------------------------------------- */
2664 static ssize_t hotkey_enable_show(struct device *dev,
2665 struct device_attribute *attr,
2666 char *buf)
2667 {
2668 int res, status;
2669
2670 printk_deprecated_attribute("hotkey_enable",
2671 "Hotkey reporting is always enabled");
2672
2673 res = hotkey_status_get(&status);
2674 if (res)
2675 return res;
2676
2677 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2678 }
2679
2680 static ssize_t hotkey_enable_store(struct device *dev,
2681 struct device_attribute *attr,
2682 const char *buf, size_t count)
2683 {
2684 unsigned long t;
2685
2686 printk_deprecated_attribute("hotkey_enable",
2687 "Hotkeys can be disabled through hotkey_mask");
2688
2689 if (parse_strtoul(buf, 1, &t))
2690 return -EINVAL;
2691
2692 if (t == 0)
2693 return -EPERM;
2694
2695 return count;
2696 }
2697
2698 static DEVICE_ATTR_RW(hotkey_enable);
2699
2700 /* sysfs hotkey mask --------------------------------------------------- */
2701 static ssize_t hotkey_mask_show(struct device *dev,
2702 struct device_attribute *attr,
2703 char *buf)
2704 {
2705 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_user_mask);
2706 }
2707
2708 static ssize_t hotkey_mask_store(struct device *dev,
2709 struct device_attribute *attr,
2710 const char *buf, size_t count)
2711 {
2712 unsigned long t;
2713 int res;
2714
2715 if (parse_strtoul(buf, 0xffffffffUL, &t))
2716 return -EINVAL;
2717
2718 if (mutex_lock_killable(&hotkey_mutex))
2719 return -ERESTARTSYS;
2720
2721 res = hotkey_user_mask_set(t);
2722
2723 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2724 hotkey_poll_setup(true);
2725 #endif
2726
2727 mutex_unlock(&hotkey_mutex);
2728
2729 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2730
2731 return (res) ? res : count;
2732 }
2733
2734 static DEVICE_ATTR_RW(hotkey_mask);
2735
2736 /* sysfs hotkey bios_enabled ------------------------------------------- */
2737 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2738 struct device_attribute *attr,
2739 char *buf)
2740 {
2741 return sprintf(buf, "0\n");
2742 }
2743
2744 static DEVICE_ATTR_RO(hotkey_bios_enabled);
2745
2746 /* sysfs hotkey bios_mask ---------------------------------------------- */
2747 static ssize_t hotkey_bios_mask_show(struct device *dev,
2748 struct device_attribute *attr,
2749 char *buf)
2750 {
2751 printk_deprecated_attribute("hotkey_bios_mask",
2752 "This attribute is useless.");
2753 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2754 }
2755
2756 static DEVICE_ATTR_RO(hotkey_bios_mask);
2757
2758 /* sysfs hotkey all_mask ----------------------------------------------- */
2759 static ssize_t hotkey_all_mask_show(struct device *dev,
2760 struct device_attribute *attr,
2761 char *buf)
2762 {
2763 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2764 hotkey_all_mask | hotkey_source_mask);
2765 }
2766
2767 static DEVICE_ATTR_RO(hotkey_all_mask);
2768
2769 /* sysfs hotkey all_mask ----------------------------------------------- */
2770 static ssize_t hotkey_adaptive_all_mask_show(struct device *dev,
2771 struct device_attribute *attr,
2772 char *buf)
2773 {
2774 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2775 hotkey_adaptive_all_mask | hotkey_source_mask);
2776 }
2777
2778 static DEVICE_ATTR_RO(hotkey_adaptive_all_mask);
2779
2780 /* sysfs hotkey recommended_mask --------------------------------------- */
2781 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2782 struct device_attribute *attr,
2783 char *buf)
2784 {
2785 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2786 (hotkey_all_mask | hotkey_source_mask)
2787 & ~hotkey_reserved_mask);
2788 }
2789
2790 static DEVICE_ATTR_RO(hotkey_recommended_mask);
2791
2792 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2793
2794 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2795 static ssize_t hotkey_source_mask_show(struct device *dev,
2796 struct device_attribute *attr,
2797 char *buf)
2798 {
2799 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2800 }
2801
2802 static ssize_t hotkey_source_mask_store(struct device *dev,
2803 struct device_attribute *attr,
2804 const char *buf, size_t count)
2805 {
2806 unsigned long t;
2807 u32 r_ev;
2808 int rc;
2809
2810 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2811 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2812 return -EINVAL;
2813
2814 if (mutex_lock_killable(&hotkey_mutex))
2815 return -ERESTARTSYS;
2816
2817 HOTKEY_CONFIG_CRITICAL_START
2818 hotkey_source_mask = t;
2819 HOTKEY_CONFIG_CRITICAL_END
2820
2821 rc = hotkey_mask_set((hotkey_user_mask | hotkey_driver_mask) &
2822 ~hotkey_source_mask);
2823 hotkey_poll_setup(true);
2824
2825 /* check if events needed by the driver got disabled */
2826 r_ev = hotkey_driver_mask & ~(hotkey_acpi_mask & hotkey_all_mask)
2827 & ~hotkey_source_mask & TPACPI_HKEY_NVRAM_KNOWN_MASK;
2828
2829 mutex_unlock(&hotkey_mutex);
2830
2831 if (rc < 0)
2832 pr_err("hotkey_source_mask: "
2833 "failed to update the firmware event mask!\n");
2834
2835 if (r_ev)
2836 pr_notice("hotkey_source_mask: "
2837 "some important events were disabled: 0x%04x\n",
2838 r_ev);
2839
2840 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2841
2842 return (rc < 0) ? rc : count;
2843 }
2844
2845 static DEVICE_ATTR_RW(hotkey_source_mask);
2846
2847 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2848 static ssize_t hotkey_poll_freq_show(struct device *dev,
2849 struct device_attribute *attr,
2850 char *buf)
2851 {
2852 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2853 }
2854
2855 static ssize_t hotkey_poll_freq_store(struct device *dev,
2856 struct device_attribute *attr,
2857 const char *buf, size_t count)
2858 {
2859 unsigned long t;
2860
2861 if (parse_strtoul(buf, 25, &t))
2862 return -EINVAL;
2863
2864 if (mutex_lock_killable(&hotkey_mutex))
2865 return -ERESTARTSYS;
2866
2867 hotkey_poll_set_freq(t);
2868 hotkey_poll_setup(true);
2869
2870 mutex_unlock(&hotkey_mutex);
2871
2872 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2873
2874 return count;
2875 }
2876
2877 static DEVICE_ATTR_RW(hotkey_poll_freq);
2878
2879 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2880
2881 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2882 static ssize_t hotkey_radio_sw_show(struct device *dev,
2883 struct device_attribute *attr,
2884 char *buf)
2885 {
2886 int res;
2887 res = hotkey_get_wlsw();
2888 if (res < 0)
2889 return res;
2890
2891 /* Opportunistic update */
2892 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2893
2894 return snprintf(buf, PAGE_SIZE, "%d\n",
2895 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2896 }
2897
2898 static DEVICE_ATTR_RO(hotkey_radio_sw);
2899
2900 static void hotkey_radio_sw_notify_change(void)
2901 {
2902 if (tp_features.hotkey_wlsw)
2903 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2904 "hotkey_radio_sw");
2905 }
2906
2907 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2908 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2909 struct device_attribute *attr,
2910 char *buf)
2911 {
2912 int res, s;
2913 res = hotkey_get_tablet_mode(&s);
2914 if (res < 0)
2915 return res;
2916
2917 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2918 }
2919
2920 static DEVICE_ATTR_RO(hotkey_tablet_mode);
2921
2922 static void hotkey_tablet_mode_notify_change(void)
2923 {
2924 if (tp_features.hotkey_tablet)
2925 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2926 "hotkey_tablet_mode");
2927 }
2928
2929 /* sysfs wakeup reason (pollable) -------------------------------------- */
2930 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2931 struct device_attribute *attr,
2932 char *buf)
2933 {
2934 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2935 }
2936
2937 static DEVICE_ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2938
2939 static void hotkey_wakeup_reason_notify_change(void)
2940 {
2941 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2942 "wakeup_reason");
2943 }
2944
2945 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2946 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2947 struct device_attribute *attr,
2948 char *buf)
2949 {
2950 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2951 }
2952
2953 static DEVICE_ATTR(wakeup_hotunplug_complete, S_IRUGO,
2954 hotkey_wakeup_hotunplug_complete_show, NULL);
2955
2956 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2957 {
2958 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2959 "wakeup_hotunplug_complete");
2960 }
2961
2962 /* sysfs adaptive kbd mode --------------------------------------------- */
2963
2964 static int adaptive_keyboard_get_mode(void);
2965 static int adaptive_keyboard_set_mode(int new_mode);
2966
2967 enum ADAPTIVE_KEY_MODE {
2968 HOME_MODE,
2969 WEB_BROWSER_MODE,
2970 WEB_CONFERENCE_MODE,
2971 FUNCTION_MODE,
2972 LAYFLAT_MODE
2973 };
2974
2975 static ssize_t adaptive_kbd_mode_show(struct device *dev,
2976 struct device_attribute *attr,
2977 char *buf)
2978 {
2979 int current_mode;
2980
2981 current_mode = adaptive_keyboard_get_mode();
2982 if (current_mode < 0)
2983 return current_mode;
2984
2985 return snprintf(buf, PAGE_SIZE, "%d\n", current_mode);
2986 }
2987
2988 static ssize_t adaptive_kbd_mode_store(struct device *dev,
2989 struct device_attribute *attr,
2990 const char *buf, size_t count)
2991 {
2992 unsigned long t;
2993 int res;
2994
2995 if (parse_strtoul(buf, LAYFLAT_MODE, &t))
2996 return -EINVAL;
2997
2998 res = adaptive_keyboard_set_mode(t);
2999 return (res < 0) ? res : count;
3000 }
3001
3002 static DEVICE_ATTR_RW(adaptive_kbd_mode);
3003
3004 static struct attribute *adaptive_kbd_attributes[] = {
3005 &dev_attr_adaptive_kbd_mode.attr,
3006 NULL
3007 };
3008
3009 static const struct attribute_group adaptive_kbd_attr_group = {
3010 .attrs = adaptive_kbd_attributes,
3011 };
3012
3013 /* --------------------------------------------------------------------- */
3014
3015 static struct attribute *hotkey_attributes[] __initdata = {
3016 &dev_attr_hotkey_enable.attr,
3017 &dev_attr_hotkey_bios_enabled.attr,
3018 &dev_attr_hotkey_bios_mask.attr,
3019 &dev_attr_wakeup_reason.attr,
3020 &dev_attr_wakeup_hotunplug_complete.attr,
3021 &dev_attr_hotkey_mask.attr,
3022 &dev_attr_hotkey_all_mask.attr,
3023 &dev_attr_hotkey_adaptive_all_mask.attr,
3024 &dev_attr_hotkey_recommended_mask.attr,
3025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3026 &dev_attr_hotkey_source_mask.attr,
3027 &dev_attr_hotkey_poll_freq.attr,
3028 #endif
3029 };
3030
3031 /*
3032 * Sync both the hw and sw blocking state of all switches
3033 */
3034 static void tpacpi_send_radiosw_update(void)
3035 {
3036 int wlsw;
3037
3038 /*
3039 * We must sync all rfkill controllers *before* issuing any
3040 * rfkill input events, or we will race the rfkill core input
3041 * handler.
3042 *
3043 * tpacpi_inputdev_send_mutex works as a synchronization point
3044 * for the above.
3045 *
3046 * We optimize to avoid numerous calls to hotkey_get_wlsw.
3047 */
3048
3049 wlsw = hotkey_get_wlsw();
3050
3051 /* Sync hw blocking state first if it is hw-blocked */
3052 if (wlsw == TPACPI_RFK_RADIO_OFF)
3053 tpacpi_rfk_update_hwblock_state(true);
3054
3055 /* Sync sw blocking state */
3056 tpacpi_rfk_update_swstate_all();
3057
3058 /* Sync hw blocking state last if it is hw-unblocked */
3059 if (wlsw == TPACPI_RFK_RADIO_ON)
3060 tpacpi_rfk_update_hwblock_state(false);
3061
3062 /* Issue rfkill input event for WLSW switch */
3063 if (!(wlsw < 0)) {
3064 mutex_lock(&tpacpi_inputdev_send_mutex);
3065
3066 input_report_switch(tpacpi_inputdev,
3067 SW_RFKILL_ALL, (wlsw > 0));
3068 input_sync(tpacpi_inputdev);
3069
3070 mutex_unlock(&tpacpi_inputdev_send_mutex);
3071 }
3072
3073 /*
3074 * this can be unconditional, as we will poll state again
3075 * if userspace uses the notify to read data
3076 */
3077 hotkey_radio_sw_notify_change();
3078 }
3079
3080 static void hotkey_exit(void)
3081 {
3082 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3083 mutex_lock(&hotkey_mutex);
3084 hotkey_poll_stop_sync();
3085 mutex_unlock(&hotkey_mutex);
3086 #endif
3087
3088 if (hotkey_dev_attributes)
3089 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3090
3091 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
3092 "restoring original HKEY status and mask\n");
3093 /* yes, there is a bitwise or below, we want the
3094 * functions to be called even if one of them fail */
3095 if (((tp_features.hotkey_mask &&
3096 hotkey_mask_set(hotkey_orig_mask)) |
3097 hotkey_status_set(false)) != 0)
3098 pr_err("failed to restore hot key mask "
3099 "to BIOS defaults\n");
3100 }
3101
3102 static void __init hotkey_unmap(const unsigned int scancode)
3103 {
3104 if (hotkey_keycode_map[scancode] != KEY_RESERVED) {
3105 clear_bit(hotkey_keycode_map[scancode],
3106 tpacpi_inputdev->keybit);
3107 hotkey_keycode_map[scancode] = KEY_RESERVED;
3108 }
3109 }
3110
3111 /*
3112 * HKEY quirks:
3113 * TPACPI_HK_Q_INIMASK: Supports FN+F3,FN+F4,FN+F12
3114 */
3115
3116 #define TPACPI_HK_Q_INIMASK 0x0001
3117
3118 static const struct tpacpi_quirk tpacpi_hotkey_qtable[] __initconst = {
3119 TPACPI_Q_IBM('I', 'H', TPACPI_HK_Q_INIMASK), /* 600E */
3120 TPACPI_Q_IBM('I', 'N', TPACPI_HK_Q_INIMASK), /* 600E */
3121 TPACPI_Q_IBM('I', 'D', TPACPI_HK_Q_INIMASK), /* 770, 770E, 770ED */
3122 TPACPI_Q_IBM('I', 'W', TPACPI_HK_Q_INIMASK), /* A20m */
3123 TPACPI_Q_IBM('I', 'V', TPACPI_HK_Q_INIMASK), /* A20p */
3124 TPACPI_Q_IBM('1', '0', TPACPI_HK_Q_INIMASK), /* A21e, A22e */
3125 TPACPI_Q_IBM('K', 'U', TPACPI_HK_Q_INIMASK), /* A21e */
3126 TPACPI_Q_IBM('K', 'X', TPACPI_HK_Q_INIMASK), /* A21m, A22m */
3127 TPACPI_Q_IBM('K', 'Y', TPACPI_HK_Q_INIMASK), /* A21p, A22p */
3128 TPACPI_Q_IBM('1', 'B', TPACPI_HK_Q_INIMASK), /* A22e */
3129 TPACPI_Q_IBM('1', '3', TPACPI_HK_Q_INIMASK), /* A22m */
3130 TPACPI_Q_IBM('1', 'E', TPACPI_HK_Q_INIMASK), /* A30/p (0) */
3131 TPACPI_Q_IBM('1', 'C', TPACPI_HK_Q_INIMASK), /* R30 */
3132 TPACPI_Q_IBM('1', 'F', TPACPI_HK_Q_INIMASK), /* R31 */
3133 TPACPI_Q_IBM('I', 'Y', TPACPI_HK_Q_INIMASK), /* T20 */
3134 TPACPI_Q_IBM('K', 'Z', TPACPI_HK_Q_INIMASK), /* T21 */
3135 TPACPI_Q_IBM('1', '6', TPACPI_HK_Q_INIMASK), /* T22 */
3136 TPACPI_Q_IBM('I', 'Z', TPACPI_HK_Q_INIMASK), /* X20, X21 */
3137 TPACPI_Q_IBM('1', 'D', TPACPI_HK_Q_INIMASK), /* X22, X23, X24 */
3138 };
3139
3140 typedef u16 tpacpi_keymap_entry_t;
3141 typedef tpacpi_keymap_entry_t tpacpi_keymap_t[TPACPI_HOTKEY_MAP_LEN];
3142
3143 static int hotkey_init_tablet_mode(void)
3144 {
3145 int in_tablet_mode = 0, res;
3146 char *type = NULL;
3147
3148 if (acpi_evalf(hkey_handle, &res, "MHKG", "qd")) {
3149 /* For X41t, X60t, X61t Tablets... */
3150 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_MHKG;
3151 in_tablet_mode = !!(res & TP_HOTKEY_TABLET_MASK);
3152 type = "MHKG";
3153 } else if (acpi_evalf(ec_handle, &res, "CMMD", "qd")) {
3154 /* For X1 Yoga (2016) */
3155 tp_features.hotkey_tablet = TP_HOTKEY_TABLET_USES_CMMD;
3156 in_tablet_mode = res == TP_EC_CMMD_TABLET_MODE;
3157 type = "CMMD";
3158 }
3159
3160 if (!tp_features.hotkey_tablet)
3161 return 0;
3162
3163 pr_info("Tablet mode switch found (type: %s), currently in %s mode\n",
3164 type, in_tablet_mode ? "tablet" : "laptop");
3165
3166 res = add_to_attr_set(hotkey_dev_attributes,
3167 &dev_attr_hotkey_tablet_mode.attr);
3168 if (res)
3169 return -1;
3170
3171 return in_tablet_mode;
3172 }
3173
3174 static int __init hotkey_init(struct ibm_init_struct *iibm)
3175 {
3176 /* Requirements for changing the default keymaps:
3177 *
3178 * 1. Many of the keys are mapped to KEY_RESERVED for very
3179 * good reasons. Do not change them unless you have deep
3180 * knowledge on the IBM and Lenovo ThinkPad firmware for
3181 * the various ThinkPad models. The driver behaves
3182 * differently for KEY_RESERVED: such keys have their
3183 * hot key mask *unset* in mask_recommended, and also
3184 * in the initial hot key mask programmed into the
3185 * firmware at driver load time, which means the firm-
3186 * ware may react very differently if you change them to
3187 * something else;
3188 *
3189 * 2. You must be subscribed to the linux-thinkpad and
3190 * ibm-acpi-devel mailing lists, and you should read the
3191 * list archives since 2007 if you want to change the
3192 * keymaps. This requirement exists so that you will
3193 * know the past history of problems with the thinkpad-
3194 * acpi driver keymaps, and also that you will be
3195 * listening to any bug reports;
3196 *
3197 * 3. Do not send thinkpad-acpi specific patches directly to
3198 * for merging, *ever*. Send them to the linux-acpi
3199 * mailinglist for comments. Merging is to be done only
3200 * through acpi-test and the ACPI maintainer.
3201 *
3202 * If the above is too much to ask, don't change the keymap.
3203 * Ask the thinkpad-acpi maintainer to do it, instead.
3204 */
3205
3206 enum keymap_index {
3207 TPACPI_KEYMAP_IBM_GENERIC = 0,
3208 TPACPI_KEYMAP_LENOVO_GENERIC,
3209 };
3210
3211 static const tpacpi_keymap_t tpacpi_keymaps[] __initconst = {
3212 /* Generic keymap for IBM ThinkPads */
3213 [TPACPI_KEYMAP_IBM_GENERIC] = {
3214 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3215 KEY_FN_F1, KEY_BATTERY, KEY_COFFEE, KEY_SLEEP,
3216 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3217 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3218
3219 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3220 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3221 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3222 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3223
3224 /* brightness: firmware always reacts to them */
3225 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
3226 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
3227
3228 /* Thinklight: firmware always react to it */
3229 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3230
3231 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3232 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3233
3234 /* Volume: firmware always react to it and reprograms
3235 * the built-in *extra* mixer. Never map it to control
3236 * another mixer by default. */
3237 KEY_RESERVED, /* 0x14: VOLUME UP */
3238 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3239 KEY_RESERVED, /* 0x16: MUTE */
3240
3241 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3242
3243 /* (assignments unknown, please report if found) */
3244 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3245 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3246
3247 /* No assignments, only used for Adaptive keyboards. */
3248 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3249 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3250 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3251 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3252 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
3253 },
3254
3255 /* Generic keymap for Lenovo ThinkPads */
3256 [TPACPI_KEYMAP_LENOVO_GENERIC] = {
3257 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
3258 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
3259 KEY_WLAN, KEY_CAMERA, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
3260 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
3261
3262 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
3263 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
3264 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
3265 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
3266
3267 /* These should be enabled --only-- when ACPI video
3268 * is disabled (i.e. in "vendor" mode), and are handled
3269 * in a special way by the init code */
3270 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
3271 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
3272
3273 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
3274
3275 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
3276 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
3277
3278 /* Volume: z60/z61, T60 (BIOS version?): firmware always
3279 * react to it and reprograms the built-in *extra* mixer.
3280 * Never map it to control another mixer by default.
3281 *
3282 * T60?, T61, R60?, R61: firmware and EC tries to send
3283 * these over the regular keyboard, so these are no-ops,
3284 * but there are still weird bugs re. MUTE, so do not
3285 * change unless you get test reports from all Lenovo
3286 * models. May cause the BIOS to interfere with the
3287 * HDA mixer.
3288 */
3289 KEY_RESERVED, /* 0x14: VOLUME UP */
3290 KEY_RESERVED, /* 0x15: VOLUME DOWN */
3291 KEY_RESERVED, /* 0x16: MUTE */
3292
3293 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
3294
3295 /* (assignments unknown, please report if found) */
3296 KEY_UNKNOWN, KEY_UNKNOWN,
3297
3298 /*
3299 * The mic mute button only sends 0x1a. It does not
3300 * automatically mute the mic or change the mute light.
3301 */
3302 KEY_MICMUTE, /* 0x1a: Mic mute (since ?400 or so) */
3303
3304 /* (assignments unknown, please report if found) */
3305 KEY_UNKNOWN,
3306
3307 /* Extra keys in use since the X240 / T440 / T540 */
3308 KEY_CONFIG, KEY_SEARCH, KEY_SCALE, KEY_FILE,
3309
3310 /*
3311 * These are the adaptive keyboard keycodes for Carbon X1 2014.
3312 * The first item in this list is the Mute button which is
3313 * emitted with 0x103 through
3314 * adaptive_keyboard_hotkey_notify_hotkey() when the sound
3315 * symbol is held.
3316 * We'll need to offset those by 0x20.
3317 */
3318 KEY_RESERVED, /* Mute held, 0x103 */
3319 KEY_BRIGHTNESS_MIN, /* Backlight off */
3320 KEY_RESERVED, /* Clipping tool */
3321 KEY_RESERVED, /* Cloud */
3322 KEY_RESERVED,
3323 KEY_VOICECOMMAND, /* Voice */
3324 KEY_RESERVED,
3325 KEY_RESERVED, /* Gestures */
3326 KEY_RESERVED,
3327 KEY_RESERVED,
3328 KEY_RESERVED,
3329 KEY_CONFIG, /* Settings */
3330 KEY_RESERVED, /* New tab */
3331 KEY_REFRESH, /* Reload */
3332 KEY_BACK, /* Back */
3333 KEY_RESERVED, /* Microphone down */
3334 KEY_RESERVED, /* Microphone up */
3335 KEY_RESERVED, /* Microphone cancellation */
3336 KEY_RESERVED, /* Camera mode */
3337 KEY_RESERVED, /* Rotate display, 0x116 */
3338 },
3339 };
3340
3341 static const struct tpacpi_quirk tpacpi_keymap_qtable[] __initconst = {
3342 /* Generic maps (fallback) */
3343 {
3344 .vendor = PCI_VENDOR_ID_IBM,
3345 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3346 .quirks = TPACPI_KEYMAP_IBM_GENERIC,
3347 },
3348 {
3349 .vendor = PCI_VENDOR_ID_LENOVO,
3350 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
3351 .quirks = TPACPI_KEYMAP_LENOVO_GENERIC,
3352 },
3353 };
3354
3355 #define TPACPI_HOTKEY_MAP_SIZE sizeof(tpacpi_keymap_t)
3356 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(tpacpi_keymap_entry_t)
3357
3358 int res, i;
3359 int status;
3360 int hkeyv;
3361 bool radiosw_state = false;
3362 bool tabletsw_state = false;
3363
3364 unsigned long quirks;
3365 unsigned long keymap_id;
3366
3367 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3368 "initializing hotkey subdriver\n");
3369
3370 BUG_ON(!tpacpi_inputdev);
3371 BUG_ON(tpacpi_inputdev->open != NULL ||
3372 tpacpi_inputdev->close != NULL);
3373
3374 TPACPI_ACPIHANDLE_INIT(hkey);
3375 mutex_init(&hotkey_mutex);
3376
3377 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3378 mutex_init(&hotkey_thread_data_mutex);
3379 #endif
3380
3381 /* hotkey not supported on 570 */
3382 tp_features.hotkey = hkey_handle != NULL;
3383
3384 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3385 "hotkeys are %s\n",
3386 str_supported(tp_features.hotkey));
3387
3388 if (!tp_features.hotkey)
3389 return 1;
3390
3391 quirks = tpacpi_check_quirks(tpacpi_hotkey_qtable,
3392 ARRAY_SIZE(tpacpi_hotkey_qtable));
3393
3394 tpacpi_disable_brightness_delay();
3395
3396 /* MUST have enough space for all attributes to be added to
3397 * hotkey_dev_attributes */
3398 hotkey_dev_attributes = create_attr_set(
3399 ARRAY_SIZE(hotkey_attributes) + 2,
3400 NULL);
3401 if (!hotkey_dev_attributes)
3402 return -ENOMEM;
3403 res = add_many_to_attr_set(hotkey_dev_attributes,
3404 hotkey_attributes,
3405 ARRAY_SIZE(hotkey_attributes));
3406 if (res)
3407 goto err_exit;
3408
3409 /* mask not supported on 600e/x, 770e, 770x, A21e, A2xm/p,
3410 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
3411 for HKEY interface version 0x100 */
3412 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
3413 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3414 "firmware HKEY interface version: 0x%x\n",
3415 hkeyv);
3416
3417 switch (hkeyv >> 8) {
3418 case 1:
3419 /*
3420 * MHKV 0x100 in A31, R40, R40e,
3421 * T4x, X31, and later
3422 */
3423
3424 /* Paranoia check AND init hotkey_all_mask */
3425 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3426 "MHKA", "qd")) {
3427 pr_err("missing MHKA handler, please report this to %s\n",
3428 TPACPI_MAIL);
3429 /* Fallback: pre-init for FN+F3,F4,F12 */
3430 hotkey_all_mask = 0x080cU;
3431 } else {
3432 tp_features.hotkey_mask = 1;
3433 }
3434 break;
3435
3436 case 2:
3437 /*
3438 * MHKV 0x200 in X1, T460s, X260, T560, X1 Tablet (2016)
3439 */
3440
3441 /* Paranoia check AND init hotkey_all_mask */
3442 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
3443 "MHKA", "dd", 1)) {
3444 pr_err("missing MHKA handler, please report this to %s\n",
3445 TPACPI_MAIL);
3446 /* Fallback: pre-init for FN+F3,F4,F12 */
3447 hotkey_all_mask = 0x080cU;
3448 } else {
3449 tp_features.hotkey_mask = 1;
3450 }
3451
3452 /*
3453 * Check if we have an adaptive keyboard, like on the
3454 * Lenovo Carbon X1 2014 (2nd Gen).
3455 */
3456 if (acpi_evalf(hkey_handle, &hotkey_adaptive_all_mask,
3457 "MHKA", "dd", 2)) {
3458 if (hotkey_adaptive_all_mask != 0) {
3459 tp_features.has_adaptive_kbd = true;
3460 res = sysfs_create_group(
3461 &tpacpi_pdev->dev.kobj,
3462 &adaptive_kbd_attr_group);
3463 if (res)
3464 goto err_exit;
3465 }
3466 } else {
3467 tp_features.has_adaptive_kbd = false;
3468 hotkey_adaptive_all_mask = 0x0U;
3469 }
3470 break;
3471
3472 default:
3473 pr_err("unknown version of the HKEY interface: 0x%x\n",
3474 hkeyv);
3475 pr_err("please report this to %s\n", TPACPI_MAIL);
3476 break;
3477 }
3478 }
3479
3480 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3481 "hotkey masks are %s\n",
3482 str_supported(tp_features.hotkey_mask));
3483
3484 /* Init hotkey_all_mask if not initialized yet */
3485 if (!tp_features.hotkey_mask && !hotkey_all_mask &&
3486 (quirks & TPACPI_HK_Q_INIMASK))
3487 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
3488
3489 /* Init hotkey_acpi_mask and hotkey_orig_mask */
3490 if (tp_features.hotkey_mask) {
3491 /* hotkey_source_mask *must* be zero for
3492 * the first hotkey_mask_get to return hotkey_orig_mask */
3493 res = hotkey_mask_get();
3494 if (res)
3495 goto err_exit;
3496
3497 hotkey_orig_mask = hotkey_acpi_mask;
3498 } else {
3499 hotkey_orig_mask = hotkey_all_mask;
3500 hotkey_acpi_mask = hotkey_all_mask;
3501 }
3502
3503 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3504 if (dbg_wlswemul) {
3505 tp_features.hotkey_wlsw = 1;
3506 radiosw_state = !!tpacpi_wlsw_emulstate;
3507 pr_info("radio switch emulation enabled\n");
3508 } else
3509 #endif
3510 /* Not all thinkpads have a hardware radio switch */
3511 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
3512 tp_features.hotkey_wlsw = 1;
3513 radiosw_state = !!status;
3514 pr_info("radio switch found; radios are %s\n",
3515 enabled(status, 0));
3516 }
3517 if (tp_features.hotkey_wlsw)
3518 res = add_to_attr_set(hotkey_dev_attributes,
3519 &dev_attr_hotkey_radio_sw.attr);
3520
3521 res = hotkey_init_tablet_mode();
3522 if (res < 0)
3523 goto err_exit;
3524
3525 tabletsw_state = res;
3526
3527 res = register_attr_set_with_sysfs(hotkey_dev_attributes,
3528 &tpacpi_pdev->dev.kobj);
3529 if (res)
3530 goto err_exit;
3531
3532 /* Set up key map */
3533 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
3534 GFP_KERNEL);
3535 if (!hotkey_keycode_map) {
3536 pr_err("failed to allocate memory for key map\n");
3537 res = -ENOMEM;
3538 goto err_exit;
3539 }
3540
3541 keymap_id = tpacpi_check_quirks(tpacpi_keymap_qtable,
3542 ARRAY_SIZE(tpacpi_keymap_qtable));
3543 BUG_ON(keymap_id >= ARRAY_SIZE(tpacpi_keymaps));
3544 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3545 "using keymap number %lu\n", keymap_id);
3546
3547 memcpy(hotkey_keycode_map, &tpacpi_keymaps[keymap_id],
3548 TPACPI_HOTKEY_MAP_SIZE);
3549
3550 input_set_capability(tpacpi_inputdev, EV_MSC, MSC_SCAN);
3551 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
3552 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
3553 tpacpi_inputdev->keycode = hotkey_keycode_map;
3554 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
3555 if (hotkey_keycode_map[i] != KEY_RESERVED) {
3556 input_set_capability(tpacpi_inputdev, EV_KEY,
3557 hotkey_keycode_map[i]);
3558 } else {
3559 if (i < sizeof(hotkey_reserved_mask)*8)
3560 hotkey_reserved_mask |= 1 << i;
3561 }
3562 }
3563
3564 if (tp_features.hotkey_wlsw) {
3565 input_set_capability(tpacpi_inputdev, EV_SW, SW_RFKILL_ALL);
3566 input_report_switch(tpacpi_inputdev,
3567 SW_RFKILL_ALL, radiosw_state);
3568 }
3569 if (tp_features.hotkey_tablet) {
3570 input_set_capability(tpacpi_inputdev, EV_SW, SW_TABLET_MODE);
3571 input_report_switch(tpacpi_inputdev,
3572 SW_TABLET_MODE, tabletsw_state);
3573 }
3574
3575 /* Do not issue duplicate brightness change events to
3576 * userspace. tpacpi_detect_brightness_capabilities() must have
3577 * been called before this point */
3578 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
3579 pr_info("This ThinkPad has standard ACPI backlight "
3580 "brightness control, supported by the ACPI "
3581 "video driver\n");
3582 pr_notice("Disabling thinkpad-acpi brightness events "
3583 "by default...\n");
3584
3585 /* Disable brightness up/down on Lenovo thinkpads when
3586 * ACPI is handling them, otherwise it is plain impossible
3587 * for userspace to do something even remotely sane */
3588 hotkey_reserved_mask |=
3589 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
3590 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
3591 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNHOME);
3592 hotkey_unmap(TP_ACPI_HOTKEYSCAN_FNEND);
3593 }
3594
3595 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
3596 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
3597 & ~hotkey_all_mask
3598 & ~hotkey_reserved_mask;
3599
3600 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3601 "hotkey source mask 0x%08x, polling freq %u\n",
3602 hotkey_source_mask, hotkey_poll_freq);
3603 #endif
3604
3605 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3606 "enabling firmware HKEY event interface...\n");
3607 res = hotkey_status_set(true);
3608 if (res) {
3609 hotkey_exit();
3610 return res;
3611 }
3612 res = hotkey_mask_set(((hotkey_all_mask & ~hotkey_reserved_mask)
3613 | hotkey_driver_mask)
3614 & ~hotkey_source_mask);
3615 if (res < 0 && res != -ENXIO) {
3616 hotkey_exit();
3617 return res;
3618 }
3619 hotkey_user_mask = (hotkey_acpi_mask | hotkey_source_mask)
3620 & ~hotkey_reserved_mask;
3621 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
3622 "initial masks: user=0x%08x, fw=0x%08x, poll=0x%08x\n",
3623 hotkey_user_mask, hotkey_acpi_mask, hotkey_source_mask);
3624
3625 tpacpi_inputdev->open = &hotkey_inputdev_open;
3626 tpacpi_inputdev->close = &hotkey_inputdev_close;
3627
3628 hotkey_poll_setup_safe(true);
3629
3630 return 0;
3631
3632 err_exit:
3633 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
3634 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3635 &adaptive_kbd_attr_group);
3636
3637 hotkey_dev_attributes = NULL;
3638
3639 return (res < 0) ? res : 1;
3640 }
3641
3642 /* Thinkpad X1 Carbon support 5 modes including Home mode, Web browser
3643 * mode, Web conference mode, Function mode and Lay-flat mode.
3644 * We support Home mode and Function mode currently.
3645 *
3646 * Will consider support rest of modes in future.
3647 *
3648 */
3649 static const int adaptive_keyboard_modes[] = {
3650 HOME_MODE,
3651 /* WEB_BROWSER_MODE = 2,
3652 WEB_CONFERENCE_MODE = 3, */
3653 FUNCTION_MODE
3654 };
3655
3656 #define DFR_CHANGE_ROW 0x101
3657 #define DFR_SHOW_QUICKVIEW_ROW 0x102
3658 #define FIRST_ADAPTIVE_KEY 0x103
3659 #define ADAPTIVE_KEY_OFFSET 0x020
3660
3661 /* press Fn key a while second, it will switch to Function Mode. Then
3662 * release Fn key, previous mode be restored.
3663 */
3664 static bool adaptive_keyboard_mode_is_saved;
3665 static int adaptive_keyboard_prev_mode;
3666
3667 static int adaptive_keyboard_get_mode(void)
3668 {
3669 int mode = 0;
3670
3671 if (!acpi_evalf(hkey_handle, &mode, "GTRW", "dd", 0)) {
3672 pr_err("Cannot read adaptive keyboard mode\n");
3673 return -EIO;
3674 }
3675
3676 return mode;
3677 }
3678
3679 static int adaptive_keyboard_set_mode(int new_mode)
3680 {
3681 if (new_mode < 0 ||
3682 new_mode > LAYFLAT_MODE)
3683 return -EINVAL;
3684
3685 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd", new_mode)) {
3686 pr_err("Cannot set adaptive keyboard mode\n");
3687 return -EIO;
3688 }
3689
3690 return 0;
3691 }
3692
3693 static int adaptive_keyboard_get_next_mode(int mode)
3694 {
3695 size_t i;
3696 size_t max_mode = ARRAY_SIZE(adaptive_keyboard_modes) - 1;
3697
3698 for (i = 0; i <= max_mode; i++) {
3699 if (adaptive_keyboard_modes[i] == mode)
3700 break;
3701 }
3702
3703 if (i >= max_mode)
3704 i = 0;
3705 else
3706 i++;
3707
3708 return adaptive_keyboard_modes[i];
3709 }
3710
3711 static bool adaptive_keyboard_hotkey_notify_hotkey(unsigned int scancode)
3712 {
3713 int current_mode = 0;
3714 int new_mode = 0;
3715 int keycode;
3716
3717 switch (scancode) {
3718 case DFR_CHANGE_ROW:
3719 if (adaptive_keyboard_mode_is_saved) {
3720 new_mode = adaptive_keyboard_prev_mode;
3721 adaptive_keyboard_mode_is_saved = false;
3722 } else {
3723 current_mode = adaptive_keyboard_get_mode();
3724 if (current_mode < 0)
3725 return false;
3726 new_mode = adaptive_keyboard_get_next_mode(
3727 current_mode);
3728 }
3729
3730 if (adaptive_keyboard_set_mode(new_mode) < 0)
3731 return false;
3732
3733 return true;
3734
3735 case DFR_SHOW_QUICKVIEW_ROW:
3736 current_mode = adaptive_keyboard_get_mode();
3737 if (current_mode < 0)
3738 return false;
3739
3740 adaptive_keyboard_prev_mode = current_mode;
3741 adaptive_keyboard_mode_is_saved = true;
3742
3743 if (adaptive_keyboard_set_mode (FUNCTION_MODE) < 0)
3744 return false;
3745 return true;
3746
3747 default:
3748 if (scancode < FIRST_ADAPTIVE_KEY ||
3749 scancode >= FIRST_ADAPTIVE_KEY + TPACPI_HOTKEY_MAP_LEN -
3750 ADAPTIVE_KEY_OFFSET) {
3751 pr_info("Unhandled adaptive keyboard key: 0x%x\n",
3752 scancode);
3753 return false;
3754 }
3755 keycode = hotkey_keycode_map[scancode - FIRST_ADAPTIVE_KEY + ADAPTIVE_KEY_OFFSET];
3756 if (keycode != KEY_RESERVED) {
3757 mutex_lock(&tpacpi_inputdev_send_mutex);
3758
3759 input_report_key(tpacpi_inputdev, keycode, 1);
3760 input_sync(tpacpi_inputdev);
3761
3762 input_report_key(tpacpi_inputdev, keycode, 0);
3763 input_sync(tpacpi_inputdev);
3764
3765 mutex_unlock(&tpacpi_inputdev_send_mutex);
3766 }
3767 return true;
3768 }
3769 }
3770
3771 static bool hotkey_notify_hotkey(const u32 hkey,
3772 bool *send_acpi_ev,
3773 bool *ignore_acpi_ev)
3774 {
3775 /* 0x1000-0x1FFF: key presses */
3776 unsigned int scancode = hkey & 0xfff;
3777 *send_acpi_ev = true;
3778 *ignore_acpi_ev = false;
3779
3780 /* HKEY event 0x1001 is scancode 0x00 */
3781 if (scancode > 0 && scancode <= TPACPI_HOTKEY_MAP_LEN) {
3782 scancode--;
3783 if (!(hotkey_source_mask & (1 << scancode))) {
3784 tpacpi_input_send_key_masked(scancode);
3785 *send_acpi_ev = false;
3786 } else {
3787 *ignore_acpi_ev = true;
3788 }
3789 return true;
3790 } else {
3791 return adaptive_keyboard_hotkey_notify_hotkey(scancode);
3792 }
3793 return false;
3794 }
3795
3796 static bool hotkey_notify_wakeup(const u32 hkey,
3797 bool *send_acpi_ev,
3798 bool *ignore_acpi_ev)
3799 {
3800 /* 0x2000-0x2FFF: Wakeup reason */
3801 *send_acpi_ev = true;
3802 *ignore_acpi_ev = false;
3803
3804 switch (hkey) {
3805 case TP_HKEY_EV_WKUP_S3_UNDOCK: /* suspend, undock */
3806 case TP_HKEY_EV_WKUP_S4_UNDOCK: /* hibernation, undock */
3807 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3808 *ignore_acpi_ev = true;
3809 break;
3810
3811 case TP_HKEY_EV_WKUP_S3_BAYEJ: /* suspend, bay eject */
3812 case TP_HKEY_EV_WKUP_S4_BAYEJ: /* hibernation, bay eject */
3813 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3814 *ignore_acpi_ev = true;
3815 break;
3816
3817 case TP_HKEY_EV_WKUP_S3_BATLOW: /* Battery on critical low level/S3 */
3818 case TP_HKEY_EV_WKUP_S4_BATLOW: /* Battery on critical low level/S4 */
3819 pr_alert("EMERGENCY WAKEUP: battery almost empty\n");
3820 /* how to auto-heal: */
3821 /* 2313: woke up from S3, go to S4/S5 */
3822 /* 2413: woke up from S4, go to S5 */
3823 break;
3824
3825 default:
3826 return false;
3827 }
3828
3829 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3830 pr_info("woke up due to a hot-unplug request...\n");
3831 hotkey_wakeup_reason_notify_change();
3832 }
3833 return true;
3834 }
3835
3836 static bool hotkey_notify_dockevent(const u32 hkey,
3837 bool *send_acpi_ev,
3838 bool *ignore_acpi_ev)
3839 {
3840 /* 0x4000-0x4FFF: dock-related events */
3841 *send_acpi_ev = true;
3842 *ignore_acpi_ev = false;
3843
3844 switch (hkey) {
3845 case TP_HKEY_EV_UNDOCK_ACK:
3846 /* ACPI undock operation completed after wakeup */
3847 hotkey_autosleep_ack = 1;
3848 pr_info("undocked\n");
3849 hotkey_wakeup_hotunplug_complete_notify_change();
3850 return true;
3851
3852 case TP_HKEY_EV_HOTPLUG_DOCK: /* docked to port replicator */
3853 pr_info("docked into hotplug port replicator\n");
3854 return true;
3855 case TP_HKEY_EV_HOTPLUG_UNDOCK: /* undocked from port replicator */
3856 pr_info("undocked from hotplug port replicator\n");
3857 return true;
3858
3859 default:
3860 return false;
3861 }
3862 }
3863
3864 static bool hotkey_notify_usrevent(const u32 hkey,
3865 bool *send_acpi_ev,
3866 bool *ignore_acpi_ev)
3867 {
3868 /* 0x5000-0x5FFF: human interface helpers */
3869 *send_acpi_ev = true;
3870 *ignore_acpi_ev = false;
3871
3872 switch (hkey) {
3873 case TP_HKEY_EV_PEN_INSERTED: /* X61t: tablet pen inserted into bay */
3874 case TP_HKEY_EV_PEN_REMOVED: /* X61t: tablet pen removed from bay */
3875 return true;
3876
3877 case TP_HKEY_EV_TABLET_TABLET: /* X41t-X61t: tablet mode */
3878 case TP_HKEY_EV_TABLET_NOTEBOOK: /* X41t-X61t: normal mode */
3879 tpacpi_input_send_tabletsw();
3880 hotkey_tablet_mode_notify_change();
3881 *send_acpi_ev = false;
3882 return true;
3883
3884 case TP_HKEY_EV_LID_CLOSE: /* Lid closed */
3885 case TP_HKEY_EV_LID_OPEN: /* Lid opened */
3886 case TP_HKEY_EV_BRGHT_CHANGED: /* brightness changed */
3887 /* do not propagate these events */
3888 *ignore_acpi_ev = true;
3889 return true;
3890
3891 default:
3892 return false;
3893 }
3894 }
3895
3896 static void thermal_dump_all_sensors(void);
3897
3898 static bool hotkey_notify_6xxx(const u32 hkey,
3899 bool *send_acpi_ev,
3900 bool *ignore_acpi_ev)
3901 {
3902 bool known = true;
3903
3904 /* 0x6000-0x6FFF: thermal alarms/notices and keyboard events */
3905 *send_acpi_ev = true;
3906 *ignore_acpi_ev = false;
3907
3908 switch (hkey) {
3909 case TP_HKEY_EV_THM_TABLE_CHANGED:
3910 pr_info("EC reports that Thermal Table has changed\n");
3911 /* recommended action: do nothing, we don't have
3912 * Lenovo ATM information */
3913 return true;
3914 case TP_HKEY_EV_ALARM_BAT_HOT:
3915 pr_crit("THERMAL ALARM: battery is too hot!\n");
3916 /* recommended action: warn user through gui */
3917 break;
3918 case TP_HKEY_EV_ALARM_BAT_XHOT:
3919 pr_alert("THERMAL EMERGENCY: battery is extremely hot!\n");
3920 /* recommended action: immediate sleep/hibernate */
3921 break;
3922 case TP_HKEY_EV_ALARM_SENSOR_HOT:
3923 pr_crit("THERMAL ALARM: "
3924 "a sensor reports something is too hot!\n");
3925 /* recommended action: warn user through gui, that */
3926 /* some internal component is too hot */
3927 break;
3928 case TP_HKEY_EV_ALARM_SENSOR_XHOT:
3929 pr_alert("THERMAL EMERGENCY: "
3930 "a sensor reports something is extremely hot!\n");
3931 /* recommended action: immediate sleep/hibernate */
3932 break;
3933 case TP_HKEY_EV_AC_CHANGED:
3934 /* X120e, X121e, X220, X220i, X220t, X230, T420, T420s, W520:
3935 * AC status changed; can be triggered by plugging or
3936 * unplugging AC adapter, docking or undocking. */
3937
3938 /* fallthrough */
3939
3940 case TP_HKEY_EV_KEY_NUMLOCK:
3941 case TP_HKEY_EV_KEY_FN:
3942 case TP_HKEY_EV_KEY_FN_ESC:
3943 /* key press events, we just ignore them as long as the EC
3944 * is still reporting them in the normal keyboard stream */
3945 *send_acpi_ev = false;
3946 *ignore_acpi_ev = true;
3947 return true;
3948
3949 case TP_HKEY_EV_TABLET_CHANGED:
3950 tpacpi_input_send_tabletsw();
3951 hotkey_tablet_mode_notify_change();
3952 *send_acpi_ev = false;
3953 break;
3954
3955 default:
3956 pr_warn("unknown possible thermal alarm or keyboard event received\n");
3957 known = false;
3958 }
3959
3960 thermal_dump_all_sensors();
3961
3962 return known;
3963 }
3964
3965 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3966 {
3967 u32 hkey;
3968 bool send_acpi_ev;
3969 bool ignore_acpi_ev;
3970 bool known_ev;
3971
3972 if (event != 0x80) {
3973 pr_err("unknown HKEY notification event %d\n", event);
3974 /* forward it to userspace, maybe it knows how to handle it */
3975 acpi_bus_generate_netlink_event(
3976 ibm->acpi->device->pnp.device_class,
3977 dev_name(&ibm->acpi->device->dev),
3978 event, 0);
3979 return;
3980 }
3981
3982 while (1) {
3983 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3984 pr_err("failed to retrieve HKEY event\n");
3985 return;
3986 }
3987
3988 if (hkey == 0) {
3989 /* queue empty */
3990 return;
3991 }
3992
3993 send_acpi_ev = true;
3994 ignore_acpi_ev = false;
3995
3996 switch (hkey >> 12) {
3997 case 1:
3998 /* 0x1000-0x1FFF: key presses */
3999 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
4000 &ignore_acpi_ev);
4001 break;
4002 case 2:
4003 /* 0x2000-0x2FFF: Wakeup reason */
4004 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
4005 &ignore_acpi_ev);
4006 break;
4007 case 3:
4008 /* 0x3000-0x3FFF: bay-related wakeups */
4009 switch (hkey) {
4010 case TP_HKEY_EV_BAYEJ_ACK:
4011 hotkey_autosleep_ack = 1;
4012 pr_info("bay ejected\n");
4013 hotkey_wakeup_hotunplug_complete_notify_change();
4014 known_ev = true;
4015 break;
4016 case TP_HKEY_EV_OPTDRV_EJ:
4017 /* FIXME: kick libata if SATA link offline */
4018 known_ev = true;
4019 break;
4020 default:
4021 known_ev = false;
4022 }
4023 break;
4024 case 4:
4025 /* 0x4000-0x4FFF: dock-related events */
4026 known_ev = hotkey_notify_dockevent(hkey, &send_acpi_ev,
4027 &ignore_acpi_ev);
4028 break;
4029 case 5:
4030 /* 0x5000-0x5FFF: human interface helpers */
4031 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
4032 &ignore_acpi_ev);
4033 break;
4034 case 6:
4035 /* 0x6000-0x6FFF: thermal alarms/notices and
4036 * keyboard events */
4037 known_ev = hotkey_notify_6xxx(hkey, &send_acpi_ev,
4038 &ignore_acpi_ev);
4039 break;
4040 case 7:
4041 /* 0x7000-0x7FFF: misc */
4042 if (tp_features.hotkey_wlsw &&
4043 hkey == TP_HKEY_EV_RFKILL_CHANGED) {
4044 tpacpi_send_radiosw_update();
4045 send_acpi_ev = 0;
4046 known_ev = true;
4047 break;
4048 }
4049 /* fallthrough to default */
4050 default:
4051 known_ev = false;
4052 }
4053 if (!known_ev) {
4054 pr_notice("unhandled HKEY event 0x%04x\n", hkey);
4055 pr_notice("please report the conditions when this "
4056 "event happened to %s\n", TPACPI_MAIL);
4057 }
4058
4059 /* netlink events */
4060 if (!ignore_acpi_ev && send_acpi_ev) {
4061 acpi_bus_generate_netlink_event(
4062 ibm->acpi->device->pnp.device_class,
4063 dev_name(&ibm->acpi->device->dev),
4064 event, hkey);
4065 }
4066 }
4067 }
4068
4069 static void hotkey_suspend(void)
4070 {
4071 /* Do these on suspend, we get the events on early resume! */
4072 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
4073 hotkey_autosleep_ack = 0;
4074
4075 /* save previous mode of adaptive keyboard of X1 Carbon */
4076 if (tp_features.has_adaptive_kbd) {
4077 if (!acpi_evalf(hkey_handle, &adaptive_keyboard_prev_mode,
4078 "GTRW", "dd", 0)) {
4079 pr_err("Cannot read adaptive keyboard mode.\n");
4080 }
4081 }
4082 }
4083
4084 static void hotkey_resume(void)
4085 {
4086 tpacpi_disable_brightness_delay();
4087
4088 if (hotkey_status_set(true) < 0 ||
4089 hotkey_mask_set(hotkey_acpi_mask) < 0)
4090 pr_err("error while attempting to reset the event "
4091 "firmware interface\n");
4092
4093 tpacpi_send_radiosw_update();
4094 hotkey_tablet_mode_notify_change();
4095 hotkey_wakeup_reason_notify_change();
4096 hotkey_wakeup_hotunplug_complete_notify_change();
4097 hotkey_poll_setup_safe(false);
4098
4099 /* restore previous mode of adapive keyboard of X1 Carbon */
4100 if (tp_features.has_adaptive_kbd) {
4101 if (!acpi_evalf(hkey_handle, NULL, "STRW", "vd",
4102 adaptive_keyboard_prev_mode)) {
4103 pr_err("Cannot set adaptive keyboard mode.\n");
4104 }
4105 }
4106 }
4107
4108 /* procfs -------------------------------------------------------------- */
4109 static int hotkey_read(struct seq_file *m)
4110 {
4111 int res, status;
4112
4113 if (!tp_features.hotkey) {
4114 seq_printf(m, "status:\t\tnot supported\n");
4115 return 0;
4116 }
4117
4118 if (mutex_lock_killable(&hotkey_mutex))
4119 return -ERESTARTSYS;
4120 res = hotkey_status_get(&status);
4121 if (!res)
4122 res = hotkey_mask_get();
4123 mutex_unlock(&hotkey_mutex);
4124 if (res)
4125 return res;
4126
4127 seq_printf(m, "status:\t\t%s\n", enabled(status, 0));
4128 if (hotkey_all_mask) {
4129 seq_printf(m, "mask:\t\t0x%08x\n", hotkey_user_mask);
4130 seq_printf(m, "commands:\tenable, disable, reset, <mask>\n");
4131 } else {
4132 seq_printf(m, "mask:\t\tnot supported\n");
4133 seq_printf(m, "commands:\tenable, disable, reset\n");
4134 }
4135
4136 return 0;
4137 }
4138
4139 static void hotkey_enabledisable_warn(bool enable)
4140 {
4141 tpacpi_log_usertask("procfs hotkey enable/disable");
4142 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
4143 pr_fmt("hotkey enable/disable functionality has been "
4144 "removed from the driver. "
4145 "Hotkeys are always enabled.\n")))
4146 pr_err("Please remove the hotkey=enable module "
4147 "parameter, it is deprecated. "
4148 "Hotkeys are always enabled.\n");
4149 }
4150
4151 static int hotkey_write(char *buf)
4152 {
4153 int res;
4154 u32 mask;
4155 char *cmd;
4156
4157 if (!tp_features.hotkey)
4158 return -ENODEV;
4159
4160 if (mutex_lock_killable(&hotkey_mutex))
4161 return -ERESTARTSYS;
4162
4163 mask = hotkey_user_mask;
4164
4165 res = 0;
4166 while ((cmd = next_cmd(&buf))) {
4167 if (strlencmp(cmd, "enable") == 0) {
4168 hotkey_enabledisable_warn(1);
4169 } else if (strlencmp(cmd, "disable") == 0) {
4170 hotkey_enabledisable_warn(0);
4171 res = -EPERM;
4172 } else if (strlencmp(cmd, "reset") == 0) {
4173 mask = (hotkey_all_mask | hotkey_source_mask)
4174 & ~hotkey_reserved_mask;
4175 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
4176 /* mask set */
4177 } else if (sscanf(cmd, "%x", &mask) == 1) {
4178 /* mask set */
4179 } else {
4180 res = -EINVAL;
4181 goto errexit;
4182 }
4183 }
4184
4185 if (!res) {
4186 tpacpi_disclose_usertask("procfs hotkey",
4187 "set mask to 0x%08x\n", mask);
4188 res = hotkey_user_mask_set(mask);
4189 }
4190
4191 errexit:
4192 mutex_unlock(&hotkey_mutex);
4193 return res;
4194 }
4195
4196 static const struct acpi_device_id ibm_htk_device_ids[] = {
4197 {TPACPI_ACPI_IBM_HKEY_HID, 0},
4198 {TPACPI_ACPI_LENOVO_HKEY_HID, 0},
4199 {TPACPI_ACPI_LENOVO_HKEY_V2_HID, 0},
4200 {"", 0},
4201 };
4202
4203 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
4204 .hid = ibm_htk_device_ids,
4205 .notify = hotkey_notify,
4206 .handle = &hkey_handle,
4207 .type = ACPI_DEVICE_NOTIFY,
4208 };
4209
4210 static struct ibm_struct hotkey_driver_data = {
4211 .name = "hotkey",
4212 .read = hotkey_read,
4213 .write = hotkey_write,
4214 .exit = hotkey_exit,
4215 .resume = hotkey_resume,
4216 .suspend = hotkey_suspend,
4217 .acpi = &ibm_hotkey_acpidriver,
4218 };
4219
4220 /*************************************************************************
4221 * Bluetooth subdriver
4222 */
4223
4224 enum {
4225 /* ACPI GBDC/SBDC bits */
4226 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
4227 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
4228 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
4229 0 = disable, 1 = enable */
4230 };
4231
4232 enum {
4233 /* ACPI \BLTH commands */
4234 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
4235 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
4236 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
4237 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
4238 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
4239 };
4240
4241 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
4242
4243 static int bluetooth_get_status(void)
4244 {
4245 int status;
4246
4247 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4248 if (dbg_bluetoothemul)
4249 return (tpacpi_bluetooth_emulstate) ?
4250 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4251 #endif
4252
4253 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
4254 return -EIO;
4255
4256 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
4257 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4258 }
4259
4260 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
4261 {
4262 int status;
4263
4264 vdbg_printk(TPACPI_DBG_RFKILL,
4265 "will attempt to %s bluetooth\n",
4266 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4267
4268 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4269 if (dbg_bluetoothemul) {
4270 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
4271 return 0;
4272 }
4273 #endif
4274
4275 if (state == TPACPI_RFK_RADIO_ON)
4276 status = TP_ACPI_BLUETOOTH_RADIOSSW
4277 | TP_ACPI_BLUETOOTH_RESUMECTRL;
4278 else
4279 status = 0;
4280
4281 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
4282 return -EIO;
4283
4284 return 0;
4285 }
4286
4287 /* sysfs bluetooth enable ---------------------------------------------- */
4288 static ssize_t bluetooth_enable_show(struct device *dev,
4289 struct device_attribute *attr,
4290 char *buf)
4291 {
4292 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
4293 attr, buf);
4294 }
4295
4296 static ssize_t bluetooth_enable_store(struct device *dev,
4297 struct device_attribute *attr,
4298 const char *buf, size_t count)
4299 {
4300 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
4301 attr, buf, count);
4302 }
4303
4304 static DEVICE_ATTR_RW(bluetooth_enable);
4305
4306 /* --------------------------------------------------------------------- */
4307
4308 static struct attribute *bluetooth_attributes[] = {
4309 &dev_attr_bluetooth_enable.attr,
4310 NULL
4311 };
4312
4313 static const struct attribute_group bluetooth_attr_group = {
4314 .attrs = bluetooth_attributes,
4315 };
4316
4317 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
4318 .get_status = bluetooth_get_status,
4319 .set_status = bluetooth_set_status,
4320 };
4321
4322 static void bluetooth_shutdown(void)
4323 {
4324 /* Order firmware to save current state to NVRAM */
4325 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
4326 TP_ACPI_BLTH_SAVE_STATE))
4327 pr_notice("failed to save bluetooth state to NVRAM\n");
4328 else
4329 vdbg_printk(TPACPI_DBG_RFKILL,
4330 "bluetooth state saved to NVRAM\n");
4331 }
4332
4333 static void bluetooth_exit(void)
4334 {
4335 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4336 &bluetooth_attr_group);
4337
4338 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4339
4340 bluetooth_shutdown();
4341 }
4342
4343 static int __init bluetooth_init(struct ibm_init_struct *iibm)
4344 {
4345 int res;
4346 int status = 0;
4347
4348 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4349 "initializing bluetooth subdriver\n");
4350
4351 TPACPI_ACPIHANDLE_INIT(hkey);
4352
4353 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4354 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
4355 tp_features.bluetooth = hkey_handle &&
4356 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
4357
4358 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4359 "bluetooth is %s, status 0x%02x\n",
4360 str_supported(tp_features.bluetooth),
4361 status);
4362
4363 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4364 if (dbg_bluetoothemul) {
4365 tp_features.bluetooth = 1;
4366 pr_info("bluetooth switch emulation enabled\n");
4367 } else
4368 #endif
4369 if (tp_features.bluetooth &&
4370 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
4371 /* no bluetooth hardware present in system */
4372 tp_features.bluetooth = 0;
4373 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4374 "bluetooth hardware not installed\n");
4375 }
4376
4377 if (!tp_features.bluetooth)
4378 return 1;
4379
4380 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
4381 &bluetooth_tprfk_ops,
4382 RFKILL_TYPE_BLUETOOTH,
4383 TPACPI_RFK_BLUETOOTH_SW_NAME,
4384 true);
4385 if (res)
4386 return res;
4387
4388 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4389 &bluetooth_attr_group);
4390 if (res) {
4391 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
4392 return res;
4393 }
4394
4395 return 0;
4396 }
4397
4398 /* procfs -------------------------------------------------------------- */
4399 static int bluetooth_read(struct seq_file *m)
4400 {
4401 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, m);
4402 }
4403
4404 static int bluetooth_write(char *buf)
4405 {
4406 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
4407 }
4408
4409 static struct ibm_struct bluetooth_driver_data = {
4410 .name = "bluetooth",
4411 .read = bluetooth_read,
4412 .write = bluetooth_write,
4413 .exit = bluetooth_exit,
4414 .shutdown = bluetooth_shutdown,
4415 };
4416
4417 /*************************************************************************
4418 * Wan subdriver
4419 */
4420
4421 enum {
4422 /* ACPI GWAN/SWAN bits */
4423 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
4424 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
4425 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
4426 0 = disable, 1 = enable */
4427 };
4428
4429 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
4430
4431 static int wan_get_status(void)
4432 {
4433 int status;
4434
4435 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4436 if (dbg_wwanemul)
4437 return (tpacpi_wwan_emulstate) ?
4438 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4439 #endif
4440
4441 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
4442 return -EIO;
4443
4444 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
4445 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4446 }
4447
4448 static int wan_set_status(enum tpacpi_rfkill_state state)
4449 {
4450 int status;
4451
4452 vdbg_printk(TPACPI_DBG_RFKILL,
4453 "will attempt to %s wwan\n",
4454 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4455
4456 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4457 if (dbg_wwanemul) {
4458 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
4459 return 0;
4460 }
4461 #endif
4462
4463 if (state == TPACPI_RFK_RADIO_ON)
4464 status = TP_ACPI_WANCARD_RADIOSSW
4465 | TP_ACPI_WANCARD_RESUMECTRL;
4466 else
4467 status = 0;
4468
4469 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
4470 return -EIO;
4471
4472 return 0;
4473 }
4474
4475 /* sysfs wan enable ---------------------------------------------------- */
4476 static ssize_t wan_enable_show(struct device *dev,
4477 struct device_attribute *attr,
4478 char *buf)
4479 {
4480 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
4481 attr, buf);
4482 }
4483
4484 static ssize_t wan_enable_store(struct device *dev,
4485 struct device_attribute *attr,
4486 const char *buf, size_t count)
4487 {
4488 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
4489 attr, buf, count);
4490 }
4491
4492 static DEVICE_ATTR(wwan_enable, S_IWUSR | S_IRUGO,
4493 wan_enable_show, wan_enable_store);
4494
4495 /* --------------------------------------------------------------------- */
4496
4497 static struct attribute *wan_attributes[] = {
4498 &dev_attr_wwan_enable.attr,
4499 NULL
4500 };
4501
4502 static const struct attribute_group wan_attr_group = {
4503 .attrs = wan_attributes,
4504 };
4505
4506 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
4507 .get_status = wan_get_status,
4508 .set_status = wan_set_status,
4509 };
4510
4511 static void wan_shutdown(void)
4512 {
4513 /* Order firmware to save current state to NVRAM */
4514 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
4515 TP_ACPI_WGSV_SAVE_STATE))
4516 pr_notice("failed to save WWAN state to NVRAM\n");
4517 else
4518 vdbg_printk(TPACPI_DBG_RFKILL,
4519 "WWAN state saved to NVRAM\n");
4520 }
4521
4522 static void wan_exit(void)
4523 {
4524 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
4525 &wan_attr_group);
4526
4527 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4528
4529 wan_shutdown();
4530 }
4531
4532 static int __init wan_init(struct ibm_init_struct *iibm)
4533 {
4534 int res;
4535 int status = 0;
4536
4537 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4538 "initializing wan subdriver\n");
4539
4540 TPACPI_ACPIHANDLE_INIT(hkey);
4541
4542 tp_features.wan = hkey_handle &&
4543 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
4544
4545 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4546 "wan is %s, status 0x%02x\n",
4547 str_supported(tp_features.wan),
4548 status);
4549
4550 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4551 if (dbg_wwanemul) {
4552 tp_features.wan = 1;
4553 pr_info("wwan switch emulation enabled\n");
4554 } else
4555 #endif
4556 if (tp_features.wan &&
4557 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
4558 /* no wan hardware present in system */
4559 tp_features.wan = 0;
4560 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4561 "wan hardware not installed\n");
4562 }
4563
4564 if (!tp_features.wan)
4565 return 1;
4566
4567 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
4568 &wan_tprfk_ops,
4569 RFKILL_TYPE_WWAN,
4570 TPACPI_RFK_WWAN_SW_NAME,
4571 true);
4572 if (res)
4573 return res;
4574
4575 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
4576 &wan_attr_group);
4577
4578 if (res) {
4579 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
4580 return res;
4581 }
4582
4583 return 0;
4584 }
4585
4586 /* procfs -------------------------------------------------------------- */
4587 static int wan_read(struct seq_file *m)
4588 {
4589 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, m);
4590 }
4591
4592 static int wan_write(char *buf)
4593 {
4594 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
4595 }
4596
4597 static struct ibm_struct wan_driver_data = {
4598 .name = "wan",
4599 .read = wan_read,
4600 .write = wan_write,
4601 .exit = wan_exit,
4602 .shutdown = wan_shutdown,
4603 };
4604
4605 /*************************************************************************
4606 * UWB subdriver
4607 */
4608
4609 enum {
4610 /* ACPI GUWB/SUWB bits */
4611 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
4612 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
4613 };
4614
4615 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
4616
4617 static int uwb_get_status(void)
4618 {
4619 int status;
4620
4621 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4622 if (dbg_uwbemul)
4623 return (tpacpi_uwb_emulstate) ?
4624 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4625 #endif
4626
4627 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
4628 return -EIO;
4629
4630 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
4631 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
4632 }
4633
4634 static int uwb_set_status(enum tpacpi_rfkill_state state)
4635 {
4636 int status;
4637
4638 vdbg_printk(TPACPI_DBG_RFKILL,
4639 "will attempt to %s UWB\n",
4640 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
4641
4642 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4643 if (dbg_uwbemul) {
4644 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
4645 return 0;
4646 }
4647 #endif
4648
4649 if (state == TPACPI_RFK_RADIO_ON)
4650 status = TP_ACPI_UWB_RADIOSSW;
4651 else
4652 status = 0;
4653
4654 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
4655 return -EIO;
4656
4657 return 0;
4658 }
4659
4660 /* --------------------------------------------------------------------- */
4661
4662 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
4663 .get_status = uwb_get_status,
4664 .set_status = uwb_set_status,
4665 };
4666
4667 static void uwb_exit(void)
4668 {
4669 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
4670 }
4671
4672 static int __init uwb_init(struct ibm_init_struct *iibm)
4673 {
4674 int res;
4675 int status = 0;
4676
4677 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4678 "initializing uwb subdriver\n");
4679
4680 TPACPI_ACPIHANDLE_INIT(hkey);
4681
4682 tp_features.uwb = hkey_handle &&
4683 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
4684
4685 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
4686 "uwb is %s, status 0x%02x\n",
4687 str_supported(tp_features.uwb),
4688 status);
4689
4690 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
4691 if (dbg_uwbemul) {
4692 tp_features.uwb = 1;
4693 pr_info("uwb switch emulation enabled\n");
4694 } else
4695 #endif
4696 if (tp_features.uwb &&
4697 !(status & TP_ACPI_UWB_HWPRESENT)) {
4698 /* no uwb hardware present in system */
4699 tp_features.uwb = 0;
4700 dbg_printk(TPACPI_DBG_INIT,
4701 "uwb hardware not installed\n");
4702 }
4703
4704 if (!tp_features.uwb)
4705 return 1;
4706
4707 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
4708 &uwb_tprfk_ops,
4709 RFKILL_TYPE_UWB,
4710 TPACPI_RFK_UWB_SW_NAME,
4711 false);
4712 return res;
4713 }
4714
4715 static struct ibm_struct uwb_driver_data = {
4716 .name = "uwb",
4717 .exit = uwb_exit,
4718 .flags.experimental = 1,
4719 };
4720
4721 /*************************************************************************
4722 * Video subdriver
4723 */
4724
4725 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
4726
4727 enum video_access_mode {
4728 TPACPI_VIDEO_NONE = 0,
4729 TPACPI_VIDEO_570, /* 570 */
4730 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
4731 TPACPI_VIDEO_NEW, /* all others */
4732 };
4733
4734 enum { /* video status flags, based on VIDEO_570 */
4735 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
4736 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
4737 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
4738 };
4739
4740 enum { /* TPACPI_VIDEO_570 constants */
4741 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
4742 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
4743 * video_status_flags */
4744 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
4745 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
4746 };
4747
4748 static enum video_access_mode video_supported;
4749 static int video_orig_autosw;
4750
4751 static int video_autosw_get(void);
4752 static int video_autosw_set(int enable);
4753
4754 TPACPI_HANDLE(vid, root,
4755 "\\_SB.PCI.AGP.VGA", /* 570 */
4756 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
4757 "\\_SB.PCI0.VID0", /* 770e */
4758 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
4759 "\\_SB.PCI0.AGP.VGA", /* X100e and a few others */
4760 "\\_SB.PCI0.AGP.VID", /* all others */
4761 ); /* R30, R31 */
4762
4763 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
4764
4765 static int __init video_init(struct ibm_init_struct *iibm)
4766 {
4767 int ivga;
4768
4769 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
4770
4771 TPACPI_ACPIHANDLE_INIT(vid);
4772 if (tpacpi_is_ibm())
4773 TPACPI_ACPIHANDLE_INIT(vid2);
4774
4775 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
4776 /* G41, assume IVGA doesn't change */
4777 vid_handle = vid2_handle;
4778
4779 if (!vid_handle)
4780 /* video switching not supported on R30, R31 */
4781 video_supported = TPACPI_VIDEO_NONE;
4782 else if (tpacpi_is_ibm() &&
4783 acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
4784 /* 570 */
4785 video_supported = TPACPI_VIDEO_570;
4786 else if (tpacpi_is_ibm() &&
4787 acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
4788 /* 600e/x, 770e, 770x */
4789 video_supported = TPACPI_VIDEO_770;
4790 else
4791 /* all others */
4792 video_supported = TPACPI_VIDEO_NEW;
4793
4794 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
4795 str_supported(video_supported != TPACPI_VIDEO_NONE),
4796 video_supported);
4797
4798 return (video_supported != TPACPI_VIDEO_NONE) ? 0 : 1;
4799 }
4800
4801 static void video_exit(void)
4802 {
4803 dbg_printk(TPACPI_DBG_EXIT,
4804 "restoring original video autoswitch mode\n");
4805 if (video_autosw_set(video_orig_autosw))
4806 pr_err("error while trying to restore original "
4807 "video autoswitch mode\n");
4808 }
4809
4810 static int video_outputsw_get(void)
4811 {
4812 int status = 0;
4813 int i;
4814
4815 switch (video_supported) {
4816 case TPACPI_VIDEO_570:
4817 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4818 TP_ACPI_VIDEO_570_PHSCMD))
4819 return -EIO;
4820 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4821 break;
4822 case TPACPI_VIDEO_770:
4823 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4824 return -EIO;
4825 if (i)
4826 status |= TP_ACPI_VIDEO_S_LCD;
4827 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4828 return -EIO;
4829 if (i)
4830 status |= TP_ACPI_VIDEO_S_CRT;
4831 break;
4832 case TPACPI_VIDEO_NEW:
4833 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4834 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4835 return -EIO;
4836 if (i)
4837 status |= TP_ACPI_VIDEO_S_CRT;
4838
4839 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4840 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4841 return -EIO;
4842 if (i)
4843 status |= TP_ACPI_VIDEO_S_LCD;
4844 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4845 return -EIO;
4846 if (i)
4847 status |= TP_ACPI_VIDEO_S_DVI;
4848 break;
4849 default:
4850 return -ENOSYS;
4851 }
4852
4853 return status;
4854 }
4855
4856 static int video_outputsw_set(int status)
4857 {
4858 int autosw;
4859 int res = 0;
4860
4861 switch (video_supported) {
4862 case TPACPI_VIDEO_570:
4863 res = acpi_evalf(NULL, NULL,
4864 "\\_SB.PHS2", "vdd",
4865 TP_ACPI_VIDEO_570_PHS2CMD,
4866 status | TP_ACPI_VIDEO_570_PHS2SET);
4867 break;
4868 case TPACPI_VIDEO_770:
4869 autosw = video_autosw_get();
4870 if (autosw < 0)
4871 return autosw;
4872
4873 res = video_autosw_set(1);
4874 if (res)
4875 return res;
4876 res = acpi_evalf(vid_handle, NULL,
4877 "ASWT", "vdd", status * 0x100, 0);
4878 if (!autosw && video_autosw_set(autosw)) {
4879 pr_err("video auto-switch left enabled due to error\n");
4880 return -EIO;
4881 }
4882 break;
4883 case TPACPI_VIDEO_NEW:
4884 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4885 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4886 break;
4887 default:
4888 return -ENOSYS;
4889 }
4890
4891 return (res) ? 0 : -EIO;
4892 }
4893
4894 static int video_autosw_get(void)
4895 {
4896 int autosw = 0;
4897
4898 switch (video_supported) {
4899 case TPACPI_VIDEO_570:
4900 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4901 return -EIO;
4902 break;
4903 case TPACPI_VIDEO_770:
4904 case TPACPI_VIDEO_NEW:
4905 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4906 return -EIO;
4907 break;
4908 default:
4909 return -ENOSYS;
4910 }
4911
4912 return autosw & 1;
4913 }
4914
4915 static int video_autosw_set(int enable)
4916 {
4917 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable) ? 1 : 0))
4918 return -EIO;
4919 return 0;
4920 }
4921
4922 static int video_outputsw_cycle(void)
4923 {
4924 int autosw = video_autosw_get();
4925 int res;
4926
4927 if (autosw < 0)
4928 return autosw;
4929
4930 switch (video_supported) {
4931 case TPACPI_VIDEO_570:
4932 res = video_autosw_set(1);
4933 if (res)
4934 return res;
4935 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4936 break;
4937 case TPACPI_VIDEO_770:
4938 case TPACPI_VIDEO_NEW:
4939 res = video_autosw_set(1);
4940 if (res)
4941 return res;
4942 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4943 break;
4944 default:
4945 return -ENOSYS;
4946 }
4947 if (!autosw && video_autosw_set(autosw)) {
4948 pr_err("video auto-switch left enabled due to error\n");
4949 return -EIO;
4950 }
4951
4952 return (res) ? 0 : -EIO;
4953 }
4954
4955 static int video_expand_toggle(void)
4956 {
4957 switch (video_supported) {
4958 case TPACPI_VIDEO_570:
4959 return acpi_evalf(ec_handle, NULL, "_Q17", "v") ?
4960 0 : -EIO;
4961 case TPACPI_VIDEO_770:
4962 return acpi_evalf(vid_handle, NULL, "VEXP", "v") ?
4963 0 : -EIO;
4964 case TPACPI_VIDEO_NEW:
4965 return acpi_evalf(NULL, NULL, "\\VEXP", "v") ?
4966 0 : -EIO;
4967 default:
4968 return -ENOSYS;
4969 }
4970 /* not reached */
4971 }
4972
4973 static int video_read(struct seq_file *m)
4974 {
4975 int status, autosw;
4976
4977 if (video_supported == TPACPI_VIDEO_NONE) {
4978 seq_printf(m, "status:\t\tnot supported\n");
4979 return 0;
4980 }
4981
4982 /* Even reads can crash X.org, so... */
4983 if (!capable(CAP_SYS_ADMIN))
4984 return -EPERM;
4985
4986 status = video_outputsw_get();
4987 if (status < 0)
4988 return status;
4989
4990 autosw = video_autosw_get();
4991 if (autosw < 0)
4992 return autosw;
4993
4994 seq_printf(m, "status:\t\tsupported\n");
4995 seq_printf(m, "lcd:\t\t%s\n", enabled(status, 0));
4996 seq_printf(m, "crt:\t\t%s\n", enabled(status, 1));
4997 if (video_supported == TPACPI_VIDEO_NEW)
4998 seq_printf(m, "dvi:\t\t%s\n", enabled(status, 3));
4999 seq_printf(m, "auto:\t\t%s\n", enabled(autosw, 0));
5000 seq_printf(m, "commands:\tlcd_enable, lcd_disable\n");
5001 seq_printf(m, "commands:\tcrt_enable, crt_disable\n");
5002 if (video_supported == TPACPI_VIDEO_NEW)
5003 seq_printf(m, "commands:\tdvi_enable, dvi_disable\n");
5004 seq_printf(m, "commands:\tauto_enable, auto_disable\n");
5005 seq_printf(m, "commands:\tvideo_switch, expand_toggle\n");
5006
5007 return 0;
5008 }
5009
5010 static int video_write(char *buf)
5011 {
5012 char *cmd;
5013 int enable, disable, status;
5014 int res;
5015
5016 if (video_supported == TPACPI_VIDEO_NONE)
5017 return -ENODEV;
5018
5019 /* Even reads can crash X.org, let alone writes... */
5020 if (!capable(CAP_SYS_ADMIN))
5021 return -EPERM;
5022
5023 enable = 0;
5024 disable = 0;
5025
5026 while ((cmd = next_cmd(&buf))) {
5027 if (strlencmp(cmd, "lcd_enable") == 0) {
5028 enable |= TP_ACPI_VIDEO_S_LCD;
5029 } else if (strlencmp(cmd, "lcd_disable") == 0) {
5030 disable |= TP_ACPI_VIDEO_S_LCD;
5031 } else if (strlencmp(cmd, "crt_enable") == 0) {
5032 enable |= TP_ACPI_VIDEO_S_CRT;
5033 } else if (strlencmp(cmd, "crt_disable") == 0) {
5034 disable |= TP_ACPI_VIDEO_S_CRT;
5035 } else if (video_supported == TPACPI_VIDEO_NEW &&
5036 strlencmp(cmd, "dvi_enable") == 0) {
5037 enable |= TP_ACPI_VIDEO_S_DVI;
5038 } else if (video_supported == TPACPI_VIDEO_NEW &&
5039 strlencmp(cmd, "dvi_disable") == 0) {
5040 disable |= TP_ACPI_VIDEO_S_DVI;
5041 } else if (strlencmp(cmd, "auto_enable") == 0) {
5042 res = video_autosw_set(1);
5043 if (res)
5044 return res;
5045 } else if (strlencmp(cmd, "auto_disable") == 0) {
5046 res = video_autosw_set(0);
5047 if (res)
5048 return res;
5049 } else if (strlencmp(cmd, "video_switch") == 0) {
5050 res = video_outputsw_cycle();
5051 if (res)
5052 return res;
5053 } else if (strlencmp(cmd, "expand_toggle") == 0) {
5054 res = video_expand_toggle();
5055 if (res)
5056 return res;
5057 } else
5058 return -EINVAL;
5059 }
5060
5061 if (enable || disable) {
5062 status = video_outputsw_get();
5063 if (status < 0)
5064 return status;
5065 res = video_outputsw_set((status & ~disable) | enable);
5066 if (res)
5067 return res;
5068 }
5069
5070 return 0;
5071 }
5072
5073 static struct ibm_struct video_driver_data = {
5074 .name = "video",
5075 .read = video_read,
5076 .write = video_write,
5077 .exit = video_exit,
5078 };
5079
5080 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
5081
5082 /*************************************************************************
5083 * Keyboard backlight subdriver
5084 */
5085
5086 static enum led_brightness kbdlight_brightness;
5087 static DEFINE_MUTEX(kbdlight_mutex);
5088
5089 static int kbdlight_set_level(int level)
5090 {
5091 int ret = 0;
5092
5093 if (!hkey_handle)
5094 return -ENXIO;
5095
5096 mutex_lock(&kbdlight_mutex);
5097
5098 if (!acpi_evalf(hkey_handle, NULL, "MLCS", "dd", level))
5099 ret = -EIO;
5100 else
5101 kbdlight_brightness = level;
5102
5103 mutex_unlock(&kbdlight_mutex);
5104
5105 return ret;
5106 }
5107
5108 static int kbdlight_get_level(void)
5109 {
5110 int status = 0;
5111
5112 if (!hkey_handle)
5113 return -ENXIO;
5114
5115 if (!acpi_evalf(hkey_handle, &status, "MLCG", "dd", 0))
5116 return -EIO;
5117
5118 if (status < 0)
5119 return status;
5120
5121 return status & 0x3;
5122 }
5123
5124 static bool kbdlight_is_supported(void)
5125 {
5126 int status = 0;
5127
5128 if (!hkey_handle)
5129 return false;
5130
5131 if (!acpi_has_method(hkey_handle, "MLCG")) {
5132 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG is unavailable\n");
5133 return false;
5134 }
5135
5136 if (!acpi_evalf(hkey_handle, &status, "MLCG", "qdd", 0)) {
5137 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG failed\n");
5138 return false;
5139 }
5140
5141 if (status < 0) {
5142 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG err: %d\n", status);
5143 return false;
5144 }
5145
5146 vdbg_printk(TPACPI_DBG_INIT, "kbdlight MLCG returned 0x%x\n", status);
5147 /*
5148 * Guessed test for keyboard backlight:
5149 *
5150 * Machines with backlight keyboard return:
5151 * b010100000010000000XX - ThinkPad X1 Carbon 3rd
5152 * b110100010010000000XX - ThinkPad x230
5153 * b010100000010000000XX - ThinkPad x240
5154 * b010100000010000000XX - ThinkPad W541
5155 * (XX is current backlight level)
5156 *
5157 * Machines without backlight keyboard return:
5158 * b10100001000000000000 - ThinkPad x230
5159 * b10110001000000000000 - ThinkPad E430
5160 * b00000000000000000000 - ThinkPad E450
5161 *
5162 * Candidate BITs for detection test (XOR):
5163 * b01000000001000000000
5164 * ^
5165 */
5166 return status & BIT(9);
5167 }
5168
5169 static int kbdlight_sysfs_set(struct led_classdev *led_cdev,
5170 enum led_brightness brightness)
5171 {
5172 return kbdlight_set_level(brightness);
5173 }
5174
5175 static enum led_brightness kbdlight_sysfs_get(struct led_classdev *led_cdev)
5176 {
5177 int level;
5178
5179 level = kbdlight_get_level();
5180 if (level < 0)
5181 return 0;
5182
5183 return level;
5184 }
5185
5186 static struct tpacpi_led_classdev tpacpi_led_kbdlight = {
5187 .led_classdev = {
5188 .name = "tpacpi::kbd_backlight",
5189 .max_brightness = 2,
5190 .flags = LED_BRIGHT_HW_CHANGED,
5191 .brightness_set_blocking = &kbdlight_sysfs_set,
5192 .brightness_get = &kbdlight_sysfs_get,
5193 }
5194 };
5195
5196 static int __init kbdlight_init(struct ibm_init_struct *iibm)
5197 {
5198 int rc;
5199
5200 vdbg_printk(TPACPI_DBG_INIT, "initializing kbdlight subdriver\n");
5201
5202 TPACPI_ACPIHANDLE_INIT(hkey);
5203
5204 if (!kbdlight_is_supported()) {
5205 tp_features.kbdlight = 0;
5206 vdbg_printk(TPACPI_DBG_INIT, "kbdlight is unsupported\n");
5207 return 1;
5208 }
5209
5210 kbdlight_brightness = kbdlight_sysfs_get(NULL);
5211 tp_features.kbdlight = 1;
5212
5213 rc = led_classdev_register(&tpacpi_pdev->dev,
5214 &tpacpi_led_kbdlight.led_classdev);
5215 if (rc < 0) {
5216 tp_features.kbdlight = 0;
5217 return rc;
5218 }
5219
5220 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask |
5221 TP_ACPI_HKEY_KBD_LIGHT_MASK);
5222 return 0;
5223 }
5224
5225 static void kbdlight_exit(void)
5226 {
5227 if (tp_features.kbdlight)
5228 led_classdev_unregister(&tpacpi_led_kbdlight.led_classdev);
5229 }
5230
5231 static int kbdlight_set_level_and_update(int level)
5232 {
5233 int ret;
5234 struct led_classdev *led_cdev;
5235
5236 ret = kbdlight_set_level(level);
5237 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5238
5239 if (ret == 0 && !(led_cdev->flags & LED_SUSPENDED))
5240 led_cdev->brightness = level;
5241
5242 return ret;
5243 }
5244
5245 static int kbdlight_read(struct seq_file *m)
5246 {
5247 int level;
5248
5249 if (!tp_features.kbdlight) {
5250 seq_printf(m, "status:\t\tnot supported\n");
5251 } else {
5252 level = kbdlight_get_level();
5253 if (level < 0)
5254 seq_printf(m, "status:\t\terror %d\n", level);
5255 else
5256 seq_printf(m, "status:\t\t%d\n", level);
5257 seq_printf(m, "commands:\t0, 1, 2\n");
5258 }
5259
5260 return 0;
5261 }
5262
5263 static int kbdlight_write(char *buf)
5264 {
5265 char *cmd;
5266 int level = -1;
5267
5268 if (!tp_features.kbdlight)
5269 return -ENODEV;
5270
5271 while ((cmd = next_cmd(&buf))) {
5272 if (strlencmp(cmd, "0") == 0)
5273 level = 0;
5274 else if (strlencmp(cmd, "1") == 0)
5275 level = 1;
5276 else if (strlencmp(cmd, "2") == 0)
5277 level = 2;
5278 else
5279 return -EINVAL;
5280 }
5281
5282 if (level == -1)
5283 return -EINVAL;
5284
5285 return kbdlight_set_level_and_update(level);
5286 }
5287
5288 static void kbdlight_suspend(void)
5289 {
5290 struct led_classdev *led_cdev;
5291
5292 if (!tp_features.kbdlight)
5293 return;
5294
5295 led_cdev = &tpacpi_led_kbdlight.led_classdev;
5296 led_update_brightness(led_cdev);
5297 led_classdev_suspend(led_cdev);
5298 }
5299
5300 static void kbdlight_resume(void)
5301 {
5302 if (!tp_features.kbdlight)
5303 return;
5304
5305 led_classdev_resume(&tpacpi_led_kbdlight.led_classdev);
5306 }
5307
5308 static struct ibm_struct kbdlight_driver_data = {
5309 .name = "kbdlight",
5310 .read = kbdlight_read,
5311 .write = kbdlight_write,
5312 .suspend = kbdlight_suspend,
5313 .resume = kbdlight_resume,
5314 .exit = kbdlight_exit,
5315 };
5316
5317 /*************************************************************************
5318 * Light (thinklight) subdriver
5319 */
5320
5321 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
5322 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
5323
5324 static int light_get_status(void)
5325 {
5326 int status = 0;
5327
5328 if (tp_features.light_status) {
5329 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
5330 return -EIO;
5331 return (!!status);
5332 }
5333
5334 return -ENXIO;
5335 }
5336
5337 static int light_set_status(int status)
5338 {
5339 int rc;
5340
5341 if (tp_features.light) {
5342 if (cmos_handle) {
5343 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
5344 (status) ?
5345 TP_CMOS_THINKLIGHT_ON :
5346 TP_CMOS_THINKLIGHT_OFF);
5347 } else {
5348 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
5349 (status) ? 1 : 0);
5350 }
5351 return (rc) ? 0 : -EIO;
5352 }
5353
5354 return -ENXIO;
5355 }
5356
5357 static int light_sysfs_set(struct led_classdev *led_cdev,
5358 enum led_brightness brightness)
5359 {
5360 return light_set_status((brightness != LED_OFF) ?
5361 TPACPI_LED_ON : TPACPI_LED_OFF);
5362 }
5363
5364 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
5365 {
5366 return (light_get_status() == 1) ? LED_FULL : LED_OFF;
5367 }
5368
5369 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
5370 .led_classdev = {
5371 .name = "tpacpi::thinklight",
5372 .brightness_set_blocking = &light_sysfs_set,
5373 .brightness_get = &light_sysfs_get,
5374 }
5375 };
5376
5377 static int __init light_init(struct ibm_init_struct *iibm)
5378 {
5379 int rc;
5380
5381 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
5382
5383 if (tpacpi_is_ibm()) {
5384 TPACPI_ACPIHANDLE_INIT(ledb);
5385 TPACPI_ACPIHANDLE_INIT(lght);
5386 }
5387 TPACPI_ACPIHANDLE_INIT(cmos);
5388
5389 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
5390 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
5391
5392 if (tp_features.light)
5393 /* light status not supported on
5394 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
5395 tp_features.light_status =
5396 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
5397
5398 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
5399 str_supported(tp_features.light),
5400 str_supported(tp_features.light_status));
5401
5402 if (!tp_features.light)
5403 return 1;
5404
5405 rc = led_classdev_register(&tpacpi_pdev->dev,
5406 &tpacpi_led_thinklight.led_classdev);
5407
5408 if (rc < 0) {
5409 tp_features.light = 0;
5410 tp_features.light_status = 0;
5411 } else {
5412 rc = 0;
5413 }
5414
5415 return rc;
5416 }
5417
5418 static void light_exit(void)
5419 {
5420 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
5421 }
5422
5423 static int light_read(struct seq_file *m)
5424 {
5425 int status;
5426
5427 if (!tp_features.light) {
5428 seq_printf(m, "status:\t\tnot supported\n");
5429 } else if (!tp_features.light_status) {
5430 seq_printf(m, "status:\t\tunknown\n");
5431 seq_printf(m, "commands:\ton, off\n");
5432 } else {
5433 status = light_get_status();
5434 if (status < 0)
5435 return status;
5436 seq_printf(m, "status:\t\t%s\n", onoff(status, 0));
5437 seq_printf(m, "commands:\ton, off\n");
5438 }
5439
5440 return 0;
5441 }
5442
5443 static int light_write(char *buf)
5444 {
5445 char *cmd;
5446 int newstatus = 0;
5447
5448 if (!tp_features.light)
5449 return -ENODEV;
5450
5451 while ((cmd = next_cmd(&buf))) {
5452 if (strlencmp(cmd, "on") == 0) {
5453 newstatus = 1;
5454 } else if (strlencmp(cmd, "off") == 0) {
5455 newstatus = 0;
5456 } else
5457 return -EINVAL;
5458 }
5459
5460 return light_set_status(newstatus);
5461 }
5462
5463 static struct ibm_struct light_driver_data = {
5464 .name = "light",
5465 .read = light_read,
5466 .write = light_write,
5467 .exit = light_exit,
5468 };
5469
5470 /*************************************************************************
5471 * CMOS subdriver
5472 */
5473
5474 /* sysfs cmos_command -------------------------------------------------- */
5475 static ssize_t cmos_command_store(struct device *dev,
5476 struct device_attribute *attr,
5477 const char *buf, size_t count)
5478 {
5479 unsigned long cmos_cmd;
5480 int res;
5481
5482 if (parse_strtoul(buf, 21, &cmos_cmd))
5483 return -EINVAL;
5484
5485 res = issue_thinkpad_cmos_command(cmos_cmd);
5486 return (res) ? res : count;
5487 }
5488
5489 static DEVICE_ATTR_WO(cmos_command);
5490
5491 /* --------------------------------------------------------------------- */
5492
5493 static int __init cmos_init(struct ibm_init_struct *iibm)
5494 {
5495 int res;
5496
5497 vdbg_printk(TPACPI_DBG_INIT,
5498 "initializing cmos commands subdriver\n");
5499
5500 TPACPI_ACPIHANDLE_INIT(cmos);
5501
5502 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
5503 str_supported(cmos_handle != NULL));
5504
5505 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5506 if (res)
5507 return res;
5508
5509 return (cmos_handle) ? 0 : 1;
5510 }
5511
5512 static void cmos_exit(void)
5513 {
5514 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
5515 }
5516
5517 static int cmos_read(struct seq_file *m)
5518 {
5519 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
5520 R30, R31, T20-22, X20-21 */
5521 if (!cmos_handle)
5522 seq_printf(m, "status:\t\tnot supported\n");
5523 else {
5524 seq_printf(m, "status:\t\tsupported\n");
5525 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-21)\n");
5526 }
5527
5528 return 0;
5529 }
5530
5531 static int cmos_write(char *buf)
5532 {
5533 char *cmd;
5534 int cmos_cmd, res;
5535
5536 while ((cmd = next_cmd(&buf))) {
5537 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
5538 cmos_cmd >= 0 && cmos_cmd <= 21) {
5539 /* cmos_cmd set */
5540 } else
5541 return -EINVAL;
5542
5543 res = issue_thinkpad_cmos_command(cmos_cmd);
5544 if (res)
5545 return res;
5546 }
5547
5548 return 0;
5549 }
5550
5551 static struct ibm_struct cmos_driver_data = {
5552 .name = "cmos",
5553 .read = cmos_read,
5554 .write = cmos_write,
5555 .exit = cmos_exit,
5556 };
5557
5558 /*************************************************************************
5559 * LED subdriver
5560 */
5561
5562 enum led_access_mode {
5563 TPACPI_LED_NONE = 0,
5564 TPACPI_LED_570, /* 570 */
5565 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5566 TPACPI_LED_NEW, /* all others */
5567 };
5568
5569 enum { /* For TPACPI_LED_OLD */
5570 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
5571 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
5572 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
5573 };
5574
5575 static enum led_access_mode led_supported;
5576
5577 static acpi_handle led_handle;
5578
5579 #define TPACPI_LED_NUMLEDS 16
5580 static struct tpacpi_led_classdev *tpacpi_leds;
5581 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
5582 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
5583 /* there's a limit of 19 chars + NULL before 2.6.26 */
5584 "tpacpi::power",
5585 "tpacpi:orange:batt",
5586 "tpacpi:green:batt",
5587 "tpacpi::dock_active",
5588 "tpacpi::bay_active",
5589 "tpacpi::dock_batt",
5590 "tpacpi::unknown_led",
5591 "tpacpi::standby",
5592 "tpacpi::dock_status1",
5593 "tpacpi::dock_status2",
5594 "tpacpi::unknown_led2",
5595 "tpacpi::unknown_led3",
5596 "tpacpi::thinkvantage",
5597 };
5598 #define TPACPI_SAFE_LEDS 0x1081U
5599
5600 static inline bool tpacpi_is_led_restricted(const unsigned int led)
5601 {
5602 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5603 return false;
5604 #else
5605 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
5606 #endif
5607 }
5608
5609 static int led_get_status(const unsigned int led)
5610 {
5611 int status;
5612 enum led_status_t led_s;
5613
5614 switch (led_supported) {
5615 case TPACPI_LED_570:
5616 if (!acpi_evalf(ec_handle,
5617 &status, "GLED", "dd", 1 << led))
5618 return -EIO;
5619 led_s = (status == 0) ?
5620 TPACPI_LED_OFF :
5621 ((status == 1) ?
5622 TPACPI_LED_ON :
5623 TPACPI_LED_BLINK);
5624 tpacpi_led_state_cache[led] = led_s;
5625 return led_s;
5626 default:
5627 return -ENXIO;
5628 }
5629
5630 /* not reached */
5631 }
5632
5633 static int led_set_status(const unsigned int led,
5634 const enum led_status_t ledstatus)
5635 {
5636 /* off, on, blink. Index is led_status_t */
5637 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
5638 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
5639
5640 int rc = 0;
5641
5642 switch (led_supported) {
5643 case TPACPI_LED_570:
5644 /* 570 */
5645 if (unlikely(led > 7))
5646 return -EINVAL;
5647 if (unlikely(tpacpi_is_led_restricted(led)))
5648 return -EPERM;
5649 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5650 (1 << led), led_sled_arg1[ledstatus]))
5651 rc = -EIO;
5652 break;
5653 case TPACPI_LED_OLD:
5654 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
5655 if (unlikely(led > 7))
5656 return -EINVAL;
5657 if (unlikely(tpacpi_is_led_restricted(led)))
5658 return -EPERM;
5659 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
5660 if (rc >= 0)
5661 rc = ec_write(TPACPI_LED_EC_HLBL,
5662 (ledstatus == TPACPI_LED_BLINK) << led);
5663 if (rc >= 0)
5664 rc = ec_write(TPACPI_LED_EC_HLCL,
5665 (ledstatus != TPACPI_LED_OFF) << led);
5666 break;
5667 case TPACPI_LED_NEW:
5668 /* all others */
5669 if (unlikely(led >= TPACPI_LED_NUMLEDS))
5670 return -EINVAL;
5671 if (unlikely(tpacpi_is_led_restricted(led)))
5672 return -EPERM;
5673 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
5674 led, led_led_arg1[ledstatus]))
5675 rc = -EIO;
5676 break;
5677 default:
5678 rc = -ENXIO;
5679 }
5680
5681 if (!rc)
5682 tpacpi_led_state_cache[led] = ledstatus;
5683
5684 return rc;
5685 }
5686
5687 static int led_sysfs_set(struct led_classdev *led_cdev,
5688 enum led_brightness brightness)
5689 {
5690 struct tpacpi_led_classdev *data = container_of(led_cdev,
5691 struct tpacpi_led_classdev, led_classdev);
5692 enum led_status_t new_state;
5693
5694 if (brightness == LED_OFF)
5695 new_state = TPACPI_LED_OFF;
5696 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
5697 new_state = TPACPI_LED_ON;
5698 else
5699 new_state = TPACPI_LED_BLINK;
5700
5701 return led_set_status(data->led, new_state);
5702 }
5703
5704 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
5705 unsigned long *delay_on, unsigned long *delay_off)
5706 {
5707 struct tpacpi_led_classdev *data = container_of(led_cdev,
5708 struct tpacpi_led_classdev, led_classdev);
5709
5710 /* Can we choose the flash rate? */
5711 if (*delay_on == 0 && *delay_off == 0) {
5712 /* yes. set them to the hardware blink rate (1 Hz) */
5713 *delay_on = 500; /* ms */
5714 *delay_off = 500; /* ms */
5715 } else if ((*delay_on != 500) || (*delay_off != 500))
5716 return -EINVAL;
5717
5718 return led_set_status(data->led, TPACPI_LED_BLINK);
5719 }
5720
5721 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
5722 {
5723 int rc;
5724
5725 struct tpacpi_led_classdev *data = container_of(led_cdev,
5726 struct tpacpi_led_classdev, led_classdev);
5727
5728 rc = led_get_status(data->led);
5729
5730 if (rc == TPACPI_LED_OFF || rc < 0)
5731 rc = LED_OFF; /* no error handling in led class :( */
5732 else
5733 rc = LED_FULL;
5734
5735 return rc;
5736 }
5737
5738 static void led_exit(void)
5739 {
5740 unsigned int i;
5741
5742 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5743 if (tpacpi_leds[i].led_classdev.name)
5744 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
5745 }
5746
5747 kfree(tpacpi_leds);
5748 }
5749
5750 static int __init tpacpi_init_led(unsigned int led)
5751 {
5752 int rc;
5753
5754 tpacpi_leds[led].led = led;
5755
5756 /* LEDs with no name don't get registered */
5757 if (!tpacpi_led_names[led])
5758 return 0;
5759
5760 tpacpi_leds[led].led_classdev.brightness_set_blocking = &led_sysfs_set;
5761 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
5762 if (led_supported == TPACPI_LED_570)
5763 tpacpi_leds[led].led_classdev.brightness_get =
5764 &led_sysfs_get;
5765
5766 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
5767
5768 rc = led_classdev_register(&tpacpi_pdev->dev,
5769 &tpacpi_leds[led].led_classdev);
5770 if (rc < 0)
5771 tpacpi_leds[led].led_classdev.name = NULL;
5772
5773 return rc;
5774 }
5775
5776 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
5777 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
5778 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
5779 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
5780
5781 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
5782 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
5783 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
5784 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
5785 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
5786 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
5787 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
5788 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
5789
5790 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
5791 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
5792 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
5793 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
5794 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
5795
5796 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
5797 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
5798 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
5799 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
5800
5801 /* (1) - may have excess leds enabled on MSB */
5802
5803 /* Defaults (order matters, keep last, don't reorder!) */
5804 { /* Lenovo */
5805 .vendor = PCI_VENDOR_ID_LENOVO,
5806 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5807 .quirks = 0x1fffU,
5808 },
5809 { /* IBM ThinkPads with no EC version string */
5810 .vendor = PCI_VENDOR_ID_IBM,
5811 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
5812 .quirks = 0x00ffU,
5813 },
5814 { /* IBM ThinkPads with EC version string */
5815 .vendor = PCI_VENDOR_ID_IBM,
5816 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
5817 .quirks = 0x00bfU,
5818 },
5819 };
5820
5821 #undef TPACPI_LEDQ_IBM
5822 #undef TPACPI_LEDQ_LNV
5823
5824 static enum led_access_mode __init led_init_detect_mode(void)
5825 {
5826 acpi_status status;
5827
5828 if (tpacpi_is_ibm()) {
5829 /* 570 */
5830 status = acpi_get_handle(ec_handle, "SLED", &led_handle);
5831 if (ACPI_SUCCESS(status))
5832 return TPACPI_LED_570;
5833
5834 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
5835 status = acpi_get_handle(ec_handle, "SYSL", &led_handle);
5836 if (ACPI_SUCCESS(status))
5837 return TPACPI_LED_OLD;
5838 }
5839
5840 /* most others */
5841 status = acpi_get_handle(ec_handle, "LED", &led_handle);
5842 if (ACPI_SUCCESS(status))
5843 return TPACPI_LED_NEW;
5844
5845 /* R30, R31, and unknown firmwares */
5846 led_handle = NULL;
5847 return TPACPI_LED_NONE;
5848 }
5849
5850 static int __init led_init(struct ibm_init_struct *iibm)
5851 {
5852 unsigned int i;
5853 int rc;
5854 unsigned long useful_leds;
5855
5856 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
5857
5858 led_supported = led_init_detect_mode();
5859
5860 if (led_supported != TPACPI_LED_NONE) {
5861 useful_leds = tpacpi_check_quirks(led_useful_qtable,
5862 ARRAY_SIZE(led_useful_qtable));
5863
5864 if (!useful_leds) {
5865 led_handle = NULL;
5866 led_supported = TPACPI_LED_NONE;
5867 }
5868 }
5869
5870 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
5871 str_supported(led_supported), led_supported);
5872
5873 if (led_supported == TPACPI_LED_NONE)
5874 return 1;
5875
5876 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
5877 GFP_KERNEL);
5878 if (!tpacpi_leds) {
5879 pr_err("Out of memory for LED data\n");
5880 return -ENOMEM;
5881 }
5882
5883 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
5884 tpacpi_leds[i].led = -1;
5885
5886 if (!tpacpi_is_led_restricted(i) &&
5887 test_bit(i, &useful_leds)) {
5888 rc = tpacpi_init_led(i);
5889 if (rc < 0) {
5890 led_exit();
5891 return rc;
5892 }
5893 }
5894 }
5895
5896 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
5897 pr_notice("warning: userspace override of important "
5898 "firmware LEDs is enabled\n");
5899 #endif
5900 return 0;
5901 }
5902
5903 #define str_led_status(s) \
5904 ((s) == TPACPI_LED_OFF ? "off" : \
5905 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
5906
5907 static int led_read(struct seq_file *m)
5908 {
5909 if (!led_supported) {
5910 seq_printf(m, "status:\t\tnot supported\n");
5911 return 0;
5912 }
5913 seq_printf(m, "status:\t\tsupported\n");
5914
5915 if (led_supported == TPACPI_LED_570) {
5916 /* 570 */
5917 int i, status;
5918 for (i = 0; i < 8; i++) {
5919 status = led_get_status(i);
5920 if (status < 0)
5921 return -EIO;
5922 seq_printf(m, "%d:\t\t%s\n",
5923 i, str_led_status(status));
5924 }
5925 }
5926
5927 seq_printf(m, "commands:\t"
5928 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
5929
5930 return 0;
5931 }
5932
5933 static int led_write(char *buf)
5934 {
5935 char *cmd;
5936 int led, rc;
5937 enum led_status_t s;
5938
5939 if (!led_supported)
5940 return -ENODEV;
5941
5942 while ((cmd = next_cmd(&buf))) {
5943 if (sscanf(cmd, "%d", &led) != 1)
5944 return -EINVAL;
5945
5946 if (led < 0 || led > (TPACPI_LED_NUMLEDS - 1) ||
5947 tpacpi_leds[led].led < 0)
5948 return -ENODEV;
5949
5950 if (strstr(cmd, "off")) {
5951 s = TPACPI_LED_OFF;
5952 } else if (strstr(cmd, "on")) {
5953 s = TPACPI_LED_ON;
5954 } else if (strstr(cmd, "blink")) {
5955 s = TPACPI_LED_BLINK;
5956 } else {
5957 return -EINVAL;
5958 }
5959
5960 rc = led_set_status(led, s);
5961 if (rc < 0)
5962 return rc;
5963 }
5964
5965 return 0;
5966 }
5967
5968 static struct ibm_struct led_driver_data = {
5969 .name = "led",
5970 .read = led_read,
5971 .write = led_write,
5972 .exit = led_exit,
5973 };
5974
5975 /*************************************************************************
5976 * Beep subdriver
5977 */
5978
5979 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5980
5981 #define TPACPI_BEEP_Q1 0x0001
5982
5983 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
5984 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
5985 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
5986 };
5987
5988 static int __init beep_init(struct ibm_init_struct *iibm)
5989 {
5990 unsigned long quirks;
5991
5992 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5993
5994 TPACPI_ACPIHANDLE_INIT(beep);
5995
5996 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5997 str_supported(beep_handle != NULL));
5998
5999 quirks = tpacpi_check_quirks(beep_quirk_table,
6000 ARRAY_SIZE(beep_quirk_table));
6001
6002 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
6003
6004 return (beep_handle) ? 0 : 1;
6005 }
6006
6007 static int beep_read(struct seq_file *m)
6008 {
6009 if (!beep_handle)
6010 seq_printf(m, "status:\t\tnot supported\n");
6011 else {
6012 seq_printf(m, "status:\t\tsupported\n");
6013 seq_printf(m, "commands:\t<cmd> (<cmd> is 0-17)\n");
6014 }
6015
6016 return 0;
6017 }
6018
6019 static int beep_write(char *buf)
6020 {
6021 char *cmd;
6022 int beep_cmd;
6023
6024 if (!beep_handle)
6025 return -ENODEV;
6026
6027 while ((cmd = next_cmd(&buf))) {
6028 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
6029 beep_cmd >= 0 && beep_cmd <= 17) {
6030 /* beep_cmd set */
6031 } else
6032 return -EINVAL;
6033 if (tp_features.beep_needs_two_args) {
6034 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
6035 beep_cmd, 0))
6036 return -EIO;
6037 } else {
6038 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
6039 beep_cmd))
6040 return -EIO;
6041 }
6042 }
6043
6044 return 0;
6045 }
6046
6047 static struct ibm_struct beep_driver_data = {
6048 .name = "beep",
6049 .read = beep_read,
6050 .write = beep_write,
6051 };
6052
6053 /*************************************************************************
6054 * Thermal subdriver
6055 */
6056
6057 enum thermal_access_mode {
6058 TPACPI_THERMAL_NONE = 0, /* No thermal support */
6059 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
6060 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
6061 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
6062 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
6063 };
6064
6065 enum { /* TPACPI_THERMAL_TPEC_* */
6066 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
6067 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
6068 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
6069
6070 TPACPI_THERMAL_SENSOR_NA = -128000, /* Sensor not available */
6071 };
6072
6073
6074 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
6075 struct ibm_thermal_sensors_struct {
6076 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
6077 };
6078
6079 static enum thermal_access_mode thermal_read_mode;
6080
6081 /* idx is zero-based */
6082 static int thermal_get_sensor(int idx, s32 *value)
6083 {
6084 int t;
6085 s8 tmp;
6086 char tmpi[5];
6087
6088 t = TP_EC_THERMAL_TMP0;
6089
6090 switch (thermal_read_mode) {
6091 #if TPACPI_MAX_THERMAL_SENSORS >= 16
6092 case TPACPI_THERMAL_TPEC_16:
6093 if (idx >= 8 && idx <= 15) {
6094 t = TP_EC_THERMAL_TMP8;
6095 idx -= 8;
6096 }
6097 /* fallthrough */
6098 #endif
6099 case TPACPI_THERMAL_TPEC_8:
6100 if (idx <= 7) {
6101 if (!acpi_ec_read(t + idx, &tmp))
6102 return -EIO;
6103 *value = tmp * 1000;
6104 return 0;
6105 }
6106 break;
6107
6108 case TPACPI_THERMAL_ACPI_UPDT:
6109 if (idx <= 7) {
6110 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6111 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
6112 return -EIO;
6113 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6114 return -EIO;
6115 *value = (t - 2732) * 100;
6116 return 0;
6117 }
6118 break;
6119
6120 case TPACPI_THERMAL_ACPI_TMP07:
6121 if (idx <= 7) {
6122 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
6123 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
6124 return -EIO;
6125 if (t > 127 || t < -127)
6126 t = TP_EC_THERMAL_TMP_NA;
6127 *value = t * 1000;
6128 return 0;
6129 }
6130 break;
6131
6132 case TPACPI_THERMAL_NONE:
6133 default:
6134 return -ENOSYS;
6135 }
6136
6137 return -EINVAL;
6138 }
6139
6140 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
6141 {
6142 int res, i;
6143 int n;
6144
6145 n = 8;
6146 i = 0;
6147
6148 if (!s)
6149 return -EINVAL;
6150
6151 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
6152 n = 16;
6153
6154 for (i = 0 ; i < n; i++) {
6155 res = thermal_get_sensor(i, &s->temp[i]);
6156 if (res)
6157 return res;
6158 }
6159
6160 return n;
6161 }
6162
6163 static void thermal_dump_all_sensors(void)
6164 {
6165 int n, i;
6166 struct ibm_thermal_sensors_struct t;
6167
6168 n = thermal_get_sensors(&t);
6169 if (n <= 0)
6170 return;
6171
6172 pr_notice("temperatures (Celsius):");
6173
6174 for (i = 0; i < n; i++) {
6175 if (t.temp[i] != TPACPI_THERMAL_SENSOR_NA)
6176 pr_cont(" %d", (int)(t.temp[i] / 1000));
6177 else
6178 pr_cont(" N/A");
6179 }
6180
6181 pr_cont("\n");
6182 }
6183
6184 /* sysfs temp##_input -------------------------------------------------- */
6185
6186 static ssize_t thermal_temp_input_show(struct device *dev,
6187 struct device_attribute *attr,
6188 char *buf)
6189 {
6190 struct sensor_device_attribute *sensor_attr =
6191 to_sensor_dev_attr(attr);
6192 int idx = sensor_attr->index;
6193 s32 value;
6194 int res;
6195
6196 res = thermal_get_sensor(idx, &value);
6197 if (res)
6198 return res;
6199 if (value == TPACPI_THERMAL_SENSOR_NA)
6200 return -ENXIO;
6201
6202 return snprintf(buf, PAGE_SIZE, "%d\n", value);
6203 }
6204
6205 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
6206 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
6207 thermal_temp_input_show, NULL, _idxB)
6208
6209 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
6210 THERMAL_SENSOR_ATTR_TEMP(1, 0),
6211 THERMAL_SENSOR_ATTR_TEMP(2, 1),
6212 THERMAL_SENSOR_ATTR_TEMP(3, 2),
6213 THERMAL_SENSOR_ATTR_TEMP(4, 3),
6214 THERMAL_SENSOR_ATTR_TEMP(5, 4),
6215 THERMAL_SENSOR_ATTR_TEMP(6, 5),
6216 THERMAL_SENSOR_ATTR_TEMP(7, 6),
6217 THERMAL_SENSOR_ATTR_TEMP(8, 7),
6218 THERMAL_SENSOR_ATTR_TEMP(9, 8),
6219 THERMAL_SENSOR_ATTR_TEMP(10, 9),
6220 THERMAL_SENSOR_ATTR_TEMP(11, 10),
6221 THERMAL_SENSOR_ATTR_TEMP(12, 11),
6222 THERMAL_SENSOR_ATTR_TEMP(13, 12),
6223 THERMAL_SENSOR_ATTR_TEMP(14, 13),
6224 THERMAL_SENSOR_ATTR_TEMP(15, 14),
6225 THERMAL_SENSOR_ATTR_TEMP(16, 15),
6226 };
6227
6228 #define THERMAL_ATTRS(X) \
6229 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
6230
6231 static struct attribute *thermal_temp_input_attr[] = {
6232 THERMAL_ATTRS(8),
6233 THERMAL_ATTRS(9),
6234 THERMAL_ATTRS(10),
6235 THERMAL_ATTRS(11),
6236 THERMAL_ATTRS(12),
6237 THERMAL_ATTRS(13),
6238 THERMAL_ATTRS(14),
6239 THERMAL_ATTRS(15),
6240 THERMAL_ATTRS(0),
6241 THERMAL_ATTRS(1),
6242 THERMAL_ATTRS(2),
6243 THERMAL_ATTRS(3),
6244 THERMAL_ATTRS(4),
6245 THERMAL_ATTRS(5),
6246 THERMAL_ATTRS(6),
6247 THERMAL_ATTRS(7),
6248 NULL
6249 };
6250
6251 static const struct attribute_group thermal_temp_input16_group = {
6252 .attrs = thermal_temp_input_attr
6253 };
6254
6255 static const struct attribute_group thermal_temp_input8_group = {
6256 .attrs = &thermal_temp_input_attr[8]
6257 };
6258
6259 #undef THERMAL_SENSOR_ATTR_TEMP
6260 #undef THERMAL_ATTRS
6261
6262 /* --------------------------------------------------------------------- */
6263
6264 static int __init thermal_init(struct ibm_init_struct *iibm)
6265 {
6266 u8 t, ta1, ta2;
6267 int i;
6268 int acpi_tmp7;
6269 int res;
6270
6271 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
6272
6273 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
6274
6275 if (thinkpad_id.ec_model) {
6276 /*
6277 * Direct EC access mode: sensors at registers
6278 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
6279 * non-implemented, thermal sensors return 0x80 when
6280 * not available
6281 */
6282
6283 ta1 = ta2 = 0;
6284 for (i = 0; i < 8; i++) {
6285 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
6286 ta1 |= t;
6287 } else {
6288 ta1 = 0;
6289 break;
6290 }
6291 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
6292 ta2 |= t;
6293 } else {
6294 ta1 = 0;
6295 break;
6296 }
6297 }
6298 if (ta1 == 0) {
6299 /* This is sheer paranoia, but we handle it anyway */
6300 if (acpi_tmp7) {
6301 pr_err("ThinkPad ACPI EC access misbehaving, "
6302 "falling back to ACPI TMPx access "
6303 "mode\n");
6304 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6305 } else {
6306 pr_err("ThinkPad ACPI EC access misbehaving, "
6307 "disabling thermal sensors access\n");
6308 thermal_read_mode = TPACPI_THERMAL_NONE;
6309 }
6310 } else {
6311 thermal_read_mode =
6312 (ta2 != 0) ?
6313 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
6314 }
6315 } else if (acpi_tmp7) {
6316 if (tpacpi_is_ibm() &&
6317 acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
6318 /* 600e/x, 770e, 770x */
6319 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
6320 } else {
6321 /* IBM/LENOVO DSDT EC.TMPx access, max 8 sensors */
6322 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
6323 }
6324 } else {
6325 /* temperatures not supported on 570, G4x, R30, R31, R32 */
6326 thermal_read_mode = TPACPI_THERMAL_NONE;
6327 }
6328
6329 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
6330 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
6331 thermal_read_mode);
6332
6333 switch (thermal_read_mode) {
6334 case TPACPI_THERMAL_TPEC_16:
6335 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6336 &thermal_temp_input16_group);
6337 if (res)
6338 return res;
6339 break;
6340 case TPACPI_THERMAL_TPEC_8:
6341 case TPACPI_THERMAL_ACPI_TMP07:
6342 case TPACPI_THERMAL_ACPI_UPDT:
6343 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6344 &thermal_temp_input8_group);
6345 if (res)
6346 return res;
6347 break;
6348 case TPACPI_THERMAL_NONE:
6349 default:
6350 return 1;
6351 }
6352
6353 return 0;
6354 }
6355
6356 static void thermal_exit(void)
6357 {
6358 switch (thermal_read_mode) {
6359 case TPACPI_THERMAL_TPEC_16:
6360 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6361 &thermal_temp_input16_group);
6362 break;
6363 case TPACPI_THERMAL_TPEC_8:
6364 case TPACPI_THERMAL_ACPI_TMP07:
6365 case TPACPI_THERMAL_ACPI_UPDT:
6366 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6367 &thermal_temp_input8_group);
6368 break;
6369 case TPACPI_THERMAL_NONE:
6370 default:
6371 break;
6372 }
6373 }
6374
6375 static int thermal_read(struct seq_file *m)
6376 {
6377 int n, i;
6378 struct ibm_thermal_sensors_struct t;
6379
6380 n = thermal_get_sensors(&t);
6381 if (unlikely(n < 0))
6382 return n;
6383
6384 seq_printf(m, "temperatures:\t");
6385
6386 if (n > 0) {
6387 for (i = 0; i < (n - 1); i++)
6388 seq_printf(m, "%d ", t.temp[i] / 1000);
6389 seq_printf(m, "%d\n", t.temp[i] / 1000);
6390 } else
6391 seq_printf(m, "not supported\n");
6392
6393 return 0;
6394 }
6395
6396 static struct ibm_struct thermal_driver_data = {
6397 .name = "thermal",
6398 .read = thermal_read,
6399 .exit = thermal_exit,
6400 };
6401
6402 /*************************************************************************
6403 * Backlight/brightness subdriver
6404 */
6405
6406 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
6407
6408 /*
6409 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
6410 * CMOS NVRAM byte 0x5E, bits 0-3.
6411 *
6412 * EC HBRV (0x31) has the following layout
6413 * Bit 7: unknown function
6414 * Bit 6: unknown function
6415 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
6416 * Bit 4: must be set to zero to avoid problems
6417 * Bit 3-0: backlight brightness level
6418 *
6419 * brightness_get_raw returns status data in the HBRV layout
6420 *
6421 * WARNING: The X61 has been verified to use HBRV for something else, so
6422 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
6423 * testing on the very early *60 Lenovo models...
6424 */
6425
6426 enum {
6427 TP_EC_BACKLIGHT = 0x31,
6428
6429 /* TP_EC_BACKLIGHT bitmasks */
6430 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
6431 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
6432 TP_EC_BACKLIGHT_MAPSW = 0x20,
6433 };
6434
6435 enum tpacpi_brightness_access_mode {
6436 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
6437 TPACPI_BRGHT_MODE_EC, /* EC control */
6438 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
6439 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
6440 TPACPI_BRGHT_MODE_MAX
6441 };
6442
6443 static struct backlight_device *ibm_backlight_device;
6444
6445 static enum tpacpi_brightness_access_mode brightness_mode =
6446 TPACPI_BRGHT_MODE_MAX;
6447
6448 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6449
6450 static struct mutex brightness_mutex;
6451
6452 /* NVRAM brightness access,
6453 * call with brightness_mutex held! */
6454 static unsigned int tpacpi_brightness_nvram_get(void)
6455 {
6456 u8 lnvram;
6457
6458 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
6459 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6460 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
6461 lnvram &= bright_maxlvl;
6462
6463 return lnvram;
6464 }
6465
6466 static void tpacpi_brightness_checkpoint_nvram(void)
6467 {
6468 u8 lec = 0;
6469 u8 b_nvram;
6470
6471 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
6472 return;
6473
6474 vdbg_printk(TPACPI_DBG_BRGHT,
6475 "trying to checkpoint backlight level to NVRAM...\n");
6476
6477 if (mutex_lock_killable(&brightness_mutex) < 0)
6478 return;
6479
6480 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6481 goto unlock;
6482 lec &= TP_EC_BACKLIGHT_LVLMSK;
6483 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
6484
6485 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
6486 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
6487 /* NVRAM needs update */
6488 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
6489 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
6490 b_nvram |= lec;
6491 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
6492 dbg_printk(TPACPI_DBG_BRGHT,
6493 "updated NVRAM backlight level to %u (0x%02x)\n",
6494 (unsigned int) lec, (unsigned int) b_nvram);
6495 } else
6496 vdbg_printk(TPACPI_DBG_BRGHT,
6497 "NVRAM backlight level already is %u (0x%02x)\n",
6498 (unsigned int) lec, (unsigned int) b_nvram);
6499
6500 unlock:
6501 mutex_unlock(&brightness_mutex);
6502 }
6503
6504
6505 /* call with brightness_mutex held! */
6506 static int tpacpi_brightness_get_raw(int *status)
6507 {
6508 u8 lec = 0;
6509
6510 switch (brightness_mode) {
6511 case TPACPI_BRGHT_MODE_UCMS_STEP:
6512 *status = tpacpi_brightness_nvram_get();
6513 return 0;
6514 case TPACPI_BRGHT_MODE_EC:
6515 case TPACPI_BRGHT_MODE_ECNVRAM:
6516 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6517 return -EIO;
6518 *status = lec;
6519 return 0;
6520 default:
6521 return -ENXIO;
6522 }
6523 }
6524
6525 /* call with brightness_mutex held! */
6526 /* do NOT call with illegal backlight level value */
6527 static int tpacpi_brightness_set_ec(unsigned int value)
6528 {
6529 u8 lec = 0;
6530
6531 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
6532 return -EIO;
6533
6534 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
6535 (lec & TP_EC_BACKLIGHT_CMDMSK) |
6536 (value & TP_EC_BACKLIGHT_LVLMSK))))
6537 return -EIO;
6538
6539 return 0;
6540 }
6541
6542 /* call with brightness_mutex held! */
6543 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
6544 {
6545 int cmos_cmd, inc;
6546 unsigned int current_value, i;
6547
6548 current_value = tpacpi_brightness_nvram_get();
6549
6550 if (value == current_value)
6551 return 0;
6552
6553 cmos_cmd = (value > current_value) ?
6554 TP_CMOS_BRIGHTNESS_UP :
6555 TP_CMOS_BRIGHTNESS_DOWN;
6556 inc = (value > current_value) ? 1 : -1;
6557
6558 for (i = current_value; i != value; i += inc)
6559 if (issue_thinkpad_cmos_command(cmos_cmd))
6560 return -EIO;
6561
6562 return 0;
6563 }
6564
6565 /* May return EINTR which can always be mapped to ERESTARTSYS */
6566 static int brightness_set(unsigned int value)
6567 {
6568 int res;
6569
6570 if (value > bright_maxlvl)
6571 return -EINVAL;
6572
6573 vdbg_printk(TPACPI_DBG_BRGHT,
6574 "set backlight level to %d\n", value);
6575
6576 res = mutex_lock_killable(&brightness_mutex);
6577 if (res < 0)
6578 return res;
6579
6580 switch (brightness_mode) {
6581 case TPACPI_BRGHT_MODE_EC:
6582 case TPACPI_BRGHT_MODE_ECNVRAM:
6583 res = tpacpi_brightness_set_ec(value);
6584 break;
6585 case TPACPI_BRGHT_MODE_UCMS_STEP:
6586 res = tpacpi_brightness_set_ucmsstep(value);
6587 break;
6588 default:
6589 res = -ENXIO;
6590 }
6591
6592 mutex_unlock(&brightness_mutex);
6593 return res;
6594 }
6595
6596 /* sysfs backlight class ----------------------------------------------- */
6597
6598 static int brightness_update_status(struct backlight_device *bd)
6599 {
6600 unsigned int level =
6601 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
6602 bd->props.power == FB_BLANK_UNBLANK) ?
6603 bd->props.brightness : 0;
6604
6605 dbg_printk(TPACPI_DBG_BRGHT,
6606 "backlight: attempt to set level to %d\n",
6607 level);
6608
6609 /* it is the backlight class's job (caller) to handle
6610 * EINTR and other errors properly */
6611 return brightness_set(level);
6612 }
6613
6614 static int brightness_get(struct backlight_device *bd)
6615 {
6616 int status, res;
6617
6618 res = mutex_lock_killable(&brightness_mutex);
6619 if (res < 0)
6620 return 0;
6621
6622 res = tpacpi_brightness_get_raw(&status);
6623
6624 mutex_unlock(&brightness_mutex);
6625
6626 if (res < 0)
6627 return 0;
6628
6629 return status & TP_EC_BACKLIGHT_LVLMSK;
6630 }
6631
6632 static void tpacpi_brightness_notify_change(void)
6633 {
6634 backlight_force_update(ibm_backlight_device,
6635 BACKLIGHT_UPDATE_HOTKEY);
6636 }
6637
6638 static const struct backlight_ops ibm_backlight_data = {
6639 .get_brightness = brightness_get,
6640 .update_status = brightness_update_status,
6641 };
6642
6643 /* --------------------------------------------------------------------- */
6644
6645 /*
6646 * Call _BCL method of video device. On some ThinkPads this will
6647 * switch the firmware to the ACPI brightness control mode.
6648 */
6649
6650 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
6651 {
6652 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
6653 union acpi_object *obj;
6654 struct acpi_device *device, *child;
6655 int rc;
6656
6657 if (acpi_bus_get_device(handle, &device))
6658 return 0;
6659
6660 rc = 0;
6661 list_for_each_entry(child, &device->children, node) {
6662 acpi_status status = acpi_evaluate_object(child->handle, "_BCL",
6663 NULL, &buffer);
6664 if (ACPI_FAILURE(status))
6665 continue;
6666
6667 obj = (union acpi_object *)buffer.pointer;
6668 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
6669 pr_err("Unknown _BCL data, please report this to %s\n",
6670 TPACPI_MAIL);
6671 rc = 0;
6672 } else {
6673 rc = obj->package.count;
6674 }
6675 break;
6676 }
6677
6678 kfree(buffer.pointer);
6679 return rc;
6680 }
6681
6682
6683 /*
6684 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
6685 */
6686 static unsigned int __init tpacpi_check_std_acpi_brightness_support(void)
6687 {
6688 acpi_handle video_device;
6689 int bcl_levels = 0;
6690
6691 tpacpi_acpi_handle_locate("video", NULL, &video_device);
6692 if (video_device)
6693 bcl_levels = tpacpi_query_bcl_levels(video_device);
6694
6695 tp_features.bright_acpimode = (bcl_levels > 0);
6696
6697 return (bcl_levels > 2) ? (bcl_levels - 2) : 0;
6698 }
6699
6700 /*
6701 * These are only useful for models that have only one possibility
6702 * of GPU. If the BIOS model handles both ATI and Intel, don't use
6703 * these quirks.
6704 */
6705 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
6706 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
6707 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
6708
6709 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
6710 /* Models with ATI GPUs known to require ECNVRAM mode */
6711 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
6712
6713 /* Models with ATI GPUs that can use ECNVRAM */
6714 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_EC), /* R50,51 T40-42 */
6715 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6716 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_EC), /* R52 */
6717 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6718
6719 /* Models with Intel Extreme Graphics 2 */
6720 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_NOEC), /* X40 */
6721 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6722 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
6723
6724 /* Models with Intel GMA900 */
6725 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
6726 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
6727 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
6728 };
6729
6730 /*
6731 * Returns < 0 for error, otherwise sets tp_features.bright_*
6732 * and bright_maxlvl.
6733 */
6734 static void __init tpacpi_detect_brightness_capabilities(void)
6735 {
6736 unsigned int b;
6737
6738 vdbg_printk(TPACPI_DBG_INIT,
6739 "detecting firmware brightness interface capabilities\n");
6740
6741 /* we could run a quirks check here (same table used by
6742 * brightness_init) if needed */
6743
6744 /*
6745 * We always attempt to detect acpi support, so as to switch
6746 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
6747 * going to publish a backlight interface
6748 */
6749 b = tpacpi_check_std_acpi_brightness_support();
6750 switch (b) {
6751 case 16:
6752 bright_maxlvl = 15;
6753 break;
6754 case 8:
6755 case 0:
6756 bright_maxlvl = 7;
6757 break;
6758 default:
6759 tp_features.bright_unkfw = 1;
6760 bright_maxlvl = b - 1;
6761 }
6762 pr_debug("detected %u brightness levels\n", bright_maxlvl + 1);
6763 }
6764
6765 static int __init brightness_init(struct ibm_init_struct *iibm)
6766 {
6767 struct backlight_properties props;
6768 int b;
6769 unsigned long quirks;
6770
6771 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
6772
6773 mutex_init(&brightness_mutex);
6774
6775 quirks = tpacpi_check_quirks(brightness_quirk_table,
6776 ARRAY_SIZE(brightness_quirk_table));
6777
6778 /* tpacpi_detect_brightness_capabilities() must have run already */
6779
6780 /* if it is unknown, we don't handle it: it wouldn't be safe */
6781 if (tp_features.bright_unkfw)
6782 return 1;
6783
6784 if (!brightness_enable) {
6785 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6786 "brightness support disabled by "
6787 "module parameter\n");
6788 return 1;
6789 }
6790
6791 if (acpi_video_get_backlight_type() != acpi_backlight_vendor) {
6792 if (brightness_enable > 1) {
6793 pr_info("Standard ACPI backlight interface "
6794 "available, not loading native one\n");
6795 return 1;
6796 } else if (brightness_enable == 1) {
6797 pr_warn("Cannot enable backlight brightness support, "
6798 "ACPI is already handling it. Refer to the "
6799 "acpi_backlight kernel parameter.\n");
6800 return 1;
6801 }
6802 } else if (tp_features.bright_acpimode && brightness_enable > 1) {
6803 pr_notice("Standard ACPI backlight interface not "
6804 "available, thinkpad_acpi native "
6805 "brightness control enabled\n");
6806 }
6807
6808 /*
6809 * Check for module parameter bogosity, note that we
6810 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
6811 * able to detect "unspecified"
6812 */
6813 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
6814 return -EINVAL;
6815
6816 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
6817 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
6818 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
6819 if (quirks & TPACPI_BRGHT_Q_EC)
6820 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
6821 else
6822 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
6823
6824 dbg_printk(TPACPI_DBG_BRGHT,
6825 "driver auto-selected brightness_mode=%d\n",
6826 brightness_mode);
6827 }
6828
6829 /* Safety */
6830 if (!tpacpi_is_ibm() &&
6831 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
6832 brightness_mode == TPACPI_BRGHT_MODE_EC))
6833 return -EINVAL;
6834
6835 if (tpacpi_brightness_get_raw(&b) < 0)
6836 return 1;
6837
6838 memset(&props, 0, sizeof(struct backlight_properties));
6839 props.type = BACKLIGHT_PLATFORM;
6840 props.max_brightness = bright_maxlvl;
6841 props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
6842 ibm_backlight_device = backlight_device_register(TPACPI_BACKLIGHT_DEV_NAME,
6843 NULL, NULL,
6844 &ibm_backlight_data,
6845 &props);
6846 if (IS_ERR(ibm_backlight_device)) {
6847 int rc = PTR_ERR(ibm_backlight_device);
6848 ibm_backlight_device = NULL;
6849 pr_err("Could not register backlight device\n");
6850 return rc;
6851 }
6852 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6853 "brightness is supported\n");
6854
6855 if (quirks & TPACPI_BRGHT_Q_ASK) {
6856 pr_notice("brightness: will use unverified default: "
6857 "brightness_mode=%d\n", brightness_mode);
6858 pr_notice("brightness: please report to %s whether it works well "
6859 "or not on your ThinkPad\n", TPACPI_MAIL);
6860 }
6861
6862 /* Added by mistake in early 2007. Probably useless, but it could
6863 * be working around some unknown firmware problem where the value
6864 * read at startup doesn't match the real hardware state... so leave
6865 * it in place just in case */
6866 backlight_update_status(ibm_backlight_device);
6867
6868 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
6869 "brightness: registering brightness hotkeys "
6870 "as change notification\n");
6871 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
6872 | TP_ACPI_HKEY_BRGHTUP_MASK
6873 | TP_ACPI_HKEY_BRGHTDWN_MASK);
6874 return 0;
6875 }
6876
6877 static void brightness_suspend(void)
6878 {
6879 tpacpi_brightness_checkpoint_nvram();
6880 }
6881
6882 static void brightness_shutdown(void)
6883 {
6884 tpacpi_brightness_checkpoint_nvram();
6885 }
6886
6887 static void brightness_exit(void)
6888 {
6889 if (ibm_backlight_device) {
6890 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
6891 "calling backlight_device_unregister()\n");
6892 backlight_device_unregister(ibm_backlight_device);
6893 }
6894
6895 tpacpi_brightness_checkpoint_nvram();
6896 }
6897
6898 static int brightness_read(struct seq_file *m)
6899 {
6900 int level;
6901
6902 level = brightness_get(NULL);
6903 if (level < 0) {
6904 seq_printf(m, "level:\t\tunreadable\n");
6905 } else {
6906 seq_printf(m, "level:\t\t%d\n", level);
6907 seq_printf(m, "commands:\tup, down\n");
6908 seq_printf(m, "commands:\tlevel <level> (<level> is 0-%d)\n",
6909 bright_maxlvl);
6910 }
6911
6912 return 0;
6913 }
6914
6915 static int brightness_write(char *buf)
6916 {
6917 int level;
6918 int rc;
6919 char *cmd;
6920
6921 level = brightness_get(NULL);
6922 if (level < 0)
6923 return level;
6924
6925 while ((cmd = next_cmd(&buf))) {
6926 if (strlencmp(cmd, "up") == 0) {
6927 if (level < bright_maxlvl)
6928 level++;
6929 } else if (strlencmp(cmd, "down") == 0) {
6930 if (level > 0)
6931 level--;
6932 } else if (sscanf(cmd, "level %d", &level) == 1 &&
6933 level >= 0 && level <= bright_maxlvl) {
6934 /* new level set */
6935 } else
6936 return -EINVAL;
6937 }
6938
6939 tpacpi_disclose_usertask("procfs brightness",
6940 "set level to %d\n", level);
6941
6942 /*
6943 * Now we know what the final level should be, so we try to set it.
6944 * Doing it this way makes the syscall restartable in case of EINTR
6945 */
6946 rc = brightness_set(level);
6947 if (!rc && ibm_backlight_device)
6948 backlight_force_update(ibm_backlight_device,
6949 BACKLIGHT_UPDATE_SYSFS);
6950 return (rc == -EINTR) ? -ERESTARTSYS : rc;
6951 }
6952
6953 static struct ibm_struct brightness_driver_data = {
6954 .name = "brightness",
6955 .read = brightness_read,
6956 .write = brightness_write,
6957 .exit = brightness_exit,
6958 .suspend = brightness_suspend,
6959 .shutdown = brightness_shutdown,
6960 };
6961
6962 /*************************************************************************
6963 * Volume subdriver
6964 */
6965
6966 /*
6967 * IBM ThinkPads have a simple volume controller with MUTE gating.
6968 * Very early Lenovo ThinkPads follow the IBM ThinkPad spec.
6969 *
6970 * Since the *61 series (and probably also the later *60 series), Lenovo
6971 * ThinkPads only implement the MUTE gate.
6972 *
6973 * EC register 0x30
6974 * Bit 6: MUTE (1 mutes sound)
6975 * Bit 3-0: Volume
6976 * Other bits should be zero as far as we know.
6977 *
6978 * This is also stored in CMOS NVRAM, byte 0x60, bit 6 (MUTE), and
6979 * bits 3-0 (volume). Other bits in NVRAM may have other functions,
6980 * such as bit 7 which is used to detect repeated presses of MUTE,
6981 * and we leave them unchanged.
6982 *
6983 * On newer Lenovo ThinkPads, the EC can automatically change the volume
6984 * in response to user input. Unfortunately, this rarely works well.
6985 * The laptop changes the state of its internal MUTE gate and, on some
6986 * models, sends KEY_MUTE, causing any user code that responds to the
6987 * mute button to get confused. The hardware MUTE gate is also
6988 * unnecessary, since user code can handle the mute button without
6989 * kernel or EC help.
6990 *
6991 * To avoid confusing userspace, we simply disable all EC-based mute
6992 * and volume controls when possible.
6993 */
6994
6995 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
6996
6997 #define TPACPI_ALSA_DRVNAME "ThinkPad EC"
6998 #define TPACPI_ALSA_SHRTNAME "ThinkPad Console Audio Control"
6999 #define TPACPI_ALSA_MIXERNAME TPACPI_ALSA_SHRTNAME
7000
7001 #if SNDRV_CARDS <= 32
7002 #define DEFAULT_ALSA_IDX ~((1 << (SNDRV_CARDS - 3)) - 1)
7003 #else
7004 #define DEFAULT_ALSA_IDX ~((1 << (32 - 3)) - 1)
7005 #endif
7006 static int alsa_index = DEFAULT_ALSA_IDX; /* last three slots */
7007 static char *alsa_id = "ThinkPadEC";
7008 static bool alsa_enable = SNDRV_DEFAULT_ENABLE1;
7009
7010 struct tpacpi_alsa_data {
7011 struct snd_card *card;
7012 struct snd_ctl_elem_id *ctl_mute_id;
7013 struct snd_ctl_elem_id *ctl_vol_id;
7014 };
7015
7016 static struct snd_card *alsa_card;
7017
7018 enum {
7019 TP_EC_AUDIO = 0x30,
7020
7021 /* TP_EC_AUDIO bits */
7022 TP_EC_AUDIO_MUTESW = 6,
7023
7024 /* TP_EC_AUDIO bitmasks */
7025 TP_EC_AUDIO_LVL_MSK = 0x0F,
7026 TP_EC_AUDIO_MUTESW_MSK = (1 << TP_EC_AUDIO_MUTESW),
7027
7028 /* Maximum volume */
7029 TP_EC_VOLUME_MAX = 14,
7030 };
7031
7032 enum tpacpi_volume_access_mode {
7033 TPACPI_VOL_MODE_AUTO = 0, /* Not implemented yet */
7034 TPACPI_VOL_MODE_EC, /* Pure EC control */
7035 TPACPI_VOL_MODE_UCMS_STEP, /* UCMS step-based control: N/A */
7036 TPACPI_VOL_MODE_ECNVRAM, /* EC control w/ NVRAM store */
7037 TPACPI_VOL_MODE_MAX
7038 };
7039
7040 enum tpacpi_volume_capabilities {
7041 TPACPI_VOL_CAP_AUTO = 0, /* Use white/blacklist */
7042 TPACPI_VOL_CAP_VOLMUTE, /* Output vol and mute */
7043 TPACPI_VOL_CAP_MUTEONLY, /* Output mute only */
7044 TPACPI_VOL_CAP_MAX
7045 };
7046
7047 enum tpacpi_mute_btn_mode {
7048 TP_EC_MUTE_BTN_LATCH = 0, /* Mute mutes; up/down unmutes */
7049 /* We don't know what mode 1 is. */
7050 TP_EC_MUTE_BTN_NONE = 2, /* Mute and up/down are just keys */
7051 TP_EC_MUTE_BTN_TOGGLE = 3, /* Mute toggles; up/down unmutes */
7052 };
7053
7054 static enum tpacpi_volume_access_mode volume_mode =
7055 TPACPI_VOL_MODE_MAX;
7056
7057 static enum tpacpi_volume_capabilities volume_capabilities;
7058 static bool volume_control_allowed;
7059 static bool software_mute_requested = true;
7060 static bool software_mute_active;
7061 static int software_mute_orig_mode;
7062
7063 /*
7064 * Used to syncronize writers to TP_EC_AUDIO and
7065 * TP_NVRAM_ADDR_MIXER, as we need to do read-modify-write
7066 */
7067 static struct mutex volume_mutex;
7068
7069 static void tpacpi_volume_checkpoint_nvram(void)
7070 {
7071 u8 lec = 0;
7072 u8 b_nvram;
7073 u8 ec_mask;
7074
7075 if (volume_mode != TPACPI_VOL_MODE_ECNVRAM)
7076 return;
7077 if (!volume_control_allowed)
7078 return;
7079 if (software_mute_active)
7080 return;
7081
7082 vdbg_printk(TPACPI_DBG_MIXER,
7083 "trying to checkpoint mixer state to NVRAM...\n");
7084
7085 if (tp_features.mixer_no_level_control)
7086 ec_mask = TP_EC_AUDIO_MUTESW_MSK;
7087 else
7088 ec_mask = TP_EC_AUDIO_MUTESW_MSK | TP_EC_AUDIO_LVL_MSK;
7089
7090 if (mutex_lock_killable(&volume_mutex) < 0)
7091 return;
7092
7093 if (unlikely(!acpi_ec_read(TP_EC_AUDIO, &lec)))
7094 goto unlock;
7095 lec &= ec_mask;
7096 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
7097
7098 if (lec != (b_nvram & ec_mask)) {
7099 /* NVRAM needs update */
7100 b_nvram &= ~ec_mask;
7101 b_nvram |= lec;
7102 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_MIXER);
7103 dbg_printk(TPACPI_DBG_MIXER,
7104 "updated NVRAM mixer status to 0x%02x (0x%02x)\n",
7105 (unsigned int) lec, (unsigned int) b_nvram);
7106 } else {
7107 vdbg_printk(TPACPI_DBG_MIXER,
7108 "NVRAM mixer status already is 0x%02x (0x%02x)\n",
7109 (unsigned int) lec, (unsigned int) b_nvram);
7110 }
7111
7112 unlock:
7113 mutex_unlock(&volume_mutex);
7114 }
7115
7116 static int volume_get_status_ec(u8 *status)
7117 {
7118 u8 s;
7119
7120 if (!acpi_ec_read(TP_EC_AUDIO, &s))
7121 return -EIO;
7122
7123 *status = s;
7124
7125 dbg_printk(TPACPI_DBG_MIXER, "status 0x%02x\n", s);
7126
7127 return 0;
7128 }
7129
7130 static int volume_get_status(u8 *status)
7131 {
7132 return volume_get_status_ec(status);
7133 }
7134
7135 static int volume_set_status_ec(const u8 status)
7136 {
7137 if (!acpi_ec_write(TP_EC_AUDIO, status))
7138 return -EIO;
7139
7140 dbg_printk(TPACPI_DBG_MIXER, "set EC mixer to 0x%02x\n", status);
7141
7142 /*
7143 * On X200s, and possibly on others, it can take a while for
7144 * reads to become correct.
7145 */
7146 msleep(1);
7147
7148 return 0;
7149 }
7150
7151 static int volume_set_status(const u8 status)
7152 {
7153 return volume_set_status_ec(status);
7154 }
7155
7156 /* returns < 0 on error, 0 on no change, 1 on change */
7157 static int __volume_set_mute_ec(const bool mute)
7158 {
7159 int rc;
7160 u8 s, n;
7161
7162 if (mutex_lock_killable(&volume_mutex) < 0)
7163 return -EINTR;
7164
7165 rc = volume_get_status_ec(&s);
7166 if (rc)
7167 goto unlock;
7168
7169 n = (mute) ? s | TP_EC_AUDIO_MUTESW_MSK :
7170 s & ~TP_EC_AUDIO_MUTESW_MSK;
7171
7172 if (n != s) {
7173 rc = volume_set_status_ec(n);
7174 if (!rc)
7175 rc = 1;
7176 }
7177
7178 unlock:
7179 mutex_unlock(&volume_mutex);
7180 return rc;
7181 }
7182
7183 static int volume_alsa_set_mute(const bool mute)
7184 {
7185 dbg_printk(TPACPI_DBG_MIXER, "ALSA: trying to %smute\n",
7186 (mute) ? "" : "un");
7187 return __volume_set_mute_ec(mute);
7188 }
7189
7190 static int volume_set_mute(const bool mute)
7191 {
7192 int rc;
7193
7194 dbg_printk(TPACPI_DBG_MIXER, "trying to %smute\n",
7195 (mute) ? "" : "un");
7196
7197 rc = __volume_set_mute_ec(mute);
7198 return (rc < 0) ? rc : 0;
7199 }
7200
7201 /* returns < 0 on error, 0 on no change, 1 on change */
7202 static int __volume_set_volume_ec(const u8 vol)
7203 {
7204 int rc;
7205 u8 s, n;
7206
7207 if (vol > TP_EC_VOLUME_MAX)
7208 return -EINVAL;
7209
7210 if (mutex_lock_killable(&volume_mutex) < 0)
7211 return -EINTR;
7212
7213 rc = volume_get_status_ec(&s);
7214 if (rc)
7215 goto unlock;
7216
7217 n = (s & ~TP_EC_AUDIO_LVL_MSK) | vol;
7218
7219 if (n != s) {
7220 rc = volume_set_status_ec(n);
7221 if (!rc)
7222 rc = 1;
7223 }
7224
7225 unlock:
7226 mutex_unlock(&volume_mutex);
7227 return rc;
7228 }
7229
7230 static int volume_set_software_mute(bool startup)
7231 {
7232 int result;
7233
7234 if (!tpacpi_is_lenovo())
7235 return -ENODEV;
7236
7237 if (startup) {
7238 if (!acpi_evalf(ec_handle, &software_mute_orig_mode,
7239 "HAUM", "qd"))
7240 return -EIO;
7241
7242 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7243 "Initial HAUM setting was %d\n",
7244 software_mute_orig_mode);
7245 }
7246
7247 if (!acpi_evalf(ec_handle, &result, "SAUM", "qdd",
7248 (int)TP_EC_MUTE_BTN_NONE))
7249 return -EIO;
7250
7251 if (result != TP_EC_MUTE_BTN_NONE)
7252 pr_warn("Unexpected SAUM result %d\n",
7253 result);
7254
7255 /*
7256 * In software mute mode, the standard codec controls take
7257 * precendence, so we unmute the ThinkPad HW switch at
7258 * startup. Just on case there are SAUM-capable ThinkPads
7259 * with level controls, set max HW volume as well.
7260 */
7261 if (tp_features.mixer_no_level_control)
7262 result = volume_set_mute(false);
7263 else
7264 result = volume_set_status(TP_EC_VOLUME_MAX);
7265
7266 if (result != 0)
7267 pr_warn("Failed to unmute the HW mute switch\n");
7268
7269 return 0;
7270 }
7271
7272 static void volume_exit_software_mute(void)
7273 {
7274 int r;
7275
7276 if (!acpi_evalf(ec_handle, &r, "SAUM", "qdd", software_mute_orig_mode)
7277 || r != software_mute_orig_mode)
7278 pr_warn("Failed to restore mute mode\n");
7279 }
7280
7281 static int volume_alsa_set_volume(const u8 vol)
7282 {
7283 dbg_printk(TPACPI_DBG_MIXER,
7284 "ALSA: trying to set volume level to %hu\n", vol);
7285 return __volume_set_volume_ec(vol);
7286 }
7287
7288 static void volume_alsa_notify_change(void)
7289 {
7290 struct tpacpi_alsa_data *d;
7291
7292 if (alsa_card && alsa_card->private_data) {
7293 d = alsa_card->private_data;
7294 if (d->ctl_mute_id)
7295 snd_ctl_notify(alsa_card,
7296 SNDRV_CTL_EVENT_MASK_VALUE,
7297 d->ctl_mute_id);
7298 if (d->ctl_vol_id)
7299 snd_ctl_notify(alsa_card,
7300 SNDRV_CTL_EVENT_MASK_VALUE,
7301 d->ctl_vol_id);
7302 }
7303 }
7304
7305 static int volume_alsa_vol_info(struct snd_kcontrol *kcontrol,
7306 struct snd_ctl_elem_info *uinfo)
7307 {
7308 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
7309 uinfo->count = 1;
7310 uinfo->value.integer.min = 0;
7311 uinfo->value.integer.max = TP_EC_VOLUME_MAX;
7312 return 0;
7313 }
7314
7315 static int volume_alsa_vol_get(struct snd_kcontrol *kcontrol,
7316 struct snd_ctl_elem_value *ucontrol)
7317 {
7318 u8 s;
7319 int rc;
7320
7321 rc = volume_get_status(&s);
7322 if (rc < 0)
7323 return rc;
7324
7325 ucontrol->value.integer.value[0] = s & TP_EC_AUDIO_LVL_MSK;
7326 return 0;
7327 }
7328
7329 static int volume_alsa_vol_put(struct snd_kcontrol *kcontrol,
7330 struct snd_ctl_elem_value *ucontrol)
7331 {
7332 tpacpi_disclose_usertask("ALSA", "set volume to %ld\n",
7333 ucontrol->value.integer.value[0]);
7334 return volume_alsa_set_volume(ucontrol->value.integer.value[0]);
7335 }
7336
7337 #define volume_alsa_mute_info snd_ctl_boolean_mono_info
7338
7339 static int volume_alsa_mute_get(struct snd_kcontrol *kcontrol,
7340 struct snd_ctl_elem_value *ucontrol)
7341 {
7342 u8 s;
7343 int rc;
7344
7345 rc = volume_get_status(&s);
7346 if (rc < 0)
7347 return rc;
7348
7349 ucontrol->value.integer.value[0] =
7350 (s & TP_EC_AUDIO_MUTESW_MSK) ? 0 : 1;
7351 return 0;
7352 }
7353
7354 static int volume_alsa_mute_put(struct snd_kcontrol *kcontrol,
7355 struct snd_ctl_elem_value *ucontrol)
7356 {
7357 tpacpi_disclose_usertask("ALSA", "%smute\n",
7358 ucontrol->value.integer.value[0] ?
7359 "un" : "");
7360 return volume_alsa_set_mute(!ucontrol->value.integer.value[0]);
7361 }
7362
7363 static struct snd_kcontrol_new volume_alsa_control_vol __initdata = {
7364 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7365 .name = "Console Playback Volume",
7366 .index = 0,
7367 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7368 .info = volume_alsa_vol_info,
7369 .get = volume_alsa_vol_get,
7370 };
7371
7372 static struct snd_kcontrol_new volume_alsa_control_mute __initdata = {
7373 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
7374 .name = "Console Playback Switch",
7375 .index = 0,
7376 .access = SNDRV_CTL_ELEM_ACCESS_READ,
7377 .info = volume_alsa_mute_info,
7378 .get = volume_alsa_mute_get,
7379 };
7380
7381 static void volume_suspend(void)
7382 {
7383 tpacpi_volume_checkpoint_nvram();
7384 }
7385
7386 static void volume_resume(void)
7387 {
7388 if (software_mute_active) {
7389 if (volume_set_software_mute(false) < 0)
7390 pr_warn("Failed to restore software mute\n");
7391 } else {
7392 volume_alsa_notify_change();
7393 }
7394 }
7395
7396 static void volume_shutdown(void)
7397 {
7398 tpacpi_volume_checkpoint_nvram();
7399 }
7400
7401 static void volume_exit(void)
7402 {
7403 if (alsa_card) {
7404 snd_card_free(alsa_card);
7405 alsa_card = NULL;
7406 }
7407
7408 tpacpi_volume_checkpoint_nvram();
7409
7410 if (software_mute_active)
7411 volume_exit_software_mute();
7412 }
7413
7414 static int __init volume_create_alsa_mixer(void)
7415 {
7416 struct snd_card *card;
7417 struct tpacpi_alsa_data *data;
7418 struct snd_kcontrol *ctl_vol;
7419 struct snd_kcontrol *ctl_mute;
7420 int rc;
7421
7422 rc = snd_card_new(&tpacpi_pdev->dev,
7423 alsa_index, alsa_id, THIS_MODULE,
7424 sizeof(struct tpacpi_alsa_data), &card);
7425 if (rc < 0 || !card) {
7426 pr_err("Failed to create ALSA card structures: %d\n", rc);
7427 return 1;
7428 }
7429
7430 BUG_ON(!card->private_data);
7431 data = card->private_data;
7432 data->card = card;
7433
7434 strlcpy(card->driver, TPACPI_ALSA_DRVNAME,
7435 sizeof(card->driver));
7436 strlcpy(card->shortname, TPACPI_ALSA_SHRTNAME,
7437 sizeof(card->shortname));
7438 snprintf(card->mixername, sizeof(card->mixername), "ThinkPad EC %s",
7439 (thinkpad_id.ec_version_str) ?
7440 thinkpad_id.ec_version_str : "(unknown)");
7441 snprintf(card->longname, sizeof(card->longname),
7442 "%s at EC reg 0x%02x, fw %s", card->shortname, TP_EC_AUDIO,
7443 (thinkpad_id.ec_version_str) ?
7444 thinkpad_id.ec_version_str : "unknown");
7445
7446 if (volume_control_allowed) {
7447 volume_alsa_control_vol.put = volume_alsa_vol_put;
7448 volume_alsa_control_vol.access =
7449 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7450
7451 volume_alsa_control_mute.put = volume_alsa_mute_put;
7452 volume_alsa_control_mute.access =
7453 SNDRV_CTL_ELEM_ACCESS_READWRITE;
7454 }
7455
7456 if (!tp_features.mixer_no_level_control) {
7457 ctl_vol = snd_ctl_new1(&volume_alsa_control_vol, NULL);
7458 rc = snd_ctl_add(card, ctl_vol);
7459 if (rc < 0) {
7460 pr_err("Failed to create ALSA volume control: %d\n",
7461 rc);
7462 goto err_exit;
7463 }
7464 data->ctl_vol_id = &ctl_vol->id;
7465 }
7466
7467 ctl_mute = snd_ctl_new1(&volume_alsa_control_mute, NULL);
7468 rc = snd_ctl_add(card, ctl_mute);
7469 if (rc < 0) {
7470 pr_err("Failed to create ALSA mute control: %d\n", rc);
7471 goto err_exit;
7472 }
7473 data->ctl_mute_id = &ctl_mute->id;
7474
7475 rc = snd_card_register(card);
7476 if (rc < 0) {
7477 pr_err("Failed to register ALSA card: %d\n", rc);
7478 goto err_exit;
7479 }
7480
7481 alsa_card = card;
7482 return 0;
7483
7484 err_exit:
7485 snd_card_free(card);
7486 return 1;
7487 }
7488
7489 #define TPACPI_VOL_Q_MUTEONLY 0x0001 /* Mute-only control available */
7490 #define TPACPI_VOL_Q_LEVEL 0x0002 /* Volume control available */
7491
7492 static const struct tpacpi_quirk volume_quirk_table[] __initconst = {
7493 /* Whitelist volume level on all IBM by default */
7494 { .vendor = PCI_VENDOR_ID_IBM,
7495 .bios = TPACPI_MATCH_ANY,
7496 .ec = TPACPI_MATCH_ANY,
7497 .quirks = TPACPI_VOL_Q_LEVEL },
7498
7499 /* Lenovo models with volume control (needs confirmation) */
7500 TPACPI_QEC_LNV('7', 'C', TPACPI_VOL_Q_LEVEL), /* R60/i */
7501 TPACPI_QEC_LNV('7', 'E', TPACPI_VOL_Q_LEVEL), /* R60e/i */
7502 TPACPI_QEC_LNV('7', '9', TPACPI_VOL_Q_LEVEL), /* T60/p */
7503 TPACPI_QEC_LNV('7', 'B', TPACPI_VOL_Q_LEVEL), /* X60/s */
7504 TPACPI_QEC_LNV('7', 'J', TPACPI_VOL_Q_LEVEL), /* X60t */
7505 TPACPI_QEC_LNV('7', '7', TPACPI_VOL_Q_LEVEL), /* Z60 */
7506 TPACPI_QEC_LNV('7', 'F', TPACPI_VOL_Q_LEVEL), /* Z61 */
7507
7508 /* Whitelist mute-only on all Lenovo by default */
7509 { .vendor = PCI_VENDOR_ID_LENOVO,
7510 .bios = TPACPI_MATCH_ANY,
7511 .ec = TPACPI_MATCH_ANY,
7512 .quirks = TPACPI_VOL_Q_MUTEONLY }
7513 };
7514
7515 static int __init volume_init(struct ibm_init_struct *iibm)
7516 {
7517 unsigned long quirks;
7518 int rc;
7519
7520 vdbg_printk(TPACPI_DBG_INIT, "initializing volume subdriver\n");
7521
7522 mutex_init(&volume_mutex);
7523
7524 /*
7525 * Check for module parameter bogosity, note that we
7526 * init volume_mode to TPACPI_VOL_MODE_MAX in order to be
7527 * able to detect "unspecified"
7528 */
7529 if (volume_mode > TPACPI_VOL_MODE_MAX)
7530 return -EINVAL;
7531
7532 if (volume_mode == TPACPI_VOL_MODE_UCMS_STEP) {
7533 pr_err("UCMS step volume mode not implemented, "
7534 "please contact %s\n", TPACPI_MAIL);
7535 return 1;
7536 }
7537
7538 if (volume_capabilities >= TPACPI_VOL_CAP_MAX)
7539 return -EINVAL;
7540
7541 /*
7542 * The ALSA mixer is our primary interface.
7543 * When disabled, don't install the subdriver at all
7544 */
7545 if (!alsa_enable) {
7546 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7547 "ALSA mixer disabled by parameter, "
7548 "not loading volume subdriver...\n");
7549 return 1;
7550 }
7551
7552 quirks = tpacpi_check_quirks(volume_quirk_table,
7553 ARRAY_SIZE(volume_quirk_table));
7554
7555 switch (volume_capabilities) {
7556 case TPACPI_VOL_CAP_AUTO:
7557 if (quirks & TPACPI_VOL_Q_MUTEONLY)
7558 tp_features.mixer_no_level_control = 1;
7559 else if (quirks & TPACPI_VOL_Q_LEVEL)
7560 tp_features.mixer_no_level_control = 0;
7561 else
7562 return 1; /* no mixer */
7563 break;
7564 case TPACPI_VOL_CAP_VOLMUTE:
7565 tp_features.mixer_no_level_control = 0;
7566 break;
7567 case TPACPI_VOL_CAP_MUTEONLY:
7568 tp_features.mixer_no_level_control = 1;
7569 break;
7570 default:
7571 return 1;
7572 }
7573
7574 if (volume_capabilities != TPACPI_VOL_CAP_AUTO)
7575 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7576 "using user-supplied volume_capabilities=%d\n",
7577 volume_capabilities);
7578
7579 if (volume_mode == TPACPI_VOL_MODE_AUTO ||
7580 volume_mode == TPACPI_VOL_MODE_MAX) {
7581 volume_mode = TPACPI_VOL_MODE_ECNVRAM;
7582
7583 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7584 "driver auto-selected volume_mode=%d\n",
7585 volume_mode);
7586 } else {
7587 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7588 "using user-supplied volume_mode=%d\n",
7589 volume_mode);
7590 }
7591
7592 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7593 "mute is supported, volume control is %s\n",
7594 str_supported(!tp_features.mixer_no_level_control));
7595
7596 if (software_mute_requested && volume_set_software_mute(true) == 0) {
7597 software_mute_active = true;
7598 } else {
7599 rc = volume_create_alsa_mixer();
7600 if (rc) {
7601 pr_err("Could not create the ALSA mixer interface\n");
7602 return rc;
7603 }
7604
7605 pr_info("Console audio control enabled, mode: %s\n",
7606 (volume_control_allowed) ?
7607 "override (read/write)" :
7608 "monitor (read only)");
7609 }
7610
7611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_MIXER,
7612 "registering volume hotkeys as change notification\n");
7613 tpacpi_hotkey_driver_mask_set(hotkey_driver_mask
7614 | TP_ACPI_HKEY_VOLUP_MASK
7615 | TP_ACPI_HKEY_VOLDWN_MASK
7616 | TP_ACPI_HKEY_MUTE_MASK);
7617
7618 return 0;
7619 }
7620
7621 static int volume_read(struct seq_file *m)
7622 {
7623 u8 status;
7624
7625 if (volume_get_status(&status) < 0) {
7626 seq_printf(m, "level:\t\tunreadable\n");
7627 } else {
7628 if (tp_features.mixer_no_level_control)
7629 seq_printf(m, "level:\t\tunsupported\n");
7630 else
7631 seq_printf(m, "level:\t\t%d\n",
7632 status & TP_EC_AUDIO_LVL_MSK);
7633
7634 seq_printf(m, "mute:\t\t%s\n",
7635 onoff(status, TP_EC_AUDIO_MUTESW));
7636
7637 if (volume_control_allowed) {
7638 seq_printf(m, "commands:\tunmute, mute\n");
7639 if (!tp_features.mixer_no_level_control) {
7640 seq_printf(m,
7641 "commands:\tup, down\n");
7642 seq_printf(m,
7643 "commands:\tlevel <level>"
7644 " (<level> is 0-%d)\n",
7645 TP_EC_VOLUME_MAX);
7646 }
7647 }
7648 }
7649
7650 return 0;
7651 }
7652
7653 static int volume_write(char *buf)
7654 {
7655 u8 s;
7656 u8 new_level, new_mute;
7657 int l;
7658 char *cmd;
7659 int rc;
7660
7661 /*
7662 * We do allow volume control at driver startup, so that the
7663 * user can set initial state through the volume=... parameter hack.
7664 */
7665 if (!volume_control_allowed && tpacpi_lifecycle != TPACPI_LIFE_INIT) {
7666 if (unlikely(!tp_warned.volume_ctrl_forbidden)) {
7667 tp_warned.volume_ctrl_forbidden = 1;
7668 pr_notice("Console audio control in monitor mode, "
7669 "changes are not allowed\n");
7670 pr_notice("Use the volume_control=1 module parameter "
7671 "to enable volume control\n");
7672 }
7673 return -EPERM;
7674 }
7675
7676 rc = volume_get_status(&s);
7677 if (rc < 0)
7678 return rc;
7679
7680 new_level = s & TP_EC_AUDIO_LVL_MSK;
7681 new_mute = s & TP_EC_AUDIO_MUTESW_MSK;
7682
7683 while ((cmd = next_cmd(&buf))) {
7684 if (!tp_features.mixer_no_level_control) {
7685 if (strlencmp(cmd, "up") == 0) {
7686 if (new_mute)
7687 new_mute = 0;
7688 else if (new_level < TP_EC_VOLUME_MAX)
7689 new_level++;
7690 continue;
7691 } else if (strlencmp(cmd, "down") == 0) {
7692 if (new_mute)
7693 new_mute = 0;
7694 else if (new_level > 0)
7695 new_level--;
7696 continue;
7697 } else if (sscanf(cmd, "level %u", &l) == 1 &&
7698 l >= 0 && l <= TP_EC_VOLUME_MAX) {
7699 new_level = l;
7700 continue;
7701 }
7702 }
7703 if (strlencmp(cmd, "mute") == 0)
7704 new_mute = TP_EC_AUDIO_MUTESW_MSK;
7705 else if (strlencmp(cmd, "unmute") == 0)
7706 new_mute = 0;
7707 else
7708 return -EINVAL;
7709 }
7710
7711 if (tp_features.mixer_no_level_control) {
7712 tpacpi_disclose_usertask("procfs volume", "%smute\n",
7713 new_mute ? "" : "un");
7714 rc = volume_set_mute(!!new_mute);
7715 } else {
7716 tpacpi_disclose_usertask("procfs volume",
7717 "%smute and set level to %d\n",
7718 new_mute ? "" : "un", new_level);
7719 rc = volume_set_status(new_mute | new_level);
7720 }
7721 volume_alsa_notify_change();
7722
7723 return (rc == -EINTR) ? -ERESTARTSYS : rc;
7724 }
7725
7726 static struct ibm_struct volume_driver_data = {
7727 .name = "volume",
7728 .read = volume_read,
7729 .write = volume_write,
7730 .exit = volume_exit,
7731 .suspend = volume_suspend,
7732 .resume = volume_resume,
7733 .shutdown = volume_shutdown,
7734 };
7735
7736 #else /* !CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7737
7738 #define alsa_card NULL
7739
7740 static inline void volume_alsa_notify_change(void)
7741 {
7742 }
7743
7744 static int __init volume_init(struct ibm_init_struct *iibm)
7745 {
7746 pr_info("volume: disabled as there is no ALSA support in this kernel\n");
7747
7748 return 1;
7749 }
7750
7751 static struct ibm_struct volume_driver_data = {
7752 .name = "volume",
7753 };
7754
7755 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
7756
7757 /*************************************************************************
7758 * Fan subdriver
7759 */
7760
7761 /*
7762 * FAN ACCESS MODES
7763 *
7764 * TPACPI_FAN_RD_ACPI_GFAN:
7765 * ACPI GFAN method: returns fan level
7766 *
7767 * see TPACPI_FAN_WR_ACPI_SFAN
7768 * EC 0x2f (HFSP) not available if GFAN exists
7769 *
7770 * TPACPI_FAN_WR_ACPI_SFAN:
7771 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
7772 *
7773 * EC 0x2f (HFSP) might be available *for reading*, but do not use
7774 * it for writing.
7775 *
7776 * TPACPI_FAN_WR_TPEC:
7777 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
7778 * Supported on almost all ThinkPads
7779 *
7780 * Fan speed changes of any sort (including those caused by the
7781 * disengaged mode) are usually done slowly by the firmware as the
7782 * maximum amount of fan duty cycle change per second seems to be
7783 * limited.
7784 *
7785 * Reading is not available if GFAN exists.
7786 * Writing is not available if SFAN exists.
7787 *
7788 * Bits
7789 * 7 automatic mode engaged;
7790 * (default operation mode of the ThinkPad)
7791 * fan level is ignored in this mode.
7792 * 6 full speed mode (takes precedence over bit 7);
7793 * not available on all thinkpads. May disable
7794 * the tachometer while the fan controller ramps up
7795 * the speed (which can take up to a few *minutes*).
7796 * Speeds up fan to 100% duty-cycle, which is far above
7797 * the standard RPM levels. It is not impossible that
7798 * it could cause hardware damage.
7799 * 5-3 unused in some models. Extra bits for fan level
7800 * in others, but still useless as all values above
7801 * 7 map to the same speed as level 7 in these models.
7802 * 2-0 fan level (0..7 usually)
7803 * 0x00 = stop
7804 * 0x07 = max (set when temperatures critical)
7805 * Some ThinkPads may have other levels, see
7806 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
7807 *
7808 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
7809 * boot. Apparently the EC does not initialize it, so unless ACPI DSDT
7810 * does so, its initial value is meaningless (0x07).
7811 *
7812 * For firmware bugs, refer to:
7813 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7814 *
7815 * ----
7816 *
7817 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
7818 * Main fan tachometer reading (in RPM)
7819 *
7820 * This register is present on all ThinkPads with a new-style EC, and
7821 * it is known not to be present on the A21m/e, and T22, as there is
7822 * something else in offset 0x84 according to the ACPI DSDT. Other
7823 * ThinkPads from this same time period (and earlier) probably lack the
7824 * tachometer as well.
7825 *
7826 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
7827 * was never fixed by IBM to report the EC firmware version string
7828 * probably support the tachometer (like the early X models), so
7829 * detecting it is quite hard. We need more data to know for sure.
7830 *
7831 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
7832 * might result.
7833 *
7834 * FIRMWARE BUG: may go stale while the EC is switching to full speed
7835 * mode.
7836 *
7837 * For firmware bugs, refer to:
7838 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
7839 *
7840 * ----
7841 *
7842 * ThinkPad EC register 0x31 bit 0 (only on select models)
7843 *
7844 * When bit 0 of EC register 0x31 is zero, the tachometer registers
7845 * show the speed of the main fan. When bit 0 of EC register 0x31
7846 * is one, the tachometer registers show the speed of the auxiliary
7847 * fan.
7848 *
7849 * Fan control seems to affect both fans, regardless of the state
7850 * of this bit.
7851 *
7852 * So far, only the firmware for the X60/X61 non-tablet versions
7853 * seem to support this (firmware TP-7M).
7854 *
7855 * TPACPI_FAN_WR_ACPI_FANS:
7856 * ThinkPad X31, X40, X41. Not available in the X60.
7857 *
7858 * FANS ACPI handle: takes three arguments: low speed, medium speed,
7859 * high speed. ACPI DSDT seems to map these three speeds to levels
7860 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
7861 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
7862 *
7863 * The speeds are stored on handles
7864 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
7865 *
7866 * There are three default speed sets, accessible as handles:
7867 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
7868 *
7869 * ACPI DSDT switches which set is in use depending on various
7870 * factors.
7871 *
7872 * TPACPI_FAN_WR_TPEC is also available and should be used to
7873 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
7874 * but the ACPI tables just mention level 7.
7875 */
7876
7877 enum { /* Fan control constants */
7878 fan_status_offset = 0x2f, /* EC register 0x2f */
7879 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
7880 * 0x84 must be read before 0x85 */
7881 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
7882 bit 0 selects which fan is active */
7883
7884 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
7885 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
7886
7887 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
7888 };
7889
7890 enum fan_status_access_mode {
7891 TPACPI_FAN_NONE = 0, /* No fan status or control */
7892 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
7893 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
7894 };
7895
7896 enum fan_control_access_mode {
7897 TPACPI_FAN_WR_NONE = 0, /* No fan control */
7898 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
7899 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
7900 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
7901 };
7902
7903 enum fan_control_commands {
7904 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
7905 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
7906 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
7907 * and also watchdog cmd */
7908 };
7909
7910 static bool fan_control_allowed;
7911
7912 static enum fan_status_access_mode fan_status_access_mode;
7913 static enum fan_control_access_mode fan_control_access_mode;
7914 static enum fan_control_commands fan_control_commands;
7915
7916 static u8 fan_control_initial_status;
7917 static u8 fan_control_desired_level;
7918 static u8 fan_control_resume_level;
7919 static int fan_watchdog_maxinterval;
7920
7921 static struct mutex fan_mutex;
7922
7923 static void fan_watchdog_fire(struct work_struct *ignored);
7924 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
7925
7926 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
7927 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
7928 "\\FSPD", /* 600e/x, 770e, 770x */
7929 ); /* all others */
7930 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
7931 "JFNS", /* 770x-JL */
7932 ); /* all others */
7933
7934 /*
7935 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
7936 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
7937 * be in auto mode (0x80).
7938 *
7939 * This is corrected by any write to HFSP either by the driver, or
7940 * by the firmware.
7941 *
7942 * We assume 0x07 really means auto mode while this quirk is active,
7943 * as this is far more likely than the ThinkPad being in level 7,
7944 * which is only used by the firmware during thermal emergencies.
7945 *
7946 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
7947 * TP-70 (T43, R52), which are known to be buggy.
7948 */
7949
7950 static void fan_quirk1_setup(void)
7951 {
7952 if (fan_control_initial_status == 0x07) {
7953 pr_notice("fan_init: initial fan status is unknown, "
7954 "assuming it is in auto mode\n");
7955 tp_features.fan_ctrl_status_undef = 1;
7956 }
7957 }
7958
7959 static void fan_quirk1_handle(u8 *fan_status)
7960 {
7961 if (unlikely(tp_features.fan_ctrl_status_undef)) {
7962 if (*fan_status != fan_control_initial_status) {
7963 /* something changed the HFSP regisnter since
7964 * driver init time, so it is not undefined
7965 * anymore */
7966 tp_features.fan_ctrl_status_undef = 0;
7967 } else {
7968 /* Return most likely status. In fact, it
7969 * might be the only possible status */
7970 *fan_status = TP_EC_FAN_AUTO;
7971 }
7972 }
7973 }
7974
7975 /* Select main fan on X60/X61, NOOP on others */
7976 static bool fan_select_fan1(void)
7977 {
7978 if (tp_features.second_fan) {
7979 u8 val;
7980
7981 if (ec_read(fan_select_offset, &val) < 0)
7982 return false;
7983 val &= 0xFEU;
7984 if (ec_write(fan_select_offset, val) < 0)
7985 return false;
7986 }
7987 return true;
7988 }
7989
7990 /* Select secondary fan on X60/X61 */
7991 static bool fan_select_fan2(void)
7992 {
7993 u8 val;
7994
7995 if (!tp_features.second_fan)
7996 return false;
7997
7998 if (ec_read(fan_select_offset, &val) < 0)
7999 return false;
8000 val |= 0x01U;
8001 if (ec_write(fan_select_offset, val) < 0)
8002 return false;
8003
8004 return true;
8005 }
8006
8007 /*
8008 * Call with fan_mutex held
8009 */
8010 static void fan_update_desired_level(u8 status)
8011 {
8012 if ((status &
8013 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8014 if (status > 7)
8015 fan_control_desired_level = 7;
8016 else
8017 fan_control_desired_level = status;
8018 }
8019 }
8020
8021 static int fan_get_status(u8 *status)
8022 {
8023 u8 s;
8024
8025 /* TODO:
8026 * Add TPACPI_FAN_RD_ACPI_FANS ? */
8027
8028 switch (fan_status_access_mode) {
8029 case TPACPI_FAN_RD_ACPI_GFAN: {
8030 /* 570, 600e/x, 770e, 770x */
8031 int res;
8032
8033 if (unlikely(!acpi_evalf(gfan_handle, &res, NULL, "d")))
8034 return -EIO;
8035
8036 if (likely(status))
8037 *status = res & 0x07;
8038
8039 break;
8040 }
8041 case TPACPI_FAN_RD_TPEC:
8042 /* all except 570, 600e/x, 770e, 770x */
8043 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
8044 return -EIO;
8045
8046 if (likely(status)) {
8047 *status = s;
8048 fan_quirk1_handle(status);
8049 }
8050
8051 break;
8052
8053 default:
8054 return -ENXIO;
8055 }
8056
8057 return 0;
8058 }
8059
8060 static int fan_get_status_safe(u8 *status)
8061 {
8062 int rc;
8063 u8 s;
8064
8065 if (mutex_lock_killable(&fan_mutex))
8066 return -ERESTARTSYS;
8067 rc = fan_get_status(&s);
8068 if (!rc)
8069 fan_update_desired_level(s);
8070 mutex_unlock(&fan_mutex);
8071
8072 if (rc)
8073 return rc;
8074 if (status)
8075 *status = s;
8076
8077 return 0;
8078 }
8079
8080 static int fan_get_speed(unsigned int *speed)
8081 {
8082 u8 hi, lo;
8083
8084 switch (fan_status_access_mode) {
8085 case TPACPI_FAN_RD_TPEC:
8086 /* all except 570, 600e/x, 770e, 770x */
8087 if (unlikely(!fan_select_fan1()))
8088 return -EIO;
8089 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
8090 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
8091 return -EIO;
8092
8093 if (likely(speed))
8094 *speed = (hi << 8) | lo;
8095
8096 break;
8097
8098 default:
8099 return -ENXIO;
8100 }
8101
8102 return 0;
8103 }
8104
8105 static int fan2_get_speed(unsigned int *speed)
8106 {
8107 u8 hi, lo;
8108 bool rc;
8109
8110 switch (fan_status_access_mode) {
8111 case TPACPI_FAN_RD_TPEC:
8112 /* all except 570, 600e/x, 770e, 770x */
8113 if (unlikely(!fan_select_fan2()))
8114 return -EIO;
8115 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
8116 !acpi_ec_read(fan_rpm_offset + 1, &hi);
8117 fan_select_fan1(); /* play it safe */
8118 if (rc)
8119 return -EIO;
8120
8121 if (likely(speed))
8122 *speed = (hi << 8) | lo;
8123
8124 break;
8125
8126 default:
8127 return -ENXIO;
8128 }
8129
8130 return 0;
8131 }
8132
8133 static int fan_set_level(int level)
8134 {
8135 if (!fan_control_allowed)
8136 return -EPERM;
8137
8138 switch (fan_control_access_mode) {
8139 case TPACPI_FAN_WR_ACPI_SFAN:
8140 if (level >= 0 && level <= 7) {
8141 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
8142 return -EIO;
8143 } else
8144 return -EINVAL;
8145 break;
8146
8147 case TPACPI_FAN_WR_ACPI_FANS:
8148 case TPACPI_FAN_WR_TPEC:
8149 if (!(level & TP_EC_FAN_AUTO) &&
8150 !(level & TP_EC_FAN_FULLSPEED) &&
8151 ((level < 0) || (level > 7)))
8152 return -EINVAL;
8153
8154 /* safety net should the EC not support AUTO
8155 * or FULLSPEED mode bits and just ignore them */
8156 if (level & TP_EC_FAN_FULLSPEED)
8157 level |= 7; /* safety min speed 7 */
8158 else if (level & TP_EC_FAN_AUTO)
8159 level |= 4; /* safety min speed 4 */
8160
8161 if (!acpi_ec_write(fan_status_offset, level))
8162 return -EIO;
8163 else
8164 tp_features.fan_ctrl_status_undef = 0;
8165 break;
8166
8167 default:
8168 return -ENXIO;
8169 }
8170
8171 vdbg_printk(TPACPI_DBG_FAN,
8172 "fan control: set fan control register to 0x%02x\n", level);
8173 return 0;
8174 }
8175
8176 static int fan_set_level_safe(int level)
8177 {
8178 int rc;
8179
8180 if (!fan_control_allowed)
8181 return -EPERM;
8182
8183 if (mutex_lock_killable(&fan_mutex))
8184 return -ERESTARTSYS;
8185
8186 if (level == TPACPI_FAN_LAST_LEVEL)
8187 level = fan_control_desired_level;
8188
8189 rc = fan_set_level(level);
8190 if (!rc)
8191 fan_update_desired_level(level);
8192
8193 mutex_unlock(&fan_mutex);
8194 return rc;
8195 }
8196
8197 static int fan_set_enable(void)
8198 {
8199 u8 s;
8200 int rc;
8201
8202 if (!fan_control_allowed)
8203 return -EPERM;
8204
8205 if (mutex_lock_killable(&fan_mutex))
8206 return -ERESTARTSYS;
8207
8208 switch (fan_control_access_mode) {
8209 case TPACPI_FAN_WR_ACPI_FANS:
8210 case TPACPI_FAN_WR_TPEC:
8211 rc = fan_get_status(&s);
8212 if (rc < 0)
8213 break;
8214
8215 /* Don't go out of emergency fan mode */
8216 if (s != 7) {
8217 s &= 0x07;
8218 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
8219 }
8220
8221 if (!acpi_ec_write(fan_status_offset, s))
8222 rc = -EIO;
8223 else {
8224 tp_features.fan_ctrl_status_undef = 0;
8225 rc = 0;
8226 }
8227 break;
8228
8229 case TPACPI_FAN_WR_ACPI_SFAN:
8230 rc = fan_get_status(&s);
8231 if (rc < 0)
8232 break;
8233
8234 s &= 0x07;
8235
8236 /* Set fan to at least level 4 */
8237 s |= 4;
8238
8239 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
8240 rc = -EIO;
8241 else
8242 rc = 0;
8243 break;
8244
8245 default:
8246 rc = -ENXIO;
8247 }
8248
8249 mutex_unlock(&fan_mutex);
8250
8251 if (!rc)
8252 vdbg_printk(TPACPI_DBG_FAN,
8253 "fan control: set fan control register to 0x%02x\n",
8254 s);
8255 return rc;
8256 }
8257
8258 static int fan_set_disable(void)
8259 {
8260 int rc;
8261
8262 if (!fan_control_allowed)
8263 return -EPERM;
8264
8265 if (mutex_lock_killable(&fan_mutex))
8266 return -ERESTARTSYS;
8267
8268 rc = 0;
8269 switch (fan_control_access_mode) {
8270 case TPACPI_FAN_WR_ACPI_FANS:
8271 case TPACPI_FAN_WR_TPEC:
8272 if (!acpi_ec_write(fan_status_offset, 0x00))
8273 rc = -EIO;
8274 else {
8275 fan_control_desired_level = 0;
8276 tp_features.fan_ctrl_status_undef = 0;
8277 }
8278 break;
8279
8280 case TPACPI_FAN_WR_ACPI_SFAN:
8281 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
8282 rc = -EIO;
8283 else
8284 fan_control_desired_level = 0;
8285 break;
8286
8287 default:
8288 rc = -ENXIO;
8289 }
8290
8291 if (!rc)
8292 vdbg_printk(TPACPI_DBG_FAN,
8293 "fan control: set fan control register to 0\n");
8294
8295 mutex_unlock(&fan_mutex);
8296 return rc;
8297 }
8298
8299 static int fan_set_speed(int speed)
8300 {
8301 int rc;
8302
8303 if (!fan_control_allowed)
8304 return -EPERM;
8305
8306 if (mutex_lock_killable(&fan_mutex))
8307 return -ERESTARTSYS;
8308
8309 rc = 0;
8310 switch (fan_control_access_mode) {
8311 case TPACPI_FAN_WR_ACPI_FANS:
8312 if (speed >= 0 && speed <= 65535) {
8313 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
8314 speed, speed, speed))
8315 rc = -EIO;
8316 } else
8317 rc = -EINVAL;
8318 break;
8319
8320 default:
8321 rc = -ENXIO;
8322 }
8323
8324 mutex_unlock(&fan_mutex);
8325 return rc;
8326 }
8327
8328 static void fan_watchdog_reset(void)
8329 {
8330 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
8331 return;
8332
8333 if (fan_watchdog_maxinterval > 0 &&
8334 tpacpi_lifecycle != TPACPI_LIFE_EXITING)
8335 mod_delayed_work(tpacpi_wq, &fan_watchdog_task,
8336 msecs_to_jiffies(fan_watchdog_maxinterval * 1000));
8337 else
8338 cancel_delayed_work(&fan_watchdog_task);
8339 }
8340
8341 static void fan_watchdog_fire(struct work_struct *ignored)
8342 {
8343 int rc;
8344
8345 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
8346 return;
8347
8348 pr_notice("fan watchdog: enabling fan\n");
8349 rc = fan_set_enable();
8350 if (rc < 0) {
8351 pr_err("fan watchdog: error %d while enabling fan, "
8352 "will try again later...\n", -rc);
8353 /* reschedule for later */
8354 fan_watchdog_reset();
8355 }
8356 }
8357
8358 /*
8359 * SYSFS fan layout: hwmon compatible (device)
8360 *
8361 * pwm*_enable:
8362 * 0: "disengaged" mode
8363 * 1: manual mode
8364 * 2: native EC "auto" mode (recommended, hardware default)
8365 *
8366 * pwm*: set speed in manual mode, ignored otherwise.
8367 * 0 is level 0; 255 is level 7. Intermediate points done with linear
8368 * interpolation.
8369 *
8370 * fan*_input: tachometer reading, RPM
8371 *
8372 *
8373 * SYSFS fan layout: extensions
8374 *
8375 * fan_watchdog (driver):
8376 * fan watchdog interval in seconds, 0 disables (default), max 120
8377 */
8378
8379 /* sysfs fan pwm1_enable ----------------------------------------------- */
8380 static ssize_t fan_pwm1_enable_show(struct device *dev,
8381 struct device_attribute *attr,
8382 char *buf)
8383 {
8384 int res, mode;
8385 u8 status;
8386
8387 res = fan_get_status_safe(&status);
8388 if (res)
8389 return res;
8390
8391 if (status & TP_EC_FAN_FULLSPEED) {
8392 mode = 0;
8393 } else if (status & TP_EC_FAN_AUTO) {
8394 mode = 2;
8395 } else
8396 mode = 1;
8397
8398 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
8399 }
8400
8401 static ssize_t fan_pwm1_enable_store(struct device *dev,
8402 struct device_attribute *attr,
8403 const char *buf, size_t count)
8404 {
8405 unsigned long t;
8406 int res, level;
8407
8408 if (parse_strtoul(buf, 2, &t))
8409 return -EINVAL;
8410
8411 tpacpi_disclose_usertask("hwmon pwm1_enable",
8412 "set fan mode to %lu\n", t);
8413
8414 switch (t) {
8415 case 0:
8416 level = TP_EC_FAN_FULLSPEED;
8417 break;
8418 case 1:
8419 level = TPACPI_FAN_LAST_LEVEL;
8420 break;
8421 case 2:
8422 level = TP_EC_FAN_AUTO;
8423 break;
8424 case 3:
8425 /* reserved for software-controlled auto mode */
8426 return -ENOSYS;
8427 default:
8428 return -EINVAL;
8429 }
8430
8431 res = fan_set_level_safe(level);
8432 if (res == -ENXIO)
8433 return -EINVAL;
8434 else if (res < 0)
8435 return res;
8436
8437 fan_watchdog_reset();
8438
8439 return count;
8440 }
8441
8442 static DEVICE_ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
8443 fan_pwm1_enable_show, fan_pwm1_enable_store);
8444
8445 /* sysfs fan pwm1 ------------------------------------------------------ */
8446 static ssize_t fan_pwm1_show(struct device *dev,
8447 struct device_attribute *attr,
8448 char *buf)
8449 {
8450 int res;
8451 u8 status;
8452
8453 res = fan_get_status_safe(&status);
8454 if (res)
8455 return res;
8456
8457 if ((status &
8458 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
8459 status = fan_control_desired_level;
8460
8461 if (status > 7)
8462 status = 7;
8463
8464 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
8465 }
8466
8467 static ssize_t fan_pwm1_store(struct device *dev,
8468 struct device_attribute *attr,
8469 const char *buf, size_t count)
8470 {
8471 unsigned long s;
8472 int rc;
8473 u8 status, newlevel;
8474
8475 if (parse_strtoul(buf, 255, &s))
8476 return -EINVAL;
8477
8478 tpacpi_disclose_usertask("hwmon pwm1",
8479 "set fan speed to %lu\n", s);
8480
8481 /* scale down from 0-255 to 0-7 */
8482 newlevel = (s >> 5) & 0x07;
8483
8484 if (mutex_lock_killable(&fan_mutex))
8485 return -ERESTARTSYS;
8486
8487 rc = fan_get_status(&status);
8488 if (!rc && (status &
8489 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
8490 rc = fan_set_level(newlevel);
8491 if (rc == -ENXIO)
8492 rc = -EINVAL;
8493 else if (!rc) {
8494 fan_update_desired_level(newlevel);
8495 fan_watchdog_reset();
8496 }
8497 }
8498
8499 mutex_unlock(&fan_mutex);
8500 return (rc) ? rc : count;
8501 }
8502
8503 static DEVICE_ATTR(pwm1, S_IWUSR | S_IRUGO, fan_pwm1_show, fan_pwm1_store);
8504
8505 /* sysfs fan fan1_input ------------------------------------------------ */
8506 static ssize_t fan_fan1_input_show(struct device *dev,
8507 struct device_attribute *attr,
8508 char *buf)
8509 {
8510 int res;
8511 unsigned int speed;
8512
8513 res = fan_get_speed(&speed);
8514 if (res < 0)
8515 return res;
8516
8517 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8518 }
8519
8520 static DEVICE_ATTR(fan1_input, S_IRUGO, fan_fan1_input_show, NULL);
8521
8522 /* sysfs fan fan2_input ------------------------------------------------ */
8523 static ssize_t fan_fan2_input_show(struct device *dev,
8524 struct device_attribute *attr,
8525 char *buf)
8526 {
8527 int res;
8528 unsigned int speed;
8529
8530 res = fan2_get_speed(&speed);
8531 if (res < 0)
8532 return res;
8533
8534 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
8535 }
8536
8537 static DEVICE_ATTR(fan2_input, S_IRUGO, fan_fan2_input_show, NULL);
8538
8539 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
8540 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
8541 char *buf)
8542 {
8543 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
8544 }
8545
8546 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
8547 const char *buf, size_t count)
8548 {
8549 unsigned long t;
8550
8551 if (parse_strtoul(buf, 120, &t))
8552 return -EINVAL;
8553
8554 if (!fan_control_allowed)
8555 return -EPERM;
8556
8557 fan_watchdog_maxinterval = t;
8558 fan_watchdog_reset();
8559
8560 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
8561
8562 return count;
8563 }
8564
8565 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
8566 fan_fan_watchdog_show, fan_fan_watchdog_store);
8567
8568 /* --------------------------------------------------------------------- */
8569 static struct attribute *fan_attributes[] = {
8570 &dev_attr_pwm1_enable.attr, &dev_attr_pwm1.attr,
8571 &dev_attr_fan1_input.attr,
8572 NULL, /* for fan2_input */
8573 NULL
8574 };
8575
8576 static const struct attribute_group fan_attr_group = {
8577 .attrs = fan_attributes,
8578 };
8579
8580 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
8581 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
8582
8583 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
8584 { .vendor = PCI_VENDOR_ID_IBM, \
8585 .bios = TPACPI_MATCH_ANY, \
8586 .ec = TPID(__id1, __id2), \
8587 .quirks = __quirks }
8588
8589 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
8590 { .vendor = PCI_VENDOR_ID_LENOVO, \
8591 .bios = TPACPI_MATCH_ANY, \
8592 .ec = TPID(__id1, __id2), \
8593 .quirks = __quirks }
8594
8595 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
8596 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
8597 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
8598 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
8599 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
8600 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
8601 };
8602
8603 #undef TPACPI_FAN_QL
8604 #undef TPACPI_FAN_QI
8605
8606 static int __init fan_init(struct ibm_init_struct *iibm)
8607 {
8608 int rc;
8609 unsigned long quirks;
8610
8611 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8612 "initializing fan subdriver\n");
8613
8614 mutex_init(&fan_mutex);
8615 fan_status_access_mode = TPACPI_FAN_NONE;
8616 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8617 fan_control_commands = 0;
8618 fan_watchdog_maxinterval = 0;
8619 tp_features.fan_ctrl_status_undef = 0;
8620 tp_features.second_fan = 0;
8621 fan_control_desired_level = 7;
8622
8623 if (tpacpi_is_ibm()) {
8624 TPACPI_ACPIHANDLE_INIT(fans);
8625 TPACPI_ACPIHANDLE_INIT(gfan);
8626 TPACPI_ACPIHANDLE_INIT(sfan);
8627 }
8628
8629 quirks = tpacpi_check_quirks(fan_quirk_table,
8630 ARRAY_SIZE(fan_quirk_table));
8631
8632 if (gfan_handle) {
8633 /* 570, 600e/x, 770e, 770x */
8634 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
8635 } else {
8636 /* all other ThinkPads: note that even old-style
8637 * ThinkPad ECs supports the fan control register */
8638 if (likely(acpi_ec_read(fan_status_offset,
8639 &fan_control_initial_status))) {
8640 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
8641 if (quirks & TPACPI_FAN_Q1)
8642 fan_quirk1_setup();
8643 if (quirks & TPACPI_FAN_2FAN) {
8644 tp_features.second_fan = 1;
8645 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8646 "secondary fan support enabled\n");
8647 }
8648 } else {
8649 pr_err("ThinkPad ACPI EC access misbehaving, "
8650 "fan status and control unavailable\n");
8651 return 1;
8652 }
8653 }
8654
8655 if (sfan_handle) {
8656 /* 570, 770x-JL */
8657 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
8658 fan_control_commands |=
8659 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
8660 } else {
8661 if (!gfan_handle) {
8662 /* gfan without sfan means no fan control */
8663 /* all other models implement TP EC 0x2f control */
8664
8665 if (fans_handle) {
8666 /* X31, X40, X41 */
8667 fan_control_access_mode =
8668 TPACPI_FAN_WR_ACPI_FANS;
8669 fan_control_commands |=
8670 TPACPI_FAN_CMD_SPEED |
8671 TPACPI_FAN_CMD_LEVEL |
8672 TPACPI_FAN_CMD_ENABLE;
8673 } else {
8674 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
8675 fan_control_commands |=
8676 TPACPI_FAN_CMD_LEVEL |
8677 TPACPI_FAN_CMD_ENABLE;
8678 }
8679 }
8680 }
8681
8682 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8683 "fan is %s, modes %d, %d\n",
8684 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
8685 fan_control_access_mode != TPACPI_FAN_WR_NONE),
8686 fan_status_access_mode, fan_control_access_mode);
8687
8688 /* fan control master switch */
8689 if (!fan_control_allowed) {
8690 fan_control_access_mode = TPACPI_FAN_WR_NONE;
8691 fan_control_commands = 0;
8692 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
8693 "fan control features disabled by parameter\n");
8694 }
8695
8696 /* update fan_control_desired_level */
8697 if (fan_status_access_mode != TPACPI_FAN_NONE)
8698 fan_get_status_safe(NULL);
8699
8700 if (fan_status_access_mode != TPACPI_FAN_NONE ||
8701 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
8702 if (tp_features.second_fan) {
8703 /* attach second fan tachometer */
8704 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
8705 &dev_attr_fan2_input.attr;
8706 }
8707 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
8708 &fan_attr_group);
8709 if (rc < 0)
8710 return rc;
8711
8712 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
8713 &driver_attr_fan_watchdog);
8714 if (rc < 0) {
8715 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
8716 &fan_attr_group);
8717 return rc;
8718 }
8719 return 0;
8720 } else
8721 return 1;
8722 }
8723
8724 static void fan_exit(void)
8725 {
8726 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
8727 "cancelling any pending fan watchdog tasks\n");
8728
8729 /* FIXME: can we really do this unconditionally? */
8730 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
8731 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
8732 &driver_attr_fan_watchdog);
8733
8734 cancel_delayed_work(&fan_watchdog_task);
8735 flush_workqueue(tpacpi_wq);
8736 }
8737
8738 static void fan_suspend(void)
8739 {
8740 int rc;
8741
8742 if (!fan_control_allowed)
8743 return;
8744
8745 /* Store fan status in cache */
8746 fan_control_resume_level = 0;
8747 rc = fan_get_status_safe(&fan_control_resume_level);
8748 if (rc < 0)
8749 pr_notice("failed to read fan level for later "
8750 "restore during resume: %d\n", rc);
8751
8752 /* if it is undefined, don't attempt to restore it.
8753 * KEEP THIS LAST */
8754 if (tp_features.fan_ctrl_status_undef)
8755 fan_control_resume_level = 0;
8756 }
8757
8758 static void fan_resume(void)
8759 {
8760 u8 current_level = 7;
8761 bool do_set = false;
8762 int rc;
8763
8764 /* DSDT *always* updates status on resume */
8765 tp_features.fan_ctrl_status_undef = 0;
8766
8767 if (!fan_control_allowed ||
8768 !fan_control_resume_level ||
8769 (fan_get_status_safe(&current_level) < 0))
8770 return;
8771
8772 switch (fan_control_access_mode) {
8773 case TPACPI_FAN_WR_ACPI_SFAN:
8774 /* never decrease fan level */
8775 do_set = (fan_control_resume_level > current_level);
8776 break;
8777 case TPACPI_FAN_WR_ACPI_FANS:
8778 case TPACPI_FAN_WR_TPEC:
8779 /* never decrease fan level, scale is:
8780 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
8781 *
8782 * We expect the firmware to set either 7 or AUTO, but we
8783 * handle FULLSPEED out of paranoia.
8784 *
8785 * So, we can safely only restore FULLSPEED or 7, anything
8786 * else could slow the fan. Restoring AUTO is useless, at
8787 * best that's exactly what the DSDT already set (it is the
8788 * slower it uses).
8789 *
8790 * Always keep in mind that the DSDT *will* have set the
8791 * fans to what the vendor supposes is the best level. We
8792 * muck with it only to speed the fan up.
8793 */
8794 if (fan_control_resume_level != 7 &&
8795 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
8796 return;
8797 else
8798 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
8799 (current_level != fan_control_resume_level);
8800 break;
8801 default:
8802 return;
8803 }
8804 if (do_set) {
8805 pr_notice("restoring fan level to 0x%02x\n",
8806 fan_control_resume_level);
8807 rc = fan_set_level_safe(fan_control_resume_level);
8808 if (rc < 0)
8809 pr_notice("failed to restore fan level: %d\n", rc);
8810 }
8811 }
8812
8813 static int fan_read(struct seq_file *m)
8814 {
8815 int rc;
8816 u8 status;
8817 unsigned int speed = 0;
8818
8819 switch (fan_status_access_mode) {
8820 case TPACPI_FAN_RD_ACPI_GFAN:
8821 /* 570, 600e/x, 770e, 770x */
8822 rc = fan_get_status_safe(&status);
8823 if (rc < 0)
8824 return rc;
8825
8826 seq_printf(m, "status:\t\t%s\n"
8827 "level:\t\t%d\n",
8828 (status != 0) ? "enabled" : "disabled", status);
8829 break;
8830
8831 case TPACPI_FAN_RD_TPEC:
8832 /* all except 570, 600e/x, 770e, 770x */
8833 rc = fan_get_status_safe(&status);
8834 if (rc < 0)
8835 return rc;
8836
8837 seq_printf(m, "status:\t\t%s\n",
8838 (status != 0) ? "enabled" : "disabled");
8839
8840 rc = fan_get_speed(&speed);
8841 if (rc < 0)
8842 return rc;
8843
8844 seq_printf(m, "speed:\t\t%d\n", speed);
8845
8846 if (status & TP_EC_FAN_FULLSPEED)
8847 /* Disengaged mode takes precedence */
8848 seq_printf(m, "level:\t\tdisengaged\n");
8849 else if (status & TP_EC_FAN_AUTO)
8850 seq_printf(m, "level:\t\tauto\n");
8851 else
8852 seq_printf(m, "level:\t\t%d\n", status);
8853 break;
8854
8855 case TPACPI_FAN_NONE:
8856 default:
8857 seq_printf(m, "status:\t\tnot supported\n");
8858 }
8859
8860 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
8861 seq_printf(m, "commands:\tlevel <level>");
8862
8863 switch (fan_control_access_mode) {
8864 case TPACPI_FAN_WR_ACPI_SFAN:
8865 seq_printf(m, " (<level> is 0-7)\n");
8866 break;
8867
8868 default:
8869 seq_printf(m, " (<level> is 0-7, "
8870 "auto, disengaged, full-speed)\n");
8871 break;
8872 }
8873 }
8874
8875 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
8876 seq_printf(m, "commands:\tenable, disable\n"
8877 "commands:\twatchdog <timeout> (<timeout> "
8878 "is 0 (off), 1-120 (seconds))\n");
8879
8880 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
8881 seq_printf(m, "commands:\tspeed <speed>"
8882 " (<speed> is 0-65535)\n");
8883
8884 return 0;
8885 }
8886
8887 static int fan_write_cmd_level(const char *cmd, int *rc)
8888 {
8889 int level;
8890
8891 if (strlencmp(cmd, "level auto") == 0)
8892 level = TP_EC_FAN_AUTO;
8893 else if ((strlencmp(cmd, "level disengaged") == 0) |
8894 (strlencmp(cmd, "level full-speed") == 0))
8895 level = TP_EC_FAN_FULLSPEED;
8896 else if (sscanf(cmd, "level %d", &level) != 1)
8897 return 0;
8898
8899 *rc = fan_set_level_safe(level);
8900 if (*rc == -ENXIO)
8901 pr_err("level command accepted for unsupported access mode %d\n",
8902 fan_control_access_mode);
8903 else if (!*rc)
8904 tpacpi_disclose_usertask("procfs fan",
8905 "set level to %d\n", level);
8906
8907 return 1;
8908 }
8909
8910 static int fan_write_cmd_enable(const char *cmd, int *rc)
8911 {
8912 if (strlencmp(cmd, "enable") != 0)
8913 return 0;
8914
8915 *rc = fan_set_enable();
8916 if (*rc == -ENXIO)
8917 pr_err("enable command accepted for unsupported access mode %d\n",
8918 fan_control_access_mode);
8919 else if (!*rc)
8920 tpacpi_disclose_usertask("procfs fan", "enable\n");
8921
8922 return 1;
8923 }
8924
8925 static int fan_write_cmd_disable(const char *cmd, int *rc)
8926 {
8927 if (strlencmp(cmd, "disable") != 0)
8928 return 0;
8929
8930 *rc = fan_set_disable();
8931 if (*rc == -ENXIO)
8932 pr_err("disable command accepted for unsupported access mode %d\n",
8933 fan_control_access_mode);
8934 else if (!*rc)
8935 tpacpi_disclose_usertask("procfs fan", "disable\n");
8936
8937 return 1;
8938 }
8939
8940 static int fan_write_cmd_speed(const char *cmd, int *rc)
8941 {
8942 int speed;
8943
8944 /* TODO:
8945 * Support speed <low> <medium> <high> ? */
8946
8947 if (sscanf(cmd, "speed %d", &speed) != 1)
8948 return 0;
8949
8950 *rc = fan_set_speed(speed);
8951 if (*rc == -ENXIO)
8952 pr_err("speed command accepted for unsupported access mode %d\n",
8953 fan_control_access_mode);
8954 else if (!*rc)
8955 tpacpi_disclose_usertask("procfs fan",
8956 "set speed to %d\n", speed);
8957
8958 return 1;
8959 }
8960
8961 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
8962 {
8963 int interval;
8964
8965 if (sscanf(cmd, "watchdog %d", &interval) != 1)
8966 return 0;
8967
8968 if (interval < 0 || interval > 120)
8969 *rc = -EINVAL;
8970 else {
8971 fan_watchdog_maxinterval = interval;
8972 tpacpi_disclose_usertask("procfs fan",
8973 "set watchdog timer to %d\n",
8974 interval);
8975 }
8976
8977 return 1;
8978 }
8979
8980 static int fan_write(char *buf)
8981 {
8982 char *cmd;
8983 int rc = 0;
8984
8985 while (!rc && (cmd = next_cmd(&buf))) {
8986 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
8987 fan_write_cmd_level(cmd, &rc)) &&
8988 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
8989 (fan_write_cmd_enable(cmd, &rc) ||
8990 fan_write_cmd_disable(cmd, &rc) ||
8991 fan_write_cmd_watchdog(cmd, &rc))) &&
8992 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
8993 fan_write_cmd_speed(cmd, &rc))
8994 )
8995 rc = -EINVAL;
8996 else if (!rc)
8997 fan_watchdog_reset();
8998 }
8999
9000 return rc;
9001 }
9002
9003 static struct ibm_struct fan_driver_data = {
9004 .name = "fan",
9005 .read = fan_read,
9006 .write = fan_write,
9007 .exit = fan_exit,
9008 .suspend = fan_suspend,
9009 .resume = fan_resume,
9010 };
9011
9012 /*************************************************************************
9013 * Mute LED subdriver
9014 */
9015
9016
9017 struct tp_led_table {
9018 acpi_string name;
9019 int on_value;
9020 int off_value;
9021 int state;
9022 };
9023
9024 static struct tp_led_table led_tables[] = {
9025 [TPACPI_LED_MUTE] = {
9026 .name = "SSMS",
9027 .on_value = 1,
9028 .off_value = 0,
9029 },
9030 [TPACPI_LED_MICMUTE] = {
9031 .name = "MMTS",
9032 .on_value = 2,
9033 .off_value = 0,
9034 },
9035 };
9036
9037 static int mute_led_on_off(struct tp_led_table *t, bool state)
9038 {
9039 acpi_handle temp;
9040 int output;
9041
9042 if (ACPI_FAILURE(acpi_get_handle(hkey_handle, t->name, &temp))) {
9043 pr_warn("Thinkpad ACPI has no %s interface.\n", t->name);
9044 return -EIO;
9045 }
9046
9047 if (!acpi_evalf(hkey_handle, &output, t->name, "dd",
9048 state ? t->on_value : t->off_value))
9049 return -EIO;
9050
9051 t->state = state;
9052 return state;
9053 }
9054
9055 int tpacpi_led_set(int whichled, bool on)
9056 {
9057 struct tp_led_table *t;
9058
9059 if (whichled < 0 || whichled >= TPACPI_LED_MAX)
9060 return -EINVAL;
9061
9062 t = &led_tables[whichled];
9063 if (t->state < 0 || t->state == on)
9064 return t->state;
9065 return mute_led_on_off(t, on);
9066 }
9067 EXPORT_SYMBOL_GPL(tpacpi_led_set);
9068
9069 static int mute_led_init(struct ibm_init_struct *iibm)
9070 {
9071 acpi_handle temp;
9072 int i;
9073
9074 for (i = 0; i < TPACPI_LED_MAX; i++) {
9075 struct tp_led_table *t = &led_tables[i];
9076 if (ACPI_SUCCESS(acpi_get_handle(hkey_handle, t->name, &temp)))
9077 mute_led_on_off(t, false);
9078 else
9079 t->state = -ENODEV;
9080 }
9081 return 0;
9082 }
9083
9084 static void mute_led_exit(void)
9085 {
9086 int i;
9087
9088 for (i = 0; i < TPACPI_LED_MAX; i++)
9089 tpacpi_led_set(i, false);
9090 }
9091
9092 static void mute_led_resume(void)
9093 {
9094 int i;
9095
9096 for (i = 0; i < TPACPI_LED_MAX; i++) {
9097 struct tp_led_table *t = &led_tables[i];
9098 if (t->state >= 0)
9099 mute_led_on_off(t, t->state);
9100 }
9101 }
9102
9103 static struct ibm_struct mute_led_driver_data = {
9104 .name = "mute_led",
9105 .exit = mute_led_exit,
9106 .resume = mute_led_resume,
9107 };
9108
9109 /****************************************************************************
9110 ****************************************************************************
9111 *
9112 * Infrastructure
9113 *
9114 ****************************************************************************
9115 ****************************************************************************/
9116
9117 /*
9118 * HKEY event callout for other subdrivers go here
9119 * (yes, it is ugly, but it is quick, safe, and gets the job done
9120 */
9121 static void tpacpi_driver_event(const unsigned int hkey_event)
9122 {
9123 if (ibm_backlight_device) {
9124 switch (hkey_event) {
9125 case TP_HKEY_EV_BRGHT_UP:
9126 case TP_HKEY_EV_BRGHT_DOWN:
9127 tpacpi_brightness_notify_change();
9128 }
9129 }
9130 if (alsa_card) {
9131 switch (hkey_event) {
9132 case TP_HKEY_EV_VOL_UP:
9133 case TP_HKEY_EV_VOL_DOWN:
9134 case TP_HKEY_EV_VOL_MUTE:
9135 volume_alsa_notify_change();
9136 }
9137 }
9138 if (tp_features.kbdlight && hkey_event == TP_HKEY_EV_KBD_LIGHT) {
9139 enum led_brightness brightness;
9140
9141 mutex_lock(&kbdlight_mutex);
9142
9143 /*
9144 * Check the brightness actually changed, setting the brightness
9145 * through kbdlight_set_level() also triggers this event.
9146 */
9147 brightness = kbdlight_sysfs_get(NULL);
9148 if (kbdlight_brightness != brightness) {
9149 kbdlight_brightness = brightness;
9150 led_classdev_notify_brightness_hw_changed(
9151 &tpacpi_led_kbdlight.led_classdev, brightness);
9152 }
9153
9154 mutex_unlock(&kbdlight_mutex);
9155 }
9156 }
9157
9158 static void hotkey_driver_event(const unsigned int scancode)
9159 {
9160 tpacpi_driver_event(TP_HKEY_EV_HOTKEY_BASE + scancode);
9161 }
9162
9163 /* sysfs name ---------------------------------------------------------- */
9164 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
9165 struct device_attribute *attr,
9166 char *buf)
9167 {
9168 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
9169 }
9170
9171 static DEVICE_ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
9172
9173 /* --------------------------------------------------------------------- */
9174
9175 /* /proc support */
9176 static struct proc_dir_entry *proc_dir;
9177
9178 /*
9179 * Module and infrastructure proble, init and exit handling
9180 */
9181
9182 static bool force_load;
9183
9184 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
9185 static const char * __init str_supported(int is_supported)
9186 {
9187 static char text_unsupported[] __initdata = "not supported";
9188
9189 return (is_supported) ? &text_unsupported[4] : &text_unsupported[0];
9190 }
9191 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
9192
9193 static void ibm_exit(struct ibm_struct *ibm)
9194 {
9195 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
9196
9197 list_del_init(&ibm->all_drivers);
9198
9199 if (ibm->flags.acpi_notify_installed) {
9200 dbg_printk(TPACPI_DBG_EXIT,
9201 "%s: acpi_remove_notify_handler\n", ibm->name);
9202 BUG_ON(!ibm->acpi);
9203 acpi_remove_notify_handler(*ibm->acpi->handle,
9204 ibm->acpi->type,
9205 dispatch_acpi_notify);
9206 ibm->flags.acpi_notify_installed = 0;
9207 }
9208
9209 if (ibm->flags.proc_created) {
9210 dbg_printk(TPACPI_DBG_EXIT,
9211 "%s: remove_proc_entry\n", ibm->name);
9212 remove_proc_entry(ibm->name, proc_dir);
9213 ibm->flags.proc_created = 0;
9214 }
9215
9216 if (ibm->flags.acpi_driver_registered) {
9217 dbg_printk(TPACPI_DBG_EXIT,
9218 "%s: acpi_bus_unregister_driver\n", ibm->name);
9219 BUG_ON(!ibm->acpi);
9220 acpi_bus_unregister_driver(ibm->acpi->driver);
9221 kfree(ibm->acpi->driver);
9222 ibm->acpi->driver = NULL;
9223 ibm->flags.acpi_driver_registered = 0;
9224 }
9225
9226 if (ibm->flags.init_called && ibm->exit) {
9227 ibm->exit();
9228 ibm->flags.init_called = 0;
9229 }
9230
9231 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
9232 }
9233
9234 static int __init ibm_init(struct ibm_init_struct *iibm)
9235 {
9236 int ret;
9237 struct ibm_struct *ibm = iibm->data;
9238 struct proc_dir_entry *entry;
9239
9240 BUG_ON(ibm == NULL);
9241
9242 INIT_LIST_HEAD(&ibm->all_drivers);
9243
9244 if (ibm->flags.experimental && !experimental)
9245 return 0;
9246
9247 dbg_printk(TPACPI_DBG_INIT,
9248 "probing for %s\n", ibm->name);
9249
9250 if (iibm->init) {
9251 ret = iibm->init(iibm);
9252 if (ret > 0)
9253 return 0; /* probe failed */
9254 if (ret)
9255 return ret;
9256
9257 ibm->flags.init_called = 1;
9258 }
9259
9260 if (ibm->acpi) {
9261 if (ibm->acpi->hid) {
9262 ret = register_tpacpi_subdriver(ibm);
9263 if (ret)
9264 goto err_out;
9265 }
9266
9267 if (ibm->acpi->notify) {
9268 ret = setup_acpi_notify(ibm);
9269 if (ret == -ENODEV) {
9270 pr_notice("disabling subdriver %s\n",
9271 ibm->name);
9272 ret = 0;
9273 goto err_out;
9274 }
9275 if (ret < 0)
9276 goto err_out;
9277 }
9278 }
9279
9280 dbg_printk(TPACPI_DBG_INIT,
9281 "%s installed\n", ibm->name);
9282
9283 if (ibm->read) {
9284 umode_t mode = iibm->base_procfs_mode;
9285
9286 if (!mode)
9287 mode = S_IRUGO;
9288 if (ibm->write)
9289 mode |= S_IWUSR;
9290 entry = proc_create_data(ibm->name, mode, proc_dir,
9291 &dispatch_proc_fops, ibm);
9292 if (!entry) {
9293 pr_err("unable to create proc entry %s\n", ibm->name);
9294 ret = -ENODEV;
9295 goto err_out;
9296 }
9297 ibm->flags.proc_created = 1;
9298 }
9299
9300 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
9301
9302 return 0;
9303
9304 err_out:
9305 dbg_printk(TPACPI_DBG_INIT,
9306 "%s: at error exit path with result %d\n",
9307 ibm->name, ret);
9308
9309 ibm_exit(ibm);
9310 return (ret < 0) ? ret : 0;
9311 }
9312
9313 /* Probing */
9314
9315 static bool __pure __init tpacpi_is_fw_digit(const char c)
9316 {
9317 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
9318 }
9319
9320 static bool __pure __init tpacpi_is_valid_fw_id(const char * const s,
9321 const char t)
9322 {
9323 /*
9324 * Most models: xxyTkkWW (#.##c)
9325 * Ancient 570/600 and -SL lacks (#.##c)
9326 */
9327 if (s && strlen(s) >= 8 &&
9328 tpacpi_is_fw_digit(s[0]) &&
9329 tpacpi_is_fw_digit(s[1]) &&
9330 s[2] == t &&
9331 (s[3] == 'T' || s[3] == 'N') &&
9332 tpacpi_is_fw_digit(s[4]) &&
9333 tpacpi_is_fw_digit(s[5]))
9334 return true;
9335
9336 /* New models: xxxyTkkW (#.##c); T550 and some others */
9337 return s && strlen(s) >= 8 &&
9338 tpacpi_is_fw_digit(s[0]) &&
9339 tpacpi_is_fw_digit(s[1]) &&
9340 tpacpi_is_fw_digit(s[2]) &&
9341 s[3] == t &&
9342 (s[4] == 'T' || s[4] == 'N') &&
9343 tpacpi_is_fw_digit(s[5]) &&
9344 tpacpi_is_fw_digit(s[6]);
9345 }
9346
9347 /* returns 0 - probe ok, or < 0 - probe error.
9348 * Probe ok doesn't mean thinkpad found.
9349 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
9350 static int __must_check __init get_thinkpad_model_data(
9351 struct thinkpad_id_data *tp)
9352 {
9353 const struct dmi_device *dev = NULL;
9354 char ec_fw_string[18];
9355 char const *s;
9356
9357 if (!tp)
9358 return -EINVAL;
9359
9360 memset(tp, 0, sizeof(*tp));
9361
9362 if (dmi_name_in_vendors("IBM"))
9363 tp->vendor = PCI_VENDOR_ID_IBM;
9364 else if (dmi_name_in_vendors("LENOVO"))
9365 tp->vendor = PCI_VENDOR_ID_LENOVO;
9366 else
9367 return 0;
9368
9369 s = dmi_get_system_info(DMI_BIOS_VERSION);
9370 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
9371 if (s && !tp->bios_version_str)
9372 return -ENOMEM;
9373
9374 /* Really ancient ThinkPad 240X will fail this, which is fine */
9375 if (!(tpacpi_is_valid_fw_id(tp->bios_version_str, 'E') ||
9376 tpacpi_is_valid_fw_id(tp->bios_version_str, 'C')))
9377 return 0;
9378
9379 tp->bios_model = tp->bios_version_str[0]
9380 | (tp->bios_version_str[1] << 8);
9381 tp->bios_release = (tp->bios_version_str[4] << 8)
9382 | tp->bios_version_str[5];
9383
9384 /*
9385 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
9386 * X32 or newer, all Z series; Some models must have an
9387 * up-to-date BIOS or they will not be detected.
9388 *
9389 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9390 */
9391 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
9392 if (sscanf(dev->name,
9393 "IBM ThinkPad Embedded Controller -[%17c",
9394 ec_fw_string) == 1) {
9395 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
9396 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
9397
9398 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
9399 if (!tp->ec_version_str)
9400 return -ENOMEM;
9401
9402 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
9403 tp->ec_model = ec_fw_string[0]
9404 | (ec_fw_string[1] << 8);
9405 tp->ec_release = (ec_fw_string[4] << 8)
9406 | ec_fw_string[5];
9407 } else {
9408 pr_notice("ThinkPad firmware release %s "
9409 "doesn't match the known patterns\n",
9410 ec_fw_string);
9411 pr_notice("please report this to %s\n",
9412 TPACPI_MAIL);
9413 }
9414 break;
9415 }
9416 }
9417
9418 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
9419 if (s && !(strncasecmp(s, "ThinkPad", 8) && strncasecmp(s, "Lenovo", 6))) {
9420 tp->model_str = kstrdup(s, GFP_KERNEL);
9421 if (!tp->model_str)
9422 return -ENOMEM;
9423 } else {
9424 s = dmi_get_system_info(DMI_BIOS_VENDOR);
9425 if (s && !(strncasecmp(s, "Lenovo", 6))) {
9426 tp->model_str = kstrdup(s, GFP_KERNEL);
9427 if (!tp->model_str)
9428 return -ENOMEM;
9429 }
9430 }
9431
9432 s = dmi_get_system_info(DMI_PRODUCT_NAME);
9433 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
9434 if (s && !tp->nummodel_str)
9435 return -ENOMEM;
9436
9437 return 0;
9438 }
9439
9440 static int __init probe_for_thinkpad(void)
9441 {
9442 int is_thinkpad;
9443
9444 if (acpi_disabled)
9445 return -ENODEV;
9446
9447 /* It would be dangerous to run the driver in this case */
9448 if (!tpacpi_is_ibm() && !tpacpi_is_lenovo())
9449 return -ENODEV;
9450
9451 /*
9452 * Non-ancient models have better DMI tagging, but very old models
9453 * don't. tpacpi_is_fw_known() is a cheat to help in that case.
9454 */
9455 is_thinkpad = (thinkpad_id.model_str != NULL) ||
9456 (thinkpad_id.ec_model != 0) ||
9457 tpacpi_is_fw_known();
9458
9459 /* The EC handler is required */
9460 tpacpi_acpi_handle_locate("ec", TPACPI_ACPI_EC_HID, &ec_handle);
9461 if (!ec_handle) {
9462 if (is_thinkpad)
9463 pr_err("Not yet supported ThinkPad detected!\n");
9464 return -ENODEV;
9465 }
9466
9467 if (!is_thinkpad && !force_load)
9468 return -ENODEV;
9469
9470 return 0;
9471 }
9472
9473 static void __init thinkpad_acpi_init_banner(void)
9474 {
9475 pr_info("%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
9476 pr_info("%s\n", TPACPI_URL);
9477
9478 pr_info("ThinkPad BIOS %s, EC %s\n",
9479 (thinkpad_id.bios_version_str) ?
9480 thinkpad_id.bios_version_str : "unknown",
9481 (thinkpad_id.ec_version_str) ?
9482 thinkpad_id.ec_version_str : "unknown");
9483
9484 BUG_ON(!thinkpad_id.vendor);
9485
9486 if (thinkpad_id.model_str)
9487 pr_info("%s %s, model %s\n",
9488 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
9489 "IBM" : ((thinkpad_id.vendor ==
9490 PCI_VENDOR_ID_LENOVO) ?
9491 "Lenovo" : "Unknown vendor"),
9492 thinkpad_id.model_str,
9493 (thinkpad_id.nummodel_str) ?
9494 thinkpad_id.nummodel_str : "unknown");
9495 }
9496
9497 /* Module init, exit, parameters */
9498
9499 static struct ibm_init_struct ibms_init[] __initdata = {
9500 {
9501 .data = &thinkpad_acpi_driver_data,
9502 },
9503 {
9504 .init = hotkey_init,
9505 .data = &hotkey_driver_data,
9506 },
9507 {
9508 .init = bluetooth_init,
9509 .data = &bluetooth_driver_data,
9510 },
9511 {
9512 .init = wan_init,
9513 .data = &wan_driver_data,
9514 },
9515 {
9516 .init = uwb_init,
9517 .data = &uwb_driver_data,
9518 },
9519 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
9520 {
9521 .init = video_init,
9522 .base_procfs_mode = S_IRUSR,
9523 .data = &video_driver_data,
9524 },
9525 #endif
9526 {
9527 .init = kbdlight_init,
9528 .data = &kbdlight_driver_data,
9529 },
9530 {
9531 .init = light_init,
9532 .data = &light_driver_data,
9533 },
9534 {
9535 .init = cmos_init,
9536 .data = &cmos_driver_data,
9537 },
9538 {
9539 .init = led_init,
9540 .data = &led_driver_data,
9541 },
9542 {
9543 .init = beep_init,
9544 .data = &beep_driver_data,
9545 },
9546 {
9547 .init = thermal_init,
9548 .data = &thermal_driver_data,
9549 },
9550 {
9551 .init = brightness_init,
9552 .data = &brightness_driver_data,
9553 },
9554 {
9555 .init = volume_init,
9556 .data = &volume_driver_data,
9557 },
9558 {
9559 .init = fan_init,
9560 .data = &fan_driver_data,
9561 },
9562 {
9563 .init = mute_led_init,
9564 .data = &mute_led_driver_data,
9565 },
9566 };
9567
9568 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
9569 {
9570 unsigned int i;
9571 struct ibm_struct *ibm;
9572
9573 if (!kp || !kp->name || !val)
9574 return -EINVAL;
9575
9576 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9577 ibm = ibms_init[i].data;
9578 WARN_ON(ibm == NULL);
9579
9580 if (!ibm || !ibm->name)
9581 continue;
9582
9583 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
9584 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
9585 return -ENOSPC;
9586 strcpy(ibms_init[i].param, val);
9587 strcat(ibms_init[i].param, ",");
9588 return 0;
9589 }
9590 }
9591
9592 return -EINVAL;
9593 }
9594
9595 module_param(experimental, int, 0444);
9596 MODULE_PARM_DESC(experimental,
9597 "Enables experimental features when non-zero");
9598
9599 module_param_named(debug, dbg_level, uint, 0);
9600 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
9601
9602 module_param(force_load, bool, 0444);
9603 MODULE_PARM_DESC(force_load,
9604 "Attempts to load the driver even on a "
9605 "mis-identified ThinkPad when true");
9606
9607 module_param_named(fan_control, fan_control_allowed, bool, 0444);
9608 MODULE_PARM_DESC(fan_control,
9609 "Enables setting fan parameters features when true");
9610
9611 module_param_named(brightness_mode, brightness_mode, uint, 0444);
9612 MODULE_PARM_DESC(brightness_mode,
9613 "Selects brightness control strategy: "
9614 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
9615
9616 module_param(brightness_enable, uint, 0444);
9617 MODULE_PARM_DESC(brightness_enable,
9618 "Enables backlight control when 1, disables when 0");
9619
9620 #ifdef CONFIG_THINKPAD_ACPI_ALSA_SUPPORT
9621 module_param_named(volume_mode, volume_mode, uint, 0444);
9622 MODULE_PARM_DESC(volume_mode,
9623 "Selects volume control strategy: "
9624 "0=auto, 1=EC, 2=N/A, 3=EC+NVRAM");
9625
9626 module_param_named(volume_capabilities, volume_capabilities, uint, 0444);
9627 MODULE_PARM_DESC(volume_capabilities,
9628 "Selects the mixer capabilites: "
9629 "0=auto, 1=volume and mute, 2=mute only");
9630
9631 module_param_named(volume_control, volume_control_allowed, bool, 0444);
9632 MODULE_PARM_DESC(volume_control,
9633 "Enables software override for the console audio "
9634 "control when true");
9635
9636 module_param_named(software_mute, software_mute_requested, bool, 0444);
9637 MODULE_PARM_DESC(software_mute,
9638 "Request full software mute control");
9639
9640 /* ALSA module API parameters */
9641 module_param_named(index, alsa_index, int, 0444);
9642 MODULE_PARM_DESC(index, "ALSA index for the ACPI EC Mixer");
9643 module_param_named(id, alsa_id, charp, 0444);
9644 MODULE_PARM_DESC(id, "ALSA id for the ACPI EC Mixer");
9645 module_param_named(enable, alsa_enable, bool, 0444);
9646 MODULE_PARM_DESC(enable, "Enable the ALSA interface for the ACPI EC Mixer");
9647 #endif /* CONFIG_THINKPAD_ACPI_ALSA_SUPPORT */
9648
9649 #define TPACPI_PARAM(feature) \
9650 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
9651 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
9652 "at module load, see documentation")
9653
9654 TPACPI_PARAM(hotkey);
9655 TPACPI_PARAM(bluetooth);
9656 TPACPI_PARAM(video);
9657 TPACPI_PARAM(light);
9658 TPACPI_PARAM(cmos);
9659 TPACPI_PARAM(led);
9660 TPACPI_PARAM(beep);
9661 TPACPI_PARAM(brightness);
9662 TPACPI_PARAM(volume);
9663 TPACPI_PARAM(fan);
9664
9665 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
9666 module_param(dbg_wlswemul, uint, 0444);
9667 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
9668 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
9669 MODULE_PARM_DESC(wlsw_state,
9670 "Initial state of the emulated WLSW switch");
9671
9672 module_param(dbg_bluetoothemul, uint, 0444);
9673 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
9674 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
9675 MODULE_PARM_DESC(bluetooth_state,
9676 "Initial state of the emulated bluetooth switch");
9677
9678 module_param(dbg_wwanemul, uint, 0444);
9679 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
9680 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
9681 MODULE_PARM_DESC(wwan_state,
9682 "Initial state of the emulated WWAN switch");
9683
9684 module_param(dbg_uwbemul, uint, 0444);
9685 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
9686 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
9687 MODULE_PARM_DESC(uwb_state,
9688 "Initial state of the emulated UWB switch");
9689 #endif
9690
9691 static void thinkpad_acpi_module_exit(void)
9692 {
9693 struct ibm_struct *ibm, *itmp;
9694
9695 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
9696
9697 list_for_each_entry_safe_reverse(ibm, itmp,
9698 &tpacpi_all_drivers,
9699 all_drivers) {
9700 ibm_exit(ibm);
9701 }
9702
9703 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
9704
9705 if (tpacpi_inputdev) {
9706 if (tp_features.input_device_registered)
9707 input_unregister_device(tpacpi_inputdev);
9708 else
9709 input_free_device(tpacpi_inputdev);
9710 kfree(hotkey_keycode_map);
9711 }
9712
9713 if (tpacpi_hwmon)
9714 hwmon_device_unregister(tpacpi_hwmon);
9715
9716 if (tp_features.sensors_pdev_attrs_registered)
9717 device_remove_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
9718 if (tpacpi_sensors_pdev)
9719 platform_device_unregister(tpacpi_sensors_pdev);
9720 if (tpacpi_pdev)
9721 platform_device_unregister(tpacpi_pdev);
9722
9723 if (tp_features.sensors_pdrv_attrs_registered)
9724 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
9725 if (tp_features.platform_drv_attrs_registered)
9726 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
9727
9728 if (tp_features.sensors_pdrv_registered)
9729 platform_driver_unregister(&tpacpi_hwmon_pdriver);
9730
9731 if (tp_features.platform_drv_registered)
9732 platform_driver_unregister(&tpacpi_pdriver);
9733
9734 if (proc_dir)
9735 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
9736
9737 if (tpacpi_wq)
9738 destroy_workqueue(tpacpi_wq);
9739
9740 kfree(thinkpad_id.bios_version_str);
9741 kfree(thinkpad_id.ec_version_str);
9742 kfree(thinkpad_id.model_str);
9743 kfree(thinkpad_id.nummodel_str);
9744 }
9745
9746
9747 static int __init thinkpad_acpi_module_init(void)
9748 {
9749 int ret, i;
9750
9751 tpacpi_lifecycle = TPACPI_LIFE_INIT;
9752
9753 /* Driver-level probe */
9754
9755 ret = get_thinkpad_model_data(&thinkpad_id);
9756 if (ret) {
9757 pr_err("unable to get DMI data: %d\n", ret);
9758 thinkpad_acpi_module_exit();
9759 return ret;
9760 }
9761 ret = probe_for_thinkpad();
9762 if (ret) {
9763 thinkpad_acpi_module_exit();
9764 return ret;
9765 }
9766
9767 /* Driver initialization */
9768
9769 thinkpad_acpi_init_banner();
9770 tpacpi_check_outdated_fw();
9771
9772 TPACPI_ACPIHANDLE_INIT(ecrd);
9773 TPACPI_ACPIHANDLE_INIT(ecwr);
9774
9775 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
9776 if (!tpacpi_wq) {
9777 thinkpad_acpi_module_exit();
9778 return -ENOMEM;
9779 }
9780
9781 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
9782 if (!proc_dir) {
9783 pr_err("unable to create proc dir " TPACPI_PROC_DIR "\n");
9784 thinkpad_acpi_module_exit();
9785 return -ENODEV;
9786 }
9787
9788 ret = platform_driver_register(&tpacpi_pdriver);
9789 if (ret) {
9790 pr_err("unable to register main platform driver\n");
9791 thinkpad_acpi_module_exit();
9792 return ret;
9793 }
9794 tp_features.platform_drv_registered = 1;
9795
9796 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
9797 if (ret) {
9798 pr_err("unable to register hwmon platform driver\n");
9799 thinkpad_acpi_module_exit();
9800 return ret;
9801 }
9802 tp_features.sensors_pdrv_registered = 1;
9803
9804 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
9805 if (!ret) {
9806 tp_features.platform_drv_attrs_registered = 1;
9807 ret = tpacpi_create_driver_attributes(
9808 &tpacpi_hwmon_pdriver.driver);
9809 }
9810 if (ret) {
9811 pr_err("unable to create sysfs driver attributes\n");
9812 thinkpad_acpi_module_exit();
9813 return ret;
9814 }
9815 tp_features.sensors_pdrv_attrs_registered = 1;
9816
9817
9818 /* Device initialization */
9819 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
9820 NULL, 0);
9821 if (IS_ERR(tpacpi_pdev)) {
9822 ret = PTR_ERR(tpacpi_pdev);
9823 tpacpi_pdev = NULL;
9824 pr_err("unable to register platform device\n");
9825 thinkpad_acpi_module_exit();
9826 return ret;
9827 }
9828 tpacpi_sensors_pdev = platform_device_register_simple(
9829 TPACPI_HWMON_DRVR_NAME,
9830 -1, NULL, 0);
9831 if (IS_ERR(tpacpi_sensors_pdev)) {
9832 ret = PTR_ERR(tpacpi_sensors_pdev);
9833 tpacpi_sensors_pdev = NULL;
9834 pr_err("unable to register hwmon platform device\n");
9835 thinkpad_acpi_module_exit();
9836 return ret;
9837 }
9838 ret = device_create_file(&tpacpi_sensors_pdev->dev, &dev_attr_name);
9839 if (ret) {
9840 pr_err("unable to create sysfs hwmon device attributes\n");
9841 thinkpad_acpi_module_exit();
9842 return ret;
9843 }
9844 tp_features.sensors_pdev_attrs_registered = 1;
9845 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
9846 if (IS_ERR(tpacpi_hwmon)) {
9847 ret = PTR_ERR(tpacpi_hwmon);
9848 tpacpi_hwmon = NULL;
9849 pr_err("unable to register hwmon device\n");
9850 thinkpad_acpi_module_exit();
9851 return ret;
9852 }
9853 mutex_init(&tpacpi_inputdev_send_mutex);
9854 tpacpi_inputdev = input_allocate_device();
9855 if (!tpacpi_inputdev) {
9856 thinkpad_acpi_module_exit();
9857 return -ENOMEM;
9858 } else {
9859 /* Prepare input device, but don't register */
9860 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
9861 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
9862 tpacpi_inputdev->id.bustype = BUS_HOST;
9863 tpacpi_inputdev->id.vendor = thinkpad_id.vendor;
9864 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
9865 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
9866 tpacpi_inputdev->dev.parent = &tpacpi_pdev->dev;
9867 }
9868
9869 /* Init subdriver dependencies */
9870 tpacpi_detect_brightness_capabilities();
9871
9872 /* Init subdrivers */
9873 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
9874 ret = ibm_init(&ibms_init[i]);
9875 if (ret >= 0 && *ibms_init[i].param)
9876 ret = ibms_init[i].data->write(ibms_init[i].param);
9877 if (ret < 0) {
9878 thinkpad_acpi_module_exit();
9879 return ret;
9880 }
9881 }
9882
9883 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
9884
9885 ret = input_register_device(tpacpi_inputdev);
9886 if (ret < 0) {
9887 pr_err("unable to register input device\n");
9888 thinkpad_acpi_module_exit();
9889 return ret;
9890 } else {
9891 tp_features.input_device_registered = 1;
9892 }
9893
9894 return 0;
9895 }
9896
9897 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
9898
9899 /*
9900 * This will autoload the driver in almost every ThinkPad
9901 * in widespread use.
9902 *
9903 * Only _VERY_ old models, like the 240, 240x and 570 lack
9904 * the HKEY event interface.
9905 */
9906 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
9907
9908 /*
9909 * DMI matching for module autoloading
9910 *
9911 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
9912 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
9913 *
9914 * Only models listed in thinkwiki will be supported, so add yours
9915 * if it is not there yet.
9916 */
9917 #define IBM_BIOS_MODULE_ALIAS(__type) \
9918 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
9919
9920 /* Ancient thinkpad BIOSes have to be identified by
9921 * BIOS type or model number, and there are far less
9922 * BIOS types than model numbers... */
9923 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
9924
9925 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
9926 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
9927 MODULE_DESCRIPTION(TPACPI_DESC);
9928 MODULE_VERSION(TPACPI_VERSION);
9929 MODULE_LICENSE("GPL");
9930
9931 module_init(thinkpad_acpi_module_init);
9932 module_exit(thinkpad_acpi_module_exit);