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