2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
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.
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.
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
24 #define TPACPI_VERSION "0.23"
25 #define TPACPI_SYSFS_VERSION 0x020400
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
170 #define TPACPI_LOG TPACPI_FILE ": "
171 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
172 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
173 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
174 #define TPACPI_ERR KERN_ERR TPACPI_LOG
175 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
176 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
177 #define TPACPI_INFO KERN_INFO TPACPI_LOG
178 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
180 /* Debugging printk groups */
181 #define TPACPI_DBG_ALL 0xffff
182 #define TPACPI_DBG_DISCLOSETASK 0x8000
183 #define TPACPI_DBG_INIT 0x0001
184 #define TPACPI_DBG_EXIT 0x0002
185 #define TPACPI_DBG_RFKILL 0x0004
186 #define TPACPI_DBG_HKEY 0x0008
187 #define TPACPI_DBG_FAN 0x0010
188 #define TPACPI_DBG_BRGHT 0x0020
190 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
191 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
192 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
195 /****************************************************************************
196 * Driver-wide structs and misc. variables
201 struct tp_acpi_drv_struct {
202 const struct acpi_device_id *hid;
203 struct acpi_driver *driver;
205 void (*notify) (struct ibm_struct *, u32);
208 struct acpi_device *device;
214 int (*read) (char *);
215 int (*write) (char *);
217 void (*resume) (void);
218 void (*suspend) (pm_message_t state);
219 void (*shutdown) (void);
221 struct list_head all_drivers;
223 struct tp_acpi_drv_struct *acpi;
226 u8 acpi_driver_registered:1;
227 u8 acpi_notify_installed:1;
234 struct ibm_init_struct {
237 int (*init) (struct ibm_init_struct *);
238 struct ibm_struct *data;
249 u32 bright_16levels:1;
250 u32 bright_acpimode:1;
253 u32 fan_ctrl_status_undef:1;
255 u32 beep_needs_two_args:1;
256 u32 input_device_registered:1;
257 u32 platform_drv_registered:1;
258 u32 platform_drv_attrs_registered:1;
259 u32 sensors_pdrv_registered:1;
260 u32 sensors_pdrv_attrs_registered:1;
261 u32 sensors_pdev_attrs_registered:1;
262 u32 hotkey_poll_active:1;
266 u16 hotkey_mask_ff:1;
269 struct thinkpad_id_data {
270 unsigned int vendor; /* ThinkPad vendor:
271 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
273 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
274 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
276 u16 bios_model; /* 1Y = 0x5931, 0 = unknown */
278 u16 bios_release; /* 1ZETK1WW = 0x314b, 0 = unknown */
281 char *model_str; /* ThinkPad T43 */
282 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
284 static struct thinkpad_id_data thinkpad_id;
287 TPACPI_LIFE_INIT = 0,
292 static int experimental;
293 static u32 dbg_level;
295 static struct workqueue_struct *tpacpi_wq;
303 /* Special LED class that can defer work */
304 struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
307 enum led_status_t new_state;
311 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312 static int dbg_wlswemul;
313 static int tpacpi_wlsw_emulstate;
314 static int dbg_bluetoothemul;
315 static int tpacpi_bluetooth_emulstate;
316 static int dbg_wwanemul;
317 static int tpacpi_wwan_emulstate;
318 static int dbg_uwbemul;
319 static int tpacpi_uwb_emulstate;
323 /*************************************************************************
327 #define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
332 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
333 #define vdbg_printk dbg_printk
334 static const char *str_supported(int is_supported);
336 #define vdbg_printk(a_dbg_level, format, arg...) \
340 static void tpacpi_log_usertask(const char * const what)
342 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
343 what, task_tgid_vnr(current));
346 #define tpacpi_disclose_usertask(what, format, arg...) \
349 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
350 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
351 printk(TPACPI_DEBUG "%s: PID %d: " format, \
352 what, task_tgid_vnr(current), ## arg); \
357 * Quirk handling helpers
359 * ThinkPad IDs and versions seen in the field so far
360 * are two-characters from the set [0-9A-Z], i.e. base 36.
362 * We use values well outside that range as specials.
365 #define TPACPI_MATCH_ANY 0xffffU
366 #define TPACPI_MATCH_UNKNOWN 0U
368 /* TPID('1', 'Y') == 0x5931 */
369 #define TPID(__c1, __c2) (((__c2) << 8) | (__c1))
371 #define TPACPI_Q_IBM(__id1, __id2, __quirk) \
372 { .vendor = PCI_VENDOR_ID_IBM, \
373 .bios = TPID(__id1, __id2), \
374 .ec = TPACPI_MATCH_ANY, \
375 .quirks = (__quirk) }
377 #define TPACPI_Q_LNV(__id1, __id2, __quirk) \
378 { .vendor = PCI_VENDOR_ID_LENOVO, \
379 .bios = TPID(__id1, __id2), \
380 .ec = TPACPI_MATCH_ANY, \
381 .quirks = (__quirk) }
383 struct tpacpi_quirk {
387 unsigned long quirks;
391 * tpacpi_check_quirks() - search BIOS/EC version on a list
392 * @qlist: array of &struct tpacpi_quirk
393 * @qlist_size: number of elements in @qlist
395 * Iterates over a quirks list until one is found that matches the
396 * ThinkPad's vendor, BIOS and EC model.
398 * Returns 0 if nothing matches, otherwise returns the quirks field of
399 * the matching &struct tpacpi_quirk entry.
401 * The match criteria is: vendor, ec and bios much match.
403 static unsigned long __init tpacpi_check_quirks(
404 const struct tpacpi_quirk *qlist,
405 unsigned int qlist_size)
408 if ((qlist->vendor == thinkpad_id.vendor ||
409 qlist->vendor == TPACPI_MATCH_ANY) &&
410 (qlist->bios == thinkpad_id.bios_model ||
411 qlist->bios == TPACPI_MATCH_ANY) &&
412 (qlist->ec == thinkpad_id.ec_model ||
413 qlist->ec == TPACPI_MATCH_ANY))
414 return qlist->quirks;
423 /****************************************************************************
424 ****************************************************************************
426 * ACPI Helpers and device model
428 ****************************************************************************
429 ****************************************************************************/
431 /*************************************************************************
435 static acpi_handle root_handle;
437 #define TPACPI_HANDLE(object, parent, paths...) \
438 static acpi_handle object##_handle; \
439 static acpi_handle *object##_parent = &parent##_handle; \
440 static char *object##_path; \
441 static char *object##_paths[] = { paths }
443 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
444 "\\_SB.PCI.ISA.EC", /* 570 */
445 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
446 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
447 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
448 "\\_SB.PCI0.ICH3.EC0", /* R31 */
449 "\\_SB.PCI0.LPC.EC", /* all others */
452 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
453 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
455 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
457 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
458 "\\CMS", /* R40, R40e */
461 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
462 "^HKEY", /* R30, R31 */
463 "HKEY", /* all others */
466 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
467 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
468 "\\_SB.PCI0.VID0", /* 770e */
469 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
470 "\\_SB.PCI0.AGP.VID", /* all others */
474 /*************************************************************************
478 static int acpi_evalf(acpi_handle handle,
479 void *res, char *method, char *fmt, ...)
482 struct acpi_object_list params;
483 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
484 struct acpi_buffer result, *resultp;
485 union acpi_object out_obj;
493 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
506 params.pointer = &in_objs[0];
513 in_objs[params.count].integer.value = va_arg(ap, int);
514 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
516 /* add more types as needed */
518 printk(TPACPI_ERR "acpi_evalf() called "
519 "with invalid format character '%c'\n", c);
525 if (res_type != 'v') {
526 result.length = sizeof(out_obj);
527 result.pointer = &out_obj;
532 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
537 *(int *)res = out_obj.integer.value;
538 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
541 success = status == AE_OK;
543 /* add more types as needed */
545 printk(TPACPI_ERR "acpi_evalf() called "
546 "with invalid format character '%c'\n", res_type);
550 if (!success && !quiet)
551 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
552 method, fmt0, status);
557 static int acpi_ec_read(int i, u8 *p)
562 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
566 if (ec_read(i, p) < 0)
573 static int acpi_ec_write(int i, u8 v)
576 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
579 if (ec_write(i, v) < 0)
586 static int issue_thinkpad_cmos_command(int cmos_cmd)
591 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
597 /*************************************************************************
601 #define TPACPI_ACPIHANDLE_INIT(object) \
602 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
603 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
605 static void drv_acpi_handle_init(char *name,
606 acpi_handle *handle, acpi_handle parent,
607 char **paths, int num_paths, char **path)
612 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
615 for (i = 0; i < num_paths; i++) {
616 status = acpi_get_handle(parent, paths[i], handle);
617 if (ACPI_SUCCESS(status)) {
619 dbg_printk(TPACPI_DBG_INIT,
620 "Found ACPI handle %s for %s\n",
626 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
631 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
633 struct ibm_struct *ibm = data;
635 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
638 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
641 ibm->acpi->notify(ibm, event);
644 static int __init setup_acpi_notify(struct ibm_struct *ibm)
651 if (!*ibm->acpi->handle)
654 vdbg_printk(TPACPI_DBG_INIT,
655 "setting up ACPI notify for %s\n", ibm->name);
657 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
659 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
664 ibm->acpi->device->driver_data = ibm;
665 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
666 TPACPI_ACPI_EVENT_PREFIX,
669 status = acpi_install_notify_handler(*ibm->acpi->handle,
670 ibm->acpi->type, dispatch_acpi_notify, ibm);
671 if (ACPI_FAILURE(status)) {
672 if (status == AE_ALREADY_EXISTS) {
674 "another device driver is already "
675 "handling %s events\n", ibm->name);
678 "acpi_install_notify_handler(%s) failed: %d\n",
683 ibm->flags.acpi_notify_installed = 1;
687 static int __init tpacpi_device_add(struct acpi_device *device)
692 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
696 dbg_printk(TPACPI_DBG_INIT,
697 "registering %s as an ACPI driver\n", ibm->name);
701 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
702 if (!ibm->acpi->driver) {
704 "failed to allocate memory for ibm->acpi->driver\n");
708 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
709 ibm->acpi->driver->ids = ibm->acpi->hid;
711 ibm->acpi->driver->ops.add = &tpacpi_device_add;
713 rc = acpi_bus_register_driver(ibm->acpi->driver);
715 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
717 kfree(ibm->acpi->driver);
718 ibm->acpi->driver = NULL;
720 ibm->flags.acpi_driver_registered = 1;
726 /****************************************************************************
727 ****************************************************************************
731 ****************************************************************************
732 ****************************************************************************/
734 static int dispatch_procfs_read(char *page, char **start, off_t off,
735 int count, int *eof, void *data)
737 struct ibm_struct *ibm = data;
740 if (!ibm || !ibm->read)
743 len = ibm->read(page);
747 if (len <= off + count)
759 static int dispatch_procfs_write(struct file *file,
760 const char __user *userbuf,
761 unsigned long count, void *data)
763 struct ibm_struct *ibm = data;
767 if (!ibm || !ibm->write)
769 if (count > PAGE_SIZE - 2)
772 kernbuf = kmalloc(count + 2, GFP_KERNEL);
776 if (copy_from_user(kernbuf, userbuf, count)) {
782 strcat(kernbuf, ",");
783 ret = ibm->write(kernbuf);
792 static char *next_cmd(char **cmds)
797 while ((end = strchr(start, ',')) && end == start)
809 /****************************************************************************
810 ****************************************************************************
812 * Device model: input, hwmon and platform
814 ****************************************************************************
815 ****************************************************************************/
817 static struct platform_device *tpacpi_pdev;
818 static struct platform_device *tpacpi_sensors_pdev;
819 static struct device *tpacpi_hwmon;
820 static struct input_dev *tpacpi_inputdev;
821 static struct mutex tpacpi_inputdev_send_mutex;
822 static LIST_HEAD(tpacpi_all_drivers);
824 static int tpacpi_suspend_handler(struct platform_device *pdev,
827 struct ibm_struct *ibm, *itmp;
829 list_for_each_entry_safe(ibm, itmp,
833 (ibm->suspend)(state);
839 static int tpacpi_resume_handler(struct platform_device *pdev)
841 struct ibm_struct *ibm, *itmp;
843 list_for_each_entry_safe(ibm, itmp,
853 static void tpacpi_shutdown_handler(struct platform_device *pdev)
855 struct ibm_struct *ibm, *itmp;
857 list_for_each_entry_safe(ibm, itmp,
865 static struct platform_driver tpacpi_pdriver = {
867 .name = TPACPI_DRVR_NAME,
868 .owner = THIS_MODULE,
870 .suspend = tpacpi_suspend_handler,
871 .resume = tpacpi_resume_handler,
872 .shutdown = tpacpi_shutdown_handler,
875 static struct platform_driver tpacpi_hwmon_pdriver = {
877 .name = TPACPI_HWMON_DRVR_NAME,
878 .owner = THIS_MODULE,
882 /*************************************************************************
883 * sysfs support helpers
886 struct attribute_set {
887 unsigned int members, max_members;
888 struct attribute_group group;
891 struct attribute_set_obj {
892 struct attribute_set s;
894 } __attribute__((packed));
896 static struct attribute_set *create_attr_set(unsigned int max_members,
899 struct attribute_set_obj *sobj;
901 if (max_members == 0)
904 /* Allocates space for implicit NULL at the end too */
905 sobj = kzalloc(sizeof(struct attribute_set_obj) +
906 max_members * sizeof(struct attribute *),
910 sobj->s.max_members = max_members;
911 sobj->s.group.attrs = &sobj->a;
912 sobj->s.group.name = name;
917 #define destroy_attr_set(_set) \
920 /* not multi-threaded safe, use it in a single thread per set */
921 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
926 if (s->members >= s->max_members)
929 s->group.attrs[s->members] = attr;
935 static int add_many_to_attr_set(struct attribute_set *s,
936 struct attribute **attr,
941 for (i = 0; i < count; i++) {
942 res = add_to_attr_set(s, attr[i]);
950 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
952 sysfs_remove_group(kobj, &s->group);
956 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
957 sysfs_create_group(_kobj, &_attr_set->group)
959 static int parse_strtoul(const char *buf,
960 unsigned long max, unsigned long *value)
964 while (*buf && isspace(*buf))
966 *value = simple_strtoul(buf, &endp, 0);
967 while (*endp && isspace(*endp))
969 if (*endp || *value > max)
975 static void tpacpi_disable_brightness_delay(void)
977 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
979 "ACPI backlight control delay disabled\n");
982 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
984 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
985 union acpi_object *obj;
988 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
989 obj = (union acpi_object *)buffer.pointer;
990 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
991 printk(TPACPI_ERR "Unknown _BCL data, "
992 "please report this to %s\n", TPACPI_MAIL);
995 rc = obj->package.count;
1001 kfree(buffer.pointer);
1005 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
1006 u32 lvl, void *context, void **rv)
1008 char name[ACPI_PATH_SEGMENT_LENGTH];
1009 struct acpi_buffer buffer = { sizeof(name), &name };
1011 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
1012 !strncmp("_BCL", name, sizeof(name) - 1)) {
1013 BUG_ON(!rv || !*rv);
1014 **(int **)rv = tpacpi_query_bcl_levels(handle);
1015 return AE_CTRL_TERMINATE;
1022 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
1024 static int __init tpacpi_check_std_acpi_brightness_support(void)
1028 void *bcl_ptr = &bcl_levels;
1031 TPACPI_ACPIHANDLE_INIT(vid);
1037 * Search for a _BCL method, and execute it. This is safe on all
1038 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
1039 * BIOS in ACPI backlight control mode. We do NOT have to care
1040 * about calling the _BCL method in an enabled video device, any
1041 * will do for our purposes.
1044 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
1045 tpacpi_acpi_walk_find_bcl, NULL,
1048 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1049 tp_features.bright_acpimode = 1;
1050 return (bcl_levels - 2);
1056 static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1065 /*************************************************************************
1066 * rfkill and radio control support helpers
1070 * ThinkPad-ACPI firmware handling model:
1072 * WLSW (master wireless switch) is event-driven, and is common to all
1073 * firmware-controlled radios. It cannot be controlled, just monitored,
1074 * as expected. It overrides all radio state in firmware
1076 * The kernel, a masked-off hotkey, and WLSW can change the radio state
1077 * (TODO: verify how WLSW interacts with the returned radio state).
1079 * The only time there are shadow radio state changes, is when
1080 * masked-off hotkeys are used.
1084 * Internal driver API for radio state:
1086 * int: < 0 = error, otherwise enum tpacpi_rfkill_state
1087 * bool: true means radio blocked (off)
1089 enum tpacpi_rfkill_state {
1090 TPACPI_RFK_RADIO_OFF = 0,
1094 /* rfkill switches */
1095 enum tpacpi_rfk_id {
1096 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
1097 TPACPI_RFK_WWAN_SW_ID,
1098 TPACPI_RFK_UWB_SW_ID,
1102 static const char *tpacpi_rfkill_names[] = {
1103 [TPACPI_RFK_BLUETOOTH_SW_ID] = "bluetooth",
1104 [TPACPI_RFK_WWAN_SW_ID] = "wwan",
1105 [TPACPI_RFK_UWB_SW_ID] = "uwb",
1106 [TPACPI_RFK_SW_MAX] = NULL
1109 /* ThinkPad-ACPI rfkill subdriver */
1111 struct rfkill *rfkill;
1112 enum tpacpi_rfk_id id;
1113 const struct tpacpi_rfk_ops *ops;
1116 struct tpacpi_rfk_ops {
1117 /* firmware interface */
1118 int (*get_status)(void);
1119 int (*set_status)(const enum tpacpi_rfkill_state);
1122 static struct tpacpi_rfk *tpacpi_rfkill_switches[TPACPI_RFK_SW_MAX];
1124 /* Query FW and update rfkill sw state for a given rfkill switch */
1125 static int tpacpi_rfk_update_swstate(const struct tpacpi_rfk *tp_rfk)
1132 status = (tp_rfk->ops->get_status)();
1136 rfkill_set_sw_state(tp_rfk->rfkill,
1137 (status == TPACPI_RFK_RADIO_OFF));
1142 /* Query FW and update rfkill sw state for all rfkill switches */
1143 static void tpacpi_rfk_update_swstate_all(void)
1147 for (i = 0; i < TPACPI_RFK_SW_MAX; i++)
1148 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[i]);
1152 * Sync the HW-blocking state of all rfkill switches,
1153 * do notice it causes the rfkill core to schedule uevents
1155 static void tpacpi_rfk_update_hwblock_state(bool blocked)
1158 struct tpacpi_rfk *tp_rfk;
1160 for (i = 0; i < TPACPI_RFK_SW_MAX; i++) {
1161 tp_rfk = tpacpi_rfkill_switches[i];
1163 if (rfkill_set_hw_state(tp_rfk->rfkill,
1165 /* ignore -- we track sw block */
1171 /* Call to get the WLSW state from the firmware */
1172 static int hotkey_get_wlsw(void);
1174 /* Call to query WLSW state and update all rfkill switches */
1175 static bool tpacpi_rfk_check_hwblock_state(void)
1177 int res = hotkey_get_wlsw();
1180 /* When unknown or unsupported, we have to assume it is unblocked */
1184 hw_blocked = (res == TPACPI_RFK_RADIO_OFF);
1185 tpacpi_rfk_update_hwblock_state(hw_blocked);
1190 static int tpacpi_rfk_hook_set_block(void *data, bool blocked)
1192 struct tpacpi_rfk *tp_rfk = data;
1195 dbg_printk(TPACPI_DBG_RFKILL,
1196 "request to change radio state to %s\n",
1197 blocked ? "blocked" : "unblocked");
1199 /* try to set radio state */
1200 res = (tp_rfk->ops->set_status)(blocked ?
1201 TPACPI_RFK_RADIO_OFF : TPACPI_RFK_RADIO_ON);
1203 /* and update the rfkill core with whatever the FW really did */
1204 tpacpi_rfk_update_swstate(tp_rfk);
1206 return (res < 0) ? res : 0;
1209 static const struct rfkill_ops tpacpi_rfk_rfkill_ops = {
1210 .set_block = tpacpi_rfk_hook_set_block,
1213 static int __init tpacpi_new_rfkill(const enum tpacpi_rfk_id id,
1214 const struct tpacpi_rfk_ops *tp_rfkops,
1215 const enum rfkill_type rfktype,
1217 const bool set_default)
1219 struct tpacpi_rfk *atp_rfk;
1221 bool sw_state = false;
1224 BUG_ON(id >= TPACPI_RFK_SW_MAX || tpacpi_rfkill_switches[id]);
1226 atp_rfk = kzalloc(sizeof(struct tpacpi_rfk), GFP_KERNEL);
1228 atp_rfk->rfkill = rfkill_alloc(name,
1231 &tpacpi_rfk_rfkill_ops,
1233 if (!atp_rfk || !atp_rfk->rfkill) {
1235 "failed to allocate memory for rfkill class\n");
1241 atp_rfk->ops = tp_rfkops;
1243 sw_status = (tp_rfkops->get_status)();
1244 if (sw_status < 0) {
1246 "failed to read initial state for %s, error %d\n",
1249 sw_state = (sw_status == TPACPI_RFK_RADIO_OFF);
1251 /* try to keep the initial state, since we ask the
1252 * firmware to preserve it across S5 in NVRAM */
1253 rfkill_init_sw_state(atp_rfk->rfkill, sw_state);
1256 rfkill_set_hw_state(atp_rfk->rfkill, tpacpi_rfk_check_hwblock_state());
1258 res = rfkill_register(atp_rfk->rfkill);
1261 "failed to register %s rfkill switch: %d\n",
1263 rfkill_destroy(atp_rfk->rfkill);
1268 tpacpi_rfkill_switches[id] = atp_rfk;
1272 static void tpacpi_destroy_rfkill(const enum tpacpi_rfk_id id)
1274 struct tpacpi_rfk *tp_rfk;
1276 BUG_ON(id >= TPACPI_RFK_SW_MAX);
1278 tp_rfk = tpacpi_rfkill_switches[id];
1280 rfkill_unregister(tp_rfk->rfkill);
1281 tpacpi_rfkill_switches[id] = NULL;
1286 static void printk_deprecated_rfkill_attribute(const char * const what)
1288 printk_deprecated_attribute(what,
1289 "Please switch to generic rfkill before year 2010");
1292 /* sysfs <radio> enable ------------------------------------------------ */
1293 static ssize_t tpacpi_rfk_sysfs_enable_show(const enum tpacpi_rfk_id id,
1294 struct device_attribute *attr,
1299 printk_deprecated_rfkill_attribute(attr->attr.name);
1301 /* This is in the ABI... */
1302 if (tpacpi_rfk_check_hwblock_state()) {
1303 status = TPACPI_RFK_RADIO_OFF;
1305 status = tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1310 return snprintf(buf, PAGE_SIZE, "%d\n",
1311 (status == TPACPI_RFK_RADIO_ON) ? 1 : 0);
1314 static ssize_t tpacpi_rfk_sysfs_enable_store(const enum tpacpi_rfk_id id,
1315 struct device_attribute *attr,
1316 const char *buf, size_t count)
1321 printk_deprecated_rfkill_attribute(attr->attr.name);
1323 if (parse_strtoul(buf, 1, &t))
1326 tpacpi_disclose_usertask(attr->attr.name, "set to %ld\n", t);
1328 /* This is in the ABI... */
1329 if (tpacpi_rfk_check_hwblock_state() && !!t)
1332 res = tpacpi_rfkill_switches[id]->ops->set_status((!!t) ?
1333 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF);
1334 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1336 return (res < 0) ? res : count;
1339 /* procfs -------------------------------------------------------------- */
1340 static int tpacpi_rfk_procfs_read(const enum tpacpi_rfk_id id, char *p)
1344 if (id >= TPACPI_RFK_SW_MAX)
1345 len += sprintf(p + len, "status:\t\tnot supported\n");
1349 /* This is in the ABI... */
1350 if (tpacpi_rfk_check_hwblock_state()) {
1351 status = TPACPI_RFK_RADIO_OFF;
1353 status = tpacpi_rfk_update_swstate(
1354 tpacpi_rfkill_switches[id]);
1359 len += sprintf(p + len, "status:\t\t%s\n",
1360 (status == TPACPI_RFK_RADIO_ON) ?
1361 "enabled" : "disabled");
1362 len += sprintf(p + len, "commands:\tenable, disable\n");
1368 static int tpacpi_rfk_procfs_write(const enum tpacpi_rfk_id id, char *buf)
1374 if (id >= TPACPI_RFK_SW_MAX)
1377 while ((cmd = next_cmd(&buf))) {
1378 if (strlencmp(cmd, "enable") == 0)
1379 status = TPACPI_RFK_RADIO_ON;
1380 else if (strlencmp(cmd, "disable") == 0)
1381 status = TPACPI_RFK_RADIO_OFF;
1387 tpacpi_disclose_usertask("procfs", "attempt to %s %s\n",
1388 (status == TPACPI_RFK_RADIO_ON) ?
1389 "enable" : "disable",
1390 tpacpi_rfkill_names[id]);
1391 res = (tpacpi_rfkill_switches[id]->ops->set_status)(status);
1392 tpacpi_rfk_update_swstate(tpacpi_rfkill_switches[id]);
1398 /*************************************************************************
1399 * thinkpad-acpi driver attributes
1402 /* interface_version --------------------------------------------------- */
1403 static ssize_t tpacpi_driver_interface_version_show(
1404 struct device_driver *drv,
1407 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1410 static DRIVER_ATTR(interface_version, S_IRUGO,
1411 tpacpi_driver_interface_version_show, NULL);
1413 /* debug_level --------------------------------------------------------- */
1414 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1417 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1420 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1421 const char *buf, size_t count)
1425 if (parse_strtoul(buf, 0xffff, &t))
1433 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1434 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1436 /* version ------------------------------------------------------------- */
1437 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1440 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1441 TPACPI_DESC, TPACPI_VERSION);
1444 static DRIVER_ATTR(version, S_IRUGO,
1445 tpacpi_driver_version_show, NULL);
1447 /* --------------------------------------------------------------------- */
1449 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1451 /* wlsw_emulstate ------------------------------------------------------ */
1452 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1455 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1458 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1459 const char *buf, size_t count)
1463 if (parse_strtoul(buf, 1, &t))
1466 if (tpacpi_wlsw_emulstate != !!t) {
1467 tpacpi_wlsw_emulstate = !!t;
1468 tpacpi_rfk_update_hwblock_state(!t); /* negative logic */
1474 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1475 tpacpi_driver_wlsw_emulstate_show,
1476 tpacpi_driver_wlsw_emulstate_store);
1478 /* bluetooth_emulstate ------------------------------------------------- */
1479 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1480 struct device_driver *drv,
1483 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1486 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1487 struct device_driver *drv,
1488 const char *buf, size_t count)
1492 if (parse_strtoul(buf, 1, &t))
1495 tpacpi_bluetooth_emulstate = !!t;
1500 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1501 tpacpi_driver_bluetooth_emulstate_show,
1502 tpacpi_driver_bluetooth_emulstate_store);
1504 /* wwan_emulstate ------------------------------------------------- */
1505 static ssize_t tpacpi_driver_wwan_emulstate_show(
1506 struct device_driver *drv,
1509 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1512 static ssize_t tpacpi_driver_wwan_emulstate_store(
1513 struct device_driver *drv,
1514 const char *buf, size_t count)
1518 if (parse_strtoul(buf, 1, &t))
1521 tpacpi_wwan_emulstate = !!t;
1526 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1527 tpacpi_driver_wwan_emulstate_show,
1528 tpacpi_driver_wwan_emulstate_store);
1530 /* uwb_emulstate ------------------------------------------------- */
1531 static ssize_t tpacpi_driver_uwb_emulstate_show(
1532 struct device_driver *drv,
1535 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1538 static ssize_t tpacpi_driver_uwb_emulstate_store(
1539 struct device_driver *drv,
1540 const char *buf, size_t count)
1544 if (parse_strtoul(buf, 1, &t))
1547 tpacpi_uwb_emulstate = !!t;
1552 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1553 tpacpi_driver_uwb_emulstate_show,
1554 tpacpi_driver_uwb_emulstate_store);
1557 /* --------------------------------------------------------------------- */
1559 static struct driver_attribute *tpacpi_driver_attributes[] = {
1560 &driver_attr_debug_level, &driver_attr_version,
1561 &driver_attr_interface_version,
1564 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1570 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1571 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1575 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1576 if (!res && dbg_wlswemul)
1577 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1578 if (!res && dbg_bluetoothemul)
1579 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1580 if (!res && dbg_wwanemul)
1581 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1582 if (!res && dbg_uwbemul)
1583 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1589 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1593 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1594 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1596 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1597 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1598 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1599 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1600 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1604 /****************************************************************************
1605 ****************************************************************************
1609 ****************************************************************************
1610 ****************************************************************************/
1612 /*************************************************************************
1613 * thinkpad-acpi init subdriver
1616 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1618 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1619 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1621 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1622 (thinkpad_id.bios_version_str) ?
1623 thinkpad_id.bios_version_str : "unknown",
1624 (thinkpad_id.ec_version_str) ?
1625 thinkpad_id.ec_version_str : "unknown");
1627 if (thinkpad_id.vendor && thinkpad_id.model_str)
1628 printk(TPACPI_INFO "%s %s, model %s\n",
1629 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1630 "IBM" : ((thinkpad_id.vendor ==
1631 PCI_VENDOR_ID_LENOVO) ?
1632 "Lenovo" : "Unknown vendor"),
1633 thinkpad_id.model_str,
1634 (thinkpad_id.nummodel_str) ?
1635 thinkpad_id.nummodel_str : "unknown");
1640 static int thinkpad_acpi_driver_read(char *p)
1644 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1645 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1650 static struct ibm_struct thinkpad_acpi_driver_data = {
1652 .read = thinkpad_acpi_driver_read,
1655 /*************************************************************************
1659 enum { /* hot key scan codes (derived from ACPI DSDT) */
1660 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1661 TP_ACPI_HOTKEYSCAN_FNF2,
1662 TP_ACPI_HOTKEYSCAN_FNF3,
1663 TP_ACPI_HOTKEYSCAN_FNF4,
1664 TP_ACPI_HOTKEYSCAN_FNF5,
1665 TP_ACPI_HOTKEYSCAN_FNF6,
1666 TP_ACPI_HOTKEYSCAN_FNF7,
1667 TP_ACPI_HOTKEYSCAN_FNF8,
1668 TP_ACPI_HOTKEYSCAN_FNF9,
1669 TP_ACPI_HOTKEYSCAN_FNF10,
1670 TP_ACPI_HOTKEYSCAN_FNF11,
1671 TP_ACPI_HOTKEYSCAN_FNF12,
1672 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1673 TP_ACPI_HOTKEYSCAN_FNINSERT,
1674 TP_ACPI_HOTKEYSCAN_FNDELETE,
1675 TP_ACPI_HOTKEYSCAN_FNHOME,
1676 TP_ACPI_HOTKEYSCAN_FNEND,
1677 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1678 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1679 TP_ACPI_HOTKEYSCAN_FNSPACE,
1680 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1681 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1682 TP_ACPI_HOTKEYSCAN_MUTE,
1683 TP_ACPI_HOTKEYSCAN_THINKPAD,
1686 enum { /* Keys available through NVRAM polling */
1687 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1688 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1691 enum { /* Positions of some of the keys in hotkey masks */
1692 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1693 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1694 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1695 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1696 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1697 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1698 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1699 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1700 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1701 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1702 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1705 enum { /* NVRAM to ACPI HKEY group map */
1706 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1707 TP_ACPI_HKEY_ZOOM_MASK |
1708 TP_ACPI_HKEY_DISPSWTCH_MASK |
1709 TP_ACPI_HKEY_HIBERNATE_MASK,
1710 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1711 TP_ACPI_HKEY_BRGHTDWN_MASK,
1712 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1713 TP_ACPI_HKEY_VOLDWN_MASK |
1714 TP_ACPI_HKEY_MUTE_MASK,
1717 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1718 struct tp_nvram_state {
1719 u16 thinkpad_toggle:1;
1721 u16 display_toggle:1;
1722 u16 thinklight_toggle:1;
1723 u16 hibernate_toggle:1;
1724 u16 displayexp_toggle:1;
1725 u16 display_state:1;
1726 u16 brightness_toggle:1;
1727 u16 volume_toggle:1;
1730 u8 brightness_level;
1734 static struct task_struct *tpacpi_hotkey_task;
1735 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1736 static int hotkey_poll_freq = 10; /* Hz */
1737 static struct mutex hotkey_thread_mutex;
1738 static struct mutex hotkey_thread_data_mutex;
1739 static unsigned int hotkey_config_change;
1741 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1743 #define hotkey_source_mask 0U
1745 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1747 static struct mutex hotkey_mutex;
1749 static enum { /* Reasons for waking up */
1750 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1751 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1752 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1753 } hotkey_wakeup_reason;
1755 static int hotkey_autosleep_ack;
1757 static u32 hotkey_orig_mask;
1758 static u32 hotkey_all_mask;
1759 static u32 hotkey_reserved_mask;
1760 static u32 hotkey_mask;
1762 static unsigned int hotkey_report_mode;
1764 static u16 *hotkey_keycode_map;
1766 static struct attribute_set *hotkey_dev_attributes;
1768 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1769 #define HOTKEY_CONFIG_CRITICAL_START \
1771 mutex_lock(&hotkey_thread_data_mutex); \
1772 hotkey_config_change++; \
1774 #define HOTKEY_CONFIG_CRITICAL_END \
1775 mutex_unlock(&hotkey_thread_data_mutex);
1777 #define HOTKEY_CONFIG_CRITICAL_START
1778 #define HOTKEY_CONFIG_CRITICAL_END
1779 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1781 /* HKEY.MHKG() return bits */
1782 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1784 static int hotkey_get_wlsw(void)
1788 if (!tp_features.hotkey_wlsw)
1791 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1793 return (tpacpi_wlsw_emulstate) ?
1794 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1797 if (!acpi_evalf(hkey_handle, &status, "WLSW", "d"))
1800 return (status) ? TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
1803 static int hotkey_get_tablet_mode(int *status)
1807 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1810 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1815 * Call with hotkey_mutex held
1817 static int hotkey_mask_get(void)
1821 if (tp_features.hotkey_mask) {
1822 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1825 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1831 * Call with hotkey_mutex held
1833 static int hotkey_mask_set(u32 mask)
1838 if (tp_features.hotkey_mask) {
1839 if (!tp_warned.hotkey_mask_ff &&
1840 (mask == 0xffff || mask == 0xffffff ||
1841 mask == 0xffffffff)) {
1842 tp_warned.hotkey_mask_ff = 1;
1843 printk(TPACPI_NOTICE
1844 "setting the hotkey mask to 0x%08x is likely "
1845 "not the best way to go about it\n", mask);
1846 printk(TPACPI_NOTICE
1847 "please consider using the driver defaults, "
1848 "and refer to up-to-date thinkpad-acpi "
1852 HOTKEY_CONFIG_CRITICAL_START
1853 for (i = 0; i < 32; i++) {
1855 /* enable in firmware mask only keys not in NVRAM
1856 * mode, but enable the key in the cached hotkey_mask
1857 * regardless of mode, or the key will end up
1858 * disabled by hotkey_mask_get() */
1859 if (!acpi_evalf(hkey_handle,
1860 NULL, "MHKM", "vdd", i + 1,
1861 !!((mask & ~hotkey_source_mask) & m))) {
1865 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1868 HOTKEY_CONFIG_CRITICAL_END
1870 /* hotkey_mask_get must be called unconditionally below */
1871 if (!hotkey_mask_get() && !rc &&
1872 (hotkey_mask & ~hotkey_source_mask) !=
1873 (mask & ~hotkey_source_mask)) {
1874 printk(TPACPI_NOTICE
1875 "requested hot key mask 0x%08x, but "
1876 "firmware forced it to 0x%08x\n",
1880 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1881 HOTKEY_CONFIG_CRITICAL_START
1882 hotkey_mask = mask & hotkey_source_mask;
1883 HOTKEY_CONFIG_CRITICAL_END
1885 if (hotkey_mask != mask) {
1886 printk(TPACPI_NOTICE
1887 "requested hot key mask 0x%08x, "
1888 "forced to 0x%08x (NVRAM poll mask is "
1889 "0x%08x): no firmware mask support\n",
1890 mask, hotkey_mask, hotkey_source_mask);
1895 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1901 static int hotkey_status_get(int *status)
1903 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1909 static int hotkey_status_set(bool enable)
1911 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1917 static void tpacpi_input_send_tabletsw(void)
1921 if (tp_features.hotkey_tablet &&
1922 !hotkey_get_tablet_mode(&state)) {
1923 mutex_lock(&tpacpi_inputdev_send_mutex);
1925 input_report_switch(tpacpi_inputdev,
1926 SW_TABLET_MODE, !!state);
1927 input_sync(tpacpi_inputdev);
1929 mutex_unlock(&tpacpi_inputdev_send_mutex);
1933 static void tpacpi_input_send_key(unsigned int scancode)
1935 unsigned int keycode;
1937 keycode = hotkey_keycode_map[scancode];
1939 if (keycode != KEY_RESERVED) {
1940 mutex_lock(&tpacpi_inputdev_send_mutex);
1942 input_report_key(tpacpi_inputdev, keycode, 1);
1943 if (keycode == KEY_UNKNOWN)
1944 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1946 input_sync(tpacpi_inputdev);
1948 input_report_key(tpacpi_inputdev, keycode, 0);
1949 if (keycode == KEY_UNKNOWN)
1950 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1952 input_sync(tpacpi_inputdev);
1954 mutex_unlock(&tpacpi_inputdev_send_mutex);
1958 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1959 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1961 static void tpacpi_hotkey_send_key(unsigned int scancode)
1963 tpacpi_input_send_key(scancode);
1964 if (hotkey_report_mode < 2) {
1965 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1966 0x80, 0x1001 + scancode);
1970 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1974 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1975 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1976 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1977 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1978 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1979 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1981 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1982 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1983 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1985 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1986 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1987 n->displayexp_toggle =
1988 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1990 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1991 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1992 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1993 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1994 n->brightness_toggle =
1995 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1997 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1998 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1999 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
2000 >> TP_NVRAM_POS_LEVEL_VOLUME;
2001 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
2002 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
2006 #define TPACPI_COMPARE_KEY(__scancode, __member) \
2008 if ((mask & (1 << __scancode)) && \
2009 oldn->__member != newn->__member) \
2010 tpacpi_hotkey_send_key(__scancode); \
2013 #define TPACPI_MAY_SEND_KEY(__scancode) \
2014 do { if (mask & (1 << __scancode)) \
2015 tpacpi_hotkey_send_key(__scancode); } while (0)
2017 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
2018 struct tp_nvram_state *newn,
2021 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
2022 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
2023 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
2024 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
2026 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
2028 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
2031 if (oldn->volume_toggle != newn->volume_toggle) {
2032 if (oldn->mute != newn->mute) {
2033 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2035 if (oldn->volume_level > newn->volume_level) {
2036 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2037 } else if (oldn->volume_level < newn->volume_level) {
2038 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2039 } else if (oldn->mute == newn->mute) {
2040 /* repeated key presses that didn't change state */
2042 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
2043 } else if (newn->volume_level != 0) {
2044 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
2046 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
2051 /* handle brightness */
2052 if (oldn->brightness_toggle != newn->brightness_toggle) {
2053 if (oldn->brightness_level < newn->brightness_level) {
2054 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2055 } else if (oldn->brightness_level > newn->brightness_level) {
2056 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2058 /* repeated key presses that didn't change state */
2059 if (newn->brightness_level != 0) {
2060 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
2062 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
2068 #undef TPACPI_COMPARE_KEY
2069 #undef TPACPI_MAY_SEND_KEY
2071 static int hotkey_kthread(void *data)
2073 struct tp_nvram_state s[2];
2075 unsigned int si, so;
2077 unsigned int change_detector, must_reset;
2079 mutex_lock(&hotkey_thread_mutex);
2081 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
2090 /* Initial state for compares */
2091 mutex_lock(&hotkey_thread_data_mutex);
2092 change_detector = hotkey_config_change;
2093 mask = hotkey_source_mask & hotkey_mask;
2094 mutex_unlock(&hotkey_thread_data_mutex);
2095 hotkey_read_nvram(&s[so], mask);
2097 while (!kthread_should_stop() && hotkey_poll_freq) {
2099 t = 1000/hotkey_poll_freq;
2100 t = msleep_interruptible(t);
2101 if (unlikely(kthread_should_stop()))
2103 must_reset = try_to_freeze();
2104 if (t > 0 && !must_reset)
2107 mutex_lock(&hotkey_thread_data_mutex);
2108 if (must_reset || hotkey_config_change != change_detector) {
2109 /* forget old state on thaw or config change */
2112 change_detector = hotkey_config_change;
2114 mask = hotkey_source_mask & hotkey_mask;
2115 mutex_unlock(&hotkey_thread_data_mutex);
2118 hotkey_read_nvram(&s[si], mask);
2119 if (likely(si != so)) {
2120 hotkey_compare_and_issue_event(&s[so], &s[si],
2130 mutex_unlock(&hotkey_thread_mutex);
2134 static void hotkey_poll_stop_sync(void)
2136 if (tpacpi_hotkey_task) {
2137 if (frozen(tpacpi_hotkey_task) ||
2138 freezing(tpacpi_hotkey_task))
2139 thaw_process(tpacpi_hotkey_task);
2141 kthread_stop(tpacpi_hotkey_task);
2142 tpacpi_hotkey_task = NULL;
2143 mutex_lock(&hotkey_thread_mutex);
2144 /* at this point, the thread did exit */
2145 mutex_unlock(&hotkey_thread_mutex);
2149 /* call with hotkey_mutex held */
2150 static void hotkey_poll_setup(int may_warn)
2152 if ((hotkey_source_mask & hotkey_mask) != 0 &&
2153 hotkey_poll_freq > 0 &&
2154 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
2155 if (!tpacpi_hotkey_task) {
2156 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
2157 NULL, TPACPI_NVRAM_KTHREAD_NAME);
2158 if (IS_ERR(tpacpi_hotkey_task)) {
2159 tpacpi_hotkey_task = NULL;
2161 "could not create kernel thread "
2162 "for hotkey polling\n");
2166 hotkey_poll_stop_sync();
2168 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
2169 printk(TPACPI_NOTICE
2170 "hot keys 0x%08x require polling, "
2171 "which is currently disabled\n",
2172 hotkey_source_mask);
2177 static void hotkey_poll_setup_safe(int may_warn)
2179 mutex_lock(&hotkey_mutex);
2180 hotkey_poll_setup(may_warn);
2181 mutex_unlock(&hotkey_mutex);
2184 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2186 static void hotkey_poll_setup_safe(int __unused)
2190 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2192 static int hotkey_inputdev_open(struct input_dev *dev)
2194 switch (tpacpi_lifecycle) {
2195 case TPACPI_LIFE_INIT:
2197 * hotkey_init will call hotkey_poll_setup_safe
2198 * at the appropriate moment
2201 case TPACPI_LIFE_EXITING:
2203 case TPACPI_LIFE_RUNNING:
2204 hotkey_poll_setup_safe(0);
2208 /* Should only happen if tpacpi_lifecycle is corrupt */
2213 static void hotkey_inputdev_close(struct input_dev *dev)
2215 /* disable hotkey polling when possible */
2216 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
2217 hotkey_poll_setup_safe(0);
2220 /* sysfs hotkey enable ------------------------------------------------- */
2221 static ssize_t hotkey_enable_show(struct device *dev,
2222 struct device_attribute *attr,
2227 printk_deprecated_attribute("hotkey_enable",
2228 "Hotkey reporting is always enabled");
2230 res = hotkey_status_get(&status);
2234 return snprintf(buf, PAGE_SIZE, "%d\n", status);
2237 static ssize_t hotkey_enable_store(struct device *dev,
2238 struct device_attribute *attr,
2239 const char *buf, size_t count)
2243 printk_deprecated_attribute("hotkey_enable",
2244 "Hotkeys can be disabled through hotkey_mask");
2246 if (parse_strtoul(buf, 1, &t))
2255 static struct device_attribute dev_attr_hotkey_enable =
2256 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
2257 hotkey_enable_show, hotkey_enable_store);
2259 /* sysfs hotkey mask --------------------------------------------------- */
2260 static ssize_t hotkey_mask_show(struct device *dev,
2261 struct device_attribute *attr,
2266 if (mutex_lock_killable(&hotkey_mutex))
2267 return -ERESTARTSYS;
2268 res = hotkey_mask_get();
2269 mutex_unlock(&hotkey_mutex);
2272 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
2275 static ssize_t hotkey_mask_store(struct device *dev,
2276 struct device_attribute *attr,
2277 const char *buf, size_t count)
2282 if (parse_strtoul(buf, 0xffffffffUL, &t))
2285 if (mutex_lock_killable(&hotkey_mutex))
2286 return -ERESTARTSYS;
2288 res = hotkey_mask_set(t);
2290 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2291 hotkey_poll_setup(1);
2294 mutex_unlock(&hotkey_mutex);
2296 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
2298 return (res) ? res : count;
2301 static struct device_attribute dev_attr_hotkey_mask =
2302 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
2303 hotkey_mask_show, hotkey_mask_store);
2305 /* sysfs hotkey bios_enabled ------------------------------------------- */
2306 static ssize_t hotkey_bios_enabled_show(struct device *dev,
2307 struct device_attribute *attr,
2310 return sprintf(buf, "0\n");
2313 static struct device_attribute dev_attr_hotkey_bios_enabled =
2314 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
2316 /* sysfs hotkey bios_mask ---------------------------------------------- */
2317 static ssize_t hotkey_bios_mask_show(struct device *dev,
2318 struct device_attribute *attr,
2321 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
2324 static struct device_attribute dev_attr_hotkey_bios_mask =
2325 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2327 /* sysfs hotkey all_mask ----------------------------------------------- */
2328 static ssize_t hotkey_all_mask_show(struct device *dev,
2329 struct device_attribute *attr,
2332 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2333 hotkey_all_mask | hotkey_source_mask);
2336 static struct device_attribute dev_attr_hotkey_all_mask =
2337 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2339 /* sysfs hotkey recommended_mask --------------------------------------- */
2340 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2341 struct device_attribute *attr,
2344 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2345 (hotkey_all_mask | hotkey_source_mask)
2346 & ~hotkey_reserved_mask);
2349 static struct device_attribute dev_attr_hotkey_recommended_mask =
2350 __ATTR(hotkey_recommended_mask, S_IRUGO,
2351 hotkey_recommended_mask_show, NULL);
2353 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2355 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2356 static ssize_t hotkey_source_mask_show(struct device *dev,
2357 struct device_attribute *attr,
2360 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2363 static ssize_t hotkey_source_mask_store(struct device *dev,
2364 struct device_attribute *attr,
2365 const char *buf, size_t count)
2369 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2370 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2373 if (mutex_lock_killable(&hotkey_mutex))
2374 return -ERESTARTSYS;
2376 HOTKEY_CONFIG_CRITICAL_START
2377 hotkey_source_mask = t;
2378 HOTKEY_CONFIG_CRITICAL_END
2380 hotkey_poll_setup(1);
2382 mutex_unlock(&hotkey_mutex);
2384 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2389 static struct device_attribute dev_attr_hotkey_source_mask =
2390 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2391 hotkey_source_mask_show, hotkey_source_mask_store);
2393 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2394 static ssize_t hotkey_poll_freq_show(struct device *dev,
2395 struct device_attribute *attr,
2398 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2401 static ssize_t hotkey_poll_freq_store(struct device *dev,
2402 struct device_attribute *attr,
2403 const char *buf, size_t count)
2407 if (parse_strtoul(buf, 25, &t))
2410 if (mutex_lock_killable(&hotkey_mutex))
2411 return -ERESTARTSYS;
2413 hotkey_poll_freq = t;
2415 hotkey_poll_setup(1);
2416 mutex_unlock(&hotkey_mutex);
2418 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2423 static struct device_attribute dev_attr_hotkey_poll_freq =
2424 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2425 hotkey_poll_freq_show, hotkey_poll_freq_store);
2427 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2429 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2430 static ssize_t hotkey_radio_sw_show(struct device *dev,
2431 struct device_attribute *attr,
2435 res = hotkey_get_wlsw();
2439 /* Opportunistic update */
2440 tpacpi_rfk_update_hwblock_state((res == TPACPI_RFK_RADIO_OFF));
2442 return snprintf(buf, PAGE_SIZE, "%d\n",
2443 (res == TPACPI_RFK_RADIO_OFF) ? 0 : 1);
2446 static struct device_attribute dev_attr_hotkey_radio_sw =
2447 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2449 static void hotkey_radio_sw_notify_change(void)
2451 if (tp_features.hotkey_wlsw)
2452 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2456 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2457 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2458 struct device_attribute *attr,
2462 res = hotkey_get_tablet_mode(&s);
2466 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2469 static struct device_attribute dev_attr_hotkey_tablet_mode =
2470 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2472 static void hotkey_tablet_mode_notify_change(void)
2474 if (tp_features.hotkey_tablet)
2475 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2476 "hotkey_tablet_mode");
2479 /* sysfs hotkey report_mode -------------------------------------------- */
2480 static ssize_t hotkey_report_mode_show(struct device *dev,
2481 struct device_attribute *attr,
2484 return snprintf(buf, PAGE_SIZE, "%d\n",
2485 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2488 static struct device_attribute dev_attr_hotkey_report_mode =
2489 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2491 /* sysfs wakeup reason (pollable) -------------------------------------- */
2492 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2493 struct device_attribute *attr,
2496 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2499 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2500 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2502 static void hotkey_wakeup_reason_notify_change(void)
2504 if (tp_features.hotkey_mask)
2505 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2509 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2510 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2511 struct device_attribute *attr,
2514 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2517 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2518 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2519 hotkey_wakeup_hotunplug_complete_show, NULL);
2521 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2523 if (tp_features.hotkey_mask)
2524 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2525 "wakeup_hotunplug_complete");
2528 /* --------------------------------------------------------------------- */
2530 static struct attribute *hotkey_attributes[] __initdata = {
2531 &dev_attr_hotkey_enable.attr,
2532 &dev_attr_hotkey_bios_enabled.attr,
2533 &dev_attr_hotkey_report_mode.attr,
2534 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2535 &dev_attr_hotkey_mask.attr,
2536 &dev_attr_hotkey_all_mask.attr,
2537 &dev_attr_hotkey_recommended_mask.attr,
2538 &dev_attr_hotkey_source_mask.attr,
2539 &dev_attr_hotkey_poll_freq.attr,
2543 static struct attribute *hotkey_mask_attributes[] __initdata = {
2544 &dev_attr_hotkey_bios_mask.attr,
2545 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2546 &dev_attr_hotkey_mask.attr,
2547 &dev_attr_hotkey_all_mask.attr,
2548 &dev_attr_hotkey_recommended_mask.attr,
2550 &dev_attr_hotkey_wakeup_reason.attr,
2551 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2555 * Sync both the hw and sw blocking state of all switches
2557 static void tpacpi_send_radiosw_update(void)
2562 * We must sync all rfkill controllers *before* issuing any
2563 * rfkill input events, or we will race the rfkill core input
2566 * tpacpi_inputdev_send_mutex works as a syncronization point
2569 * We optimize to avoid numerous calls to hotkey_get_wlsw.
2572 wlsw = hotkey_get_wlsw();
2574 /* Sync hw blocking state first if it is hw-blocked */
2575 if (wlsw == TPACPI_RFK_RADIO_OFF)
2576 tpacpi_rfk_update_hwblock_state(true);
2578 /* Sync sw blocking state */
2579 tpacpi_rfk_update_swstate_all();
2581 /* Sync hw blocking state last if it is hw-unblocked */
2582 if (wlsw == TPACPI_RFK_RADIO_ON)
2583 tpacpi_rfk_update_hwblock_state(false);
2585 /* Issue rfkill input event for WLSW switch */
2587 mutex_lock(&tpacpi_inputdev_send_mutex);
2589 input_report_switch(tpacpi_inputdev,
2590 SW_RFKILL_ALL, (wlsw > 0));
2591 input_sync(tpacpi_inputdev);
2593 mutex_unlock(&tpacpi_inputdev_send_mutex);
2597 * this can be unconditional, as we will poll state again
2598 * if userspace uses the notify to read data
2600 hotkey_radio_sw_notify_change();
2603 static void hotkey_exit(void)
2605 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2606 hotkey_poll_stop_sync();
2609 if (hotkey_dev_attributes)
2610 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2612 kfree(hotkey_keycode_map);
2614 if (tp_features.hotkey) {
2615 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2616 "restoring original hot key mask\n");
2617 /* no short-circuit boolean operator below! */
2618 if ((hotkey_mask_set(hotkey_orig_mask) |
2619 hotkey_status_set(false)) != 0)
2621 "failed to restore hot key mask "
2622 "to BIOS defaults\n");
2626 static int __init hotkey_init(struct ibm_init_struct *iibm)
2628 /* Requirements for changing the default keymaps:
2630 * 1. Many of the keys are mapped to KEY_RESERVED for very
2631 * good reasons. Do not change them unless you have deep
2632 * knowledge on the IBM and Lenovo ThinkPad firmware for
2633 * the various ThinkPad models. The driver behaves
2634 * differently for KEY_RESERVED: such keys have their
2635 * hot key mask *unset* in mask_recommended, and also
2636 * in the initial hot key mask programmed into the
2637 * firmware at driver load time, which means the firm-
2638 * ware may react very differently if you change them to
2641 * 2. You must be subscribed to the linux-thinkpad and
2642 * ibm-acpi-devel mailing lists, and you should read the
2643 * list archives since 2007 if you want to change the
2644 * keymaps. This requirement exists so that you will
2645 * know the past history of problems with the thinkpad-
2646 * acpi driver keymaps, and also that you will be
2647 * listening to any bug reports;
2649 * 3. Do not send thinkpad-acpi specific patches directly to
2650 * for merging, *ever*. Send them to the linux-acpi
2651 * mailinglist for comments. Merging is to be done only
2652 * through acpi-test and the ACPI maintainer.
2654 * If the above is too much to ask, don't change the keymap.
2655 * Ask the thinkpad-acpi maintainer to do it, instead.
2657 static u16 ibm_keycode_map[] __initdata = {
2658 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2659 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2660 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2661 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2663 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2664 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2665 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2666 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2668 /* brightness: firmware always reacts to them, unless
2669 * X.org did some tricks in the radeon BIOS scratch
2670 * registers of *some* models */
2671 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2672 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2674 /* Thinklight: firmware always react to it */
2675 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2677 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2678 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2680 /* Volume: firmware always react to it and reprograms
2681 * the built-in *extra* mixer. Never map it to control
2682 * another mixer by default. */
2683 KEY_RESERVED, /* 0x14: VOLUME UP */
2684 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2685 KEY_RESERVED, /* 0x16: MUTE */
2687 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2689 /* (assignments unknown, please report if found) */
2690 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2691 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2693 static u16 lenovo_keycode_map[] __initdata = {
2694 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2695 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2696 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2697 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2699 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2700 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2701 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2702 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2704 /* These either have to go through ACPI video, or
2705 * act like in the IBM ThinkPads, so don't ever
2706 * enable them by default */
2707 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2708 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2710 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2712 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2713 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2715 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2716 * react to it and reprograms the built-in *extra* mixer.
2717 * Never map it to control another mixer by default.
2719 * T60?, T61, R60?, R61: firmware and EC tries to send
2720 * these over the regular keyboard, so these are no-ops,
2721 * but there are still weird bugs re. MUTE, so do not
2722 * change unless you get test reports from all Lenovo
2723 * models. May cause the BIOS to interfere with the
2726 KEY_RESERVED, /* 0x14: VOLUME UP */
2727 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2728 KEY_RESERVED, /* 0x16: MUTE */
2730 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2732 /* (assignments unknown, please report if found) */
2733 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2734 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2737 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2738 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2739 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2745 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2746 "initializing hotkey subdriver\n");
2748 BUG_ON(!tpacpi_inputdev);
2749 BUG_ON(tpacpi_inputdev->open != NULL ||
2750 tpacpi_inputdev->close != NULL);
2752 TPACPI_ACPIHANDLE_INIT(hkey);
2753 mutex_init(&hotkey_mutex);
2755 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2756 mutex_init(&hotkey_thread_mutex);
2757 mutex_init(&hotkey_thread_data_mutex);
2760 /* hotkey not supported on 570 */
2761 tp_features.hotkey = hkey_handle != NULL;
2763 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2765 str_supported(tp_features.hotkey));
2767 if (!tp_features.hotkey)
2770 tpacpi_disable_brightness_delay();
2772 hotkey_dev_attributes = create_attr_set(13, NULL);
2773 if (!hotkey_dev_attributes)
2775 res = add_many_to_attr_set(hotkey_dev_attributes,
2777 ARRAY_SIZE(hotkey_attributes));
2781 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2782 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2783 for HKEY interface version 0x100 */
2784 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2785 if ((hkeyv >> 8) != 1) {
2786 printk(TPACPI_ERR "unknown version of the "
2787 "HKEY interface: 0x%x\n", hkeyv);
2788 printk(TPACPI_ERR "please report this to %s\n",
2792 * MHKV 0x100 in A31, R40, R40e,
2793 * T4x, X31, and later
2795 tp_features.hotkey_mask = 1;
2796 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2797 "firmware HKEY interface version: 0x%x\n",
2802 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2803 "hotkey masks are %s\n",
2804 str_supported(tp_features.hotkey_mask));
2806 if (tp_features.hotkey_mask) {
2807 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2810 "missing MHKA handler, "
2811 "please report this to %s\n",
2813 /* FN+F12, FN+F4, FN+F3 */
2814 hotkey_all_mask = 0x080cU;
2818 /* hotkey_source_mask *must* be zero for
2819 * the first hotkey_mask_get */
2820 if (tp_features.hotkey_mask) {
2821 res = hotkey_mask_get();
2825 hotkey_orig_mask = hotkey_mask;
2826 res = add_many_to_attr_set(
2827 hotkey_dev_attributes,
2828 hotkey_mask_attributes,
2829 ARRAY_SIZE(hotkey_mask_attributes));
2834 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2835 if (tp_features.hotkey_mask) {
2836 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2839 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2842 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2843 "hotkey source mask 0x%08x, polling freq %d\n",
2844 hotkey_source_mask, hotkey_poll_freq);
2847 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2849 tp_features.hotkey_wlsw = 1;
2851 "radio switch emulation enabled\n");
2854 /* Not all thinkpads have a hardware radio switch */
2855 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2856 tp_features.hotkey_wlsw = 1;
2858 "radio switch found; radios are %s\n",
2859 enabled(status, 0));
2861 if (tp_features.hotkey_wlsw)
2862 res = add_to_attr_set(hotkey_dev_attributes,
2863 &dev_attr_hotkey_radio_sw.attr);
2865 /* For X41t, X60t, X61t Tablets... */
2866 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2867 tp_features.hotkey_tablet = 1;
2869 "possible tablet mode switch found; "
2870 "ThinkPad in %s mode\n",
2871 (status & TP_HOTKEY_TABLET_MASK)?
2872 "tablet" : "laptop");
2873 res = add_to_attr_set(hotkey_dev_attributes,
2874 &dev_attr_hotkey_tablet_mode.attr);
2878 res = register_attr_set_with_sysfs(
2879 hotkey_dev_attributes,
2880 &tpacpi_pdev->dev.kobj);
2884 /* Set up key map */
2886 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2888 if (!hotkey_keycode_map) {
2890 "failed to allocate memory for key map\n");
2895 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2896 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2897 "using Lenovo default hot key map\n");
2898 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2899 TPACPI_HOTKEY_MAP_SIZE);
2901 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2902 "using IBM default hot key map\n");
2903 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2904 TPACPI_HOTKEY_MAP_SIZE);
2907 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2908 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2909 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2910 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2911 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2912 tpacpi_inputdev->keycode = hotkey_keycode_map;
2913 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2914 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2915 set_bit(hotkey_keycode_map[i],
2916 tpacpi_inputdev->keybit);
2918 if (i < sizeof(hotkey_reserved_mask)*8)
2919 hotkey_reserved_mask |= 1 << i;
2923 if (tp_features.hotkey_wlsw) {
2924 set_bit(EV_SW, tpacpi_inputdev->evbit);
2925 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2927 if (tp_features.hotkey_tablet) {
2928 set_bit(EV_SW, tpacpi_inputdev->evbit);
2929 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2932 /* Do not issue duplicate brightness change events to
2934 if (!tp_features.bright_acpimode)
2935 /* update bright_acpimode... */
2936 tpacpi_check_std_acpi_brightness_support();
2938 if (tp_features.bright_acpimode && acpi_video_backlight_support()) {
2940 "This ThinkPad has standard ACPI backlight "
2941 "brightness control, supported by the ACPI "
2943 printk(TPACPI_NOTICE
2944 "Disabling thinkpad-acpi brightness events "
2947 /* The hotkey_reserved_mask change below is not
2948 * necessary while the keys are at KEY_RESERVED in the
2949 * default map, but better safe than sorry, leave it
2950 * here as a marker of what we have to do, especially
2951 * when we finally become able to set this at runtime
2952 * on response to X.org requests */
2953 hotkey_reserved_mask |=
2954 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2955 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2958 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2959 "enabling firmware HKEY event interface...\n");
2960 res = hotkey_status_set(true);
2965 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2966 & ~hotkey_reserved_mask)
2967 | hotkey_orig_mask);
2968 if (res < 0 && res != -ENXIO) {
2973 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2974 "legacy ibm/hotkey event reporting over procfs %s\n",
2975 (hotkey_report_mode < 2) ?
2976 "enabled" : "disabled");
2978 tpacpi_inputdev->open = &hotkey_inputdev_open;
2979 tpacpi_inputdev->close = &hotkey_inputdev_close;
2981 hotkey_poll_setup_safe(1);
2982 tpacpi_send_radiosw_update();
2983 tpacpi_input_send_tabletsw();
2988 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2989 hotkey_dev_attributes = NULL;
2991 return (res < 0)? res : 1;
2994 static bool hotkey_notify_hotkey(const u32 hkey,
2996 bool *ignore_acpi_ev)
2998 /* 0x1000-0x1FFF: key presses */
2999 unsigned int scancode = hkey & 0xfff;
3000 *send_acpi_ev = true;
3001 *ignore_acpi_ev = false;
3003 if (scancode > 0 && scancode < 0x21) {
3005 if (!(hotkey_source_mask & (1 << scancode))) {
3006 tpacpi_input_send_key(scancode);
3007 *send_acpi_ev = false;
3009 *ignore_acpi_ev = true;
3016 static bool hotkey_notify_wakeup(const u32 hkey,
3018 bool *ignore_acpi_ev)
3020 /* 0x2000-0x2FFF: Wakeup reason */
3021 *send_acpi_ev = true;
3022 *ignore_acpi_ev = false;
3025 case 0x2304: /* suspend, undock */
3026 case 0x2404: /* hibernation, undock */
3027 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
3028 *ignore_acpi_ev = true;
3031 case 0x2305: /* suspend, bay eject */
3032 case 0x2405: /* hibernation, bay eject */
3033 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
3034 *ignore_acpi_ev = true;
3037 case 0x2313: /* Battery on critical low level (S3) */
3038 case 0x2413: /* Battery on critical low level (S4) */
3040 "EMERGENCY WAKEUP: battery almost empty\n");
3041 /* how to auto-heal: */
3042 /* 2313: woke up from S3, go to S4/S5 */
3043 /* 2413: woke up from S4, go to S5 */
3050 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
3052 "woke up due to a hot-unplug "
3054 hotkey_wakeup_reason_notify_change();
3059 static bool hotkey_notify_usrevent(const u32 hkey,
3061 bool *ignore_acpi_ev)
3063 /* 0x5000-0x5FFF: human interface helpers */
3064 *send_acpi_ev = true;
3065 *ignore_acpi_ev = false;
3068 case 0x5010: /* Lenovo new BIOS: brightness changed */
3069 case 0x500b: /* X61t: tablet pen inserted into bay */
3070 case 0x500c: /* X61t: tablet pen removed from bay */
3073 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
3074 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
3075 tpacpi_input_send_tabletsw();
3076 hotkey_tablet_mode_notify_change();
3077 *send_acpi_ev = false;
3082 /* LID switch events. Do not propagate */
3083 *ignore_acpi_ev = true;
3091 static bool hotkey_notify_thermal(const u32 hkey,
3093 bool *ignore_acpi_ev)
3095 /* 0x6000-0x6FFF: thermal alarms */
3096 *send_acpi_ev = true;
3097 *ignore_acpi_ev = false;
3102 "THERMAL ALARM: battery is too hot!\n");
3103 /* recommended action: warn user through gui */
3107 "THERMAL EMERGENCY: battery is extremely hot!\n");
3108 /* recommended action: immediate sleep/hibernate */
3113 "a sensor reports something is too hot!\n");
3114 /* recommended action: warn user through gui, that */
3115 /* some internal component is too hot */
3119 "THERMAL EMERGENCY: "
3120 "a sensor reports something is extremely hot!\n");
3121 /* recommended action: immediate sleep/hibernate */
3125 "EC reports that Thermal Table has changed\n");
3126 /* recommended action: do nothing, we don't have
3127 * Lenovo ATM information */
3131 "THERMAL ALERT: unknown thermal alarm received\n");
3136 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
3140 bool ignore_acpi_ev;
3143 if (event != 0x80) {
3145 "unknown HKEY notification event %d\n", event);
3146 /* forward it to userspace, maybe it knows how to handle it */
3147 acpi_bus_generate_netlink_event(
3148 ibm->acpi->device->pnp.device_class,
3149 dev_name(&ibm->acpi->device->dev),
3155 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
3156 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
3165 send_acpi_ev = true;
3166 ignore_acpi_ev = false;
3168 switch (hkey >> 12) {
3170 /* 0x1000-0x1FFF: key presses */
3171 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
3175 /* 0x2000-0x2FFF: Wakeup reason */
3176 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
3180 /* 0x3000-0x3FFF: bay-related wakeups */
3181 if (hkey == 0x3003) {
3182 hotkey_autosleep_ack = 1;
3185 hotkey_wakeup_hotunplug_complete_notify_change();
3192 /* 0x4000-0x4FFF: dock-related wakeups */
3193 if (hkey == 0x4003) {
3194 hotkey_autosleep_ack = 1;
3197 hotkey_wakeup_hotunplug_complete_notify_change();
3204 /* 0x5000-0x5FFF: human interface helpers */
3205 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
3209 /* 0x6000-0x6FFF: thermal alarms */
3210 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
3214 /* 0x7000-0x7FFF: misc */
3215 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
3216 tpacpi_send_radiosw_update();
3221 /* fallthrough to default */
3226 printk(TPACPI_NOTICE
3227 "unhandled HKEY event 0x%04x\n", hkey);
3228 printk(TPACPI_NOTICE
3229 "please report the conditions when this "
3230 "event happened to %s\n", TPACPI_MAIL);
3234 if (!ignore_acpi_ev &&
3235 (send_acpi_ev || hotkey_report_mode < 2)) {
3236 acpi_bus_generate_proc_event(ibm->acpi->device,
3240 /* netlink events */
3241 if (!ignore_acpi_ev && send_acpi_ev) {
3242 acpi_bus_generate_netlink_event(
3243 ibm->acpi->device->pnp.device_class,
3244 dev_name(&ibm->acpi->device->dev),
3250 static void hotkey_suspend(pm_message_t state)
3252 /* Do these on suspend, we get the events on early resume! */
3253 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
3254 hotkey_autosleep_ack = 0;
3257 static void hotkey_resume(void)
3259 tpacpi_disable_brightness_delay();
3261 if (hotkey_mask_get())
3263 "error while trying to read hot key mask "
3265 tpacpi_send_radiosw_update();
3266 hotkey_tablet_mode_notify_change();
3267 hotkey_wakeup_reason_notify_change();
3268 hotkey_wakeup_hotunplug_complete_notify_change();
3269 hotkey_poll_setup_safe(0);
3272 /* procfs -------------------------------------------------------------- */
3273 static int hotkey_read(char *p)
3278 if (!tp_features.hotkey) {
3279 len += sprintf(p + len, "status:\t\tnot supported\n");
3283 if (mutex_lock_killable(&hotkey_mutex))
3284 return -ERESTARTSYS;
3285 res = hotkey_status_get(&status);
3287 res = hotkey_mask_get();
3288 mutex_unlock(&hotkey_mutex);
3292 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
3293 if (tp_features.hotkey_mask) {
3294 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
3295 len += sprintf(p + len,
3296 "commands:\tenable, disable, reset, <mask>\n");
3298 len += sprintf(p + len, "mask:\t\tnot supported\n");
3299 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
3305 static void hotkey_enabledisable_warn(bool enable)
3307 tpacpi_log_usertask("procfs hotkey enable/disable");
3308 if (!WARN((tpacpi_lifecycle == TPACPI_LIFE_RUNNING || !enable),
3310 "hotkey enable/disable functionality has been "
3311 "removed from the driver. Hotkeys are always "
3314 "Please remove the hotkey=enable module "
3315 "parameter, it is deprecated. Hotkeys are always "
3319 static int hotkey_write(char *buf)
3325 if (!tp_features.hotkey)
3328 if (mutex_lock_killable(&hotkey_mutex))
3329 return -ERESTARTSYS;
3334 while ((cmd = next_cmd(&buf))) {
3335 if (strlencmp(cmd, "enable") == 0) {
3336 hotkey_enabledisable_warn(1);
3337 } else if (strlencmp(cmd, "disable") == 0) {
3338 hotkey_enabledisable_warn(0);
3340 } else if (strlencmp(cmd, "reset") == 0) {
3341 mask = hotkey_orig_mask;
3342 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
3344 } else if (sscanf(cmd, "%x", &mask) == 1) {
3353 tpacpi_disclose_usertask("procfs hotkey",
3354 "set mask to 0x%08x\n", mask);
3356 if (!res && mask != hotkey_mask)
3357 res = hotkey_mask_set(mask);
3360 mutex_unlock(&hotkey_mutex);
3364 static const struct acpi_device_id ibm_htk_device_ids[] = {
3365 {TPACPI_ACPI_HKEY_HID, 0},
3369 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3370 .hid = ibm_htk_device_ids,
3371 .notify = hotkey_notify,
3372 .handle = &hkey_handle,
3373 .type = ACPI_DEVICE_NOTIFY,
3376 static struct ibm_struct hotkey_driver_data = {
3378 .read = hotkey_read,
3379 .write = hotkey_write,
3380 .exit = hotkey_exit,
3381 .resume = hotkey_resume,
3382 .suspend = hotkey_suspend,
3383 .acpi = &ibm_hotkey_acpidriver,
3386 /*************************************************************************
3387 * Bluetooth subdriver
3391 /* ACPI GBDC/SBDC bits */
3392 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3393 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3394 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3399 /* ACPI \BLTH commands */
3400 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3401 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3402 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3403 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3404 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3407 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3409 static void bluetooth_suspend(pm_message_t state)
3411 /* Try to make sure radio will resume powered off */
3412 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3413 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3414 vdbg_printk(TPACPI_DBG_RFKILL,
3415 "bluetooth power down on resume request failed\n");
3418 static int bluetooth_get_status(void)
3422 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3423 if (dbg_bluetoothemul)
3424 return (tpacpi_bluetooth_emulstate) ?
3425 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3428 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3431 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3432 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3435 static int bluetooth_set_status(enum tpacpi_rfkill_state state)
3439 vdbg_printk(TPACPI_DBG_RFKILL,
3440 "will attempt to %s bluetooth\n",
3441 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3443 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3444 if (dbg_bluetoothemul) {
3445 tpacpi_bluetooth_emulstate = (state == TPACPI_RFK_RADIO_ON);
3450 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3451 if (state == TPACPI_RFK_RADIO_ON)
3452 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3456 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3462 /* sysfs bluetooth enable ---------------------------------------------- */
3463 static ssize_t bluetooth_enable_show(struct device *dev,
3464 struct device_attribute *attr,
3467 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_BLUETOOTH_SW_ID,
3471 static ssize_t bluetooth_enable_store(struct device *dev,
3472 struct device_attribute *attr,
3473 const char *buf, size_t count)
3475 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_BLUETOOTH_SW_ID,
3479 static struct device_attribute dev_attr_bluetooth_enable =
3480 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3481 bluetooth_enable_show, bluetooth_enable_store);
3483 /* --------------------------------------------------------------------- */
3485 static struct attribute *bluetooth_attributes[] = {
3486 &dev_attr_bluetooth_enable.attr,
3490 static const struct attribute_group bluetooth_attr_group = {
3491 .attrs = bluetooth_attributes,
3494 static const struct tpacpi_rfk_ops bluetooth_tprfk_ops = {
3495 .get_status = bluetooth_get_status,
3496 .set_status = bluetooth_set_status,
3499 static void bluetooth_shutdown(void)
3501 /* Order firmware to save current state to NVRAM */
3502 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3503 TP_ACPI_BLTH_SAVE_STATE))
3504 printk(TPACPI_NOTICE
3505 "failed to save bluetooth state to NVRAM\n");
3507 vdbg_printk(TPACPI_DBG_RFKILL,
3508 "bluestooth state saved to NVRAM\n");
3511 static void bluetooth_exit(void)
3513 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3514 &bluetooth_attr_group);
3516 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3518 bluetooth_shutdown();
3521 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3526 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3527 "initializing bluetooth subdriver\n");
3529 TPACPI_ACPIHANDLE_INIT(hkey);
3531 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3532 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3533 tp_features.bluetooth = hkey_handle &&
3534 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3536 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3537 "bluetooth is %s, status 0x%02x\n",
3538 str_supported(tp_features.bluetooth),
3541 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3542 if (dbg_bluetoothemul) {
3543 tp_features.bluetooth = 1;
3545 "bluetooth switch emulation enabled\n");
3548 if (tp_features.bluetooth &&
3549 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3550 /* no bluetooth hardware present in system */
3551 tp_features.bluetooth = 0;
3552 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3553 "bluetooth hardware not installed\n");
3556 if (!tp_features.bluetooth)
3559 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3560 &bluetooth_tprfk_ops,
3561 RFKILL_TYPE_BLUETOOTH,
3562 TPACPI_RFK_BLUETOOTH_SW_NAME,
3567 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3568 &bluetooth_attr_group);
3570 tpacpi_destroy_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID);
3577 /* procfs -------------------------------------------------------------- */
3578 static int bluetooth_read(char *p)
3580 return tpacpi_rfk_procfs_read(TPACPI_RFK_BLUETOOTH_SW_ID, p);
3583 static int bluetooth_write(char *buf)
3585 return tpacpi_rfk_procfs_write(TPACPI_RFK_BLUETOOTH_SW_ID, buf);
3588 static struct ibm_struct bluetooth_driver_data = {
3589 .name = "bluetooth",
3590 .read = bluetooth_read,
3591 .write = bluetooth_write,
3592 .exit = bluetooth_exit,
3593 .suspend = bluetooth_suspend,
3594 .shutdown = bluetooth_shutdown,
3597 /*************************************************************************
3602 /* ACPI GWAN/SWAN bits */
3603 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3604 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3605 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3609 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3611 static void wan_suspend(pm_message_t state)
3613 /* Try to make sure radio will resume powered off */
3614 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3615 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3616 vdbg_printk(TPACPI_DBG_RFKILL,
3617 "WWAN power down on resume request failed\n");
3620 static int wan_get_status(void)
3624 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3626 return (tpacpi_wwan_emulstate) ?
3627 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3630 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3633 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3634 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3637 static int wan_set_status(enum tpacpi_rfkill_state state)
3641 vdbg_printk(TPACPI_DBG_RFKILL,
3642 "will attempt to %s wwan\n",
3643 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3645 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3647 tpacpi_wwan_emulstate = (state == TPACPI_RFK_RADIO_ON);
3652 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3653 if (state == TPACPI_RFK_RADIO_ON)
3654 status = TP_ACPI_WANCARD_RADIOSSW;
3658 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3664 /* sysfs wan enable ---------------------------------------------------- */
3665 static ssize_t wan_enable_show(struct device *dev,
3666 struct device_attribute *attr,
3669 return tpacpi_rfk_sysfs_enable_show(TPACPI_RFK_WWAN_SW_ID,
3673 static ssize_t wan_enable_store(struct device *dev,
3674 struct device_attribute *attr,
3675 const char *buf, size_t count)
3677 return tpacpi_rfk_sysfs_enable_store(TPACPI_RFK_WWAN_SW_ID,
3681 static struct device_attribute dev_attr_wan_enable =
3682 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3683 wan_enable_show, wan_enable_store);
3685 /* --------------------------------------------------------------------- */
3687 static struct attribute *wan_attributes[] = {
3688 &dev_attr_wan_enable.attr,
3692 static const struct attribute_group wan_attr_group = {
3693 .attrs = wan_attributes,
3696 static const struct tpacpi_rfk_ops wan_tprfk_ops = {
3697 .get_status = wan_get_status,
3698 .set_status = wan_set_status,
3701 static void wan_shutdown(void)
3703 /* Order firmware to save current state to NVRAM */
3704 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3705 TP_ACPI_WGSV_SAVE_STATE))
3706 printk(TPACPI_NOTICE
3707 "failed to save WWAN state to NVRAM\n");
3709 vdbg_printk(TPACPI_DBG_RFKILL,
3710 "WWAN state saved to NVRAM\n");
3713 static void wan_exit(void)
3715 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3718 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3723 static int __init wan_init(struct ibm_init_struct *iibm)
3728 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3729 "initializing wan subdriver\n");
3731 TPACPI_ACPIHANDLE_INIT(hkey);
3733 tp_features.wan = hkey_handle &&
3734 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3736 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3737 "wan is %s, status 0x%02x\n",
3738 str_supported(tp_features.wan),
3741 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3743 tp_features.wan = 1;
3745 "wwan switch emulation enabled\n");
3748 if (tp_features.wan &&
3749 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3750 /* no wan hardware present in system */
3751 tp_features.wan = 0;
3752 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3753 "wan hardware not installed\n");
3756 if (!tp_features.wan)
3759 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3762 TPACPI_RFK_WWAN_SW_NAME,
3767 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3771 tpacpi_destroy_rfkill(TPACPI_RFK_WWAN_SW_ID);
3778 /* procfs -------------------------------------------------------------- */
3779 static int wan_read(char *p)
3781 return tpacpi_rfk_procfs_read(TPACPI_RFK_WWAN_SW_ID, p);
3784 static int wan_write(char *buf)
3786 return tpacpi_rfk_procfs_write(TPACPI_RFK_WWAN_SW_ID, buf);
3789 static struct ibm_struct wan_driver_data = {
3794 .suspend = wan_suspend,
3795 .shutdown = wan_shutdown,
3798 /*************************************************************************
3803 /* ACPI GUWB/SUWB bits */
3804 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3805 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3808 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3810 static int uwb_get_status(void)
3814 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3816 return (tpacpi_uwb_emulstate) ?
3817 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3820 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3823 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3824 TPACPI_RFK_RADIO_ON : TPACPI_RFK_RADIO_OFF;
3827 static int uwb_set_status(enum tpacpi_rfkill_state state)
3831 vdbg_printk(TPACPI_DBG_RFKILL,
3832 "will attempt to %s UWB\n",
3833 (state == TPACPI_RFK_RADIO_ON) ? "enable" : "disable");
3835 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3837 tpacpi_uwb_emulstate = (state == TPACPI_RFK_RADIO_ON);
3842 if (state == TPACPI_RFK_RADIO_ON)
3843 status = TP_ACPI_UWB_RADIOSSW;
3847 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3853 /* --------------------------------------------------------------------- */
3855 static const struct tpacpi_rfk_ops uwb_tprfk_ops = {
3856 .get_status = uwb_get_status,
3857 .set_status = uwb_set_status,
3860 static void uwb_exit(void)
3862 tpacpi_destroy_rfkill(TPACPI_RFK_UWB_SW_ID);
3865 static int __init uwb_init(struct ibm_init_struct *iibm)
3870 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3871 "initializing uwb subdriver\n");
3873 TPACPI_ACPIHANDLE_INIT(hkey);
3875 tp_features.uwb = hkey_handle &&
3876 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3878 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3879 "uwb is %s, status 0x%02x\n",
3880 str_supported(tp_features.uwb),
3883 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3885 tp_features.uwb = 1;
3887 "uwb switch emulation enabled\n");
3890 if (tp_features.uwb &&
3891 !(status & TP_ACPI_UWB_HWPRESENT)) {
3892 /* no uwb hardware present in system */
3893 tp_features.uwb = 0;
3894 dbg_printk(TPACPI_DBG_INIT,
3895 "uwb hardware not installed\n");
3898 if (!tp_features.uwb)
3901 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3904 TPACPI_RFK_UWB_SW_NAME,
3909 static struct ibm_struct uwb_driver_data = {
3912 .flags.experimental = 1,
3915 /*************************************************************************
3919 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3921 enum video_access_mode {
3922 TPACPI_VIDEO_NONE = 0,
3923 TPACPI_VIDEO_570, /* 570 */
3924 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3925 TPACPI_VIDEO_NEW, /* all others */
3928 enum { /* video status flags, based on VIDEO_570 */
3929 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3930 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3931 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3934 enum { /* TPACPI_VIDEO_570 constants */
3935 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3936 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3937 * video_status_flags */
3938 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3939 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3942 static enum video_access_mode video_supported;
3943 static int video_orig_autosw;
3945 static int video_autosw_get(void);
3946 static int video_autosw_set(int enable);
3948 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3950 static int __init video_init(struct ibm_init_struct *iibm)
3954 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3956 TPACPI_ACPIHANDLE_INIT(vid);
3957 TPACPI_ACPIHANDLE_INIT(vid2);
3959 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3960 /* G41, assume IVGA doesn't change */
3961 vid_handle = vid2_handle;
3964 /* video switching not supported on R30, R31 */
3965 video_supported = TPACPI_VIDEO_NONE;
3966 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3968 video_supported = TPACPI_VIDEO_570;
3969 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3970 /* 600e/x, 770e, 770x */
3971 video_supported = TPACPI_VIDEO_770;
3974 video_supported = TPACPI_VIDEO_NEW;
3976 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3977 str_supported(video_supported != TPACPI_VIDEO_NONE),
3980 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3983 static void video_exit(void)
3985 dbg_printk(TPACPI_DBG_EXIT,
3986 "restoring original video autoswitch mode\n");
3987 if (video_autosw_set(video_orig_autosw))
3988 printk(TPACPI_ERR "error while trying to restore original "
3989 "video autoswitch mode\n");
3992 static int video_outputsw_get(void)
3997 switch (video_supported) {
3998 case TPACPI_VIDEO_570:
3999 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
4000 TP_ACPI_VIDEO_570_PHSCMD))
4002 status = i & TP_ACPI_VIDEO_570_PHSMASK;
4004 case TPACPI_VIDEO_770:
4005 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
4008 status |= TP_ACPI_VIDEO_S_LCD;
4009 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
4012 status |= TP_ACPI_VIDEO_S_CRT;
4014 case TPACPI_VIDEO_NEW:
4015 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
4016 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
4019 status |= TP_ACPI_VIDEO_S_CRT;
4021 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
4022 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
4025 status |= TP_ACPI_VIDEO_S_LCD;
4026 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
4029 status |= TP_ACPI_VIDEO_S_DVI;
4038 static int video_outputsw_set(int status)
4043 switch (video_supported) {
4044 case TPACPI_VIDEO_570:
4045 res = acpi_evalf(NULL, NULL,
4046 "\\_SB.PHS2", "vdd",
4047 TP_ACPI_VIDEO_570_PHS2CMD,
4048 status | TP_ACPI_VIDEO_570_PHS2SET);
4050 case TPACPI_VIDEO_770:
4051 autosw = video_autosw_get();
4055 res = video_autosw_set(1);
4058 res = acpi_evalf(vid_handle, NULL,
4059 "ASWT", "vdd", status * 0x100, 0);
4060 if (!autosw && video_autosw_set(autosw)) {
4062 "video auto-switch left enabled due to error\n");
4066 case TPACPI_VIDEO_NEW:
4067 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
4068 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
4074 return (res)? 0 : -EIO;
4077 static int video_autosw_get(void)
4081 switch (video_supported) {
4082 case TPACPI_VIDEO_570:
4083 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
4086 case TPACPI_VIDEO_770:
4087 case TPACPI_VIDEO_NEW:
4088 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
4098 static int video_autosw_set(int enable)
4100 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4105 static int video_outputsw_cycle(void)
4107 int autosw = video_autosw_get();
4113 switch (video_supported) {
4114 case TPACPI_VIDEO_570:
4115 res = video_autosw_set(1);
4118 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4120 case TPACPI_VIDEO_770:
4121 case TPACPI_VIDEO_NEW:
4122 res = video_autosw_set(1);
4125 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4130 if (!autosw && video_autosw_set(autosw)) {
4132 "video auto-switch left enabled due to error\n");
4136 return (res)? 0 : -EIO;
4139 static int video_expand_toggle(void)
4141 switch (video_supported) {
4142 case TPACPI_VIDEO_570:
4143 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4145 case TPACPI_VIDEO_770:
4146 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4148 case TPACPI_VIDEO_NEW:
4149 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4157 static int video_read(char *p)
4162 if (video_supported == TPACPI_VIDEO_NONE) {
4163 len += sprintf(p + len, "status:\t\tnot supported\n");
4167 status = video_outputsw_get();
4171 autosw = video_autosw_get();
4175 len += sprintf(p + len, "status:\t\tsupported\n");
4176 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4177 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4178 if (video_supported == TPACPI_VIDEO_NEW)
4179 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4180 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4181 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4182 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4183 if (video_supported == TPACPI_VIDEO_NEW)
4184 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4185 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4186 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4191 static int video_write(char *buf)
4194 int enable, disable, status;
4197 if (video_supported == TPACPI_VIDEO_NONE)
4203 while ((cmd = next_cmd(&buf))) {
4204 if (strlencmp(cmd, "lcd_enable") == 0) {
4205 enable |= TP_ACPI_VIDEO_S_LCD;
4206 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4207 disable |= TP_ACPI_VIDEO_S_LCD;
4208 } else if (strlencmp(cmd, "crt_enable") == 0) {
4209 enable |= TP_ACPI_VIDEO_S_CRT;
4210 } else if (strlencmp(cmd, "crt_disable") == 0) {
4211 disable |= TP_ACPI_VIDEO_S_CRT;
4212 } else if (video_supported == TPACPI_VIDEO_NEW &&
4213 strlencmp(cmd, "dvi_enable") == 0) {
4214 enable |= TP_ACPI_VIDEO_S_DVI;
4215 } else if (video_supported == TPACPI_VIDEO_NEW &&
4216 strlencmp(cmd, "dvi_disable") == 0) {
4217 disable |= TP_ACPI_VIDEO_S_DVI;
4218 } else if (strlencmp(cmd, "auto_enable") == 0) {
4219 res = video_autosw_set(1);
4222 } else if (strlencmp(cmd, "auto_disable") == 0) {
4223 res = video_autosw_set(0);
4226 } else if (strlencmp(cmd, "video_switch") == 0) {
4227 res = video_outputsw_cycle();
4230 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4231 res = video_expand_toggle();
4238 if (enable || disable) {
4239 status = video_outputsw_get();
4242 res = video_outputsw_set((status & ~disable) | enable);
4250 static struct ibm_struct video_driver_data = {
4253 .write = video_write,
4257 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4259 /*************************************************************************
4260 * Light (thinklight) subdriver
4263 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4264 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4266 static int light_get_status(void)
4270 if (tp_features.light_status) {
4271 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4279 static int light_set_status(int status)
4283 if (tp_features.light) {
4285 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4287 TP_CMOS_THINKLIGHT_ON :
4288 TP_CMOS_THINKLIGHT_OFF);
4290 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4293 return (rc)? 0 : -EIO;
4299 static void light_set_status_worker(struct work_struct *work)
4301 struct tpacpi_led_classdev *data =
4302 container_of(work, struct tpacpi_led_classdev, work);
4304 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4305 light_set_status((data->new_state != TPACPI_LED_OFF));
4308 static void light_sysfs_set(struct led_classdev *led_cdev,
4309 enum led_brightness brightness)
4311 struct tpacpi_led_classdev *data =
4312 container_of(led_cdev,
4313 struct tpacpi_led_classdev,
4315 data->new_state = (brightness != LED_OFF) ?
4316 TPACPI_LED_ON : TPACPI_LED_OFF;
4317 queue_work(tpacpi_wq, &data->work);
4320 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4322 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4325 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4327 .name = "tpacpi::thinklight",
4328 .brightness_set = &light_sysfs_set,
4329 .brightness_get = &light_sysfs_get,
4333 static int __init light_init(struct ibm_init_struct *iibm)
4337 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4339 TPACPI_ACPIHANDLE_INIT(ledb);
4340 TPACPI_ACPIHANDLE_INIT(lght);
4341 TPACPI_ACPIHANDLE_INIT(cmos);
4342 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4344 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4345 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4347 if (tp_features.light)
4348 /* light status not supported on
4349 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4350 tp_features.light_status =
4351 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4353 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4354 str_supported(tp_features.light),
4355 str_supported(tp_features.light_status));
4357 if (!tp_features.light)
4360 rc = led_classdev_register(&tpacpi_pdev->dev,
4361 &tpacpi_led_thinklight.led_classdev);
4364 tp_features.light = 0;
4365 tp_features.light_status = 0;
4373 static void light_exit(void)
4375 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4376 if (work_pending(&tpacpi_led_thinklight.work))
4377 flush_workqueue(tpacpi_wq);
4380 static int light_read(char *p)
4385 if (!tp_features.light) {
4386 len += sprintf(p + len, "status:\t\tnot supported\n");
4387 } else if (!tp_features.light_status) {
4388 len += sprintf(p + len, "status:\t\tunknown\n");
4389 len += sprintf(p + len, "commands:\ton, off\n");
4391 status = light_get_status();
4394 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4395 len += sprintf(p + len, "commands:\ton, off\n");
4401 static int light_write(char *buf)
4406 if (!tp_features.light)
4409 while ((cmd = next_cmd(&buf))) {
4410 if (strlencmp(cmd, "on") == 0) {
4412 } else if (strlencmp(cmd, "off") == 0) {
4418 return light_set_status(newstatus);
4421 static struct ibm_struct light_driver_data = {
4424 .write = light_write,
4428 /*************************************************************************
4432 /* sysfs cmos_command -------------------------------------------------- */
4433 static ssize_t cmos_command_store(struct device *dev,
4434 struct device_attribute *attr,
4435 const char *buf, size_t count)
4437 unsigned long cmos_cmd;
4440 if (parse_strtoul(buf, 21, &cmos_cmd))
4443 res = issue_thinkpad_cmos_command(cmos_cmd);
4444 return (res)? res : count;
4447 static struct device_attribute dev_attr_cmos_command =
4448 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4450 /* --------------------------------------------------------------------- */
4452 static int __init cmos_init(struct ibm_init_struct *iibm)
4456 vdbg_printk(TPACPI_DBG_INIT,
4457 "initializing cmos commands subdriver\n");
4459 TPACPI_ACPIHANDLE_INIT(cmos);
4461 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4462 str_supported(cmos_handle != NULL));
4464 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4468 return (cmos_handle)? 0 : 1;
4471 static void cmos_exit(void)
4473 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4476 static int cmos_read(char *p)
4480 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4481 R30, R31, T20-22, X20-21 */
4483 len += sprintf(p + len, "status:\t\tnot supported\n");
4485 len += sprintf(p + len, "status:\t\tsupported\n");
4486 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4492 static int cmos_write(char *buf)
4497 while ((cmd = next_cmd(&buf))) {
4498 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4499 cmos_cmd >= 0 && cmos_cmd <= 21) {
4504 res = issue_thinkpad_cmos_command(cmos_cmd);
4512 static struct ibm_struct cmos_driver_data = {
4515 .write = cmos_write,
4519 /*************************************************************************
4523 enum led_access_mode {
4524 TPACPI_LED_NONE = 0,
4525 TPACPI_LED_570, /* 570 */
4526 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4527 TPACPI_LED_NEW, /* all others */
4530 enum { /* For TPACPI_LED_OLD */
4531 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4532 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4533 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4536 static enum led_access_mode led_supported;
4538 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4539 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4540 /* T20-22, X20-21 */
4541 "LED", /* all others */
4544 #define TPACPI_LED_NUMLEDS 16
4545 static struct tpacpi_led_classdev *tpacpi_leds;
4546 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4547 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4548 /* there's a limit of 19 chars + NULL before 2.6.26 */
4550 "tpacpi:orange:batt",
4551 "tpacpi:green:batt",
4552 "tpacpi::dock_active",
4553 "tpacpi::bay_active",
4554 "tpacpi::dock_batt",
4555 "tpacpi::unknown_led",
4557 "tpacpi::dock_status1",
4558 "tpacpi::dock_status2",
4559 "tpacpi::unknown_led2",
4560 "tpacpi::unknown_led3",
4561 "tpacpi::thinkvantage",
4563 #define TPACPI_SAFE_LEDS 0x1081U
4565 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4567 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4570 return (1U & (TPACPI_SAFE_LEDS >> led)) == 0;
4574 static int led_get_status(const unsigned int led)
4577 enum led_status_t led_s;
4579 switch (led_supported) {
4580 case TPACPI_LED_570:
4581 if (!acpi_evalf(ec_handle,
4582 &status, "GLED", "dd", 1 << led))
4584 led_s = (status == 0)?
4589 tpacpi_led_state_cache[led] = led_s;
4598 static int led_set_status(const unsigned int led,
4599 const enum led_status_t ledstatus)
4601 /* off, on, blink. Index is led_status_t */
4602 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4603 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4607 switch (led_supported) {
4608 case TPACPI_LED_570:
4610 if (unlikely(led > 7))
4612 if (unlikely(tpacpi_is_led_restricted(led)))
4614 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4615 (1 << led), led_sled_arg1[ledstatus]))
4618 case TPACPI_LED_OLD:
4619 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4620 if (unlikely(led > 7))
4622 if (unlikely(tpacpi_is_led_restricted(led)))
4624 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4626 rc = ec_write(TPACPI_LED_EC_HLBL,
4627 (ledstatus == TPACPI_LED_BLINK) << led);
4629 rc = ec_write(TPACPI_LED_EC_HLCL,
4630 (ledstatus != TPACPI_LED_OFF) << led);
4632 case TPACPI_LED_NEW:
4634 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4636 if (unlikely(tpacpi_is_led_restricted(led)))
4638 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4639 led, led_led_arg1[ledstatus]))
4647 tpacpi_led_state_cache[led] = ledstatus;
4652 static void led_set_status_worker(struct work_struct *work)
4654 struct tpacpi_led_classdev *data =
4655 container_of(work, struct tpacpi_led_classdev, work);
4657 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4658 led_set_status(data->led, data->new_state);
4661 static void led_sysfs_set(struct led_classdev *led_cdev,
4662 enum led_brightness brightness)
4664 struct tpacpi_led_classdev *data = container_of(led_cdev,
4665 struct tpacpi_led_classdev, led_classdev);
4667 if (brightness == LED_OFF)
4668 data->new_state = TPACPI_LED_OFF;
4669 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4670 data->new_state = TPACPI_LED_ON;
4672 data->new_state = TPACPI_LED_BLINK;
4674 queue_work(tpacpi_wq, &data->work);
4677 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4678 unsigned long *delay_on, unsigned long *delay_off)
4680 struct tpacpi_led_classdev *data = container_of(led_cdev,
4681 struct tpacpi_led_classdev, led_classdev);
4683 /* Can we choose the flash rate? */
4684 if (*delay_on == 0 && *delay_off == 0) {
4685 /* yes. set them to the hardware blink rate (1 Hz) */
4686 *delay_on = 500; /* ms */
4687 *delay_off = 500; /* ms */
4688 } else if ((*delay_on != 500) || (*delay_off != 500))
4691 data->new_state = TPACPI_LED_BLINK;
4692 queue_work(tpacpi_wq, &data->work);
4697 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4701 struct tpacpi_led_classdev *data = container_of(led_cdev,
4702 struct tpacpi_led_classdev, led_classdev);
4704 rc = led_get_status(data->led);
4706 if (rc == TPACPI_LED_OFF || rc < 0)
4707 rc = LED_OFF; /* no error handling in led class :( */
4714 static void led_exit(void)
4718 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4719 if (tpacpi_leds[i].led_classdev.name)
4720 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4726 static int __init tpacpi_init_led(unsigned int led)
4730 tpacpi_leds[led].led = led;
4732 /* LEDs with no name don't get registered */
4733 if (!tpacpi_led_names[led])
4736 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4737 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4738 if (led_supported == TPACPI_LED_570)
4739 tpacpi_leds[led].led_classdev.brightness_get =
4742 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4744 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4746 rc = led_classdev_register(&tpacpi_pdev->dev,
4747 &tpacpi_leds[led].led_classdev);
4749 tpacpi_leds[led].led_classdev.name = NULL;
4754 static const struct tpacpi_quirk led_useful_qtable[] __initconst = {
4755 TPACPI_Q_IBM('1', 'E', 0x009f), /* A30 */
4756 TPACPI_Q_IBM('1', 'N', 0x009f), /* A31 */
4757 TPACPI_Q_IBM('1', 'G', 0x009f), /* A31 */
4759 TPACPI_Q_IBM('1', 'I', 0x0097), /* T30 */
4760 TPACPI_Q_IBM('1', 'R', 0x0097), /* T40, T41, T42, R50, R51 */
4761 TPACPI_Q_IBM('7', '0', 0x0097), /* T43, R52 */
4762 TPACPI_Q_IBM('1', 'Y', 0x0097), /* T43 */
4763 TPACPI_Q_IBM('1', 'W', 0x0097), /* R50e */
4764 TPACPI_Q_IBM('1', 'V', 0x0097), /* R51 */
4765 TPACPI_Q_IBM('7', '8', 0x0097), /* R51e */
4766 TPACPI_Q_IBM('7', '6', 0x0097), /* R52 */
4768 TPACPI_Q_IBM('1', 'K', 0x00bf), /* X30 */
4769 TPACPI_Q_IBM('1', 'Q', 0x00bf), /* X31, X32 */
4770 TPACPI_Q_IBM('1', 'U', 0x00bf), /* X40 */
4771 TPACPI_Q_IBM('7', '4', 0x00bf), /* X41 */
4772 TPACPI_Q_IBM('7', '5', 0x00bf), /* X41t */
4774 TPACPI_Q_IBM('7', '9', 0x1f97), /* T60 (1) */
4775 TPACPI_Q_IBM('7', '7', 0x1f97), /* Z60* (1) */
4776 TPACPI_Q_IBM('7', 'F', 0x1f97), /* Z61* (1) */
4777 TPACPI_Q_IBM('7', 'B', 0x1fb7), /* X60 (1) */
4779 /* (1) - may have excess leds enabled on MSB */
4781 /* Defaults (order matters, keep last, don't reorder!) */
4783 .vendor = PCI_VENDOR_ID_LENOVO,
4784 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4787 { /* IBM ThinkPads with no EC version string */
4788 .vendor = PCI_VENDOR_ID_IBM,
4789 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_UNKNOWN,
4792 { /* IBM ThinkPads with EC version string */
4793 .vendor = PCI_VENDOR_ID_IBM,
4794 .bios = TPACPI_MATCH_ANY, .ec = TPACPI_MATCH_ANY,
4799 #undef TPACPI_LEDQ_IBM
4800 #undef TPACPI_LEDQ_LNV
4802 static int __init led_init(struct ibm_init_struct *iibm)
4806 unsigned long useful_leds;
4808 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4810 TPACPI_ACPIHANDLE_INIT(led);
4813 /* led not supported on R30, R31 */
4814 led_supported = TPACPI_LED_NONE;
4815 else if (strlencmp(led_path, "SLED") == 0)
4817 led_supported = TPACPI_LED_570;
4818 else if (strlencmp(led_path, "SYSL") == 0)
4819 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4820 led_supported = TPACPI_LED_OLD;
4823 led_supported = TPACPI_LED_NEW;
4825 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4826 str_supported(led_supported), led_supported);
4828 if (led_supported == TPACPI_LED_NONE)
4831 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4834 printk(TPACPI_ERR "Out of memory for LED data\n");
4838 useful_leds = tpacpi_check_quirks(led_useful_qtable,
4839 ARRAY_SIZE(led_useful_qtable));
4841 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4842 if (!tpacpi_is_led_restricted(i) &&
4843 test_bit(i, &useful_leds)) {
4844 rc = tpacpi_init_led(i);
4852 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4853 printk(TPACPI_NOTICE
4854 "warning: userspace override of important "
4855 "firmware LEDs is enabled\n");
4860 #define str_led_status(s) \
4861 ((s) == TPACPI_LED_OFF ? "off" : \
4862 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4864 static int led_read(char *p)
4868 if (!led_supported) {
4869 len += sprintf(p + len, "status:\t\tnot supported\n");
4872 len += sprintf(p + len, "status:\t\tsupported\n");
4874 if (led_supported == TPACPI_LED_570) {
4877 for (i = 0; i < 8; i++) {
4878 status = led_get_status(i);
4881 len += sprintf(p + len, "%d:\t\t%s\n",
4882 i, str_led_status(status));
4886 len += sprintf(p + len, "commands:\t"
4887 "<led> on, <led> off, <led> blink (<led> is 0-15)\n");
4892 static int led_write(char *buf)
4896 enum led_status_t s;
4901 while ((cmd = next_cmd(&buf))) {
4902 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 15)
4905 if (strstr(cmd, "off")) {
4907 } else if (strstr(cmd, "on")) {
4909 } else if (strstr(cmd, "blink")) {
4910 s = TPACPI_LED_BLINK;
4915 rc = led_set_status(led, s);
4923 static struct ibm_struct led_driver_data = {
4930 /*************************************************************************
4934 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4936 #define TPACPI_BEEP_Q1 0x0001
4938 static const struct tpacpi_quirk beep_quirk_table[] __initconst = {
4939 TPACPI_Q_IBM('I', 'M', TPACPI_BEEP_Q1), /* 570 */
4940 TPACPI_Q_IBM('I', 'U', TPACPI_BEEP_Q1), /* 570E - unverified */
4943 static int __init beep_init(struct ibm_init_struct *iibm)
4945 unsigned long quirks;
4947 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4949 TPACPI_ACPIHANDLE_INIT(beep);
4951 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4952 str_supported(beep_handle != NULL));
4954 quirks = tpacpi_check_quirks(beep_quirk_table,
4955 ARRAY_SIZE(beep_quirk_table));
4957 tp_features.beep_needs_two_args = !!(quirks & TPACPI_BEEP_Q1);
4959 return (beep_handle)? 0 : 1;
4962 static int beep_read(char *p)
4967 len += sprintf(p + len, "status:\t\tnot supported\n");
4969 len += sprintf(p + len, "status:\t\tsupported\n");
4970 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4976 static int beep_write(char *buf)
4984 while ((cmd = next_cmd(&buf))) {
4985 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4986 beep_cmd >= 0 && beep_cmd <= 17) {
4990 if (tp_features.beep_needs_two_args) {
4991 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd",
4995 if (!acpi_evalf(beep_handle, NULL, NULL, "vd",
5004 static struct ibm_struct beep_driver_data = {
5007 .write = beep_write,
5010 /*************************************************************************
5014 enum thermal_access_mode {
5015 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5016 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5017 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5018 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5019 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5022 enum { /* TPACPI_THERMAL_TPEC_* */
5023 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5024 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5025 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5028 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5029 struct ibm_thermal_sensors_struct {
5030 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5033 static enum thermal_access_mode thermal_read_mode;
5035 /* idx is zero-based */
5036 static int thermal_get_sensor(int idx, s32 *value)
5042 t = TP_EC_THERMAL_TMP0;
5044 switch (thermal_read_mode) {
5045 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5046 case TPACPI_THERMAL_TPEC_16:
5047 if (idx >= 8 && idx <= 15) {
5048 t = TP_EC_THERMAL_TMP8;
5053 case TPACPI_THERMAL_TPEC_8:
5055 if (!acpi_ec_read(t + idx, &tmp))
5057 *value = tmp * 1000;
5062 case TPACPI_THERMAL_ACPI_UPDT:
5064 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5065 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5067 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5069 *value = (t - 2732) * 100;
5074 case TPACPI_THERMAL_ACPI_TMP07:
5076 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5077 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5079 if (t > 127 || t < -127)
5080 t = TP_EC_THERMAL_TMP_NA;
5086 case TPACPI_THERMAL_NONE:
5094 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5105 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5108 for (i = 0 ; i < n; i++) {
5109 res = thermal_get_sensor(i, &s->temp[i]);
5117 /* sysfs temp##_input -------------------------------------------------- */
5119 static ssize_t thermal_temp_input_show(struct device *dev,
5120 struct device_attribute *attr,
5123 struct sensor_device_attribute *sensor_attr =
5124 to_sensor_dev_attr(attr);
5125 int idx = sensor_attr->index;
5129 res = thermal_get_sensor(idx, &value);
5132 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5135 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5138 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5139 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5140 thermal_temp_input_show, NULL, _idxB)
5142 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5143 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5144 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5145 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5146 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5147 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5148 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5149 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5150 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5151 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5152 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5153 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5154 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5155 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5156 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5157 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5158 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5161 #define THERMAL_ATTRS(X) \
5162 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5164 static struct attribute *thermal_temp_input_attr[] = {
5184 static const struct attribute_group thermal_temp_input16_group = {
5185 .attrs = thermal_temp_input_attr
5188 static const struct attribute_group thermal_temp_input8_group = {
5189 .attrs = &thermal_temp_input_attr[8]
5192 #undef THERMAL_SENSOR_ATTR_TEMP
5193 #undef THERMAL_ATTRS
5195 /* --------------------------------------------------------------------- */
5197 static int __init thermal_init(struct ibm_init_struct *iibm)
5204 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5206 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5208 if (thinkpad_id.ec_model) {
5210 * Direct EC access mode: sensors at registers
5211 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5212 * non-implemented, thermal sensors return 0x80 when
5217 for (i = 0; i < 8; i++) {
5218 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5224 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5232 /* This is sheer paranoia, but we handle it anyway */
5235 "ThinkPad ACPI EC access misbehaving, "
5236 "falling back to ACPI TMPx access "
5238 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5241 "ThinkPad ACPI EC access misbehaving, "
5242 "disabling thermal sensors access\n");
5243 thermal_read_mode = TPACPI_THERMAL_NONE;
5248 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5250 } else if (acpi_tmp7) {
5251 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5252 /* 600e/x, 770e, 770x */
5253 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5255 /* Standard ACPI TMPx access, max 8 sensors */
5256 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5259 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5260 thermal_read_mode = TPACPI_THERMAL_NONE;
5263 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5264 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5267 switch (thermal_read_mode) {
5268 case TPACPI_THERMAL_TPEC_16:
5269 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5270 &thermal_temp_input16_group);
5274 case TPACPI_THERMAL_TPEC_8:
5275 case TPACPI_THERMAL_ACPI_TMP07:
5276 case TPACPI_THERMAL_ACPI_UPDT:
5277 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5278 &thermal_temp_input8_group);
5282 case TPACPI_THERMAL_NONE:
5290 static void thermal_exit(void)
5292 switch (thermal_read_mode) {
5293 case TPACPI_THERMAL_TPEC_16:
5294 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5295 &thermal_temp_input16_group);
5297 case TPACPI_THERMAL_TPEC_8:
5298 case TPACPI_THERMAL_ACPI_TMP07:
5299 case TPACPI_THERMAL_ACPI_UPDT:
5300 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5301 &thermal_temp_input16_group);
5303 case TPACPI_THERMAL_NONE:
5309 static int thermal_read(char *p)
5313 struct ibm_thermal_sensors_struct t;
5315 n = thermal_get_sensors(&t);
5316 if (unlikely(n < 0))
5319 len += sprintf(p + len, "temperatures:\t");
5322 for (i = 0; i < (n - 1); i++)
5323 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5324 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5326 len += sprintf(p + len, "not supported\n");
5331 static struct ibm_struct thermal_driver_data = {
5333 .read = thermal_read,
5334 .exit = thermal_exit,
5337 /*************************************************************************
5341 static u8 ecdump_regs[256];
5343 static int ecdump_read(char *p)
5349 len += sprintf(p + len, "EC "
5350 " +00 +01 +02 +03 +04 +05 +06 +07"
5351 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5352 for (i = 0; i < 256; i += 16) {
5353 len += sprintf(p + len, "EC 0x%02x:", i);
5354 for (j = 0; j < 16; j++) {
5355 if (!acpi_ec_read(i + j, &v))
5357 if (v != ecdump_regs[i + j])
5358 len += sprintf(p + len, " *%02x", v);
5360 len += sprintf(p + len, " %02x", v);
5361 ecdump_regs[i + j] = v;
5363 len += sprintf(p + len, "\n");
5368 /* These are way too dangerous to advertise openly... */
5370 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5371 " (<offset> is 00-ff, <value> is 00-ff)\n");
5372 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5373 " (<offset> is 00-ff, <value> is 0-255)\n");
5378 static int ecdump_write(char *buf)
5383 while ((cmd = next_cmd(&buf))) {
5384 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5386 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5390 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5391 if (!acpi_ec_write(i, v))
5400 static struct ibm_struct ecdump_driver_data = {
5402 .read = ecdump_read,
5403 .write = ecdump_write,
5404 .flags.experimental = 1,
5407 /*************************************************************************
5408 * Backlight/brightness subdriver
5411 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5414 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5415 * CMOS NVRAM byte 0x5E, bits 0-3.
5417 * EC HBRV (0x31) has the following layout
5418 * Bit 7: unknown function
5419 * Bit 6: unknown function
5420 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5421 * Bit 4: must be set to zero to avoid problems
5422 * Bit 3-0: backlight brightness level
5424 * brightness_get_raw returns status data in the HBRV layout
5426 * WARNING: The X61 has been verified to use HBRV for something else, so
5427 * this should be used _only_ on IBM ThinkPads, and maybe with some careful
5428 * testing on the very early *60 Lenovo models...
5432 TP_EC_BACKLIGHT = 0x31,
5434 /* TP_EC_BACKLIGHT bitmasks */
5435 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5436 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5437 TP_EC_BACKLIGHT_MAPSW = 0x20,
5440 enum tpacpi_brightness_access_mode {
5441 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5442 TPACPI_BRGHT_MODE_EC, /* EC control */
5443 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5444 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5445 TPACPI_BRGHT_MODE_MAX
5448 static struct backlight_device *ibm_backlight_device;
5450 static enum tpacpi_brightness_access_mode brightness_mode =
5451 TPACPI_BRGHT_MODE_MAX;
5453 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5455 static struct mutex brightness_mutex;
5457 /* NVRAM brightness access,
5458 * call with brightness_mutex held! */
5459 static unsigned int tpacpi_brightness_nvram_get(void)
5463 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5464 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5465 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5466 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5471 static void tpacpi_brightness_checkpoint_nvram(void)
5476 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5479 vdbg_printk(TPACPI_DBG_BRGHT,
5480 "trying to checkpoint backlight level to NVRAM...\n");
5482 if (mutex_lock_killable(&brightness_mutex) < 0)
5485 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5487 lec &= TP_EC_BACKLIGHT_LVLMSK;
5488 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5490 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5491 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5492 /* NVRAM needs update */
5493 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5494 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5496 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5497 dbg_printk(TPACPI_DBG_BRGHT,
5498 "updated NVRAM backlight level to %u (0x%02x)\n",
5499 (unsigned int) lec, (unsigned int) b_nvram);
5501 vdbg_printk(TPACPI_DBG_BRGHT,
5502 "NVRAM backlight level already is %u (0x%02x)\n",
5503 (unsigned int) lec, (unsigned int) b_nvram);
5506 mutex_unlock(&brightness_mutex);
5510 /* call with brightness_mutex held! */
5511 static int tpacpi_brightness_get_raw(int *status)
5515 switch (brightness_mode) {
5516 case TPACPI_BRGHT_MODE_UCMS_STEP:
5517 *status = tpacpi_brightness_nvram_get();
5519 case TPACPI_BRGHT_MODE_EC:
5520 case TPACPI_BRGHT_MODE_ECNVRAM:
5521 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5530 /* call with brightness_mutex held! */
5531 /* do NOT call with illegal backlight level value */
5532 static int tpacpi_brightness_set_ec(unsigned int value)
5536 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5539 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5540 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5541 (value & TP_EC_BACKLIGHT_LVLMSK))))
5547 /* call with brightness_mutex held! */
5548 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5551 unsigned int current_value, i;
5553 current_value = tpacpi_brightness_nvram_get();
5555 if (value == current_value)
5558 cmos_cmd = (value > current_value) ?
5559 TP_CMOS_BRIGHTNESS_UP :
5560 TP_CMOS_BRIGHTNESS_DOWN;
5561 inc = (value > current_value) ? 1 : -1;
5563 for (i = current_value; i != value; i += inc)
5564 if (issue_thinkpad_cmos_command(cmos_cmd))
5570 /* May return EINTR which can always be mapped to ERESTARTSYS */
5571 static int brightness_set(unsigned int value)
5575 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5579 vdbg_printk(TPACPI_DBG_BRGHT,
5580 "set backlight level to %d\n", value);
5582 res = mutex_lock_killable(&brightness_mutex);
5586 switch (brightness_mode) {
5587 case TPACPI_BRGHT_MODE_EC:
5588 case TPACPI_BRGHT_MODE_ECNVRAM:
5589 res = tpacpi_brightness_set_ec(value);
5591 case TPACPI_BRGHT_MODE_UCMS_STEP:
5592 res = tpacpi_brightness_set_ucmsstep(value);
5598 mutex_unlock(&brightness_mutex);
5602 /* sysfs backlight class ----------------------------------------------- */
5604 static int brightness_update_status(struct backlight_device *bd)
5606 unsigned int level =
5607 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5608 bd->props.power == FB_BLANK_UNBLANK) ?
5609 bd->props.brightness : 0;
5611 dbg_printk(TPACPI_DBG_BRGHT,
5612 "backlight: attempt to set level to %d\n",
5615 /* it is the backlight class's job (caller) to handle
5616 * EINTR and other errors properly */
5617 return brightness_set(level);
5620 static int brightness_get(struct backlight_device *bd)
5624 res = mutex_lock_killable(&brightness_mutex);
5628 res = tpacpi_brightness_get_raw(&status);
5630 mutex_unlock(&brightness_mutex);
5635 return status & TP_EC_BACKLIGHT_LVLMSK;
5638 static struct backlight_ops ibm_backlight_data = {
5639 .get_brightness = brightness_get,
5640 .update_status = brightness_update_status,
5643 /* --------------------------------------------------------------------- */
5646 * These are only useful for models that have only one possibility
5647 * of GPU. If the BIOS model handles both ATI and Intel, don't use
5650 #define TPACPI_BRGHT_Q_NOEC 0x0001 /* Must NOT use EC HBRV */
5651 #define TPACPI_BRGHT_Q_EC 0x0002 /* Should or must use EC HBRV */
5652 #define TPACPI_BRGHT_Q_ASK 0x8000 /* Ask for user report */
5654 static const struct tpacpi_quirk brightness_quirk_table[] __initconst = {
5655 /* Models with ATI GPUs known to require ECNVRAM mode */
5656 TPACPI_Q_IBM('1', 'Y', TPACPI_BRGHT_Q_EC), /* T43/p ATI */
5658 /* Models with ATI GPUs (waiting confirmation) */
5659 TPACPI_Q_IBM('1', 'R', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5660 TPACPI_Q_IBM('1', 'Q', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5661 TPACPI_Q_IBM('7', '6', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5662 TPACPI_Q_IBM('7', '8', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_EC),
5664 /* Models with Intel Extreme Graphics 2 (waiting confirmation) */
5665 TPACPI_Q_IBM('1', 'V', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5666 TPACPI_Q_IBM('1', 'W', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5667 TPACPI_Q_IBM('1', 'U', TPACPI_BRGHT_Q_ASK|TPACPI_BRGHT_Q_NOEC),
5669 /* Models with Intel GMA900 */
5670 TPACPI_Q_IBM('7', '0', TPACPI_BRGHT_Q_NOEC), /* T43, R52 */
5671 TPACPI_Q_IBM('7', '4', TPACPI_BRGHT_Q_NOEC), /* X41 */
5672 TPACPI_Q_IBM('7', '5', TPACPI_BRGHT_Q_NOEC), /* X41 Tablet */
5675 static int __init brightness_init(struct ibm_init_struct *iibm)
5678 unsigned long quirks;
5680 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5682 mutex_init(&brightness_mutex);
5684 quirks = tpacpi_check_quirks(brightness_quirk_table,
5685 ARRAY_SIZE(brightness_quirk_table));
5688 * We always attempt to detect acpi support, so as to switch
5689 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5690 * going to publish a backlight interface
5692 b = tpacpi_check_std_acpi_brightness_support();
5695 if (acpi_video_backlight_support()) {
5696 if (brightness_enable > 1) {
5697 printk(TPACPI_NOTICE
5698 "Standard ACPI backlight interface "
5699 "available, not loading native one.\n");
5701 } else if (brightness_enable == 1) {
5702 printk(TPACPI_NOTICE
5703 "Backlight control force enabled, even if standard "
5704 "ACPI backlight interface is available\n");
5707 if (brightness_enable > 1) {
5708 printk(TPACPI_NOTICE
5709 "Standard ACPI backlight interface not "
5710 "available, thinkpad_acpi native "
5711 "brightness control enabled\n");
5716 if (!brightness_enable) {
5717 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5718 "brightness support disabled by "
5719 "module parameter\n");
5725 "Unsupported brightness interface, "
5726 "please contact %s\n", TPACPI_MAIL);
5730 tp_features.bright_16levels = 1;
5733 * Check for module parameter bogosity, note that we
5734 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5735 * able to detect "unspecified"
5737 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5740 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5741 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5742 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5743 if (quirks & TPACPI_BRGHT_Q_EC)
5744 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5746 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5748 dbg_printk(TPACPI_DBG_BRGHT,
5749 "driver auto-selected brightness_mode=%d\n",
5754 if (thinkpad_id.vendor != PCI_VENDOR_ID_IBM &&
5755 (brightness_mode == TPACPI_BRGHT_MODE_ECNVRAM ||
5756 brightness_mode == TPACPI_BRGHT_MODE_EC))
5759 if (tpacpi_brightness_get_raw(&b) < 0)
5762 if (tp_features.bright_16levels)
5764 "detected a 16-level brightness capable ThinkPad\n");
5766 ibm_backlight_device = backlight_device_register(
5767 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5768 &ibm_backlight_data);
5769 if (IS_ERR(ibm_backlight_device)) {
5770 printk(TPACPI_ERR "Could not register backlight device\n");
5771 return PTR_ERR(ibm_backlight_device);
5773 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5774 "brightness is supported\n");
5776 if (quirks & TPACPI_BRGHT_Q_ASK) {
5777 printk(TPACPI_NOTICE
5778 "brightness: will use unverified default: "
5779 "brightness_mode=%d\n", brightness_mode);
5780 printk(TPACPI_NOTICE
5781 "brightness: please report to %s whether it works well "
5782 "or not on your ThinkPad\n", TPACPI_MAIL);
5785 ibm_backlight_device->props.max_brightness =
5786 (tp_features.bright_16levels)? 15 : 7;
5787 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5788 backlight_update_status(ibm_backlight_device);
5793 static void brightness_suspend(pm_message_t state)
5795 tpacpi_brightness_checkpoint_nvram();
5798 static void brightness_shutdown(void)
5800 tpacpi_brightness_checkpoint_nvram();
5803 static void brightness_exit(void)
5805 if (ibm_backlight_device) {
5806 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5807 "calling backlight_device_unregister()\n");
5808 backlight_device_unregister(ibm_backlight_device);
5811 tpacpi_brightness_checkpoint_nvram();
5814 static int brightness_read(char *p)
5819 level = brightness_get(NULL);
5821 len += sprintf(p + len, "level:\t\tunreadable\n");
5823 len += sprintf(p + len, "level:\t\t%d\n", level);
5824 len += sprintf(p + len, "commands:\tup, down\n");
5825 len += sprintf(p + len, "commands:\tlevel <level>"
5826 " (<level> is 0-%d)\n",
5827 (tp_features.bright_16levels) ? 15 : 7);
5833 static int brightness_write(char *buf)
5838 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5840 level = brightness_get(NULL);
5844 while ((cmd = next_cmd(&buf))) {
5845 if (strlencmp(cmd, "up") == 0) {
5846 if (level < max_level)
5848 } else if (strlencmp(cmd, "down") == 0) {
5851 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5852 level >= 0 && level <= max_level) {
5858 tpacpi_disclose_usertask("procfs brightness",
5859 "set level to %d\n", level);
5862 * Now we know what the final level should be, so we try to set it.
5863 * Doing it this way makes the syscall restartable in case of EINTR
5865 rc = brightness_set(level);
5866 return (rc == -EINTR)? ERESTARTSYS : rc;
5869 static struct ibm_struct brightness_driver_data = {
5870 .name = "brightness",
5871 .read = brightness_read,
5872 .write = brightness_write,
5873 .exit = brightness_exit,
5874 .suspend = brightness_suspend,
5875 .shutdown = brightness_shutdown,
5878 /*************************************************************************
5882 static int volume_offset = 0x30;
5884 static int volume_read(char *p)
5889 if (!acpi_ec_read(volume_offset, &level)) {
5890 len += sprintf(p + len, "level:\t\tunreadable\n");
5892 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5893 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5894 len += sprintf(p + len, "commands:\tup, down, mute\n");
5895 len += sprintf(p + len, "commands:\tlevel <level>"
5896 " (<level> is 0-15)\n");
5902 static int volume_write(char *buf)
5904 int cmos_cmd, inc, i;
5906 int new_level, new_mute;
5909 while ((cmd = next_cmd(&buf))) {
5910 if (!acpi_ec_read(volume_offset, &level))
5912 new_mute = mute = level & 0x40;
5913 new_level = level = level & 0xf;
5915 if (strlencmp(cmd, "up") == 0) {
5919 new_level = level == 15 ? 15 : level + 1;
5920 } else if (strlencmp(cmd, "down") == 0) {
5924 new_level = level == 0 ? 0 : level - 1;
5925 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5926 new_level >= 0 && new_level <= 15) {
5928 } else if (strlencmp(cmd, "mute") == 0) {
5933 if (new_level != level) {
5934 /* mute doesn't change */
5936 cmos_cmd = (new_level > level) ?
5937 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5938 inc = new_level > level ? 1 : -1;
5940 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5941 !acpi_ec_write(volume_offset, level)))
5944 for (i = level; i != new_level; i += inc)
5945 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5946 !acpi_ec_write(volume_offset, i + inc))
5950 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5951 !acpi_ec_write(volume_offset, new_level + mute))) {
5956 if (new_mute != mute) {
5957 /* level doesn't change */
5959 cmos_cmd = (new_mute) ?
5960 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5962 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5963 !acpi_ec_write(volume_offset, level + new_mute))
5971 static struct ibm_struct volume_driver_data = {
5973 .read = volume_read,
5974 .write = volume_write,
5977 /*************************************************************************
5984 * TPACPI_FAN_RD_ACPI_GFAN:
5985 * ACPI GFAN method: returns fan level
5987 * see TPACPI_FAN_WR_ACPI_SFAN
5988 * EC 0x2f (HFSP) not available if GFAN exists
5990 * TPACPI_FAN_WR_ACPI_SFAN:
5991 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5993 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5996 * TPACPI_FAN_WR_TPEC:
5997 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5998 * Supported on almost all ThinkPads
6000 * Fan speed changes of any sort (including those caused by the
6001 * disengaged mode) are usually done slowly by the firmware as the
6002 * maximum ammount of fan duty cycle change per second seems to be
6005 * Reading is not available if GFAN exists.
6006 * Writing is not available if SFAN exists.
6009 * 7 automatic mode engaged;
6010 * (default operation mode of the ThinkPad)
6011 * fan level is ignored in this mode.
6012 * 6 full speed mode (takes precedence over bit 7);
6013 * not available on all thinkpads. May disable
6014 * the tachometer while the fan controller ramps up
6015 * the speed (which can take up to a few *minutes*).
6016 * Speeds up fan to 100% duty-cycle, which is far above
6017 * the standard RPM levels. It is not impossible that
6018 * it could cause hardware damage.
6019 * 5-3 unused in some models. Extra bits for fan level
6020 * in others, but still useless as all values above
6021 * 7 map to the same speed as level 7 in these models.
6022 * 2-0 fan level (0..7 usually)
6024 * 0x07 = max (set when temperatures critical)
6025 * Some ThinkPads may have other levels, see
6026 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
6028 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
6029 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
6030 * does so, its initial value is meaningless (0x07).
6032 * For firmware bugs, refer to:
6033 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6037 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
6038 * Main fan tachometer reading (in RPM)
6040 * This register is present on all ThinkPads with a new-style EC, and
6041 * it is known not to be present on the A21m/e, and T22, as there is
6042 * something else in offset 0x84 according to the ACPI DSDT. Other
6043 * ThinkPads from this same time period (and earlier) probably lack the
6044 * tachometer as well.
6046 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
6047 * was never fixed by IBM to report the EC firmware version string
6048 * probably support the tachometer (like the early X models), so
6049 * detecting it is quite hard. We need more data to know for sure.
6051 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
6054 * FIRMWARE BUG: may go stale while the EC is switching to full speed
6057 * For firmware bugs, refer to:
6058 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6062 * ThinkPad EC register 0x31 bit 0 (only on select models)
6064 * When bit 0 of EC register 0x31 is zero, the tachometer registers
6065 * show the speed of the main fan. When bit 0 of EC register 0x31
6066 * is one, the tachometer registers show the speed of the auxiliary
6069 * Fan control seems to affect both fans, regardless of the state
6072 * So far, only the firmware for the X60/X61 non-tablet versions
6073 * seem to support this (firmware TP-7M).
6075 * TPACPI_FAN_WR_ACPI_FANS:
6076 * ThinkPad X31, X40, X41. Not available in the X60.
6078 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6079 * high speed. ACPI DSDT seems to map these three speeds to levels
6080 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6081 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6083 * The speeds are stored on handles
6084 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6086 * There are three default speed sets, acessible as handles:
6087 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6089 * ACPI DSDT switches which set is in use depending on various
6092 * TPACPI_FAN_WR_TPEC is also available and should be used to
6093 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6094 * but the ACPI tables just mention level 7.
6097 enum { /* Fan control constants */
6098 fan_status_offset = 0x2f, /* EC register 0x2f */
6099 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6100 * 0x84 must be read before 0x85 */
6101 fan_select_offset = 0x31, /* EC register 0x31 (Firmware 7M)
6102 bit 0 selects which fan is active */
6104 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6105 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6107 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6110 enum fan_status_access_mode {
6111 TPACPI_FAN_NONE = 0, /* No fan status or control */
6112 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6113 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6116 enum fan_control_access_mode {
6117 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6118 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6119 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6120 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6123 enum fan_control_commands {
6124 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6125 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6126 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6127 * and also watchdog cmd */
6130 static int fan_control_allowed;
6132 static enum fan_status_access_mode fan_status_access_mode;
6133 static enum fan_control_access_mode fan_control_access_mode;
6134 static enum fan_control_commands fan_control_commands;
6136 static u8 fan_control_initial_status;
6137 static u8 fan_control_desired_level;
6138 static u8 fan_control_resume_level;
6139 static int fan_watchdog_maxinterval;
6141 static struct mutex fan_mutex;
6143 static void fan_watchdog_fire(struct work_struct *ignored);
6144 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6146 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6147 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6148 "\\FSPD", /* 600e/x, 770e, 770x */
6150 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6151 "JFNS", /* 770x-JL */
6155 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6156 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6157 * be in auto mode (0x80).
6159 * This is corrected by any write to HFSP either by the driver, or
6162 * We assume 0x07 really means auto mode while this quirk is active,
6163 * as this is far more likely than the ThinkPad being in level 7,
6164 * which is only used by the firmware during thermal emergencies.
6166 * Enable for TP-1Y (T43), TP-78 (R51e), TP-76 (R52),
6167 * TP-70 (T43, R52), which are known to be buggy.
6170 static void fan_quirk1_setup(void)
6172 if (fan_control_initial_status == 0x07) {
6173 printk(TPACPI_NOTICE
6174 "fan_init: initial fan status is unknown, "
6175 "assuming it is in auto mode\n");
6176 tp_features.fan_ctrl_status_undef = 1;
6180 static void fan_quirk1_handle(u8 *fan_status)
6182 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6183 if (*fan_status != fan_control_initial_status) {
6184 /* something changed the HFSP regisnter since
6185 * driver init time, so it is not undefined
6187 tp_features.fan_ctrl_status_undef = 0;
6189 /* Return most likely status. In fact, it
6190 * might be the only possible status */
6191 *fan_status = TP_EC_FAN_AUTO;
6196 /* Select main fan on X60/X61, NOOP on others */
6197 static bool fan_select_fan1(void)
6199 if (tp_features.second_fan) {
6202 if (ec_read(fan_select_offset, &val) < 0)
6205 if (ec_write(fan_select_offset, val) < 0)
6211 /* Select secondary fan on X60/X61 */
6212 static bool fan_select_fan2(void)
6216 if (!tp_features.second_fan)
6219 if (ec_read(fan_select_offset, &val) < 0)
6222 if (ec_write(fan_select_offset, val) < 0)
6229 * Call with fan_mutex held
6231 static void fan_update_desired_level(u8 status)
6234 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6236 fan_control_desired_level = 7;
6238 fan_control_desired_level = status;
6242 static int fan_get_status(u8 *status)
6247 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6249 switch (fan_status_access_mode) {
6250 case TPACPI_FAN_RD_ACPI_GFAN:
6251 /* 570, 600e/x, 770e, 770x */
6253 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6261 case TPACPI_FAN_RD_TPEC:
6262 /* all except 570, 600e/x, 770e, 770x */
6263 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6266 if (likely(status)) {
6268 fan_quirk1_handle(status);
6280 static int fan_get_status_safe(u8 *status)
6285 if (mutex_lock_killable(&fan_mutex))
6286 return -ERESTARTSYS;
6287 rc = fan_get_status(&s);
6289 fan_update_desired_level(s);
6290 mutex_unlock(&fan_mutex);
6298 static int fan_get_speed(unsigned int *speed)
6302 switch (fan_status_access_mode) {
6303 case TPACPI_FAN_RD_TPEC:
6304 /* all except 570, 600e/x, 770e, 770x */
6305 if (unlikely(!fan_select_fan1()))
6307 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6308 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6312 *speed = (hi << 8) | lo;
6323 static int fan2_get_speed(unsigned int *speed)
6328 switch (fan_status_access_mode) {
6329 case TPACPI_FAN_RD_TPEC:
6330 /* all except 570, 600e/x, 770e, 770x */
6331 if (unlikely(!fan_select_fan2()))
6333 rc = !acpi_ec_read(fan_rpm_offset, &lo) ||
6334 !acpi_ec_read(fan_rpm_offset + 1, &hi);
6335 fan_select_fan1(); /* play it safe */
6340 *speed = (hi << 8) | lo;
6351 static int fan_set_level(int level)
6353 if (!fan_control_allowed)
6356 switch (fan_control_access_mode) {
6357 case TPACPI_FAN_WR_ACPI_SFAN:
6358 if (level >= 0 && level <= 7) {
6359 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6365 case TPACPI_FAN_WR_ACPI_FANS:
6366 case TPACPI_FAN_WR_TPEC:
6367 if (!(level & TP_EC_FAN_AUTO) &&
6368 !(level & TP_EC_FAN_FULLSPEED) &&
6369 ((level < 0) || (level > 7)))
6372 /* safety net should the EC not support AUTO
6373 * or FULLSPEED mode bits and just ignore them */
6374 if (level & TP_EC_FAN_FULLSPEED)
6375 level |= 7; /* safety min speed 7 */
6376 else if (level & TP_EC_FAN_AUTO)
6377 level |= 4; /* safety min speed 4 */
6379 if (!acpi_ec_write(fan_status_offset, level))
6382 tp_features.fan_ctrl_status_undef = 0;
6389 vdbg_printk(TPACPI_DBG_FAN,
6390 "fan control: set fan control register to 0x%02x\n", level);
6394 static int fan_set_level_safe(int level)
6398 if (!fan_control_allowed)
6401 if (mutex_lock_killable(&fan_mutex))
6402 return -ERESTARTSYS;
6404 if (level == TPACPI_FAN_LAST_LEVEL)
6405 level = fan_control_desired_level;
6407 rc = fan_set_level(level);
6409 fan_update_desired_level(level);
6411 mutex_unlock(&fan_mutex);
6415 static int fan_set_enable(void)
6420 if (!fan_control_allowed)
6423 if (mutex_lock_killable(&fan_mutex))
6424 return -ERESTARTSYS;
6426 switch (fan_control_access_mode) {
6427 case TPACPI_FAN_WR_ACPI_FANS:
6428 case TPACPI_FAN_WR_TPEC:
6429 rc = fan_get_status(&s);
6433 /* Don't go out of emergency fan mode */
6436 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6439 if (!acpi_ec_write(fan_status_offset, s))
6442 tp_features.fan_ctrl_status_undef = 0;
6447 case TPACPI_FAN_WR_ACPI_SFAN:
6448 rc = fan_get_status(&s);
6454 /* Set fan to at least level 4 */
6457 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6467 mutex_unlock(&fan_mutex);
6470 vdbg_printk(TPACPI_DBG_FAN,
6471 "fan control: set fan control register to 0x%02x\n",
6476 static int fan_set_disable(void)
6480 if (!fan_control_allowed)
6483 if (mutex_lock_killable(&fan_mutex))
6484 return -ERESTARTSYS;
6487 switch (fan_control_access_mode) {
6488 case TPACPI_FAN_WR_ACPI_FANS:
6489 case TPACPI_FAN_WR_TPEC:
6490 if (!acpi_ec_write(fan_status_offset, 0x00))
6493 fan_control_desired_level = 0;
6494 tp_features.fan_ctrl_status_undef = 0;
6498 case TPACPI_FAN_WR_ACPI_SFAN:
6499 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6502 fan_control_desired_level = 0;
6510 vdbg_printk(TPACPI_DBG_FAN,
6511 "fan control: set fan control register to 0\n");
6513 mutex_unlock(&fan_mutex);
6517 static int fan_set_speed(int speed)
6521 if (!fan_control_allowed)
6524 if (mutex_lock_killable(&fan_mutex))
6525 return -ERESTARTSYS;
6528 switch (fan_control_access_mode) {
6529 case TPACPI_FAN_WR_ACPI_FANS:
6530 if (speed >= 0 && speed <= 65535) {
6531 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6532 speed, speed, speed))
6542 mutex_unlock(&fan_mutex);
6546 static void fan_watchdog_reset(void)
6548 static int fan_watchdog_active;
6550 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6553 if (fan_watchdog_active)
6554 cancel_delayed_work(&fan_watchdog_task);
6556 if (fan_watchdog_maxinterval > 0 &&
6557 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6558 fan_watchdog_active = 1;
6559 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6560 msecs_to_jiffies(fan_watchdog_maxinterval
6563 "failed to queue the fan watchdog, "
6564 "watchdog will not trigger\n");
6567 fan_watchdog_active = 0;
6570 static void fan_watchdog_fire(struct work_struct *ignored)
6574 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6577 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6578 rc = fan_set_enable();
6580 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6581 "will try again later...\n", -rc);
6582 /* reschedule for later */
6583 fan_watchdog_reset();
6588 * SYSFS fan layout: hwmon compatible (device)
6591 * 0: "disengaged" mode
6593 * 2: native EC "auto" mode (recommended, hardware default)
6595 * pwm*: set speed in manual mode, ignored otherwise.
6596 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6599 * fan*_input: tachometer reading, RPM
6602 * SYSFS fan layout: extensions
6604 * fan_watchdog (driver):
6605 * fan watchdog interval in seconds, 0 disables (default), max 120
6608 /* sysfs fan pwm1_enable ----------------------------------------------- */
6609 static ssize_t fan_pwm1_enable_show(struct device *dev,
6610 struct device_attribute *attr,
6616 res = fan_get_status_safe(&status);
6620 if (status & TP_EC_FAN_FULLSPEED) {
6622 } else if (status & TP_EC_FAN_AUTO) {
6627 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6630 static ssize_t fan_pwm1_enable_store(struct device *dev,
6631 struct device_attribute *attr,
6632 const char *buf, size_t count)
6637 if (parse_strtoul(buf, 2, &t))
6640 tpacpi_disclose_usertask("hwmon pwm1_enable",
6641 "set fan mode to %lu\n", t);
6645 level = TP_EC_FAN_FULLSPEED;
6648 level = TPACPI_FAN_LAST_LEVEL;
6651 level = TP_EC_FAN_AUTO;
6654 /* reserved for software-controlled auto mode */
6660 res = fan_set_level_safe(level);
6666 fan_watchdog_reset();
6671 static struct device_attribute dev_attr_fan_pwm1_enable =
6672 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6673 fan_pwm1_enable_show, fan_pwm1_enable_store);
6675 /* sysfs fan pwm1 ------------------------------------------------------ */
6676 static ssize_t fan_pwm1_show(struct device *dev,
6677 struct device_attribute *attr,
6683 res = fan_get_status_safe(&status);
6688 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6689 status = fan_control_desired_level;
6694 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6697 static ssize_t fan_pwm1_store(struct device *dev,
6698 struct device_attribute *attr,
6699 const char *buf, size_t count)
6703 u8 status, newlevel;
6705 if (parse_strtoul(buf, 255, &s))
6708 tpacpi_disclose_usertask("hwmon pwm1",
6709 "set fan speed to %lu\n", s);
6711 /* scale down from 0-255 to 0-7 */
6712 newlevel = (s >> 5) & 0x07;
6714 if (mutex_lock_killable(&fan_mutex))
6715 return -ERESTARTSYS;
6717 rc = fan_get_status(&status);
6718 if (!rc && (status &
6719 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6720 rc = fan_set_level(newlevel);
6724 fan_update_desired_level(newlevel);
6725 fan_watchdog_reset();
6729 mutex_unlock(&fan_mutex);
6730 return (rc)? rc : count;
6733 static struct device_attribute dev_attr_fan_pwm1 =
6734 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6735 fan_pwm1_show, fan_pwm1_store);
6737 /* sysfs fan fan1_input ------------------------------------------------ */
6738 static ssize_t fan_fan1_input_show(struct device *dev,
6739 struct device_attribute *attr,
6745 res = fan_get_speed(&speed);
6749 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6752 static struct device_attribute dev_attr_fan_fan1_input =
6753 __ATTR(fan1_input, S_IRUGO,
6754 fan_fan1_input_show, NULL);
6756 /* sysfs fan fan2_input ------------------------------------------------ */
6757 static ssize_t fan_fan2_input_show(struct device *dev,
6758 struct device_attribute *attr,
6764 res = fan2_get_speed(&speed);
6768 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6771 static struct device_attribute dev_attr_fan_fan2_input =
6772 __ATTR(fan2_input, S_IRUGO,
6773 fan_fan2_input_show, NULL);
6775 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6776 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6779 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6782 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6783 const char *buf, size_t count)
6787 if (parse_strtoul(buf, 120, &t))
6790 if (!fan_control_allowed)
6793 fan_watchdog_maxinterval = t;
6794 fan_watchdog_reset();
6796 tpacpi_disclose_usertask("fan_watchdog", "set to %lu\n", t);
6801 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6802 fan_fan_watchdog_show, fan_fan_watchdog_store);
6804 /* --------------------------------------------------------------------- */
6805 static struct attribute *fan_attributes[] = {
6806 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6807 &dev_attr_fan_fan1_input.attr,
6808 NULL, /* for fan2_input */
6812 static const struct attribute_group fan_attr_group = {
6813 .attrs = fan_attributes,
6816 #define TPACPI_FAN_Q1 0x0001 /* Unitialized HFSP */
6817 #define TPACPI_FAN_2FAN 0x0002 /* EC 0x31 bit 0 selects fan2 */
6819 #define TPACPI_FAN_QI(__id1, __id2, __quirks) \
6820 { .vendor = PCI_VENDOR_ID_IBM, \
6821 .bios = TPACPI_MATCH_ANY, \
6822 .ec = TPID(__id1, __id2), \
6823 .quirks = __quirks }
6825 #define TPACPI_FAN_QL(__id1, __id2, __quirks) \
6826 { .vendor = PCI_VENDOR_ID_LENOVO, \
6827 .bios = TPACPI_MATCH_ANY, \
6828 .ec = TPID(__id1, __id2), \
6829 .quirks = __quirks }
6831 static const struct tpacpi_quirk fan_quirk_table[] __initconst = {
6832 TPACPI_FAN_QI('1', 'Y', TPACPI_FAN_Q1),
6833 TPACPI_FAN_QI('7', '8', TPACPI_FAN_Q1),
6834 TPACPI_FAN_QI('7', '6', TPACPI_FAN_Q1),
6835 TPACPI_FAN_QI('7', '0', TPACPI_FAN_Q1),
6836 TPACPI_FAN_QL('7', 'M', TPACPI_FAN_2FAN),
6839 #undef TPACPI_FAN_QL
6840 #undef TPACPI_FAN_QI
6842 static int __init fan_init(struct ibm_init_struct *iibm)
6845 unsigned long quirks;
6847 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6848 "initializing fan subdriver\n");
6850 mutex_init(&fan_mutex);
6851 fan_status_access_mode = TPACPI_FAN_NONE;
6852 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6853 fan_control_commands = 0;
6854 fan_watchdog_maxinterval = 0;
6855 tp_features.fan_ctrl_status_undef = 0;
6856 tp_features.second_fan = 0;
6857 fan_control_desired_level = 7;
6859 TPACPI_ACPIHANDLE_INIT(fans);
6860 TPACPI_ACPIHANDLE_INIT(gfan);
6861 TPACPI_ACPIHANDLE_INIT(sfan);
6863 quirks = tpacpi_check_quirks(fan_quirk_table,
6864 ARRAY_SIZE(fan_quirk_table));
6867 /* 570, 600e/x, 770e, 770x */
6868 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6870 /* all other ThinkPads: note that even old-style
6871 * ThinkPad ECs supports the fan control register */
6872 if (likely(acpi_ec_read(fan_status_offset,
6873 &fan_control_initial_status))) {
6874 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6875 if (quirks & TPACPI_FAN_Q1)
6877 if (quirks & TPACPI_FAN_2FAN) {
6878 tp_features.second_fan = 1;
6879 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6880 "secondary fan support enabled\n");
6884 "ThinkPad ACPI EC access misbehaving, "
6885 "fan status and control unavailable\n");
6892 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6893 fan_control_commands |=
6894 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6897 /* gfan without sfan means no fan control */
6898 /* all other models implement TP EC 0x2f control */
6902 fan_control_access_mode =
6903 TPACPI_FAN_WR_ACPI_FANS;
6904 fan_control_commands |=
6905 TPACPI_FAN_CMD_SPEED |
6906 TPACPI_FAN_CMD_LEVEL |
6907 TPACPI_FAN_CMD_ENABLE;
6909 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6910 fan_control_commands |=
6911 TPACPI_FAN_CMD_LEVEL |
6912 TPACPI_FAN_CMD_ENABLE;
6917 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6918 "fan is %s, modes %d, %d\n",
6919 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6920 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6921 fan_status_access_mode, fan_control_access_mode);
6923 /* fan control master switch */
6924 if (!fan_control_allowed) {
6925 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6926 fan_control_commands = 0;
6927 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_FAN,
6928 "fan control features disabled by parameter\n");
6931 /* update fan_control_desired_level */
6932 if (fan_status_access_mode != TPACPI_FAN_NONE)
6933 fan_get_status_safe(NULL);
6935 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6936 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6937 if (tp_features.second_fan) {
6938 /* attach second fan tachometer */
6939 fan_attributes[ARRAY_SIZE(fan_attributes)-2] =
6940 &dev_attr_fan_fan2_input.attr;
6942 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6947 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6948 &driver_attr_fan_watchdog);
6950 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6959 static void fan_exit(void)
6961 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_FAN,
6962 "cancelling any pending fan watchdog tasks\n");
6964 /* FIXME: can we really do this unconditionally? */
6965 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6966 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6967 &driver_attr_fan_watchdog);
6969 cancel_delayed_work(&fan_watchdog_task);
6970 flush_workqueue(tpacpi_wq);
6973 static void fan_suspend(pm_message_t state)
6977 if (!fan_control_allowed)
6980 /* Store fan status in cache */
6981 fan_control_resume_level = 0;
6982 rc = fan_get_status_safe(&fan_control_resume_level);
6984 printk(TPACPI_NOTICE
6985 "failed to read fan level for later "
6986 "restore during resume: %d\n", rc);
6988 /* if it is undefined, don't attempt to restore it.
6990 if (tp_features.fan_ctrl_status_undef)
6991 fan_control_resume_level = 0;
6994 static void fan_resume(void)
6996 u8 current_level = 7;
6997 bool do_set = false;
7000 /* DSDT *always* updates status on resume */
7001 tp_features.fan_ctrl_status_undef = 0;
7003 if (!fan_control_allowed ||
7004 !fan_control_resume_level ||
7005 (fan_get_status_safe(¤t_level) < 0))
7008 switch (fan_control_access_mode) {
7009 case TPACPI_FAN_WR_ACPI_SFAN:
7010 /* never decrease fan level */
7011 do_set = (fan_control_resume_level > current_level);
7013 case TPACPI_FAN_WR_ACPI_FANS:
7014 case TPACPI_FAN_WR_TPEC:
7015 /* never decrease fan level, scale is:
7016 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
7018 * We expect the firmware to set either 7 or AUTO, but we
7019 * handle FULLSPEED out of paranoia.
7021 * So, we can safely only restore FULLSPEED or 7, anything
7022 * else could slow the fan. Restoring AUTO is useless, at
7023 * best that's exactly what the DSDT already set (it is the
7026 * Always keep in mind that the DSDT *will* have set the
7027 * fans to what the vendor supposes is the best level. We
7028 * muck with it only to speed the fan up.
7030 if (fan_control_resume_level != 7 &&
7031 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
7034 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
7035 (current_level != fan_control_resume_level);
7041 printk(TPACPI_NOTICE
7042 "restoring fan level to 0x%02x\n",
7043 fan_control_resume_level);
7044 rc = fan_set_level_safe(fan_control_resume_level);
7046 printk(TPACPI_NOTICE
7047 "failed to restore fan level: %d\n", rc);
7051 static int fan_read(char *p)
7056 unsigned int speed = 0;
7058 switch (fan_status_access_mode) {
7059 case TPACPI_FAN_RD_ACPI_GFAN:
7060 /* 570, 600e/x, 770e, 770x */
7061 rc = fan_get_status_safe(&status);
7065 len += sprintf(p + len, "status:\t\t%s\n"
7067 (status != 0) ? "enabled" : "disabled", status);
7070 case TPACPI_FAN_RD_TPEC:
7071 /* all except 570, 600e/x, 770e, 770x */
7072 rc = fan_get_status_safe(&status);
7076 len += sprintf(p + len, "status:\t\t%s\n",
7077 (status != 0) ? "enabled" : "disabled");
7079 rc = fan_get_speed(&speed);
7083 len += sprintf(p + len, "speed:\t\t%d\n", speed);
7085 if (status & TP_EC_FAN_FULLSPEED)
7086 /* Disengaged mode takes precedence */
7087 len += sprintf(p + len, "level:\t\tdisengaged\n");
7088 else if (status & TP_EC_FAN_AUTO)
7089 len += sprintf(p + len, "level:\t\tauto\n");
7091 len += sprintf(p + len, "level:\t\t%d\n", status);
7094 case TPACPI_FAN_NONE:
7096 len += sprintf(p + len, "status:\t\tnot supported\n");
7099 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
7100 len += sprintf(p + len, "commands:\tlevel <level>");
7102 switch (fan_control_access_mode) {
7103 case TPACPI_FAN_WR_ACPI_SFAN:
7104 len += sprintf(p + len, " (<level> is 0-7)\n");
7108 len += sprintf(p + len, " (<level> is 0-7, "
7109 "auto, disengaged, full-speed)\n");
7114 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
7115 len += sprintf(p + len, "commands:\tenable, disable\n"
7116 "commands:\twatchdog <timeout> (<timeout> "
7117 "is 0 (off), 1-120 (seconds))\n");
7119 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
7120 len += sprintf(p + len, "commands:\tspeed <speed>"
7121 " (<speed> is 0-65535)\n");
7126 static int fan_write_cmd_level(const char *cmd, int *rc)
7130 if (strlencmp(cmd, "level auto") == 0)
7131 level = TP_EC_FAN_AUTO;
7132 else if ((strlencmp(cmd, "level disengaged") == 0) |
7133 (strlencmp(cmd, "level full-speed") == 0))
7134 level = TP_EC_FAN_FULLSPEED;
7135 else if (sscanf(cmd, "level %d", &level) != 1)
7138 *rc = fan_set_level_safe(level);
7140 printk(TPACPI_ERR "level command accepted for unsupported "
7141 "access mode %d", fan_control_access_mode);
7143 tpacpi_disclose_usertask("procfs fan",
7144 "set level to %d\n", level);
7149 static int fan_write_cmd_enable(const char *cmd, int *rc)
7151 if (strlencmp(cmd, "enable") != 0)
7154 *rc = fan_set_enable();
7156 printk(TPACPI_ERR "enable command accepted for unsupported "
7157 "access mode %d", fan_control_access_mode);
7159 tpacpi_disclose_usertask("procfs fan", "enable\n");
7164 static int fan_write_cmd_disable(const char *cmd, int *rc)
7166 if (strlencmp(cmd, "disable") != 0)
7169 *rc = fan_set_disable();
7171 printk(TPACPI_ERR "disable command accepted for unsupported "
7172 "access mode %d", fan_control_access_mode);
7174 tpacpi_disclose_usertask("procfs fan", "disable\n");
7179 static int fan_write_cmd_speed(const char *cmd, int *rc)
7184 * Support speed <low> <medium> <high> ? */
7186 if (sscanf(cmd, "speed %d", &speed) != 1)
7189 *rc = fan_set_speed(speed);
7191 printk(TPACPI_ERR "speed command accepted for unsupported "
7192 "access mode %d", fan_control_access_mode);
7194 tpacpi_disclose_usertask("procfs fan",
7195 "set speed to %d\n", speed);
7200 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
7204 if (sscanf(cmd, "watchdog %d", &interval) != 1)
7207 if (interval < 0 || interval > 120)
7210 fan_watchdog_maxinterval = interval;
7211 tpacpi_disclose_usertask("procfs fan",
7212 "set watchdog timer to %d\n",
7219 static int fan_write(char *buf)
7224 while (!rc && (cmd = next_cmd(&buf))) {
7225 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7226 fan_write_cmd_level(cmd, &rc)) &&
7227 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7228 (fan_write_cmd_enable(cmd, &rc) ||
7229 fan_write_cmd_disable(cmd, &rc) ||
7230 fan_write_cmd_watchdog(cmd, &rc))) &&
7231 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7232 fan_write_cmd_speed(cmd, &rc))
7236 fan_watchdog_reset();
7242 static struct ibm_struct fan_driver_data = {
7247 .suspend = fan_suspend,
7248 .resume = fan_resume,
7251 /****************************************************************************
7252 ****************************************************************************
7256 ****************************************************************************
7257 ****************************************************************************/
7259 /* sysfs name ---------------------------------------------------------- */
7260 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7261 struct device_attribute *attr,
7264 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7267 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7268 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7270 /* --------------------------------------------------------------------- */
7273 static struct proc_dir_entry *proc_dir;
7276 * Module and infrastructure proble, init and exit handling
7279 static int force_load;
7281 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7282 static const char * __init str_supported(int is_supported)
7284 static char text_unsupported[] __initdata = "not supported";
7286 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7288 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7290 static void ibm_exit(struct ibm_struct *ibm)
7292 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7294 list_del_init(&ibm->all_drivers);
7296 if (ibm->flags.acpi_notify_installed) {
7297 dbg_printk(TPACPI_DBG_EXIT,
7298 "%s: acpi_remove_notify_handler\n", ibm->name);
7300 acpi_remove_notify_handler(*ibm->acpi->handle,
7302 dispatch_acpi_notify);
7303 ibm->flags.acpi_notify_installed = 0;
7304 ibm->flags.acpi_notify_installed = 0;
7307 if (ibm->flags.proc_created) {
7308 dbg_printk(TPACPI_DBG_EXIT,
7309 "%s: remove_proc_entry\n", ibm->name);
7310 remove_proc_entry(ibm->name, proc_dir);
7311 ibm->flags.proc_created = 0;
7314 if (ibm->flags.acpi_driver_registered) {
7315 dbg_printk(TPACPI_DBG_EXIT,
7316 "%s: acpi_bus_unregister_driver\n", ibm->name);
7318 acpi_bus_unregister_driver(ibm->acpi->driver);
7319 kfree(ibm->acpi->driver);
7320 ibm->acpi->driver = NULL;
7321 ibm->flags.acpi_driver_registered = 0;
7324 if (ibm->flags.init_called && ibm->exit) {
7326 ibm->flags.init_called = 0;
7329 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7332 static int __init ibm_init(struct ibm_init_struct *iibm)
7335 struct ibm_struct *ibm = iibm->data;
7336 struct proc_dir_entry *entry;
7338 BUG_ON(ibm == NULL);
7340 INIT_LIST_HEAD(&ibm->all_drivers);
7342 if (ibm->flags.experimental && !experimental)
7345 dbg_printk(TPACPI_DBG_INIT,
7346 "probing for %s\n", ibm->name);
7349 ret = iibm->init(iibm);
7351 return 0; /* probe failed */
7355 ibm->flags.init_called = 1;
7359 if (ibm->acpi->hid) {
7360 ret = register_tpacpi_subdriver(ibm);
7365 if (ibm->acpi->notify) {
7366 ret = setup_acpi_notify(ibm);
7367 if (ret == -ENODEV) {
7368 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7378 dbg_printk(TPACPI_DBG_INIT,
7379 "%s installed\n", ibm->name);
7382 entry = create_proc_entry(ibm->name,
7383 S_IFREG | S_IRUGO | S_IWUSR,
7386 printk(TPACPI_ERR "unable to create proc entry %s\n",
7392 entry->read_proc = &dispatch_procfs_read;
7394 entry->write_proc = &dispatch_procfs_write;
7395 ibm->flags.proc_created = 1;
7398 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7403 dbg_printk(TPACPI_DBG_INIT,
7404 "%s: at error exit path with result %d\n",
7408 return (ret < 0)? ret : 0;
7413 static bool __pure __init tpacpi_is_fw_digit(const char c)
7415 return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z');
7418 /* Most models: xxyTkkWW (#.##c); Ancient 570/600 and -SL lacks (#.##c) */
7419 static bool __pure __init tpacpi_is_valid_fw_id(const char* const s,
7422 return s && strlen(s) >= 8 &&
7423 tpacpi_is_fw_digit(s[0]) &&
7424 tpacpi_is_fw_digit(s[1]) &&
7425 s[2] == t && s[3] == 'T' &&
7426 tpacpi_is_fw_digit(s[4]) &&
7427 tpacpi_is_fw_digit(s[5]) &&
7428 s[6] == 'W' && s[7] == 'W';
7431 /* returns 0 - probe ok, or < 0 - probe error.
7432 * Probe ok doesn't mean thinkpad found.
7433 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7434 static int __must_check __init get_thinkpad_model_data(
7435 struct thinkpad_id_data *tp)
7437 const struct dmi_device *dev = NULL;
7438 char ec_fw_string[18];
7444 memset(tp, 0, sizeof(*tp));
7446 if (dmi_name_in_vendors("IBM"))
7447 tp->vendor = PCI_VENDOR_ID_IBM;
7448 else if (dmi_name_in_vendors("LENOVO"))
7449 tp->vendor = PCI_VENDOR_ID_LENOVO;
7453 s = dmi_get_system_info(DMI_BIOS_VERSION);
7454 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7455 if (s && !tp->bios_version_str)
7458 /* Really ancient ThinkPad 240X will fail this, which is fine */
7459 if (!tpacpi_is_valid_fw_id(tp->bios_version_str, 'E'))
7462 tp->bios_model = tp->bios_version_str[0]
7463 | (tp->bios_version_str[1] << 8);
7464 tp->bios_release = (tp->bios_version_str[4] << 8)
7465 | tp->bios_version_str[5];
7468 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7469 * X32 or newer, all Z series; Some models must have an
7470 * up-to-date BIOS or they will not be detected.
7472 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7474 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7475 if (sscanf(dev->name,
7476 "IBM ThinkPad Embedded Controller -[%17c",
7477 ec_fw_string) == 1) {
7478 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7479 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7481 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7482 if (!tp->ec_version_str)
7485 if (tpacpi_is_valid_fw_id(ec_fw_string, 'H')) {
7486 tp->ec_model = ec_fw_string[0]
7487 | (ec_fw_string[1] << 8);
7488 tp->ec_release = (ec_fw_string[4] << 8)
7491 printk(TPACPI_NOTICE
7492 "ThinkPad firmware release %s "
7493 "doesn't match the known patterns\n",
7495 printk(TPACPI_NOTICE
7496 "please report this to %s\n",
7503 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7504 if (s && !strnicmp(s, "ThinkPad", 8)) {
7505 tp->model_str = kstrdup(s, GFP_KERNEL);
7510 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7511 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7512 if (s && !tp->nummodel_str)
7518 static int __init probe_for_thinkpad(void)
7526 * Non-ancient models have better DMI tagging, but very old models
7529 is_thinkpad = (thinkpad_id.model_str != NULL);
7531 /* ec is required because many other handles are relative to it */
7532 TPACPI_ACPIHANDLE_INIT(ec);
7536 "Not yet supported ThinkPad detected!\n");
7541 * Risks a regression on very old machines, but reduces potential
7542 * false positives a damn great deal
7545 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7547 if (!is_thinkpad && !force_load)
7554 /* Module init, exit, parameters */
7556 static struct ibm_init_struct ibms_init[] __initdata = {
7558 .init = thinkpad_acpi_driver_init,
7559 .data = &thinkpad_acpi_driver_data,
7562 .init = hotkey_init,
7563 .data = &hotkey_driver_data,
7566 .init = bluetooth_init,
7567 .data = &bluetooth_driver_data,
7571 .data = &wan_driver_data,
7575 .data = &uwb_driver_data,
7577 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7580 .data = &video_driver_data,
7585 .data = &light_driver_data,
7589 .data = &cmos_driver_data,
7593 .data = &led_driver_data,
7597 .data = &beep_driver_data,
7600 .init = thermal_init,
7601 .data = &thermal_driver_data,
7604 .data = &ecdump_driver_data,
7607 .init = brightness_init,
7608 .data = &brightness_driver_data,
7611 .data = &volume_driver_data,
7615 .data = &fan_driver_data,
7619 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7622 struct ibm_struct *ibm;
7624 if (!kp || !kp->name || !val)
7627 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7628 ibm = ibms_init[i].data;
7629 WARN_ON(ibm == NULL);
7631 if (!ibm || !ibm->name)
7634 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7635 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7637 strcpy(ibms_init[i].param, val);
7638 strcat(ibms_init[i].param, ",");
7646 module_param(experimental, int, 0);
7647 MODULE_PARM_DESC(experimental,
7648 "Enables experimental features when non-zero");
7650 module_param_named(debug, dbg_level, uint, 0);
7651 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7653 module_param(force_load, bool, 0);
7654 MODULE_PARM_DESC(force_load,
7655 "Attempts to load the driver even on a "
7656 "mis-identified ThinkPad when true");
7658 module_param_named(fan_control, fan_control_allowed, bool, 0);
7659 MODULE_PARM_DESC(fan_control,
7660 "Enables setting fan parameters features when true");
7662 module_param_named(brightness_mode, brightness_mode, uint, 0);
7663 MODULE_PARM_DESC(brightness_mode,
7664 "Selects brightness control strategy: "
7665 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7667 module_param(brightness_enable, uint, 0);
7668 MODULE_PARM_DESC(brightness_enable,
7669 "Enables backlight control when 1, disables when 0");
7671 module_param(hotkey_report_mode, uint, 0);
7672 MODULE_PARM_DESC(hotkey_report_mode,
7673 "used for backwards compatibility with userspace, "
7674 "see documentation");
7676 #define TPACPI_PARAM(feature) \
7677 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7678 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7679 "at module load, see documentation")
7681 TPACPI_PARAM(hotkey);
7682 TPACPI_PARAM(bluetooth);
7683 TPACPI_PARAM(video);
7684 TPACPI_PARAM(light);
7688 TPACPI_PARAM(ecdump);
7689 TPACPI_PARAM(brightness);
7690 TPACPI_PARAM(volume);
7693 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7694 module_param(dbg_wlswemul, uint, 0);
7695 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7696 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7697 MODULE_PARM_DESC(wlsw_state,
7698 "Initial state of the emulated WLSW switch");
7700 module_param(dbg_bluetoothemul, uint, 0);
7701 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7702 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7703 MODULE_PARM_DESC(bluetooth_state,
7704 "Initial state of the emulated bluetooth switch");
7706 module_param(dbg_wwanemul, uint, 0);
7707 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7708 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7709 MODULE_PARM_DESC(wwan_state,
7710 "Initial state of the emulated WWAN switch");
7712 module_param(dbg_uwbemul, uint, 0);
7713 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7714 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7715 MODULE_PARM_DESC(uwb_state,
7716 "Initial state of the emulated UWB switch");
7719 static void thinkpad_acpi_module_exit(void)
7721 struct ibm_struct *ibm, *itmp;
7723 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7725 list_for_each_entry_safe_reverse(ibm, itmp,
7726 &tpacpi_all_drivers,
7731 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7733 if (tpacpi_inputdev) {
7734 if (tp_features.input_device_registered)
7735 input_unregister_device(tpacpi_inputdev);
7737 input_free_device(tpacpi_inputdev);
7741 hwmon_device_unregister(tpacpi_hwmon);
7743 if (tp_features.sensors_pdev_attrs_registered)
7744 device_remove_file(&tpacpi_sensors_pdev->dev,
7745 &dev_attr_thinkpad_acpi_pdev_name);
7746 if (tpacpi_sensors_pdev)
7747 platform_device_unregister(tpacpi_sensors_pdev);
7749 platform_device_unregister(tpacpi_pdev);
7751 if (tp_features.sensors_pdrv_attrs_registered)
7752 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7753 if (tp_features.platform_drv_attrs_registered)
7754 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7756 if (tp_features.sensors_pdrv_registered)
7757 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7759 if (tp_features.platform_drv_registered)
7760 platform_driver_unregister(&tpacpi_pdriver);
7763 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7766 destroy_workqueue(tpacpi_wq);
7768 kfree(thinkpad_id.bios_version_str);
7769 kfree(thinkpad_id.ec_version_str);
7770 kfree(thinkpad_id.model_str);
7774 static int __init thinkpad_acpi_module_init(void)
7778 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7780 /* Parameter checking */
7781 if (hotkey_report_mode > 2)
7784 /* Driver-level probe */
7786 ret = get_thinkpad_model_data(&thinkpad_id);
7789 "unable to get DMI data: %d\n", ret);
7790 thinkpad_acpi_module_exit();
7793 ret = probe_for_thinkpad();
7795 thinkpad_acpi_module_exit();
7799 /* Driver initialization */
7801 TPACPI_ACPIHANDLE_INIT(ecrd);
7802 TPACPI_ACPIHANDLE_INIT(ecwr);
7804 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7806 thinkpad_acpi_module_exit();
7810 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7813 "unable to create proc dir " TPACPI_PROC_DIR);
7814 thinkpad_acpi_module_exit();
7818 ret = platform_driver_register(&tpacpi_pdriver);
7821 "unable to register main platform driver\n");
7822 thinkpad_acpi_module_exit();
7825 tp_features.platform_drv_registered = 1;
7827 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7830 "unable to register hwmon platform driver\n");
7831 thinkpad_acpi_module_exit();
7834 tp_features.sensors_pdrv_registered = 1;
7836 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7838 tp_features.platform_drv_attrs_registered = 1;
7839 ret = tpacpi_create_driver_attributes(
7840 &tpacpi_hwmon_pdriver.driver);
7844 "unable to create sysfs driver attributes\n");
7845 thinkpad_acpi_module_exit();
7848 tp_features.sensors_pdrv_attrs_registered = 1;
7851 /* Device initialization */
7852 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7854 if (IS_ERR(tpacpi_pdev)) {
7855 ret = PTR_ERR(tpacpi_pdev);
7857 printk(TPACPI_ERR "unable to register platform device\n");
7858 thinkpad_acpi_module_exit();
7861 tpacpi_sensors_pdev = platform_device_register_simple(
7862 TPACPI_HWMON_DRVR_NAME,
7864 if (IS_ERR(tpacpi_sensors_pdev)) {
7865 ret = PTR_ERR(tpacpi_sensors_pdev);
7866 tpacpi_sensors_pdev = NULL;
7868 "unable to register hwmon platform device\n");
7869 thinkpad_acpi_module_exit();
7872 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7873 &dev_attr_thinkpad_acpi_pdev_name);
7876 "unable to create sysfs hwmon device attributes\n");
7877 thinkpad_acpi_module_exit();
7880 tp_features.sensors_pdev_attrs_registered = 1;
7881 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7882 if (IS_ERR(tpacpi_hwmon)) {
7883 ret = PTR_ERR(tpacpi_hwmon);
7884 tpacpi_hwmon = NULL;
7885 printk(TPACPI_ERR "unable to register hwmon device\n");
7886 thinkpad_acpi_module_exit();
7889 mutex_init(&tpacpi_inputdev_send_mutex);
7890 tpacpi_inputdev = input_allocate_device();
7891 if (!tpacpi_inputdev) {
7892 printk(TPACPI_ERR "unable to allocate input device\n");
7893 thinkpad_acpi_module_exit();
7896 /* Prepare input device, but don't register */
7897 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7898 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7899 tpacpi_inputdev->id.bustype = BUS_HOST;
7900 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7901 thinkpad_id.vendor :
7903 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7904 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7906 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7907 ret = ibm_init(&ibms_init[i]);
7908 if (ret >= 0 && *ibms_init[i].param)
7909 ret = ibms_init[i].data->write(ibms_init[i].param);
7911 thinkpad_acpi_module_exit();
7915 ret = input_register_device(tpacpi_inputdev);
7917 printk(TPACPI_ERR "unable to register input device\n");
7918 thinkpad_acpi_module_exit();
7921 tp_features.input_device_registered = 1;
7924 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7928 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7931 * This will autoload the driver in almost every ThinkPad
7932 * in widespread use.
7934 * Only _VERY_ old models, like the 240, 240x and 570 lack
7935 * the HKEY event interface.
7937 MODULE_DEVICE_TABLE(acpi, ibm_htk_device_ids);
7940 * DMI matching for module autoloading
7942 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7943 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7945 * Only models listed in thinkwiki will be supported, so add yours
7946 * if it is not there yet.
7948 #define IBM_BIOS_MODULE_ALIAS(__type) \
7949 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7951 /* Ancient thinkpad BIOSes have to be identified by
7952 * BIOS type or model number, and there are far less
7953 * BIOS types than model numbers... */
7954 IBM_BIOS_MODULE_ALIAS("I[MU]"); /* 570, 570e */
7956 MODULE_AUTHOR("Borislav Deianov <borislav@users.sf.net>");
7957 MODULE_AUTHOR("Henrique de Moraes Holschuh <hmh@hmh.eng.br>");
7958 MODULE_DESCRIPTION(TPACPI_DESC);
7959 MODULE_VERSION(TPACPI_VERSION);
7960 MODULE_LICENSE("GPL");
7962 module_init(thinkpad_acpi_module_init);
7963 module_exit(thinkpad_acpi_module_exit);