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.22"
25 #define TPACPI_SYSFS_VERSION 0x020300
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
169 /* rfkill switches */
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
194 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
195 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
196 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
199 /****************************************************************************
200 * Driver-wide structs and misc. variables
205 struct tp_acpi_drv_struct {
206 const struct acpi_device_id *hid;
207 struct acpi_driver *driver;
209 void (*notify) (struct ibm_struct *, u32);
212 struct acpi_device *device;
218 int (*read) (char *);
219 int (*write) (char *);
221 void (*resume) (void);
222 void (*suspend) (pm_message_t state);
223 void (*shutdown) (void);
225 struct list_head all_drivers;
227 struct tp_acpi_drv_struct *acpi;
230 u8 acpi_driver_registered:1;
231 u8 acpi_notify_installed:1;
238 struct ibm_init_struct {
241 int (*init) (struct ibm_init_struct *);
242 struct ibm_struct *data;
246 #ifdef CONFIG_THINKPAD_ACPI_BAY
259 u32 bright_16levels:1;
260 u32 bright_acpimode:1;
263 u32 fan_ctrl_status_undef:1;
264 u32 input_device_registered:1;
265 u32 platform_drv_registered:1;
266 u32 platform_drv_attrs_registered:1;
267 u32 sensors_pdrv_registered:1;
268 u32 sensors_pdrv_attrs_registered:1;
269 u32 sensors_pdev_attrs_registered:1;
270 u32 hotkey_poll_active:1;
274 u16 hotkey_mask_ff:1;
275 u16 bright_cmos_ec_unsync:1;
278 struct thinkpad_id_data {
279 unsigned int vendor; /* ThinkPad vendor:
280 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
282 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
283 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
285 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
294 TPACPI_LIFE_INIT = 0,
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 /* Special LED class that can defer work */
305 struct tpacpi_led_classdev {
306 struct led_classdev led_classdev;
307 struct work_struct work;
308 enum led_brightness new_brightness;
312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
313 static int dbg_wlswemul;
314 static int tpacpi_wlsw_emulstate;
315 static int dbg_bluetoothemul;
316 static int tpacpi_bluetooth_emulstate;
317 static int dbg_wwanemul;
318 static int tpacpi_wwan_emulstate;
319 static int dbg_uwbemul;
320 static int tpacpi_uwb_emulstate;
324 /*************************************************************************
328 #define dbg_printk(a_dbg_level, format, arg...) \
329 do { if (dbg_level & (a_dbg_level)) \
330 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
333 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
334 #define vdbg_printk dbg_printk
335 static const char *str_supported(int is_supported);
337 #define vdbg_printk(a_dbg_level, format, arg...) \
341 static void tpacpi_log_usertask(const char * const what)
343 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
344 what, task_tgid_vnr(current));
347 #define tpacpi_disclose_usertask(what, format, arg...) \
350 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
351 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
352 printk(TPACPI_DEBUG "%s: PID %d: " format, \
353 what, task_tgid_vnr(current), ## arg); \
357 /****************************************************************************
358 ****************************************************************************
360 * ACPI Helpers and device model
362 ****************************************************************************
363 ****************************************************************************/
365 /*************************************************************************
369 static acpi_handle root_handle;
371 #define TPACPI_HANDLE(object, parent, paths...) \
372 static acpi_handle object##_handle; \
373 static acpi_handle *object##_parent = &parent##_handle; \
374 static char *object##_path; \
375 static char *object##_paths[] = { paths }
377 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
378 "\\_SB.PCI.ISA.EC", /* 570 */
379 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
380 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
381 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
382 "\\_SB.PCI0.ICH3.EC0", /* R31 */
383 "\\_SB.PCI0.LPC.EC", /* all others */
386 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
387 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
389 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
391 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
392 "\\CMS", /* R40, R40e */
395 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
396 "^HKEY", /* R30, R31 */
397 "HKEY", /* all others */
400 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
401 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
402 "\\_SB.PCI0.VID0", /* 770e */
403 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
404 "\\_SB.PCI0.AGP.VID", /* all others */
408 /*************************************************************************
412 static int acpi_evalf(acpi_handle handle,
413 void *res, char *method, char *fmt, ...)
416 struct acpi_object_list params;
417 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
418 struct acpi_buffer result, *resultp;
419 union acpi_object out_obj;
427 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
440 params.pointer = &in_objs[0];
447 in_objs[params.count].integer.value = va_arg(ap, int);
448 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
450 /* add more types as needed */
452 printk(TPACPI_ERR "acpi_evalf() called "
453 "with invalid format character '%c'\n", c);
459 if (res_type != 'v') {
460 result.length = sizeof(out_obj);
461 result.pointer = &out_obj;
466 status = acpi_evaluate_object(handle, method, ¶ms, resultp);
471 *(int *)res = out_obj.integer.value;
472 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
475 success = status == AE_OK;
477 /* add more types as needed */
479 printk(TPACPI_ERR "acpi_evalf() called "
480 "with invalid format character '%c'\n", res_type);
484 if (!success && !quiet)
485 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
486 method, fmt0, status);
491 static int acpi_ec_read(int i, u8 *p)
496 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
500 if (ec_read(i, p) < 0)
507 static int acpi_ec_write(int i, u8 v)
510 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
513 if (ec_write(i, v) < 0)
520 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
521 static int _sta(acpi_handle handle)
525 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
532 static int issue_thinkpad_cmos_command(int cmos_cmd)
537 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
543 /*************************************************************************
547 #define TPACPI_ACPIHANDLE_INIT(object) \
548 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
549 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
551 static void drv_acpi_handle_init(char *name,
552 acpi_handle *handle, acpi_handle parent,
553 char **paths, int num_paths, char **path)
558 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
561 for (i = 0; i < num_paths; i++) {
562 status = acpi_get_handle(parent, paths[i], handle);
563 if (ACPI_SUCCESS(status)) {
565 dbg_printk(TPACPI_DBG_INIT,
566 "Found ACPI handle %s for %s\n",
572 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
577 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
579 struct ibm_struct *ibm = data;
581 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
584 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
587 ibm->acpi->notify(ibm, event);
590 static int __init setup_acpi_notify(struct ibm_struct *ibm)
597 if (!*ibm->acpi->handle)
600 vdbg_printk(TPACPI_DBG_INIT,
601 "setting up ACPI notify for %s\n", ibm->name);
603 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
605 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
610 ibm->acpi->device->driver_data = ibm;
611 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
612 TPACPI_ACPI_EVENT_PREFIX,
615 status = acpi_install_notify_handler(*ibm->acpi->handle,
616 ibm->acpi->type, dispatch_acpi_notify, ibm);
617 if (ACPI_FAILURE(status)) {
618 if (status == AE_ALREADY_EXISTS) {
620 "another device driver is already "
621 "handling %s events\n", ibm->name);
624 "acpi_install_notify_handler(%s) failed: %d\n",
629 ibm->flags.acpi_notify_installed = 1;
633 static int __init tpacpi_device_add(struct acpi_device *device)
638 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
642 dbg_printk(TPACPI_DBG_INIT,
643 "registering %s as an ACPI driver\n", ibm->name);
647 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
648 if (!ibm->acpi->driver) {
650 "failed to allocate memory for ibm->acpi->driver\n");
654 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
655 ibm->acpi->driver->ids = ibm->acpi->hid;
657 ibm->acpi->driver->ops.add = &tpacpi_device_add;
659 rc = acpi_bus_register_driver(ibm->acpi->driver);
661 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
663 kfree(ibm->acpi->driver);
664 ibm->acpi->driver = NULL;
666 ibm->flags.acpi_driver_registered = 1;
672 /****************************************************************************
673 ****************************************************************************
677 ****************************************************************************
678 ****************************************************************************/
680 static int dispatch_procfs_read(char *page, char **start, off_t off,
681 int count, int *eof, void *data)
683 struct ibm_struct *ibm = data;
686 if (!ibm || !ibm->read)
689 len = ibm->read(page);
693 if (len <= off + count)
705 static int dispatch_procfs_write(struct file *file,
706 const char __user *userbuf,
707 unsigned long count, void *data)
709 struct ibm_struct *ibm = data;
713 if (!ibm || !ibm->write)
716 kernbuf = kmalloc(count + 2, GFP_KERNEL);
720 if (copy_from_user(kernbuf, userbuf, count)) {
726 strcat(kernbuf, ",");
727 ret = ibm->write(kernbuf);
736 static char *next_cmd(char **cmds)
741 while ((end = strchr(start, ',')) && end == start)
753 /****************************************************************************
754 ****************************************************************************
756 * Device model: input, hwmon and platform
758 ****************************************************************************
759 ****************************************************************************/
761 static struct platform_device *tpacpi_pdev;
762 static struct platform_device *tpacpi_sensors_pdev;
763 static struct device *tpacpi_hwmon;
764 static struct input_dev *tpacpi_inputdev;
765 static struct mutex tpacpi_inputdev_send_mutex;
766 static LIST_HEAD(tpacpi_all_drivers);
768 static int tpacpi_suspend_handler(struct platform_device *pdev,
771 struct ibm_struct *ibm, *itmp;
773 list_for_each_entry_safe(ibm, itmp,
777 (ibm->suspend)(state);
783 static int tpacpi_resume_handler(struct platform_device *pdev)
785 struct ibm_struct *ibm, *itmp;
787 list_for_each_entry_safe(ibm, itmp,
797 static void tpacpi_shutdown_handler(struct platform_device *pdev)
799 struct ibm_struct *ibm, *itmp;
801 list_for_each_entry_safe(ibm, itmp,
809 static struct platform_driver tpacpi_pdriver = {
811 .name = TPACPI_DRVR_NAME,
812 .owner = THIS_MODULE,
814 .suspend = tpacpi_suspend_handler,
815 .resume = tpacpi_resume_handler,
816 .shutdown = tpacpi_shutdown_handler,
819 static struct platform_driver tpacpi_hwmon_pdriver = {
821 .name = TPACPI_HWMON_DRVR_NAME,
822 .owner = THIS_MODULE,
826 /*************************************************************************
827 * sysfs support helpers
830 struct attribute_set {
831 unsigned int members, max_members;
832 struct attribute_group group;
835 struct attribute_set_obj {
836 struct attribute_set s;
838 } __attribute__((packed));
840 static struct attribute_set *create_attr_set(unsigned int max_members,
843 struct attribute_set_obj *sobj;
845 if (max_members == 0)
848 /* Allocates space for implicit NULL at the end too */
849 sobj = kzalloc(sizeof(struct attribute_set_obj) +
850 max_members * sizeof(struct attribute *),
854 sobj->s.max_members = max_members;
855 sobj->s.group.attrs = &sobj->a;
856 sobj->s.group.name = name;
861 #define destroy_attr_set(_set) \
864 /* not multi-threaded safe, use it in a single thread per set */
865 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
870 if (s->members >= s->max_members)
873 s->group.attrs[s->members] = attr;
879 static int add_many_to_attr_set(struct attribute_set *s,
880 struct attribute **attr,
885 for (i = 0; i < count; i++) {
886 res = add_to_attr_set(s, attr[i]);
894 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
896 sysfs_remove_group(kobj, &s->group);
900 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
901 sysfs_create_group(_kobj, &_attr_set->group)
903 static int parse_strtoul(const char *buf,
904 unsigned long max, unsigned long *value)
908 while (*buf && isspace(*buf))
910 *value = simple_strtoul(buf, &endp, 0);
911 while (*endp && isspace(*endp))
913 if (*endp || *value > max)
919 static void tpacpi_disable_brightness_delay(void)
921 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
923 "ACPI backlight control delay disabled\n");
926 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
928 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
929 union acpi_object *obj;
932 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
933 obj = (union acpi_object *)buffer.pointer;
934 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
935 printk(TPACPI_ERR "Unknown _BCL data, "
936 "please report this to %s\n", TPACPI_MAIL);
939 rc = obj->package.count;
945 kfree(buffer.pointer);
949 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
950 u32 lvl, void *context, void **rv)
952 char name[ACPI_PATH_SEGMENT_LENGTH];
953 struct acpi_buffer buffer = { sizeof(name), &name };
955 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
956 !strncmp("_BCL", name, sizeof(name) - 1)) {
958 **(int **)rv = tpacpi_query_bcl_levels(handle);
959 return AE_CTRL_TERMINATE;
966 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
968 static int __init tpacpi_check_std_acpi_brightness_support(void)
972 void *bcl_ptr = &bcl_levels;
975 TPACPI_ACPIHANDLE_INIT(vid);
981 * Search for a _BCL method, and execute it. This is safe on all
982 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
983 * BIOS in ACPI backlight control mode. We do NOT have to care
984 * about calling the _BCL method in an enabled video device, any
985 * will do for our purposes.
988 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
989 tpacpi_acpi_walk_find_bcl, NULL,
992 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
993 tp_features.bright_acpimode = 1;
994 return (bcl_levels - 2);
1000 static int __init tpacpi_new_rfkill(const unsigned int id,
1001 struct rfkill **rfk,
1002 const enum rfkill_type rfktype,
1004 const bool set_default,
1005 int (*toggle_radio)(void *, enum rfkill_state),
1006 int (*get_state)(void *, enum rfkill_state *))
1009 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1011 res = get_state(NULL, &initial_state);
1014 "failed to read initial state for %s, error %d; "
1015 "will turn radio off\n", name, res);
1016 } else if (set_default) {
1017 /* try to set the initial state as the default for the rfkill
1018 * type, since we ask the firmware to preserve it across S5 in
1020 if (rfkill_set_default(rfktype,
1021 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1022 RFKILL_STATE_UNBLOCKED :
1023 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1024 vdbg_printk(TPACPI_DBG_RFKILL,
1025 "Default state for %s cannot be changed\n",
1029 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1032 "failed to allocate memory for rfkill class\n");
1036 (*rfk)->name = name;
1037 (*rfk)->get_state = get_state;
1038 (*rfk)->toggle_radio = toggle_radio;
1039 (*rfk)->state = initial_state;
1041 res = rfkill_register(*rfk);
1044 "failed to register %s rfkill switch: %d\n",
1054 static void printk_deprecated_attribute(const char * const what,
1055 const char * const details)
1057 tpacpi_log_usertask("deprecated sysfs attribute");
1058 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1059 "will be removed. %s\n",
1063 static void printk_deprecated_rfkill_attribute(const char * const what)
1065 printk_deprecated_attribute(what,
1066 "Please switch to generic rfkill before year 2010");
1069 /*************************************************************************
1070 * thinkpad-acpi driver attributes
1073 /* interface_version --------------------------------------------------- */
1074 static ssize_t tpacpi_driver_interface_version_show(
1075 struct device_driver *drv,
1078 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1081 static DRIVER_ATTR(interface_version, S_IRUGO,
1082 tpacpi_driver_interface_version_show, NULL);
1084 /* debug_level --------------------------------------------------------- */
1085 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1088 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1091 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1092 const char *buf, size_t count)
1096 if (parse_strtoul(buf, 0xffff, &t))
1104 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1105 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1107 /* version ------------------------------------------------------------- */
1108 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1111 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1112 TPACPI_DESC, TPACPI_VERSION);
1115 static DRIVER_ATTR(version, S_IRUGO,
1116 tpacpi_driver_version_show, NULL);
1118 /* --------------------------------------------------------------------- */
1120 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1122 static void tpacpi_send_radiosw_update(void);
1124 /* wlsw_emulstate ------------------------------------------------------ */
1125 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1128 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1131 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1132 const char *buf, size_t count)
1136 if (parse_strtoul(buf, 1, &t))
1139 if (tpacpi_wlsw_emulstate != t) {
1140 tpacpi_wlsw_emulstate = !!t;
1141 tpacpi_send_radiosw_update();
1143 tpacpi_wlsw_emulstate = !!t;
1148 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1149 tpacpi_driver_wlsw_emulstate_show,
1150 tpacpi_driver_wlsw_emulstate_store);
1152 /* bluetooth_emulstate ------------------------------------------------- */
1153 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1154 struct device_driver *drv,
1157 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1160 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1161 struct device_driver *drv,
1162 const char *buf, size_t count)
1166 if (parse_strtoul(buf, 1, &t))
1169 tpacpi_bluetooth_emulstate = !!t;
1174 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1175 tpacpi_driver_bluetooth_emulstate_show,
1176 tpacpi_driver_bluetooth_emulstate_store);
1178 /* wwan_emulstate ------------------------------------------------- */
1179 static ssize_t tpacpi_driver_wwan_emulstate_show(
1180 struct device_driver *drv,
1183 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1186 static ssize_t tpacpi_driver_wwan_emulstate_store(
1187 struct device_driver *drv,
1188 const char *buf, size_t count)
1192 if (parse_strtoul(buf, 1, &t))
1195 tpacpi_wwan_emulstate = !!t;
1200 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1201 tpacpi_driver_wwan_emulstate_show,
1202 tpacpi_driver_wwan_emulstate_store);
1204 /* uwb_emulstate ------------------------------------------------- */
1205 static ssize_t tpacpi_driver_uwb_emulstate_show(
1206 struct device_driver *drv,
1209 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1212 static ssize_t tpacpi_driver_uwb_emulstate_store(
1213 struct device_driver *drv,
1214 const char *buf, size_t count)
1218 if (parse_strtoul(buf, 1, &t))
1221 tpacpi_uwb_emulstate = !!t;
1226 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1227 tpacpi_driver_uwb_emulstate_show,
1228 tpacpi_driver_uwb_emulstate_store);
1231 /* --------------------------------------------------------------------- */
1233 static struct driver_attribute *tpacpi_driver_attributes[] = {
1234 &driver_attr_debug_level, &driver_attr_version,
1235 &driver_attr_interface_version,
1238 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1244 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1245 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1249 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1250 if (!res && dbg_wlswemul)
1251 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1252 if (!res && dbg_bluetoothemul)
1253 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1254 if (!res && dbg_wwanemul)
1255 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1256 if (!res && dbg_uwbemul)
1257 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1263 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1267 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1268 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1270 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1271 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1272 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1273 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1274 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1278 /****************************************************************************
1279 ****************************************************************************
1283 ****************************************************************************
1284 ****************************************************************************/
1286 /*************************************************************************
1287 * thinkpad-acpi init subdriver
1290 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1292 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1293 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1295 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1296 (thinkpad_id.bios_version_str) ?
1297 thinkpad_id.bios_version_str : "unknown",
1298 (thinkpad_id.ec_version_str) ?
1299 thinkpad_id.ec_version_str : "unknown");
1301 if (thinkpad_id.vendor && thinkpad_id.model_str)
1302 printk(TPACPI_INFO "%s %s, model %s\n",
1303 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1304 "IBM" : ((thinkpad_id.vendor ==
1305 PCI_VENDOR_ID_LENOVO) ?
1306 "Lenovo" : "Unknown vendor"),
1307 thinkpad_id.model_str,
1308 (thinkpad_id.nummodel_str) ?
1309 thinkpad_id.nummodel_str : "unknown");
1314 static int thinkpad_acpi_driver_read(char *p)
1318 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1319 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1324 static struct ibm_struct thinkpad_acpi_driver_data = {
1326 .read = thinkpad_acpi_driver_read,
1329 /*************************************************************************
1333 enum { /* hot key scan codes (derived from ACPI DSDT) */
1334 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1335 TP_ACPI_HOTKEYSCAN_FNF2,
1336 TP_ACPI_HOTKEYSCAN_FNF3,
1337 TP_ACPI_HOTKEYSCAN_FNF4,
1338 TP_ACPI_HOTKEYSCAN_FNF5,
1339 TP_ACPI_HOTKEYSCAN_FNF6,
1340 TP_ACPI_HOTKEYSCAN_FNF7,
1341 TP_ACPI_HOTKEYSCAN_FNF8,
1342 TP_ACPI_HOTKEYSCAN_FNF9,
1343 TP_ACPI_HOTKEYSCAN_FNF10,
1344 TP_ACPI_HOTKEYSCAN_FNF11,
1345 TP_ACPI_HOTKEYSCAN_FNF12,
1346 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1347 TP_ACPI_HOTKEYSCAN_FNINSERT,
1348 TP_ACPI_HOTKEYSCAN_FNDELETE,
1349 TP_ACPI_HOTKEYSCAN_FNHOME,
1350 TP_ACPI_HOTKEYSCAN_FNEND,
1351 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1352 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1353 TP_ACPI_HOTKEYSCAN_FNSPACE,
1354 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1355 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1356 TP_ACPI_HOTKEYSCAN_MUTE,
1357 TP_ACPI_HOTKEYSCAN_THINKPAD,
1360 enum { /* Keys available through NVRAM polling */
1361 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1362 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1365 enum { /* Positions of some of the keys in hotkey masks */
1366 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1367 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1368 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1369 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1370 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1371 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1372 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1373 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1374 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1375 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1376 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1379 enum { /* NVRAM to ACPI HKEY group map */
1380 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1381 TP_ACPI_HKEY_ZOOM_MASK |
1382 TP_ACPI_HKEY_DISPSWTCH_MASK |
1383 TP_ACPI_HKEY_HIBERNATE_MASK,
1384 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1385 TP_ACPI_HKEY_BRGHTDWN_MASK,
1386 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1387 TP_ACPI_HKEY_VOLDWN_MASK |
1388 TP_ACPI_HKEY_MUTE_MASK,
1391 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1392 struct tp_nvram_state {
1393 u16 thinkpad_toggle:1;
1395 u16 display_toggle:1;
1396 u16 thinklight_toggle:1;
1397 u16 hibernate_toggle:1;
1398 u16 displayexp_toggle:1;
1399 u16 display_state:1;
1400 u16 brightness_toggle:1;
1401 u16 volume_toggle:1;
1404 u8 brightness_level;
1408 static struct task_struct *tpacpi_hotkey_task;
1409 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1410 static int hotkey_poll_freq = 10; /* Hz */
1411 static struct mutex hotkey_thread_mutex;
1412 static struct mutex hotkey_thread_data_mutex;
1413 static unsigned int hotkey_config_change;
1415 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1417 #define hotkey_source_mask 0U
1419 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1421 static struct mutex hotkey_mutex;
1423 static enum { /* Reasons for waking up */
1424 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1425 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1426 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1427 } hotkey_wakeup_reason;
1429 static int hotkey_autosleep_ack;
1431 static u32 hotkey_orig_mask;
1432 static u32 hotkey_all_mask;
1433 static u32 hotkey_reserved_mask;
1434 static u32 hotkey_mask;
1436 static unsigned int hotkey_report_mode;
1438 static u16 *hotkey_keycode_map;
1440 static struct attribute_set *hotkey_dev_attributes;
1442 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1443 #define HOTKEY_CONFIG_CRITICAL_START \
1445 mutex_lock(&hotkey_thread_data_mutex); \
1446 hotkey_config_change++; \
1448 #define HOTKEY_CONFIG_CRITICAL_END \
1449 mutex_unlock(&hotkey_thread_data_mutex);
1451 #define HOTKEY_CONFIG_CRITICAL_START
1452 #define HOTKEY_CONFIG_CRITICAL_END
1453 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1455 /* HKEY.MHKG() return bits */
1456 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1458 static int hotkey_get_wlsw(int *status)
1460 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1462 *status = !!tpacpi_wlsw_emulstate;
1466 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1471 static int hotkey_get_tablet_mode(int *status)
1475 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1478 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1483 * Call with hotkey_mutex held
1485 static int hotkey_mask_get(void)
1489 if (tp_features.hotkey_mask) {
1490 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1493 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1499 * Call with hotkey_mutex held
1501 static int hotkey_mask_set(u32 mask)
1506 if (tp_features.hotkey_mask) {
1507 if (!tp_warned.hotkey_mask_ff &&
1508 (mask == 0xffff || mask == 0xffffff ||
1509 mask == 0xffffffff)) {
1510 tp_warned.hotkey_mask_ff = 1;
1511 printk(TPACPI_NOTICE
1512 "setting the hotkey mask to 0x%08x is likely "
1513 "not the best way to go about it\n", mask);
1514 printk(TPACPI_NOTICE
1515 "please consider using the driver defaults, "
1516 "and refer to up-to-date thinkpad-acpi "
1520 HOTKEY_CONFIG_CRITICAL_START
1521 for (i = 0; i < 32; i++) {
1523 /* enable in firmware mask only keys not in NVRAM
1524 * mode, but enable the key in the cached hotkey_mask
1525 * regardless of mode, or the key will end up
1526 * disabled by hotkey_mask_get() */
1527 if (!acpi_evalf(hkey_handle,
1528 NULL, "MHKM", "vdd", i + 1,
1529 !!((mask & ~hotkey_source_mask) & m))) {
1533 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1536 HOTKEY_CONFIG_CRITICAL_END
1538 /* hotkey_mask_get must be called unconditionally below */
1539 if (!hotkey_mask_get() && !rc &&
1540 (hotkey_mask & ~hotkey_source_mask) !=
1541 (mask & ~hotkey_source_mask)) {
1542 printk(TPACPI_NOTICE
1543 "requested hot key mask 0x%08x, but "
1544 "firmware forced it to 0x%08x\n",
1548 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1549 HOTKEY_CONFIG_CRITICAL_START
1550 hotkey_mask = mask & hotkey_source_mask;
1551 HOTKEY_CONFIG_CRITICAL_END
1553 if (hotkey_mask != mask) {
1554 printk(TPACPI_NOTICE
1555 "requested hot key mask 0x%08x, "
1556 "forced to 0x%08x (NVRAM poll mask is "
1557 "0x%08x): no firmware mask support\n",
1558 mask, hotkey_mask, hotkey_source_mask);
1563 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1569 static int hotkey_status_get(int *status)
1571 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1577 static int hotkey_status_set(bool enable)
1579 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1585 static void tpacpi_input_send_tabletsw(void)
1589 if (tp_features.hotkey_tablet &&
1590 !hotkey_get_tablet_mode(&state)) {
1591 mutex_lock(&tpacpi_inputdev_send_mutex);
1593 input_report_switch(tpacpi_inputdev,
1594 SW_TABLET_MODE, !!state);
1595 input_sync(tpacpi_inputdev);
1597 mutex_unlock(&tpacpi_inputdev_send_mutex);
1601 static void tpacpi_input_send_key(unsigned int scancode)
1603 unsigned int keycode;
1605 keycode = hotkey_keycode_map[scancode];
1607 if (keycode != KEY_RESERVED) {
1608 mutex_lock(&tpacpi_inputdev_send_mutex);
1610 input_report_key(tpacpi_inputdev, keycode, 1);
1611 if (keycode == KEY_UNKNOWN)
1612 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1614 input_sync(tpacpi_inputdev);
1616 input_report_key(tpacpi_inputdev, keycode, 0);
1617 if (keycode == KEY_UNKNOWN)
1618 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1620 input_sync(tpacpi_inputdev);
1622 mutex_unlock(&tpacpi_inputdev_send_mutex);
1626 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1627 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1629 static void tpacpi_hotkey_send_key(unsigned int scancode)
1631 tpacpi_input_send_key(scancode);
1632 if (hotkey_report_mode < 2) {
1633 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1634 0x80, 0x1001 + scancode);
1638 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1642 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1643 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1644 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1645 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1646 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1647 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1649 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1650 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1651 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1653 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1654 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1655 n->displayexp_toggle =
1656 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1658 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1659 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1660 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1661 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1662 n->brightness_toggle =
1663 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1665 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1666 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1667 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1668 >> TP_NVRAM_POS_LEVEL_VOLUME;
1669 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1670 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1674 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1676 if ((mask & (1 << __scancode)) && \
1677 oldn->__member != newn->__member) \
1678 tpacpi_hotkey_send_key(__scancode); \
1681 #define TPACPI_MAY_SEND_KEY(__scancode) \
1682 do { if (mask & (1 << __scancode)) \
1683 tpacpi_hotkey_send_key(__scancode); } while (0)
1685 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1686 struct tp_nvram_state *newn,
1689 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1690 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1691 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1694 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1696 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1699 if (oldn->volume_toggle != newn->volume_toggle) {
1700 if (oldn->mute != newn->mute) {
1701 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1703 if (oldn->volume_level > newn->volume_level) {
1704 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1705 } else if (oldn->volume_level < newn->volume_level) {
1706 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1707 } else if (oldn->mute == newn->mute) {
1708 /* repeated key presses that didn't change state */
1710 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1711 } else if (newn->volume_level != 0) {
1712 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1714 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1719 /* handle brightness */
1720 if (oldn->brightness_toggle != newn->brightness_toggle) {
1721 if (oldn->brightness_level < newn->brightness_level) {
1722 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1723 } else if (oldn->brightness_level > newn->brightness_level) {
1724 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1726 /* repeated key presses that didn't change state */
1727 if (newn->brightness_level != 0) {
1728 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1730 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1736 #undef TPACPI_COMPARE_KEY
1737 #undef TPACPI_MAY_SEND_KEY
1739 static int hotkey_kthread(void *data)
1741 struct tp_nvram_state s[2];
1743 unsigned int si, so;
1745 unsigned int change_detector, must_reset;
1747 mutex_lock(&hotkey_thread_mutex);
1749 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1758 /* Initial state for compares */
1759 mutex_lock(&hotkey_thread_data_mutex);
1760 change_detector = hotkey_config_change;
1761 mask = hotkey_source_mask & hotkey_mask;
1762 mutex_unlock(&hotkey_thread_data_mutex);
1763 hotkey_read_nvram(&s[so], mask);
1765 while (!kthread_should_stop() && hotkey_poll_freq) {
1767 t = 1000/hotkey_poll_freq;
1768 t = msleep_interruptible(t);
1769 if (unlikely(kthread_should_stop()))
1771 must_reset = try_to_freeze();
1772 if (t > 0 && !must_reset)
1775 mutex_lock(&hotkey_thread_data_mutex);
1776 if (must_reset || hotkey_config_change != change_detector) {
1777 /* forget old state on thaw or config change */
1780 change_detector = hotkey_config_change;
1782 mask = hotkey_source_mask & hotkey_mask;
1783 mutex_unlock(&hotkey_thread_data_mutex);
1786 hotkey_read_nvram(&s[si], mask);
1787 if (likely(si != so)) {
1788 hotkey_compare_and_issue_event(&s[so], &s[si],
1798 mutex_unlock(&hotkey_thread_mutex);
1802 static void hotkey_poll_stop_sync(void)
1804 if (tpacpi_hotkey_task) {
1805 if (frozen(tpacpi_hotkey_task) ||
1806 freezing(tpacpi_hotkey_task))
1807 thaw_process(tpacpi_hotkey_task);
1809 kthread_stop(tpacpi_hotkey_task);
1810 tpacpi_hotkey_task = NULL;
1811 mutex_lock(&hotkey_thread_mutex);
1812 /* at this point, the thread did exit */
1813 mutex_unlock(&hotkey_thread_mutex);
1817 /* call with hotkey_mutex held */
1818 static void hotkey_poll_setup(int may_warn)
1820 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1821 hotkey_poll_freq > 0 &&
1822 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1823 if (!tpacpi_hotkey_task) {
1824 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1825 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1826 if (IS_ERR(tpacpi_hotkey_task)) {
1827 tpacpi_hotkey_task = NULL;
1829 "could not create kernel thread "
1830 "for hotkey polling\n");
1834 hotkey_poll_stop_sync();
1836 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1837 printk(TPACPI_NOTICE
1838 "hot keys 0x%08x require polling, "
1839 "which is currently disabled\n",
1840 hotkey_source_mask);
1845 static void hotkey_poll_setup_safe(int may_warn)
1847 mutex_lock(&hotkey_mutex);
1848 hotkey_poll_setup(may_warn);
1849 mutex_unlock(&hotkey_mutex);
1852 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1854 static void hotkey_poll_setup_safe(int __unused)
1858 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1860 static int hotkey_inputdev_open(struct input_dev *dev)
1862 switch (tpacpi_lifecycle) {
1863 case TPACPI_LIFE_INIT:
1865 * hotkey_init will call hotkey_poll_setup_safe
1866 * at the appropriate moment
1869 case TPACPI_LIFE_EXITING:
1871 case TPACPI_LIFE_RUNNING:
1872 hotkey_poll_setup_safe(0);
1876 /* Should only happen if tpacpi_lifecycle is corrupt */
1881 static void hotkey_inputdev_close(struct input_dev *dev)
1883 /* disable hotkey polling when possible */
1884 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1885 hotkey_poll_setup_safe(0);
1888 /* sysfs hotkey enable ------------------------------------------------- */
1889 static ssize_t hotkey_enable_show(struct device *dev,
1890 struct device_attribute *attr,
1895 printk_deprecated_attribute("hotkey_enable",
1896 "Hotkey reporting is always enabled");
1898 res = hotkey_status_get(&status);
1902 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1905 static ssize_t hotkey_enable_store(struct device *dev,
1906 struct device_attribute *attr,
1907 const char *buf, size_t count)
1911 printk_deprecated_attribute("hotkey_enable",
1912 "Hotkeys can be disabled through hotkey_mask");
1914 if (parse_strtoul(buf, 1, &t))
1923 static struct device_attribute dev_attr_hotkey_enable =
1924 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1925 hotkey_enable_show, hotkey_enable_store);
1927 /* sysfs hotkey mask --------------------------------------------------- */
1928 static ssize_t hotkey_mask_show(struct device *dev,
1929 struct device_attribute *attr,
1934 if (mutex_lock_killable(&hotkey_mutex))
1935 return -ERESTARTSYS;
1936 res = hotkey_mask_get();
1937 mutex_unlock(&hotkey_mutex);
1940 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1943 static ssize_t hotkey_mask_store(struct device *dev,
1944 struct device_attribute *attr,
1945 const char *buf, size_t count)
1950 if (parse_strtoul(buf, 0xffffffffUL, &t))
1953 if (mutex_lock_killable(&hotkey_mutex))
1954 return -ERESTARTSYS;
1956 res = hotkey_mask_set(t);
1958 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1959 hotkey_poll_setup(1);
1962 mutex_unlock(&hotkey_mutex);
1964 return (res) ? res : count;
1967 static struct device_attribute dev_attr_hotkey_mask =
1968 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1969 hotkey_mask_show, hotkey_mask_store);
1971 /* sysfs hotkey bios_enabled ------------------------------------------- */
1972 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1973 struct device_attribute *attr,
1976 return sprintf(buf, "0\n");
1979 static struct device_attribute dev_attr_hotkey_bios_enabled =
1980 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1982 /* sysfs hotkey bios_mask ---------------------------------------------- */
1983 static ssize_t hotkey_bios_mask_show(struct device *dev,
1984 struct device_attribute *attr,
1987 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1990 static struct device_attribute dev_attr_hotkey_bios_mask =
1991 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1993 /* sysfs hotkey all_mask ----------------------------------------------- */
1994 static ssize_t hotkey_all_mask_show(struct device *dev,
1995 struct device_attribute *attr,
1998 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1999 hotkey_all_mask | hotkey_source_mask);
2002 static struct device_attribute dev_attr_hotkey_all_mask =
2003 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2005 /* sysfs hotkey recommended_mask --------------------------------------- */
2006 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2007 struct device_attribute *attr,
2010 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2011 (hotkey_all_mask | hotkey_source_mask)
2012 & ~hotkey_reserved_mask);
2015 static struct device_attribute dev_attr_hotkey_recommended_mask =
2016 __ATTR(hotkey_recommended_mask, S_IRUGO,
2017 hotkey_recommended_mask_show, NULL);
2019 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2021 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2022 static ssize_t hotkey_source_mask_show(struct device *dev,
2023 struct device_attribute *attr,
2026 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2029 static ssize_t hotkey_source_mask_store(struct device *dev,
2030 struct device_attribute *attr,
2031 const char *buf, size_t count)
2035 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2036 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2039 if (mutex_lock_killable(&hotkey_mutex))
2040 return -ERESTARTSYS;
2042 HOTKEY_CONFIG_CRITICAL_START
2043 hotkey_source_mask = t;
2044 HOTKEY_CONFIG_CRITICAL_END
2046 hotkey_poll_setup(1);
2048 mutex_unlock(&hotkey_mutex);
2053 static struct device_attribute dev_attr_hotkey_source_mask =
2054 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2055 hotkey_source_mask_show, hotkey_source_mask_store);
2057 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2058 static ssize_t hotkey_poll_freq_show(struct device *dev,
2059 struct device_attribute *attr,
2062 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2065 static ssize_t hotkey_poll_freq_store(struct device *dev,
2066 struct device_attribute *attr,
2067 const char *buf, size_t count)
2071 if (parse_strtoul(buf, 25, &t))
2074 if (mutex_lock_killable(&hotkey_mutex))
2075 return -ERESTARTSYS;
2077 hotkey_poll_freq = t;
2079 hotkey_poll_setup(1);
2080 mutex_unlock(&hotkey_mutex);
2085 static struct device_attribute dev_attr_hotkey_poll_freq =
2086 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2087 hotkey_poll_freq_show, hotkey_poll_freq_store);
2089 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2091 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2092 static ssize_t hotkey_radio_sw_show(struct device *dev,
2093 struct device_attribute *attr,
2097 res = hotkey_get_wlsw(&s);
2101 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2104 static struct device_attribute dev_attr_hotkey_radio_sw =
2105 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2107 static void hotkey_radio_sw_notify_change(void)
2109 if (tp_features.hotkey_wlsw)
2110 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2114 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2115 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2116 struct device_attribute *attr,
2120 res = hotkey_get_tablet_mode(&s);
2124 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2127 static struct device_attribute dev_attr_hotkey_tablet_mode =
2128 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2130 static void hotkey_tablet_mode_notify_change(void)
2132 if (tp_features.hotkey_tablet)
2133 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2134 "hotkey_tablet_mode");
2137 /* sysfs hotkey report_mode -------------------------------------------- */
2138 static ssize_t hotkey_report_mode_show(struct device *dev,
2139 struct device_attribute *attr,
2142 return snprintf(buf, PAGE_SIZE, "%d\n",
2143 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2146 static struct device_attribute dev_attr_hotkey_report_mode =
2147 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2149 /* sysfs wakeup reason (pollable) -------------------------------------- */
2150 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2151 struct device_attribute *attr,
2154 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2157 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2158 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2160 static void hotkey_wakeup_reason_notify_change(void)
2162 if (tp_features.hotkey_mask)
2163 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2167 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2168 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2169 struct device_attribute *attr,
2172 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2175 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2176 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2177 hotkey_wakeup_hotunplug_complete_show, NULL);
2179 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2181 if (tp_features.hotkey_mask)
2182 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2183 "wakeup_hotunplug_complete");
2186 /* --------------------------------------------------------------------- */
2188 static struct attribute *hotkey_attributes[] __initdata = {
2189 &dev_attr_hotkey_enable.attr,
2190 &dev_attr_hotkey_bios_enabled.attr,
2191 &dev_attr_hotkey_report_mode.attr,
2192 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2193 &dev_attr_hotkey_mask.attr,
2194 &dev_attr_hotkey_all_mask.attr,
2195 &dev_attr_hotkey_recommended_mask.attr,
2196 &dev_attr_hotkey_source_mask.attr,
2197 &dev_attr_hotkey_poll_freq.attr,
2201 static struct attribute *hotkey_mask_attributes[] __initdata = {
2202 &dev_attr_hotkey_bios_mask.attr,
2203 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2204 &dev_attr_hotkey_mask.attr,
2205 &dev_attr_hotkey_all_mask.attr,
2206 &dev_attr_hotkey_recommended_mask.attr,
2208 &dev_attr_hotkey_wakeup_reason.attr,
2209 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2212 static void bluetooth_update_rfk(void);
2213 static void wan_update_rfk(void);
2214 static void uwb_update_rfk(void);
2215 static void tpacpi_send_radiosw_update(void)
2219 /* Sync these BEFORE sending any rfkill events */
2220 if (tp_features.bluetooth)
2221 bluetooth_update_rfk();
2222 if (tp_features.wan)
2224 if (tp_features.uwb)
2227 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2228 mutex_lock(&tpacpi_inputdev_send_mutex);
2230 input_report_switch(tpacpi_inputdev,
2231 SW_RFKILL_ALL, !!wlsw);
2232 input_sync(tpacpi_inputdev);
2234 mutex_unlock(&tpacpi_inputdev_send_mutex);
2236 hotkey_radio_sw_notify_change();
2239 static void hotkey_exit(void)
2241 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2242 hotkey_poll_stop_sync();
2245 if (hotkey_dev_attributes)
2246 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2248 kfree(hotkey_keycode_map);
2250 if (tp_features.hotkey) {
2251 dbg_printk(TPACPI_DBG_EXIT,
2252 "restoring original hot key mask\n");
2253 /* no short-circuit boolean operator below! */
2254 if ((hotkey_mask_set(hotkey_orig_mask) |
2255 hotkey_status_set(false)) != 0)
2257 "failed to restore hot key mask "
2258 "to BIOS defaults\n");
2262 static int __init hotkey_init(struct ibm_init_struct *iibm)
2264 /* Requirements for changing the default keymaps:
2266 * 1. Many of the keys are mapped to KEY_RESERVED for very
2267 * good reasons. Do not change them unless you have deep
2268 * knowledge on the IBM and Lenovo ThinkPad firmware for
2269 * the various ThinkPad models. The driver behaves
2270 * differently for KEY_RESERVED: such keys have their
2271 * hot key mask *unset* in mask_recommended, and also
2272 * in the initial hot key mask programmed into the
2273 * firmware at driver load time, which means the firm-
2274 * ware may react very differently if you change them to
2277 * 2. You must be subscribed to the linux-thinkpad and
2278 * ibm-acpi-devel mailing lists, and you should read the
2279 * list archives since 2007 if you want to change the
2280 * keymaps. This requirement exists so that you will
2281 * know the past history of problems with the thinkpad-
2282 * acpi driver keymaps, and also that you will be
2283 * listening to any bug reports;
2285 * 3. Do not send thinkpad-acpi specific patches directly to
2286 * for merging, *ever*. Send them to the linux-acpi
2287 * mailinglist for comments. Merging is to be done only
2288 * through acpi-test and the ACPI maintainer.
2290 * If the above is too much to ask, don't change the keymap.
2291 * Ask the thinkpad-acpi maintainer to do it, instead.
2293 static u16 ibm_keycode_map[] __initdata = {
2294 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2295 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2296 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2297 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2299 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2300 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2301 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2302 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2304 /* brightness: firmware always reacts to them, unless
2305 * X.org did some tricks in the radeon BIOS scratch
2306 * registers of *some* models */
2307 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2308 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2310 /* Thinklight: firmware always react to it */
2311 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2313 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2314 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2316 /* Volume: firmware always react to it and reprograms
2317 * the built-in *extra* mixer. Never map it to control
2318 * another mixer by default. */
2319 KEY_RESERVED, /* 0x14: VOLUME UP */
2320 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2321 KEY_RESERVED, /* 0x16: MUTE */
2323 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2325 /* (assignments unknown, please report if found) */
2326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2327 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2329 static u16 lenovo_keycode_map[] __initdata = {
2330 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2331 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2332 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2333 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2335 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2336 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2337 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2338 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2340 /* These either have to go through ACPI video, or
2341 * act like in the IBM ThinkPads, so don't ever
2342 * enable them by default */
2343 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2344 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2346 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2348 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2349 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2351 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2352 * react to it and reprograms the built-in *extra* mixer.
2353 * Never map it to control another mixer by default.
2355 * T60?, T61, R60?, R61: firmware and EC tries to send
2356 * these over the regular keyboard, so these are no-ops,
2357 * but there are still weird bugs re. MUTE, so do not
2358 * change unless you get test reports from all Lenovo
2359 * models. May cause the BIOS to interfere with the
2362 KEY_RESERVED, /* 0x14: VOLUME UP */
2363 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2364 KEY_RESERVED, /* 0x16: MUTE */
2366 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2368 /* (assignments unknown, please report if found) */
2369 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2370 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2373 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2374 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2375 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2381 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2383 BUG_ON(!tpacpi_inputdev);
2384 BUG_ON(tpacpi_inputdev->open != NULL ||
2385 tpacpi_inputdev->close != NULL);
2387 TPACPI_ACPIHANDLE_INIT(hkey);
2388 mutex_init(&hotkey_mutex);
2390 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2391 mutex_init(&hotkey_thread_mutex);
2392 mutex_init(&hotkey_thread_data_mutex);
2395 /* hotkey not supported on 570 */
2396 tp_features.hotkey = hkey_handle != NULL;
2398 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2399 str_supported(tp_features.hotkey));
2401 if (!tp_features.hotkey)
2404 tpacpi_disable_brightness_delay();
2406 hotkey_dev_attributes = create_attr_set(13, NULL);
2407 if (!hotkey_dev_attributes)
2409 res = add_many_to_attr_set(hotkey_dev_attributes,
2411 ARRAY_SIZE(hotkey_attributes));
2415 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2416 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2417 for HKEY interface version 0x100 */
2418 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2419 if ((hkeyv >> 8) != 1) {
2420 printk(TPACPI_ERR "unknown version of the "
2421 "HKEY interface: 0x%x\n", hkeyv);
2422 printk(TPACPI_ERR "please report this to %s\n",
2426 * MHKV 0x100 in A31, R40, R40e,
2427 * T4x, X31, and later
2429 tp_features.hotkey_mask = 1;
2433 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2434 str_supported(tp_features.hotkey_mask));
2436 if (tp_features.hotkey_mask) {
2437 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2440 "missing MHKA handler, "
2441 "please report this to %s\n",
2443 /* FN+F12, FN+F4, FN+F3 */
2444 hotkey_all_mask = 0x080cU;
2448 /* hotkey_source_mask *must* be zero for
2449 * the first hotkey_mask_get */
2450 if (tp_features.hotkey_mask) {
2451 res = hotkey_mask_get();
2455 hotkey_orig_mask = hotkey_mask;
2456 res = add_many_to_attr_set(
2457 hotkey_dev_attributes,
2458 hotkey_mask_attributes,
2459 ARRAY_SIZE(hotkey_mask_attributes));
2464 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2465 if (tp_features.hotkey_mask) {
2466 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2469 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2472 vdbg_printk(TPACPI_DBG_INIT,
2473 "hotkey source mask 0x%08x, polling freq %d\n",
2474 hotkey_source_mask, hotkey_poll_freq);
2477 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2479 tp_features.hotkey_wlsw = 1;
2481 "radio switch emulation enabled\n");
2484 /* Not all thinkpads have a hardware radio switch */
2485 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2486 tp_features.hotkey_wlsw = 1;
2488 "radio switch found; radios are %s\n",
2489 enabled(status, 0));
2491 if (tp_features.hotkey_wlsw)
2492 res = add_to_attr_set(hotkey_dev_attributes,
2493 &dev_attr_hotkey_radio_sw.attr);
2495 /* For X41t, X60t, X61t Tablets... */
2496 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2497 tp_features.hotkey_tablet = 1;
2499 "possible tablet mode switch found; "
2500 "ThinkPad in %s mode\n",
2501 (status & TP_HOTKEY_TABLET_MASK)?
2502 "tablet" : "laptop");
2503 res = add_to_attr_set(hotkey_dev_attributes,
2504 &dev_attr_hotkey_tablet_mode.attr);
2508 res = register_attr_set_with_sysfs(
2509 hotkey_dev_attributes,
2510 &tpacpi_pdev->dev.kobj);
2514 /* Set up key map */
2516 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2518 if (!hotkey_keycode_map) {
2520 "failed to allocate memory for key map\n");
2525 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2526 dbg_printk(TPACPI_DBG_INIT,
2527 "using Lenovo default hot key map\n");
2528 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2529 TPACPI_HOTKEY_MAP_SIZE);
2531 dbg_printk(TPACPI_DBG_INIT,
2532 "using IBM default hot key map\n");
2533 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2534 TPACPI_HOTKEY_MAP_SIZE);
2537 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2538 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2539 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2540 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2541 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2542 tpacpi_inputdev->keycode = hotkey_keycode_map;
2543 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2544 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2545 set_bit(hotkey_keycode_map[i],
2546 tpacpi_inputdev->keybit);
2548 if (i < sizeof(hotkey_reserved_mask)*8)
2549 hotkey_reserved_mask |= 1 << i;
2553 if (tp_features.hotkey_wlsw) {
2554 set_bit(EV_SW, tpacpi_inputdev->evbit);
2555 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2557 if (tp_features.hotkey_tablet) {
2558 set_bit(EV_SW, tpacpi_inputdev->evbit);
2559 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2562 /* Do not issue duplicate brightness change events to
2564 if (!tp_features.bright_acpimode)
2565 /* update bright_acpimode... */
2566 tpacpi_check_std_acpi_brightness_support();
2568 if (tp_features.bright_acpimode) {
2570 "This ThinkPad has standard ACPI backlight "
2571 "brightness control, supported by the ACPI "
2573 printk(TPACPI_NOTICE
2574 "Disabling thinkpad-acpi brightness events "
2577 /* The hotkey_reserved_mask change below is not
2578 * necessary while the keys are at KEY_RESERVED in the
2579 * default map, but better safe than sorry, leave it
2580 * here as a marker of what we have to do, especially
2581 * when we finally become able to set this at runtime
2582 * on response to X.org requests */
2583 hotkey_reserved_mask |=
2584 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2585 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2588 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2589 res = hotkey_status_set(true);
2594 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2595 & ~hotkey_reserved_mask)
2596 | hotkey_orig_mask);
2597 if (res < 0 && res != -ENXIO) {
2602 dbg_printk(TPACPI_DBG_INIT,
2603 "legacy hot key reporting over procfs %s\n",
2604 (hotkey_report_mode < 2) ?
2605 "enabled" : "disabled");
2607 tpacpi_inputdev->open = &hotkey_inputdev_open;
2608 tpacpi_inputdev->close = &hotkey_inputdev_close;
2610 hotkey_poll_setup_safe(1);
2611 tpacpi_send_radiosw_update();
2612 tpacpi_input_send_tabletsw();
2617 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2618 hotkey_dev_attributes = NULL;
2620 return (res < 0)? res : 1;
2623 static bool hotkey_notify_hotkey(const u32 hkey,
2625 bool *ignore_acpi_ev)
2627 /* 0x1000-0x1FFF: key presses */
2628 unsigned int scancode = hkey & 0xfff;
2629 *send_acpi_ev = true;
2630 *ignore_acpi_ev = false;
2632 if (scancode > 0 && scancode < 0x21) {
2634 if (!(hotkey_source_mask & (1 << scancode))) {
2635 tpacpi_input_send_key(scancode);
2636 *send_acpi_ev = false;
2638 *ignore_acpi_ev = true;
2645 static bool hotkey_notify_wakeup(const u32 hkey,
2647 bool *ignore_acpi_ev)
2649 /* 0x2000-0x2FFF: Wakeup reason */
2650 *send_acpi_ev = true;
2651 *ignore_acpi_ev = false;
2654 case 0x2304: /* suspend, undock */
2655 case 0x2404: /* hibernation, undock */
2656 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2657 *ignore_acpi_ev = true;
2660 case 0x2305: /* suspend, bay eject */
2661 case 0x2405: /* hibernation, bay eject */
2662 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2663 *ignore_acpi_ev = true;
2666 case 0x2313: /* Battery on critical low level (S3) */
2667 case 0x2413: /* Battery on critical low level (S4) */
2669 "EMERGENCY WAKEUP: battery almost empty\n");
2670 /* how to auto-heal: */
2671 /* 2313: woke up from S3, go to S4/S5 */
2672 /* 2413: woke up from S4, go to S5 */
2679 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2681 "woke up due to a hot-unplug "
2683 hotkey_wakeup_reason_notify_change();
2688 static bool hotkey_notify_usrevent(const u32 hkey,
2690 bool *ignore_acpi_ev)
2692 /* 0x5000-0x5FFF: human interface helpers */
2693 *send_acpi_ev = true;
2694 *ignore_acpi_ev = false;
2697 case 0x5010: /* Lenovo new BIOS: brightness changed */
2698 case 0x500b: /* X61t: tablet pen inserted into bay */
2699 case 0x500c: /* X61t: tablet pen removed from bay */
2702 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2703 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2704 tpacpi_input_send_tabletsw();
2705 hotkey_tablet_mode_notify_change();
2706 *send_acpi_ev = false;
2711 /* LID switch events. Do not propagate */
2712 *ignore_acpi_ev = true;
2720 static bool hotkey_notify_thermal(const u32 hkey,
2722 bool *ignore_acpi_ev)
2724 /* 0x6000-0x6FFF: thermal alarms */
2725 *send_acpi_ev = true;
2726 *ignore_acpi_ev = false;
2731 "THERMAL ALARM: battery is too hot!\n");
2732 /* recommended action: warn user through gui */
2736 "THERMAL EMERGENCY: battery is extremely hot!\n");
2737 /* recommended action: immediate sleep/hibernate */
2742 "a sensor reports something is too hot!\n");
2743 /* recommended action: warn user through gui, that */
2744 /* some internal component is too hot */
2748 "THERMAL EMERGENCY: "
2749 "a sensor reports something is extremely hot!\n");
2750 /* recommended action: immediate sleep/hibernate */
2754 "EC reports that Thermal Table has changed\n");
2755 /* recommended action: do nothing, we don't have
2756 * Lenovo ATM information */
2760 "THERMAL ALERT: unknown thermal alarm received\n");
2765 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2769 bool ignore_acpi_ev;
2772 if (event != 0x80) {
2774 "unknown HKEY notification event %d\n", event);
2775 /* forward it to userspace, maybe it knows how to handle it */
2776 acpi_bus_generate_netlink_event(
2777 ibm->acpi->device->pnp.device_class,
2778 dev_name(&ibm->acpi->device->dev),
2784 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2785 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2794 send_acpi_ev = true;
2795 ignore_acpi_ev = false;
2797 switch (hkey >> 12) {
2799 /* 0x1000-0x1FFF: key presses */
2800 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2804 /* 0x2000-0x2FFF: Wakeup reason */
2805 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2809 /* 0x3000-0x3FFF: bay-related wakeups */
2810 if (hkey == 0x3003) {
2811 hotkey_autosleep_ack = 1;
2814 hotkey_wakeup_hotunplug_complete_notify_change();
2821 /* 0x4000-0x4FFF: dock-related wakeups */
2822 if (hkey == 0x4003) {
2823 hotkey_autosleep_ack = 1;
2826 hotkey_wakeup_hotunplug_complete_notify_change();
2833 /* 0x5000-0x5FFF: human interface helpers */
2834 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2838 /* 0x6000-0x6FFF: thermal alarms */
2839 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2843 /* 0x7000-0x7FFF: misc */
2844 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2845 tpacpi_send_radiosw_update();
2850 /* fallthrough to default */
2855 printk(TPACPI_NOTICE
2856 "unhandled HKEY event 0x%04x\n", hkey);
2857 printk(TPACPI_NOTICE
2858 "please report the conditions when this "
2859 "event happened to %s\n", TPACPI_MAIL);
2863 if (!ignore_acpi_ev &&
2864 (send_acpi_ev || hotkey_report_mode < 2)) {
2865 acpi_bus_generate_proc_event(ibm->acpi->device,
2869 /* netlink events */
2870 if (!ignore_acpi_ev && send_acpi_ev) {
2871 acpi_bus_generate_netlink_event(
2872 ibm->acpi->device->pnp.device_class,
2873 dev_name(&ibm->acpi->device->dev),
2879 static void hotkey_suspend(pm_message_t state)
2881 /* Do these on suspend, we get the events on early resume! */
2882 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2883 hotkey_autosleep_ack = 0;
2886 static void hotkey_resume(void)
2888 tpacpi_disable_brightness_delay();
2890 if (hotkey_mask_get())
2892 "error while trying to read hot key mask "
2894 tpacpi_send_radiosw_update();
2895 hotkey_tablet_mode_notify_change();
2896 hotkey_wakeup_reason_notify_change();
2897 hotkey_wakeup_hotunplug_complete_notify_change();
2898 hotkey_poll_setup_safe(0);
2901 /* procfs -------------------------------------------------------------- */
2902 static int hotkey_read(char *p)
2907 if (!tp_features.hotkey) {
2908 len += sprintf(p + len, "status:\t\tnot supported\n");
2912 if (mutex_lock_killable(&hotkey_mutex))
2913 return -ERESTARTSYS;
2914 res = hotkey_status_get(&status);
2916 res = hotkey_mask_get();
2917 mutex_unlock(&hotkey_mutex);
2921 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2922 if (tp_features.hotkey_mask) {
2923 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2924 len += sprintf(p + len,
2925 "commands:\tenable, disable, reset, <mask>\n");
2927 len += sprintf(p + len, "mask:\t\tnot supported\n");
2928 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2934 static void hotkey_enabledisable_warn(void)
2936 tpacpi_log_usertask("procfs hotkey enable/disable");
2938 "hotkey enable/disable functionality has been "
2939 "removed from the driver. Hotkeys are always enabled.\n");
2942 static int hotkey_write(char *buf)
2948 if (!tp_features.hotkey)
2951 if (mutex_lock_killable(&hotkey_mutex))
2952 return -ERESTARTSYS;
2957 while ((cmd = next_cmd(&buf))) {
2958 if (strlencmp(cmd, "enable") == 0) {
2959 hotkey_enabledisable_warn();
2960 } else if (strlencmp(cmd, "disable") == 0) {
2961 hotkey_enabledisable_warn();
2963 } else if (strlencmp(cmd, "reset") == 0) {
2964 mask = hotkey_orig_mask;
2965 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2967 } else if (sscanf(cmd, "%x", &mask) == 1) {
2974 if (!res && mask != hotkey_mask)
2975 res = hotkey_mask_set(mask);
2978 mutex_unlock(&hotkey_mutex);
2982 static const struct acpi_device_id ibm_htk_device_ids[] = {
2983 {TPACPI_ACPI_HKEY_HID, 0},
2987 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2988 .hid = ibm_htk_device_ids,
2989 .notify = hotkey_notify,
2990 .handle = &hkey_handle,
2991 .type = ACPI_DEVICE_NOTIFY,
2994 static struct ibm_struct hotkey_driver_data = {
2996 .read = hotkey_read,
2997 .write = hotkey_write,
2998 .exit = hotkey_exit,
2999 .resume = hotkey_resume,
3000 .suspend = hotkey_suspend,
3001 .acpi = &ibm_hotkey_acpidriver,
3004 /*************************************************************************
3005 * Bluetooth subdriver
3009 /* ACPI GBDC/SBDC bits */
3010 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3011 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3012 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3017 /* ACPI \BLTH commands */
3018 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3019 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3020 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3021 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3022 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3025 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3027 static struct rfkill *tpacpi_bluetooth_rfkill;
3029 static void bluetooth_suspend(pm_message_t state)
3031 /* Try to make sure radio will resume powered off */
3032 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3033 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3034 vdbg_printk(TPACPI_DBG_RFKILL,
3035 "bluetooth power down on resume request failed\n");
3038 static int bluetooth_get_radiosw(void)
3042 if (!tp_features.bluetooth)
3045 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3046 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3047 return RFKILL_STATE_HARD_BLOCKED;
3049 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3050 if (dbg_bluetoothemul)
3051 return (tpacpi_bluetooth_emulstate) ?
3052 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3055 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3058 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3059 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3062 static void bluetooth_update_rfk(void)
3066 if (!tpacpi_bluetooth_rfkill)
3069 status = bluetooth_get_radiosw();
3072 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3074 vdbg_printk(TPACPI_DBG_RFKILL,
3075 "forced rfkill state to %d\n",
3079 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3083 if (!tp_features.bluetooth)
3086 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3087 * reason to risk weird behaviour. */
3088 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3092 vdbg_printk(TPACPI_DBG_RFKILL,
3093 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3095 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3096 if (dbg_bluetoothemul) {
3097 tpacpi_bluetooth_emulstate = !!radio_on;
3099 bluetooth_update_rfk();
3104 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3106 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3109 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3113 bluetooth_update_rfk();
3118 /* sysfs bluetooth enable ---------------------------------------------- */
3119 static ssize_t bluetooth_enable_show(struct device *dev,
3120 struct device_attribute *attr,
3125 printk_deprecated_rfkill_attribute("bluetooth_enable");
3127 status = bluetooth_get_radiosw();
3131 return snprintf(buf, PAGE_SIZE, "%d\n",
3132 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3135 static ssize_t bluetooth_enable_store(struct device *dev,
3136 struct device_attribute *attr,
3137 const char *buf, size_t count)
3142 printk_deprecated_rfkill_attribute("bluetooth_enable");
3144 if (parse_strtoul(buf, 1, &t))
3147 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3149 res = bluetooth_set_radiosw(t, 1);
3151 return (res) ? res : count;
3154 static struct device_attribute dev_attr_bluetooth_enable =
3155 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3156 bluetooth_enable_show, bluetooth_enable_store);
3158 /* --------------------------------------------------------------------- */
3160 static struct attribute *bluetooth_attributes[] = {
3161 &dev_attr_bluetooth_enable.attr,
3165 static const struct attribute_group bluetooth_attr_group = {
3166 .attrs = bluetooth_attributes,
3169 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3171 int bts = bluetooth_get_radiosw();
3180 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3182 dbg_printk(TPACPI_DBG_RFKILL,
3183 "request to change radio state to %d\n", state);
3184 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3187 static void bluetooth_shutdown(void)
3189 /* Order firmware to save current state to NVRAM */
3190 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3191 TP_ACPI_BLTH_SAVE_STATE))
3192 printk(TPACPI_NOTICE
3193 "failed to save bluetooth state to NVRAM\n");
3195 vdbg_printk(TPACPI_DBG_RFKILL,
3196 "bluestooth state saved to NVRAM\n");
3199 static void bluetooth_exit(void)
3201 bluetooth_shutdown();
3203 if (tpacpi_bluetooth_rfkill)
3204 rfkill_unregister(tpacpi_bluetooth_rfkill);
3206 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3207 &bluetooth_attr_group);
3210 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3215 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3216 "initializing bluetooth subdriver\n");
3218 TPACPI_ACPIHANDLE_INIT(hkey);
3220 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3221 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3222 tp_features.bluetooth = hkey_handle &&
3223 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3225 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3226 "bluetooth is %s, status 0x%02x\n",
3227 str_supported(tp_features.bluetooth),
3230 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3231 if (dbg_bluetoothemul) {
3232 tp_features.bluetooth = 1;
3234 "bluetooth switch emulation enabled\n");
3237 if (tp_features.bluetooth &&
3238 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3239 /* no bluetooth hardware present in system */
3240 tp_features.bluetooth = 0;
3241 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3242 "bluetooth hardware not installed\n");
3245 if (!tp_features.bluetooth)
3248 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3249 &bluetooth_attr_group);
3253 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3254 &tpacpi_bluetooth_rfkill,
3255 RFKILL_TYPE_BLUETOOTH,
3256 TPACPI_RFK_BLUETOOTH_SW_NAME,
3258 tpacpi_bluetooth_rfk_set,
3259 tpacpi_bluetooth_rfk_get);
3268 /* procfs -------------------------------------------------------------- */
3269 static int bluetooth_read(char *p)
3272 int status = bluetooth_get_radiosw();
3274 if (!tp_features.bluetooth)
3275 len += sprintf(p + len, "status:\t\tnot supported\n");
3277 len += sprintf(p + len, "status:\t\t%s\n",
3278 (status == RFKILL_STATE_UNBLOCKED) ?
3279 "enabled" : "disabled");
3280 len += sprintf(p + len, "commands:\tenable, disable\n");
3286 static int bluetooth_write(char *buf)
3291 if (!tp_features.bluetooth)
3294 while ((cmd = next_cmd(&buf))) {
3295 if (strlencmp(cmd, "enable") == 0) {
3297 } else if (strlencmp(cmd, "disable") == 0) {
3304 tpacpi_disclose_usertask("procfs bluetooth",
3306 state ? "enable" : "disable");
3307 bluetooth_set_radiosw(state, 1);
3313 static struct ibm_struct bluetooth_driver_data = {
3314 .name = "bluetooth",
3315 .read = bluetooth_read,
3316 .write = bluetooth_write,
3317 .exit = bluetooth_exit,
3318 .suspend = bluetooth_suspend,
3319 .shutdown = bluetooth_shutdown,
3322 /*************************************************************************
3327 /* ACPI GWAN/SWAN bits */
3328 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3329 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3330 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3334 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3336 static struct rfkill *tpacpi_wan_rfkill;
3338 static void wan_suspend(pm_message_t state)
3340 /* Try to make sure radio will resume powered off */
3341 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3342 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3343 vdbg_printk(TPACPI_DBG_RFKILL,
3344 "WWAN power down on resume request failed\n");
3347 static int wan_get_radiosw(void)
3351 if (!tp_features.wan)
3354 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3355 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3356 return RFKILL_STATE_HARD_BLOCKED;
3358 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3360 return (tpacpi_wwan_emulstate) ?
3361 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3364 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3367 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3368 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3371 static void wan_update_rfk(void)
3375 if (!tpacpi_wan_rfkill)
3378 status = wan_get_radiosw();
3381 rfkill_force_state(tpacpi_wan_rfkill, status);
3383 vdbg_printk(TPACPI_DBG_RFKILL,
3384 "forced rfkill state to %d\n",
3388 static int wan_set_radiosw(int radio_on, int update_rfk)
3392 if (!tp_features.wan)
3395 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3396 * reason to risk weird behaviour. */
3397 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3401 vdbg_printk(TPACPI_DBG_RFKILL,
3402 "will %s WWAN\n", radio_on ? "enable" : "disable");
3404 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3406 tpacpi_wwan_emulstate = !!radio_on;
3413 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3415 status = TP_ACPI_WANCARD_RADIOSSW;
3418 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3427 /* sysfs wan enable ---------------------------------------------------- */
3428 static ssize_t wan_enable_show(struct device *dev,
3429 struct device_attribute *attr,
3434 printk_deprecated_rfkill_attribute("wwan_enable");
3436 status = wan_get_radiosw();
3440 return snprintf(buf, PAGE_SIZE, "%d\n",
3441 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3444 static ssize_t wan_enable_store(struct device *dev,
3445 struct device_attribute *attr,
3446 const char *buf, size_t count)
3451 printk_deprecated_rfkill_attribute("wwan_enable");
3453 if (parse_strtoul(buf, 1, &t))
3456 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3458 res = wan_set_radiosw(t, 1);
3460 return (res) ? res : count;
3463 static struct device_attribute dev_attr_wan_enable =
3464 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3465 wan_enable_show, wan_enable_store);
3467 /* --------------------------------------------------------------------- */
3469 static struct attribute *wan_attributes[] = {
3470 &dev_attr_wan_enable.attr,
3474 static const struct attribute_group wan_attr_group = {
3475 .attrs = wan_attributes,
3478 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3480 int wans = wan_get_radiosw();
3489 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3491 dbg_printk(TPACPI_DBG_RFKILL,
3492 "request to change radio state to %d\n", state);
3493 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3496 static void wan_shutdown(void)
3498 /* Order firmware to save current state to NVRAM */
3499 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3500 TP_ACPI_WGSV_SAVE_STATE))
3501 printk(TPACPI_NOTICE
3502 "failed to save WWAN state to NVRAM\n");
3504 vdbg_printk(TPACPI_DBG_RFKILL,
3505 "WWAN state saved to NVRAM\n");
3508 static void wan_exit(void)
3512 if (tpacpi_wan_rfkill)
3513 rfkill_unregister(tpacpi_wan_rfkill);
3515 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3519 static int __init wan_init(struct ibm_init_struct *iibm)
3524 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3525 "initializing wan subdriver\n");
3527 TPACPI_ACPIHANDLE_INIT(hkey);
3529 tp_features.wan = hkey_handle &&
3530 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3532 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3533 "wan is %s, status 0x%02x\n",
3534 str_supported(tp_features.wan),
3537 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3539 tp_features.wan = 1;
3541 "wwan switch emulation enabled\n");
3544 if (tp_features.wan &&
3545 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3546 /* no wan hardware present in system */
3547 tp_features.wan = 0;
3548 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3549 "wan hardware not installed\n");
3552 if (!tp_features.wan)
3555 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3560 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3563 TPACPI_RFK_WWAN_SW_NAME,
3566 tpacpi_wan_rfk_get);
3575 /* procfs -------------------------------------------------------------- */
3576 static int wan_read(char *p)
3579 int status = wan_get_radiosw();
3581 tpacpi_disclose_usertask("procfs wan", "read");
3583 if (!tp_features.wan)
3584 len += sprintf(p + len, "status:\t\tnot supported\n");
3586 len += sprintf(p + len, "status:\t\t%s\n",
3587 (status == RFKILL_STATE_UNBLOCKED) ?
3588 "enabled" : "disabled");
3589 len += sprintf(p + len, "commands:\tenable, disable\n");
3595 static int wan_write(char *buf)
3600 if (!tp_features.wan)
3603 while ((cmd = next_cmd(&buf))) {
3604 if (strlencmp(cmd, "enable") == 0) {
3606 } else if (strlencmp(cmd, "disable") == 0) {
3613 tpacpi_disclose_usertask("procfs wan",
3615 state ? "enable" : "disable");
3616 wan_set_radiosw(state, 1);
3622 static struct ibm_struct wan_driver_data = {
3627 .suspend = wan_suspend,
3628 .shutdown = wan_shutdown,
3631 /*************************************************************************
3636 /* ACPI GUWB/SUWB bits */
3637 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3638 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3641 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3643 static struct rfkill *tpacpi_uwb_rfkill;
3645 static int uwb_get_radiosw(void)
3649 if (!tp_features.uwb)
3652 /* WLSW overrides UWB in firmware/hardware, reflect that */
3653 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3654 return RFKILL_STATE_HARD_BLOCKED;
3656 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3658 return (tpacpi_uwb_emulstate) ?
3659 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3662 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3665 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3666 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3669 static void uwb_update_rfk(void)
3673 if (!tpacpi_uwb_rfkill)
3676 status = uwb_get_radiosw();
3679 rfkill_force_state(tpacpi_uwb_rfkill, status);
3681 vdbg_printk(TPACPI_DBG_RFKILL,
3682 "forced rfkill state to %d\n",
3686 static int uwb_set_radiosw(int radio_on, int update_rfk)
3690 if (!tp_features.uwb)
3693 /* WLSW overrides UWB in firmware/hardware, but there is no
3694 * reason to risk weird behaviour. */
3695 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3699 vdbg_printk(TPACPI_DBG_RFKILL,
3700 "will %s UWB\n", radio_on ? "enable" : "disable");
3702 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3704 tpacpi_uwb_emulstate = !!radio_on;
3711 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3712 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3721 /* --------------------------------------------------------------------- */
3723 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3725 int uwbs = uwb_get_radiosw();
3734 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3736 dbg_printk(TPACPI_DBG_RFKILL,
3737 "request to change radio state to %d\n", state);
3738 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3741 static void uwb_exit(void)
3743 if (tpacpi_uwb_rfkill)
3744 rfkill_unregister(tpacpi_uwb_rfkill);
3747 static int __init uwb_init(struct ibm_init_struct *iibm)
3752 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3753 "initializing uwb subdriver\n");
3755 TPACPI_ACPIHANDLE_INIT(hkey);
3757 tp_features.uwb = hkey_handle &&
3758 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3760 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3761 "uwb is %s, status 0x%02x\n",
3762 str_supported(tp_features.uwb),
3765 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3767 tp_features.uwb = 1;
3769 "uwb switch emulation enabled\n");
3772 if (tp_features.uwb &&
3773 !(status & TP_ACPI_UWB_HWPRESENT)) {
3774 /* no uwb hardware present in system */
3775 tp_features.uwb = 0;
3776 dbg_printk(TPACPI_DBG_INIT,
3777 "uwb hardware not installed\n");
3780 if (!tp_features.uwb)
3783 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3786 TPACPI_RFK_UWB_SW_NAME,
3789 tpacpi_uwb_rfk_get);
3794 static struct ibm_struct uwb_driver_data = {
3797 .flags.experimental = 1,
3800 /*************************************************************************
3804 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3806 enum video_access_mode {
3807 TPACPI_VIDEO_NONE = 0,
3808 TPACPI_VIDEO_570, /* 570 */
3809 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3810 TPACPI_VIDEO_NEW, /* all others */
3813 enum { /* video status flags, based on VIDEO_570 */
3814 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3815 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3816 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3819 enum { /* TPACPI_VIDEO_570 constants */
3820 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3821 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3822 * video_status_flags */
3823 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3824 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3827 static enum video_access_mode video_supported;
3828 static int video_orig_autosw;
3830 static int video_autosw_get(void);
3831 static int video_autosw_set(int enable);
3833 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3835 static int __init video_init(struct ibm_init_struct *iibm)
3839 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3841 TPACPI_ACPIHANDLE_INIT(vid);
3842 TPACPI_ACPIHANDLE_INIT(vid2);
3844 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3845 /* G41, assume IVGA doesn't change */
3846 vid_handle = vid2_handle;
3849 /* video switching not supported on R30, R31 */
3850 video_supported = TPACPI_VIDEO_NONE;
3851 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3853 video_supported = TPACPI_VIDEO_570;
3854 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3855 /* 600e/x, 770e, 770x */
3856 video_supported = TPACPI_VIDEO_770;
3859 video_supported = TPACPI_VIDEO_NEW;
3861 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3862 str_supported(video_supported != TPACPI_VIDEO_NONE),
3865 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3868 static void video_exit(void)
3870 dbg_printk(TPACPI_DBG_EXIT,
3871 "restoring original video autoswitch mode\n");
3872 if (video_autosw_set(video_orig_autosw))
3873 printk(TPACPI_ERR "error while trying to restore original "
3874 "video autoswitch mode\n");
3877 static int video_outputsw_get(void)
3882 switch (video_supported) {
3883 case TPACPI_VIDEO_570:
3884 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3885 TP_ACPI_VIDEO_570_PHSCMD))
3887 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3889 case TPACPI_VIDEO_770:
3890 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3893 status |= TP_ACPI_VIDEO_S_LCD;
3894 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3897 status |= TP_ACPI_VIDEO_S_CRT;
3899 case TPACPI_VIDEO_NEW:
3900 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3901 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3904 status |= TP_ACPI_VIDEO_S_CRT;
3906 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3907 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3910 status |= TP_ACPI_VIDEO_S_LCD;
3911 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3914 status |= TP_ACPI_VIDEO_S_DVI;
3923 static int video_outputsw_set(int status)
3928 switch (video_supported) {
3929 case TPACPI_VIDEO_570:
3930 res = acpi_evalf(NULL, NULL,
3931 "\\_SB.PHS2", "vdd",
3932 TP_ACPI_VIDEO_570_PHS2CMD,
3933 status | TP_ACPI_VIDEO_570_PHS2SET);
3935 case TPACPI_VIDEO_770:
3936 autosw = video_autosw_get();
3940 res = video_autosw_set(1);
3943 res = acpi_evalf(vid_handle, NULL,
3944 "ASWT", "vdd", status * 0x100, 0);
3945 if (!autosw && video_autosw_set(autosw)) {
3947 "video auto-switch left enabled due to error\n");
3951 case TPACPI_VIDEO_NEW:
3952 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3953 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3959 return (res)? 0 : -EIO;
3962 static int video_autosw_get(void)
3966 switch (video_supported) {
3967 case TPACPI_VIDEO_570:
3968 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3971 case TPACPI_VIDEO_770:
3972 case TPACPI_VIDEO_NEW:
3973 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3983 static int video_autosw_set(int enable)
3985 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3990 static int video_outputsw_cycle(void)
3992 int autosw = video_autosw_get();
3998 switch (video_supported) {
3999 case TPACPI_VIDEO_570:
4000 res = video_autosw_set(1);
4003 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4005 case TPACPI_VIDEO_770:
4006 case TPACPI_VIDEO_NEW:
4007 res = video_autosw_set(1);
4010 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4015 if (!autosw && video_autosw_set(autosw)) {
4017 "video auto-switch left enabled due to error\n");
4021 return (res)? 0 : -EIO;
4024 static int video_expand_toggle(void)
4026 switch (video_supported) {
4027 case TPACPI_VIDEO_570:
4028 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4030 case TPACPI_VIDEO_770:
4031 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4033 case TPACPI_VIDEO_NEW:
4034 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4042 static int video_read(char *p)
4047 if (video_supported == TPACPI_VIDEO_NONE) {
4048 len += sprintf(p + len, "status:\t\tnot supported\n");
4052 status = video_outputsw_get();
4056 autosw = video_autosw_get();
4060 len += sprintf(p + len, "status:\t\tsupported\n");
4061 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4062 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4063 if (video_supported == TPACPI_VIDEO_NEW)
4064 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4065 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4066 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4067 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4068 if (video_supported == TPACPI_VIDEO_NEW)
4069 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4070 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4071 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4076 static int video_write(char *buf)
4079 int enable, disable, status;
4082 if (video_supported == TPACPI_VIDEO_NONE)
4088 while ((cmd = next_cmd(&buf))) {
4089 if (strlencmp(cmd, "lcd_enable") == 0) {
4090 enable |= TP_ACPI_VIDEO_S_LCD;
4091 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4092 disable |= TP_ACPI_VIDEO_S_LCD;
4093 } else if (strlencmp(cmd, "crt_enable") == 0) {
4094 enable |= TP_ACPI_VIDEO_S_CRT;
4095 } else if (strlencmp(cmd, "crt_disable") == 0) {
4096 disable |= TP_ACPI_VIDEO_S_CRT;
4097 } else if (video_supported == TPACPI_VIDEO_NEW &&
4098 strlencmp(cmd, "dvi_enable") == 0) {
4099 enable |= TP_ACPI_VIDEO_S_DVI;
4100 } else if (video_supported == TPACPI_VIDEO_NEW &&
4101 strlencmp(cmd, "dvi_disable") == 0) {
4102 disable |= TP_ACPI_VIDEO_S_DVI;
4103 } else if (strlencmp(cmd, "auto_enable") == 0) {
4104 res = video_autosw_set(1);
4107 } else if (strlencmp(cmd, "auto_disable") == 0) {
4108 res = video_autosw_set(0);
4111 } else if (strlencmp(cmd, "video_switch") == 0) {
4112 res = video_outputsw_cycle();
4115 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4116 res = video_expand_toggle();
4123 if (enable || disable) {
4124 status = video_outputsw_get();
4127 res = video_outputsw_set((status & ~disable) | enable);
4135 static struct ibm_struct video_driver_data = {
4138 .write = video_write,
4142 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4144 /*************************************************************************
4145 * Light (thinklight) subdriver
4148 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4149 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4151 static int light_get_status(void)
4155 if (tp_features.light_status) {
4156 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4164 static int light_set_status(int status)
4168 if (tp_features.light) {
4170 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4172 TP_CMOS_THINKLIGHT_ON :
4173 TP_CMOS_THINKLIGHT_OFF);
4175 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4178 return (rc)? 0 : -EIO;
4184 static void light_set_status_worker(struct work_struct *work)
4186 struct tpacpi_led_classdev *data =
4187 container_of(work, struct tpacpi_led_classdev, work);
4189 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4190 light_set_status((data->new_brightness != LED_OFF));
4193 static void light_sysfs_set(struct led_classdev *led_cdev,
4194 enum led_brightness brightness)
4196 struct tpacpi_led_classdev *data =
4197 container_of(led_cdev,
4198 struct tpacpi_led_classdev,
4200 data->new_brightness = brightness;
4201 queue_work(tpacpi_wq, &data->work);
4204 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4206 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4209 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4211 .name = "tpacpi::thinklight",
4212 .brightness_set = &light_sysfs_set,
4213 .brightness_get = &light_sysfs_get,
4217 static int __init light_init(struct ibm_init_struct *iibm)
4221 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4223 TPACPI_ACPIHANDLE_INIT(ledb);
4224 TPACPI_ACPIHANDLE_INIT(lght);
4225 TPACPI_ACPIHANDLE_INIT(cmos);
4226 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4228 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4229 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4231 if (tp_features.light)
4232 /* light status not supported on
4233 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4234 tp_features.light_status =
4235 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4237 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4238 str_supported(tp_features.light),
4239 str_supported(tp_features.light_status));
4241 if (!tp_features.light)
4244 rc = led_classdev_register(&tpacpi_pdev->dev,
4245 &tpacpi_led_thinklight.led_classdev);
4248 tp_features.light = 0;
4249 tp_features.light_status = 0;
4257 static void light_exit(void)
4259 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4260 if (work_pending(&tpacpi_led_thinklight.work))
4261 flush_workqueue(tpacpi_wq);
4264 static int light_read(char *p)
4269 if (!tp_features.light) {
4270 len += sprintf(p + len, "status:\t\tnot supported\n");
4271 } else if (!tp_features.light_status) {
4272 len += sprintf(p + len, "status:\t\tunknown\n");
4273 len += sprintf(p + len, "commands:\ton, off\n");
4275 status = light_get_status();
4278 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4279 len += sprintf(p + len, "commands:\ton, off\n");
4285 static int light_write(char *buf)
4290 if (!tp_features.light)
4293 while ((cmd = next_cmd(&buf))) {
4294 if (strlencmp(cmd, "on") == 0) {
4296 } else if (strlencmp(cmd, "off") == 0) {
4302 return light_set_status(newstatus);
4305 static struct ibm_struct light_driver_data = {
4308 .write = light_write,
4312 /*************************************************************************
4316 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4318 static void dock_notify(struct ibm_struct *ibm, u32 event);
4319 static int dock_read(char *p);
4320 static int dock_write(char *buf);
4322 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4323 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4324 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4325 "\\_SB.PCI.ISA.SLCE", /* 570 */
4326 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4328 /* don't list other alternatives as we install a notify handler on the 570 */
4329 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4331 static const struct acpi_device_id ibm_pci_device_ids[] = {
4332 {PCI_ROOT_HID_STRING, 0},
4336 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4338 .notify = dock_notify,
4339 .handle = &dock_handle,
4340 .type = ACPI_SYSTEM_NOTIFY,
4343 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4344 * We just use it to get notifications of dock hotplug
4345 * in very old thinkpads */
4346 .hid = ibm_pci_device_ids,
4347 .notify = dock_notify,
4348 .handle = &pci_handle,
4349 .type = ACPI_SYSTEM_NOTIFY,
4353 static struct ibm_struct dock_driver_data[2] = {
4357 .write = dock_write,
4358 .acpi = &ibm_dock_acpidriver[0],
4362 .acpi = &ibm_dock_acpidriver[1],
4366 #define dock_docked() (_sta(dock_handle) & 1)
4368 static int __init dock_init(struct ibm_init_struct *iibm)
4370 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4372 TPACPI_ACPIHANDLE_INIT(dock);
4374 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4375 str_supported(dock_handle != NULL));
4377 return (dock_handle)? 0 : 1;
4380 static int __init dock_init2(struct ibm_init_struct *iibm)
4384 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4386 if (dock_driver_data[0].flags.acpi_driver_registered &&
4387 dock_driver_data[0].flags.acpi_notify_installed) {
4388 TPACPI_ACPIHANDLE_INIT(pci);
4389 dock2_needed = (pci_handle != NULL);
4390 vdbg_printk(TPACPI_DBG_INIT,
4391 "dock PCI handler for the TP 570 is %s\n",
4392 str_supported(dock2_needed));
4394 vdbg_printk(TPACPI_DBG_INIT,
4395 "dock subdriver part 2 not required\n");
4399 return (dock2_needed)? 0 : 1;
4402 static void dock_notify(struct ibm_struct *ibm, u32 event)
4404 int docked = dock_docked();
4405 int pci = ibm->acpi->hid && ibm->acpi->device &&
4406 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4409 if (event == 1 && !pci) /* 570 */
4410 data = 1; /* button */
4411 else if (event == 1 && pci) /* 570 */
4412 data = 3; /* dock */
4413 else if (event == 3 && docked)
4414 data = 1; /* button */
4415 else if (event == 3 && !docked)
4416 data = 2; /* undock */
4417 else if (event == 0 && docked)
4418 data = 3; /* dock */
4420 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4421 event, _sta(dock_handle));
4422 data = 0; /* unknown */
4424 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4425 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4426 dev_name(&ibm->acpi->device->dev),
4430 static int dock_read(char *p)
4433 int docked = dock_docked();
4436 len += sprintf(p + len, "status:\t\tnot supported\n");
4438 len += sprintf(p + len, "status:\t\tundocked\n");
4440 len += sprintf(p + len, "status:\t\tdocked\n");
4441 len += sprintf(p + len, "commands:\tdock, undock\n");
4447 static int dock_write(char *buf)
4454 while ((cmd = next_cmd(&buf))) {
4455 if (strlencmp(cmd, "undock") == 0) {
4456 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4457 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4459 } else if (strlencmp(cmd, "dock") == 0) {
4460 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4469 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4471 /*************************************************************************
4475 #ifdef CONFIG_THINKPAD_ACPI_BAY
4477 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4478 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4479 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4480 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4481 ); /* A21e, R30, R31 */
4482 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4483 "_EJ0", /* all others */
4484 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4485 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4486 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4488 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4492 static int __init bay_init(struct ibm_init_struct *iibm)
4494 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4496 TPACPI_ACPIHANDLE_INIT(bay);
4498 TPACPI_ACPIHANDLE_INIT(bay_ej);
4499 TPACPI_ACPIHANDLE_INIT(bay2);
4501 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4503 tp_features.bay_status = bay_handle &&
4504 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4505 tp_features.bay_status2 = bay2_handle &&
4506 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4508 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4509 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4510 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4511 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4513 vdbg_printk(TPACPI_DBG_INIT,
4514 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4515 str_supported(tp_features.bay_status),
4516 str_supported(tp_features.bay_eject),
4517 str_supported(tp_features.bay_status2),
4518 str_supported(tp_features.bay_eject2));
4520 return (tp_features.bay_status || tp_features.bay_eject ||
4521 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4524 static void bay_notify(struct ibm_struct *ibm, u32 event)
4526 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4527 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4528 dev_name(&ibm->acpi->device->dev),
4532 #define bay_occupied(b) (_sta(b##_handle) & 1)
4534 static int bay_read(char *p)
4537 int occupied = bay_occupied(bay);
4538 int occupied2 = bay_occupied(bay2);
4541 len += sprintf(p + len, "status:\t\t%s\n",
4542 tp_features.bay_status ?
4543 (occupied ? "occupied" : "unoccupied") :
4545 if (tp_features.bay_status2)
4546 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4547 "occupied" : "unoccupied");
4549 eject = tp_features.bay_eject && occupied;
4550 eject2 = tp_features.bay_eject2 && occupied2;
4552 if (eject && eject2)
4553 len += sprintf(p + len, "commands:\teject, eject2\n");
4555 len += sprintf(p + len, "commands:\teject\n");
4557 len += sprintf(p + len, "commands:\teject2\n");
4562 static int bay_write(char *buf)
4566 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4569 while ((cmd = next_cmd(&buf))) {
4570 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4571 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4573 } else if (tp_features.bay_eject2 &&
4574 strlencmp(cmd, "eject2") == 0) {
4575 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4584 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4585 .notify = bay_notify,
4586 .handle = &bay_handle,
4587 .type = ACPI_SYSTEM_NOTIFY,
4590 static struct ibm_struct bay_driver_data = {
4594 .acpi = &ibm_bay_acpidriver,
4597 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4599 /*************************************************************************
4603 /* sysfs cmos_command -------------------------------------------------- */
4604 static ssize_t cmos_command_store(struct device *dev,
4605 struct device_attribute *attr,
4606 const char *buf, size_t count)
4608 unsigned long cmos_cmd;
4611 if (parse_strtoul(buf, 21, &cmos_cmd))
4614 res = issue_thinkpad_cmos_command(cmos_cmd);
4615 return (res)? res : count;
4618 static struct device_attribute dev_attr_cmos_command =
4619 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4621 /* --------------------------------------------------------------------- */
4623 static int __init cmos_init(struct ibm_init_struct *iibm)
4627 vdbg_printk(TPACPI_DBG_INIT,
4628 "initializing cmos commands subdriver\n");
4630 TPACPI_ACPIHANDLE_INIT(cmos);
4632 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4633 str_supported(cmos_handle != NULL));
4635 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4639 return (cmos_handle)? 0 : 1;
4642 static void cmos_exit(void)
4644 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4647 static int cmos_read(char *p)
4651 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4652 R30, R31, T20-22, X20-21 */
4654 len += sprintf(p + len, "status:\t\tnot supported\n");
4656 len += sprintf(p + len, "status:\t\tsupported\n");
4657 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4663 static int cmos_write(char *buf)
4668 while ((cmd = next_cmd(&buf))) {
4669 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4670 cmos_cmd >= 0 && cmos_cmd <= 21) {
4675 res = issue_thinkpad_cmos_command(cmos_cmd);
4683 static struct ibm_struct cmos_driver_data = {
4686 .write = cmos_write,
4690 /*************************************************************************
4694 enum led_access_mode {
4695 TPACPI_LED_NONE = 0,
4696 TPACPI_LED_570, /* 570 */
4697 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4698 TPACPI_LED_NEW, /* all others */
4701 enum { /* For TPACPI_LED_OLD */
4702 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4703 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4704 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4713 static enum led_access_mode led_supported;
4715 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4716 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4717 /* T20-22, X20-21 */
4718 "LED", /* all others */
4721 #define TPACPI_LED_NUMLEDS 8
4722 static struct tpacpi_led_classdev *tpacpi_leds;
4723 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4724 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4725 /* there's a limit of 19 chars + NULL before 2.6.26 */
4727 "tpacpi:orange:batt",
4728 "tpacpi:green:batt",
4729 "tpacpi::dock_active",
4730 "tpacpi::bay_active",
4731 "tpacpi::dock_batt",
4732 "tpacpi::unknown_led",
4735 #define TPACPI_SAFE_LEDS 0x0081U
4737 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4739 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4742 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4746 static int led_get_status(const unsigned int led)
4749 enum led_status_t led_s;
4751 switch (led_supported) {
4752 case TPACPI_LED_570:
4753 if (!acpi_evalf(ec_handle,
4754 &status, "GLED", "dd", 1 << led))
4756 led_s = (status == 0)?
4761 tpacpi_led_state_cache[led] = led_s;
4770 static int led_set_status(const unsigned int led,
4771 const enum led_status_t ledstatus)
4773 /* off, on, blink. Index is led_status_t */
4774 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4775 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4779 switch (led_supported) {
4780 case TPACPI_LED_570:
4782 if (unlikely(led > 7))
4784 if (unlikely(tpacpi_is_led_restricted(led)))
4786 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4787 (1 << led), led_sled_arg1[ledstatus]))
4790 case TPACPI_LED_OLD:
4791 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4792 if (unlikely(led > 7))
4794 if (unlikely(tpacpi_is_led_restricted(led)))
4796 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4798 rc = ec_write(TPACPI_LED_EC_HLBL,
4799 (ledstatus == TPACPI_LED_BLINK) << led);
4801 rc = ec_write(TPACPI_LED_EC_HLCL,
4802 (ledstatus != TPACPI_LED_OFF) << led);
4804 case TPACPI_LED_NEW:
4806 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4808 if (unlikely(tpacpi_is_led_restricted(led)))
4810 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4811 led, led_led_arg1[ledstatus]))
4819 tpacpi_led_state_cache[led] = ledstatus;
4824 static void led_sysfs_set_status(unsigned int led,
4825 enum led_brightness brightness)
4828 (brightness == LED_OFF) ?
4830 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4831 TPACPI_LED_BLINK : TPACPI_LED_ON);
4834 static void led_set_status_worker(struct work_struct *work)
4836 struct tpacpi_led_classdev *data =
4837 container_of(work, struct tpacpi_led_classdev, work);
4839 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4840 led_sysfs_set_status(data->led, data->new_brightness);
4843 static void led_sysfs_set(struct led_classdev *led_cdev,
4844 enum led_brightness brightness)
4846 struct tpacpi_led_classdev *data = container_of(led_cdev,
4847 struct tpacpi_led_classdev, led_classdev);
4849 data->new_brightness = brightness;
4850 queue_work(tpacpi_wq, &data->work);
4853 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4854 unsigned long *delay_on, unsigned long *delay_off)
4856 struct tpacpi_led_classdev *data = container_of(led_cdev,
4857 struct tpacpi_led_classdev, led_classdev);
4859 /* Can we choose the flash rate? */
4860 if (*delay_on == 0 && *delay_off == 0) {
4861 /* yes. set them to the hardware blink rate (1 Hz) */
4862 *delay_on = 500; /* ms */
4863 *delay_off = 500; /* ms */
4864 } else if ((*delay_on != 500) || (*delay_off != 500))
4867 data->new_brightness = TPACPI_LED_BLINK;
4868 queue_work(tpacpi_wq, &data->work);
4873 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4877 struct tpacpi_led_classdev *data = container_of(led_cdev,
4878 struct tpacpi_led_classdev, led_classdev);
4880 rc = led_get_status(data->led);
4882 if (rc == TPACPI_LED_OFF || rc < 0)
4883 rc = LED_OFF; /* no error handling in led class :( */
4890 static void led_exit(void)
4894 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4895 if (tpacpi_leds[i].led_classdev.name)
4896 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4902 static int __init tpacpi_init_led(unsigned int led)
4906 tpacpi_leds[led].led = led;
4908 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4909 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4910 if (led_supported == TPACPI_LED_570)
4911 tpacpi_leds[led].led_classdev.brightness_get =
4914 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4916 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4918 rc = led_classdev_register(&tpacpi_pdev->dev,
4919 &tpacpi_leds[led].led_classdev);
4921 tpacpi_leds[led].led_classdev.name = NULL;
4926 static int __init led_init(struct ibm_init_struct *iibm)
4931 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4933 TPACPI_ACPIHANDLE_INIT(led);
4936 /* led not supported on R30, R31 */
4937 led_supported = TPACPI_LED_NONE;
4938 else if (strlencmp(led_path, "SLED") == 0)
4940 led_supported = TPACPI_LED_570;
4941 else if (strlencmp(led_path, "SYSL") == 0)
4942 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4943 led_supported = TPACPI_LED_OLD;
4946 led_supported = TPACPI_LED_NEW;
4948 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4949 str_supported(led_supported), led_supported);
4951 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4954 printk(TPACPI_ERR "Out of memory for LED data\n");
4958 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4959 if (!tpacpi_is_led_restricted(i)) {
4960 rc = tpacpi_init_led(i);
4968 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4969 if (led_supported != TPACPI_LED_NONE)
4970 printk(TPACPI_NOTICE
4971 "warning: userspace override of important "
4972 "firmware LEDs is enabled\n");
4974 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4977 #define str_led_status(s) \
4978 ((s) == TPACPI_LED_OFF ? "off" : \
4979 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4981 static int led_read(char *p)
4985 if (!led_supported) {
4986 len += sprintf(p + len, "status:\t\tnot supported\n");
4989 len += sprintf(p + len, "status:\t\tsupported\n");
4991 if (led_supported == TPACPI_LED_570) {
4994 for (i = 0; i < 8; i++) {
4995 status = led_get_status(i);
4998 len += sprintf(p + len, "%d:\t\t%s\n",
4999 i, str_led_status(status));
5003 len += sprintf(p + len, "commands:\t"
5004 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5009 static int led_write(char *buf)
5013 enum led_status_t s;
5018 while ((cmd = next_cmd(&buf))) {
5019 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5022 if (strstr(cmd, "off")) {
5024 } else if (strstr(cmd, "on")) {
5026 } else if (strstr(cmd, "blink")) {
5027 s = TPACPI_LED_BLINK;
5032 rc = led_set_status(led, s);
5040 static struct ibm_struct led_driver_data = {
5047 /*************************************************************************
5051 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5053 static int __init beep_init(struct ibm_init_struct *iibm)
5055 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5057 TPACPI_ACPIHANDLE_INIT(beep);
5059 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5060 str_supported(beep_handle != NULL));
5062 return (beep_handle)? 0 : 1;
5065 static int beep_read(char *p)
5070 len += sprintf(p + len, "status:\t\tnot supported\n");
5072 len += sprintf(p + len, "status:\t\tsupported\n");
5073 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5079 static int beep_write(char *buf)
5087 while ((cmd = next_cmd(&buf))) {
5088 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5089 beep_cmd >= 0 && beep_cmd <= 17) {
5093 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5100 static struct ibm_struct beep_driver_data = {
5103 .write = beep_write,
5106 /*************************************************************************
5110 enum thermal_access_mode {
5111 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5112 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5113 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5114 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5115 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5118 enum { /* TPACPI_THERMAL_TPEC_* */
5119 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5120 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5121 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5124 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5125 struct ibm_thermal_sensors_struct {
5126 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5129 static enum thermal_access_mode thermal_read_mode;
5131 /* idx is zero-based */
5132 static int thermal_get_sensor(int idx, s32 *value)
5138 t = TP_EC_THERMAL_TMP0;
5140 switch (thermal_read_mode) {
5141 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5142 case TPACPI_THERMAL_TPEC_16:
5143 if (idx >= 8 && idx <= 15) {
5144 t = TP_EC_THERMAL_TMP8;
5149 case TPACPI_THERMAL_TPEC_8:
5151 if (!acpi_ec_read(t + idx, &tmp))
5153 *value = tmp * 1000;
5158 case TPACPI_THERMAL_ACPI_UPDT:
5160 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5161 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5163 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5165 *value = (t - 2732) * 100;
5170 case TPACPI_THERMAL_ACPI_TMP07:
5172 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5173 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5175 if (t > 127 || t < -127)
5176 t = TP_EC_THERMAL_TMP_NA;
5182 case TPACPI_THERMAL_NONE:
5190 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5201 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5204 for (i = 0 ; i < n; i++) {
5205 res = thermal_get_sensor(i, &s->temp[i]);
5213 /* sysfs temp##_input -------------------------------------------------- */
5215 static ssize_t thermal_temp_input_show(struct device *dev,
5216 struct device_attribute *attr,
5219 struct sensor_device_attribute *sensor_attr =
5220 to_sensor_dev_attr(attr);
5221 int idx = sensor_attr->index;
5225 res = thermal_get_sensor(idx, &value);
5228 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5231 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5234 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5235 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5236 thermal_temp_input_show, NULL, _idxB)
5238 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5239 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5240 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5241 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5242 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5243 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5244 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5245 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5246 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5247 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5248 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5249 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5250 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5251 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5252 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5253 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5254 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5257 #define THERMAL_ATTRS(X) \
5258 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5260 static struct attribute *thermal_temp_input_attr[] = {
5280 static const struct attribute_group thermal_temp_input16_group = {
5281 .attrs = thermal_temp_input_attr
5284 static const struct attribute_group thermal_temp_input8_group = {
5285 .attrs = &thermal_temp_input_attr[8]
5288 #undef THERMAL_SENSOR_ATTR_TEMP
5289 #undef THERMAL_ATTRS
5291 /* --------------------------------------------------------------------- */
5293 static int __init thermal_init(struct ibm_init_struct *iibm)
5300 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5302 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5304 if (thinkpad_id.ec_model) {
5306 * Direct EC access mode: sensors at registers
5307 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5308 * non-implemented, thermal sensors return 0x80 when
5313 for (i = 0; i < 8; i++) {
5314 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5320 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5328 /* This is sheer paranoia, but we handle it anyway */
5331 "ThinkPad ACPI EC access misbehaving, "
5332 "falling back to ACPI TMPx access "
5334 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5337 "ThinkPad ACPI EC access misbehaving, "
5338 "disabling thermal sensors access\n");
5339 thermal_read_mode = TPACPI_THERMAL_NONE;
5344 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5346 } else if (acpi_tmp7) {
5347 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5348 /* 600e/x, 770e, 770x */
5349 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5351 /* Standard ACPI TMPx access, max 8 sensors */
5352 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5355 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5356 thermal_read_mode = TPACPI_THERMAL_NONE;
5359 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5360 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5363 switch (thermal_read_mode) {
5364 case TPACPI_THERMAL_TPEC_16:
5365 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5366 &thermal_temp_input16_group);
5370 case TPACPI_THERMAL_TPEC_8:
5371 case TPACPI_THERMAL_ACPI_TMP07:
5372 case TPACPI_THERMAL_ACPI_UPDT:
5373 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5374 &thermal_temp_input8_group);
5378 case TPACPI_THERMAL_NONE:
5386 static void thermal_exit(void)
5388 switch (thermal_read_mode) {
5389 case TPACPI_THERMAL_TPEC_16:
5390 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5391 &thermal_temp_input16_group);
5393 case TPACPI_THERMAL_TPEC_8:
5394 case TPACPI_THERMAL_ACPI_TMP07:
5395 case TPACPI_THERMAL_ACPI_UPDT:
5396 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5397 &thermal_temp_input16_group);
5399 case TPACPI_THERMAL_NONE:
5405 static int thermal_read(char *p)
5409 struct ibm_thermal_sensors_struct t;
5411 n = thermal_get_sensors(&t);
5412 if (unlikely(n < 0))
5415 len += sprintf(p + len, "temperatures:\t");
5418 for (i = 0; i < (n - 1); i++)
5419 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5420 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5422 len += sprintf(p + len, "not supported\n");
5427 static struct ibm_struct thermal_driver_data = {
5429 .read = thermal_read,
5430 .exit = thermal_exit,
5433 /*************************************************************************
5437 static u8 ecdump_regs[256];
5439 static int ecdump_read(char *p)
5445 len += sprintf(p + len, "EC "
5446 " +00 +01 +02 +03 +04 +05 +06 +07"
5447 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5448 for (i = 0; i < 256; i += 16) {
5449 len += sprintf(p + len, "EC 0x%02x:", i);
5450 for (j = 0; j < 16; j++) {
5451 if (!acpi_ec_read(i + j, &v))
5453 if (v != ecdump_regs[i + j])
5454 len += sprintf(p + len, " *%02x", v);
5456 len += sprintf(p + len, " %02x", v);
5457 ecdump_regs[i + j] = v;
5459 len += sprintf(p + len, "\n");
5464 /* These are way too dangerous to advertise openly... */
5466 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5467 " (<offset> is 00-ff, <value> is 00-ff)\n");
5468 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5469 " (<offset> is 00-ff, <value> is 0-255)\n");
5474 static int ecdump_write(char *buf)
5479 while ((cmd = next_cmd(&buf))) {
5480 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5482 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5486 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5487 if (!acpi_ec_write(i, v))
5496 static struct ibm_struct ecdump_driver_data = {
5498 .read = ecdump_read,
5499 .write = ecdump_write,
5500 .flags.experimental = 1,
5503 /*************************************************************************
5504 * Backlight/brightness subdriver
5507 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5510 TP_EC_BACKLIGHT = 0x31,
5512 /* TP_EC_BACKLIGHT bitmasks */
5513 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5514 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5515 TP_EC_BACKLIGHT_MAPSW = 0x20,
5518 static struct backlight_device *ibm_backlight_device;
5519 static int brightness_mode;
5520 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5522 static struct mutex brightness_mutex;
5525 * ThinkPads can read brightness from two places: EC 0x31, or
5526 * CMOS NVRAM byte 0x5E, bits 0-3.
5528 * EC 0x31 has the following layout
5529 * Bit 7: unknown function
5530 * Bit 6: unknown function
5531 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5532 * Bit 4: must be set to zero to avoid problems
5533 * Bit 3-0: backlight brightness level
5535 * brightness_get_raw returns status data in the EC 0x31 layout
5537 static int brightness_get_raw(int *status)
5539 u8 lec = 0, lcmos = 0, level = 0;
5541 if (brightness_mode & 1) {
5542 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5544 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5546 if (brightness_mode & 2) {
5547 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5548 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5549 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5550 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5554 if (brightness_mode == 3) {
5555 *status = lec; /* Prefer EC, CMOS is just a backing store */
5556 lec &= TP_EC_BACKLIGHT_LVLMSK;
5558 tp_warned.bright_cmos_ec_unsync = 0;
5560 if (!tp_warned.bright_cmos_ec_unsync) {
5562 "CMOS NVRAM (%u) and EC (%u) do not "
5563 "agree on display brightness level\n",
5564 (unsigned int) lcmos,
5565 (unsigned int) lec);
5566 tp_warned.bright_cmos_ec_unsync = 1;
5577 /* May return EINTR which can always be mapped to ERESTARTSYS */
5578 static int brightness_set(int value)
5580 int cmos_cmd, inc, i, res;
5584 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5588 res = mutex_lock_killable(&brightness_mutex);
5592 res = brightness_get_raw(¤t_value);
5596 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5597 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5599 cmos_cmd = value > current_value ?
5600 TP_CMOS_BRIGHTNESS_UP :
5601 TP_CMOS_BRIGHTNESS_DOWN;
5602 inc = (value > current_value)? 1 : -1;
5605 for (i = current_value; i != value; i += inc) {
5606 if ((brightness_mode & 2) &&
5607 issue_thinkpad_cmos_command(cmos_cmd)) {
5611 if ((brightness_mode & 1) &&
5612 !acpi_ec_write(TP_EC_BACKLIGHT,
5613 (i + inc) | command_bits)) {
5620 mutex_unlock(&brightness_mutex);
5624 /* sysfs backlight class ----------------------------------------------- */
5626 static int brightness_update_status(struct backlight_device *bd)
5628 /* it is the backlight class's job (caller) to handle
5629 * EINTR and other errors properly */
5630 return brightness_set(
5631 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5632 bd->props.power == FB_BLANK_UNBLANK) ?
5633 bd->props.brightness : 0);
5636 static int brightness_get(struct backlight_device *bd)
5640 res = brightness_get_raw(&status);
5642 return 0; /* FIXME: teach backlight about error handling */
5644 return status & TP_EC_BACKLIGHT_LVLMSK;
5647 static struct backlight_ops ibm_backlight_data = {
5648 .get_brightness = brightness_get,
5649 .update_status = brightness_update_status,
5652 /* --------------------------------------------------------------------- */
5654 static int __init brightness_init(struct ibm_init_struct *iibm)
5658 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5660 mutex_init(&brightness_mutex);
5663 * We always attempt to detect acpi support, so as to switch
5664 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5665 * going to publish a backlight interface
5667 b = tpacpi_check_std_acpi_brightness_support();
5670 if (acpi_video_backlight_support()) {
5671 if (brightness_enable > 1) {
5672 printk(TPACPI_NOTICE
5673 "Standard ACPI backlight interface "
5674 "available, not loading native one.\n");
5676 } else if (brightness_enable == 1) {
5677 printk(TPACPI_NOTICE
5678 "Backlight control force enabled, even if standard "
5679 "ACPI backlight interface is available\n");
5682 if (brightness_enable > 1) {
5683 printk(TPACPI_NOTICE
5684 "Standard ACPI backlight interface not "
5685 "available, thinkpad_acpi native "
5686 "brightness control enabled\n");
5691 if (!brightness_enable) {
5692 dbg_printk(TPACPI_DBG_INIT,
5693 "brightness support disabled by "
5694 "module parameter\n");
5700 "Unsupported brightness interface, "
5701 "please contact %s\n", TPACPI_MAIL);
5705 tp_features.bright_16levels = 1;
5707 if (!brightness_mode) {
5708 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5709 brightness_mode = 2;
5711 brightness_mode = 3;
5713 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5717 if (brightness_mode > 3)
5720 if (brightness_get_raw(&b) < 0)
5723 if (tp_features.bright_16levels)
5725 "detected a 16-level brightness capable ThinkPad\n");
5727 ibm_backlight_device = backlight_device_register(
5728 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5729 &ibm_backlight_data);
5730 if (IS_ERR(ibm_backlight_device)) {
5731 printk(TPACPI_ERR "Could not register backlight device\n");
5732 return PTR_ERR(ibm_backlight_device);
5734 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5736 ibm_backlight_device->props.max_brightness =
5737 (tp_features.bright_16levels)? 15 : 7;
5738 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5739 backlight_update_status(ibm_backlight_device);
5744 static void brightness_exit(void)
5746 if (ibm_backlight_device) {
5747 vdbg_printk(TPACPI_DBG_EXIT,
5748 "calling backlight_device_unregister()\n");
5749 backlight_device_unregister(ibm_backlight_device);
5753 static int brightness_read(char *p)
5758 level = brightness_get(NULL);
5760 len += sprintf(p + len, "level:\t\tunreadable\n");
5762 len += sprintf(p + len, "level:\t\t%d\n", level);
5763 len += sprintf(p + len, "commands:\tup, down\n");
5764 len += sprintf(p + len, "commands:\tlevel <level>"
5765 " (<level> is 0-%d)\n",
5766 (tp_features.bright_16levels) ? 15 : 7);
5772 static int brightness_write(char *buf)
5777 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5779 level = brightness_get(NULL);
5783 while ((cmd = next_cmd(&buf))) {
5784 if (strlencmp(cmd, "up") == 0) {
5785 if (level < max_level)
5787 } else if (strlencmp(cmd, "down") == 0) {
5790 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5791 level >= 0 && level <= max_level) {
5798 * Now we know what the final level should be, so we try to set it.
5799 * Doing it this way makes the syscall restartable in case of EINTR
5801 rc = brightness_set(level);
5802 return (rc == -EINTR)? ERESTARTSYS : rc;
5805 static struct ibm_struct brightness_driver_data = {
5806 .name = "brightness",
5807 .read = brightness_read,
5808 .write = brightness_write,
5809 .exit = brightness_exit,
5812 /*************************************************************************
5816 static int volume_offset = 0x30;
5818 static int volume_read(char *p)
5823 if (!acpi_ec_read(volume_offset, &level)) {
5824 len += sprintf(p + len, "level:\t\tunreadable\n");
5826 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5827 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5828 len += sprintf(p + len, "commands:\tup, down, mute\n");
5829 len += sprintf(p + len, "commands:\tlevel <level>"
5830 " (<level> is 0-15)\n");
5836 static int volume_write(char *buf)
5838 int cmos_cmd, inc, i;
5840 int new_level, new_mute;
5843 while ((cmd = next_cmd(&buf))) {
5844 if (!acpi_ec_read(volume_offset, &level))
5846 new_mute = mute = level & 0x40;
5847 new_level = level = level & 0xf;
5849 if (strlencmp(cmd, "up") == 0) {
5853 new_level = level == 15 ? 15 : level + 1;
5854 } else if (strlencmp(cmd, "down") == 0) {
5858 new_level = level == 0 ? 0 : level - 1;
5859 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5860 new_level >= 0 && new_level <= 15) {
5862 } else if (strlencmp(cmd, "mute") == 0) {
5867 if (new_level != level) {
5868 /* mute doesn't change */
5870 cmos_cmd = (new_level > level) ?
5871 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5872 inc = new_level > level ? 1 : -1;
5874 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5875 !acpi_ec_write(volume_offset, level)))
5878 for (i = level; i != new_level; i += inc)
5879 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5880 !acpi_ec_write(volume_offset, i + inc))
5884 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5885 !acpi_ec_write(volume_offset, new_level + mute))) {
5890 if (new_mute != mute) {
5891 /* level doesn't change */
5893 cmos_cmd = (new_mute) ?
5894 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5896 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5897 !acpi_ec_write(volume_offset, level + new_mute))
5905 static struct ibm_struct volume_driver_data = {
5907 .read = volume_read,
5908 .write = volume_write,
5911 /*************************************************************************
5918 * TPACPI_FAN_RD_ACPI_GFAN:
5919 * ACPI GFAN method: returns fan level
5921 * see TPACPI_FAN_WR_ACPI_SFAN
5922 * EC 0x2f (HFSP) not available if GFAN exists
5924 * TPACPI_FAN_WR_ACPI_SFAN:
5925 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5927 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5930 * TPACPI_FAN_WR_TPEC:
5931 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5932 * Supported on almost all ThinkPads
5934 * Fan speed changes of any sort (including those caused by the
5935 * disengaged mode) are usually done slowly by the firmware as the
5936 * maximum ammount of fan duty cycle change per second seems to be
5939 * Reading is not available if GFAN exists.
5940 * Writing is not available if SFAN exists.
5943 * 7 automatic mode engaged;
5944 * (default operation mode of the ThinkPad)
5945 * fan level is ignored in this mode.
5946 * 6 full speed mode (takes precedence over bit 7);
5947 * not available on all thinkpads. May disable
5948 * the tachometer while the fan controller ramps up
5949 * the speed (which can take up to a few *minutes*).
5950 * Speeds up fan to 100% duty-cycle, which is far above
5951 * the standard RPM levels. It is not impossible that
5952 * it could cause hardware damage.
5953 * 5-3 unused in some models. Extra bits for fan level
5954 * in others, but still useless as all values above
5955 * 7 map to the same speed as level 7 in these models.
5956 * 2-0 fan level (0..7 usually)
5958 * 0x07 = max (set when temperatures critical)
5959 * Some ThinkPads may have other levels, see
5960 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5962 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5963 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5964 * does so, its initial value is meaningless (0x07).
5966 * For firmware bugs, refer to:
5967 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5971 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5972 * Main fan tachometer reading (in RPM)
5974 * This register is present on all ThinkPads with a new-style EC, and
5975 * it is known not to be present on the A21m/e, and T22, as there is
5976 * something else in offset 0x84 according to the ACPI DSDT. Other
5977 * ThinkPads from this same time period (and earlier) probably lack the
5978 * tachometer as well.
5980 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5981 * was never fixed by IBM to report the EC firmware version string
5982 * probably support the tachometer (like the early X models), so
5983 * detecting it is quite hard. We need more data to know for sure.
5985 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5988 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5991 * For firmware bugs, refer to:
5992 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5994 * TPACPI_FAN_WR_ACPI_FANS:
5995 * ThinkPad X31, X40, X41. Not available in the X60.
5997 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5998 * high speed. ACPI DSDT seems to map these three speeds to levels
5999 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6000 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6002 * The speeds are stored on handles
6003 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6005 * There are three default speed sets, acessible as handles:
6006 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6008 * ACPI DSDT switches which set is in use depending on various
6011 * TPACPI_FAN_WR_TPEC is also available and should be used to
6012 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6013 * but the ACPI tables just mention level 7.
6016 enum { /* Fan control constants */
6017 fan_status_offset = 0x2f, /* EC register 0x2f */
6018 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6019 * 0x84 must be read before 0x85 */
6021 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6022 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6024 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6027 enum fan_status_access_mode {
6028 TPACPI_FAN_NONE = 0, /* No fan status or control */
6029 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6030 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6033 enum fan_control_access_mode {
6034 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6035 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6036 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6037 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6040 enum fan_control_commands {
6041 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6042 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6043 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6044 * and also watchdog cmd */
6047 static int fan_control_allowed;
6049 static enum fan_status_access_mode fan_status_access_mode;
6050 static enum fan_control_access_mode fan_control_access_mode;
6051 static enum fan_control_commands fan_control_commands;
6053 static u8 fan_control_initial_status;
6054 static u8 fan_control_desired_level;
6055 static u8 fan_control_resume_level;
6056 static int fan_watchdog_maxinterval;
6058 static struct mutex fan_mutex;
6060 static void fan_watchdog_fire(struct work_struct *ignored);
6061 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6063 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6064 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6065 "\\FSPD", /* 600e/x, 770e, 770x */
6067 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6068 "JFNS", /* 770x-JL */
6072 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6073 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6074 * be in auto mode (0x80).
6076 * This is corrected by any write to HFSP either by the driver, or
6079 * We assume 0x07 really means auto mode while this quirk is active,
6080 * as this is far more likely than the ThinkPad being in level 7,
6081 * which is only used by the firmware during thermal emergencies.
6084 static void fan_quirk1_detect(void)
6086 /* In some ThinkPads, neither the EC nor the ACPI
6087 * DSDT initialize the HFSP register, and it ends up
6088 * being initially set to 0x07 when it *could* be
6089 * either 0x07 or 0x80.
6091 * Enable for TP-1Y (T43), TP-78 (R51e),
6092 * TP-76 (R52), TP-70 (T43, R52), which are known
6094 if (fan_control_initial_status == 0x07) {
6095 switch (thinkpad_id.ec_model) {
6096 case 0x5931: /* TP-1Y */
6097 case 0x3837: /* TP-78 */
6098 case 0x3637: /* TP-76 */
6099 case 0x3037: /* TP-70 */
6100 printk(TPACPI_NOTICE
6101 "fan_init: initial fan status is unknown, "
6102 "assuming it is in auto mode\n");
6103 tp_features.fan_ctrl_status_undef = 1;
6109 static void fan_quirk1_handle(u8 *fan_status)
6111 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6112 if (*fan_status != fan_control_initial_status) {
6113 /* something changed the HFSP regisnter since
6114 * driver init time, so it is not undefined
6116 tp_features.fan_ctrl_status_undef = 0;
6118 /* Return most likely status. In fact, it
6119 * might be the only possible status */
6120 *fan_status = TP_EC_FAN_AUTO;
6126 * Call with fan_mutex held
6128 static void fan_update_desired_level(u8 status)
6131 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6133 fan_control_desired_level = 7;
6135 fan_control_desired_level = status;
6139 static int fan_get_status(u8 *status)
6144 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6146 switch (fan_status_access_mode) {
6147 case TPACPI_FAN_RD_ACPI_GFAN:
6148 /* 570, 600e/x, 770e, 770x */
6150 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6158 case TPACPI_FAN_RD_TPEC:
6159 /* all except 570, 600e/x, 770e, 770x */
6160 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6163 if (likely(status)) {
6165 fan_quirk1_handle(status);
6177 static int fan_get_status_safe(u8 *status)
6182 if (mutex_lock_killable(&fan_mutex))
6183 return -ERESTARTSYS;
6184 rc = fan_get_status(&s);
6186 fan_update_desired_level(s);
6187 mutex_unlock(&fan_mutex);
6195 static int fan_get_speed(unsigned int *speed)
6199 switch (fan_status_access_mode) {
6200 case TPACPI_FAN_RD_TPEC:
6201 /* all except 570, 600e/x, 770e, 770x */
6202 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6203 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6207 *speed = (hi << 8) | lo;
6218 static int fan_set_level(int level)
6220 if (!fan_control_allowed)
6223 switch (fan_control_access_mode) {
6224 case TPACPI_FAN_WR_ACPI_SFAN:
6225 if (level >= 0 && level <= 7) {
6226 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6232 case TPACPI_FAN_WR_ACPI_FANS:
6233 case TPACPI_FAN_WR_TPEC:
6234 if (!(level & TP_EC_FAN_AUTO) &&
6235 !(level & TP_EC_FAN_FULLSPEED) &&
6236 ((level < 0) || (level > 7)))
6239 /* safety net should the EC not support AUTO
6240 * or FULLSPEED mode bits and just ignore them */
6241 if (level & TP_EC_FAN_FULLSPEED)
6242 level |= 7; /* safety min speed 7 */
6243 else if (level & TP_EC_FAN_AUTO)
6244 level |= 4; /* safety min speed 4 */
6246 if (!acpi_ec_write(fan_status_offset, level))
6249 tp_features.fan_ctrl_status_undef = 0;
6258 static int fan_set_level_safe(int level)
6262 if (!fan_control_allowed)
6265 if (mutex_lock_killable(&fan_mutex))
6266 return -ERESTARTSYS;
6268 if (level == TPACPI_FAN_LAST_LEVEL)
6269 level = fan_control_desired_level;
6271 rc = fan_set_level(level);
6273 fan_update_desired_level(level);
6275 mutex_unlock(&fan_mutex);
6279 static int fan_set_enable(void)
6284 if (!fan_control_allowed)
6287 if (mutex_lock_killable(&fan_mutex))
6288 return -ERESTARTSYS;
6290 switch (fan_control_access_mode) {
6291 case TPACPI_FAN_WR_ACPI_FANS:
6292 case TPACPI_FAN_WR_TPEC:
6293 rc = fan_get_status(&s);
6297 /* Don't go out of emergency fan mode */
6300 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6303 if (!acpi_ec_write(fan_status_offset, s))
6306 tp_features.fan_ctrl_status_undef = 0;
6311 case TPACPI_FAN_WR_ACPI_SFAN:
6312 rc = fan_get_status(&s);
6318 /* Set fan to at least level 4 */
6321 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6331 mutex_unlock(&fan_mutex);
6335 static int fan_set_disable(void)
6339 if (!fan_control_allowed)
6342 if (mutex_lock_killable(&fan_mutex))
6343 return -ERESTARTSYS;
6346 switch (fan_control_access_mode) {
6347 case TPACPI_FAN_WR_ACPI_FANS:
6348 case TPACPI_FAN_WR_TPEC:
6349 if (!acpi_ec_write(fan_status_offset, 0x00))
6352 fan_control_desired_level = 0;
6353 tp_features.fan_ctrl_status_undef = 0;
6357 case TPACPI_FAN_WR_ACPI_SFAN:
6358 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6361 fan_control_desired_level = 0;
6369 mutex_unlock(&fan_mutex);
6373 static int fan_set_speed(int speed)
6377 if (!fan_control_allowed)
6380 if (mutex_lock_killable(&fan_mutex))
6381 return -ERESTARTSYS;
6384 switch (fan_control_access_mode) {
6385 case TPACPI_FAN_WR_ACPI_FANS:
6386 if (speed >= 0 && speed <= 65535) {
6387 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6388 speed, speed, speed))
6398 mutex_unlock(&fan_mutex);
6402 static void fan_watchdog_reset(void)
6404 static int fan_watchdog_active;
6406 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6409 if (fan_watchdog_active)
6410 cancel_delayed_work(&fan_watchdog_task);
6412 if (fan_watchdog_maxinterval > 0 &&
6413 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6414 fan_watchdog_active = 1;
6415 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6416 msecs_to_jiffies(fan_watchdog_maxinterval
6419 "failed to queue the fan watchdog, "
6420 "watchdog will not trigger\n");
6423 fan_watchdog_active = 0;
6426 static void fan_watchdog_fire(struct work_struct *ignored)
6430 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6433 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6434 rc = fan_set_enable();
6436 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6437 "will try again later...\n", -rc);
6438 /* reschedule for later */
6439 fan_watchdog_reset();
6444 * SYSFS fan layout: hwmon compatible (device)
6447 * 0: "disengaged" mode
6449 * 2: native EC "auto" mode (recommended, hardware default)
6451 * pwm*: set speed in manual mode, ignored otherwise.
6452 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6455 * fan*_input: tachometer reading, RPM
6458 * SYSFS fan layout: extensions
6460 * fan_watchdog (driver):
6461 * fan watchdog interval in seconds, 0 disables (default), max 120
6464 /* sysfs fan pwm1_enable ----------------------------------------------- */
6465 static ssize_t fan_pwm1_enable_show(struct device *dev,
6466 struct device_attribute *attr,
6472 res = fan_get_status_safe(&status);
6476 if (status & TP_EC_FAN_FULLSPEED) {
6478 } else if (status & TP_EC_FAN_AUTO) {
6483 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6486 static ssize_t fan_pwm1_enable_store(struct device *dev,
6487 struct device_attribute *attr,
6488 const char *buf, size_t count)
6493 if (parse_strtoul(buf, 2, &t))
6498 level = TP_EC_FAN_FULLSPEED;
6501 level = TPACPI_FAN_LAST_LEVEL;
6504 level = TP_EC_FAN_AUTO;
6507 /* reserved for software-controlled auto mode */
6513 res = fan_set_level_safe(level);
6519 fan_watchdog_reset();
6524 static struct device_attribute dev_attr_fan_pwm1_enable =
6525 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6526 fan_pwm1_enable_show, fan_pwm1_enable_store);
6528 /* sysfs fan pwm1 ------------------------------------------------------ */
6529 static ssize_t fan_pwm1_show(struct device *dev,
6530 struct device_attribute *attr,
6536 res = fan_get_status_safe(&status);
6541 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6542 status = fan_control_desired_level;
6547 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6550 static ssize_t fan_pwm1_store(struct device *dev,
6551 struct device_attribute *attr,
6552 const char *buf, size_t count)
6556 u8 status, newlevel;
6558 if (parse_strtoul(buf, 255, &s))
6561 /* scale down from 0-255 to 0-7 */
6562 newlevel = (s >> 5) & 0x07;
6564 if (mutex_lock_killable(&fan_mutex))
6565 return -ERESTARTSYS;
6567 rc = fan_get_status(&status);
6568 if (!rc && (status &
6569 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6570 rc = fan_set_level(newlevel);
6574 fan_update_desired_level(newlevel);
6575 fan_watchdog_reset();
6579 mutex_unlock(&fan_mutex);
6580 return (rc)? rc : count;
6583 static struct device_attribute dev_attr_fan_pwm1 =
6584 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6585 fan_pwm1_show, fan_pwm1_store);
6587 /* sysfs fan fan1_input ------------------------------------------------ */
6588 static ssize_t fan_fan1_input_show(struct device *dev,
6589 struct device_attribute *attr,
6595 res = fan_get_speed(&speed);
6599 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6602 static struct device_attribute dev_attr_fan_fan1_input =
6603 __ATTR(fan1_input, S_IRUGO,
6604 fan_fan1_input_show, NULL);
6606 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6607 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6610 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6613 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6614 const char *buf, size_t count)
6618 if (parse_strtoul(buf, 120, &t))
6621 if (!fan_control_allowed)
6624 fan_watchdog_maxinterval = t;
6625 fan_watchdog_reset();
6630 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6631 fan_fan_watchdog_show, fan_fan_watchdog_store);
6633 /* --------------------------------------------------------------------- */
6634 static struct attribute *fan_attributes[] = {
6635 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6636 &dev_attr_fan_fan1_input.attr,
6640 static const struct attribute_group fan_attr_group = {
6641 .attrs = fan_attributes,
6644 static int __init fan_init(struct ibm_init_struct *iibm)
6648 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6650 mutex_init(&fan_mutex);
6651 fan_status_access_mode = TPACPI_FAN_NONE;
6652 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6653 fan_control_commands = 0;
6654 fan_watchdog_maxinterval = 0;
6655 tp_features.fan_ctrl_status_undef = 0;
6656 fan_control_desired_level = 7;
6658 TPACPI_ACPIHANDLE_INIT(fans);
6659 TPACPI_ACPIHANDLE_INIT(gfan);
6660 TPACPI_ACPIHANDLE_INIT(sfan);
6663 /* 570, 600e/x, 770e, 770x */
6664 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6666 /* all other ThinkPads: note that even old-style
6667 * ThinkPad ECs supports the fan control register */
6668 if (likely(acpi_ec_read(fan_status_offset,
6669 &fan_control_initial_status))) {
6670 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6671 fan_quirk1_detect();
6674 "ThinkPad ACPI EC access misbehaving, "
6675 "fan status and control unavailable\n");
6682 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6683 fan_control_commands |=
6684 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6687 /* gfan without sfan means no fan control */
6688 /* all other models implement TP EC 0x2f control */
6692 fan_control_access_mode =
6693 TPACPI_FAN_WR_ACPI_FANS;
6694 fan_control_commands |=
6695 TPACPI_FAN_CMD_SPEED |
6696 TPACPI_FAN_CMD_LEVEL |
6697 TPACPI_FAN_CMD_ENABLE;
6699 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6700 fan_control_commands |=
6701 TPACPI_FAN_CMD_LEVEL |
6702 TPACPI_FAN_CMD_ENABLE;
6707 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6708 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6709 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6710 fan_status_access_mode, fan_control_access_mode);
6712 /* fan control master switch */
6713 if (!fan_control_allowed) {
6714 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6715 fan_control_commands = 0;
6716 dbg_printk(TPACPI_DBG_INIT,
6717 "fan control features disabled by parameter\n");
6720 /* update fan_control_desired_level */
6721 if (fan_status_access_mode != TPACPI_FAN_NONE)
6722 fan_get_status_safe(NULL);
6724 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6725 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6726 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6731 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6732 &driver_attr_fan_watchdog);
6734 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6743 static void fan_exit(void)
6745 vdbg_printk(TPACPI_DBG_EXIT,
6746 "cancelling any pending fan watchdog tasks\n");
6748 /* FIXME: can we really do this unconditionally? */
6749 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6750 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6751 &driver_attr_fan_watchdog);
6753 cancel_delayed_work(&fan_watchdog_task);
6754 flush_workqueue(tpacpi_wq);
6757 static void fan_suspend(pm_message_t state)
6761 if (!fan_control_allowed)
6764 /* Store fan status in cache */
6765 fan_control_resume_level = 0;
6766 rc = fan_get_status_safe(&fan_control_resume_level);
6768 printk(TPACPI_NOTICE
6769 "failed to read fan level for later "
6770 "restore during resume: %d\n", rc);
6772 /* if it is undefined, don't attempt to restore it.
6774 if (tp_features.fan_ctrl_status_undef)
6775 fan_control_resume_level = 0;
6778 static void fan_resume(void)
6780 u8 current_level = 7;
6781 bool do_set = false;
6784 /* DSDT *always* updates status on resume */
6785 tp_features.fan_ctrl_status_undef = 0;
6787 if (!fan_control_allowed ||
6788 !fan_control_resume_level ||
6789 (fan_get_status_safe(¤t_level) < 0))
6792 switch (fan_control_access_mode) {
6793 case TPACPI_FAN_WR_ACPI_SFAN:
6794 /* never decrease fan level */
6795 do_set = (fan_control_resume_level > current_level);
6797 case TPACPI_FAN_WR_ACPI_FANS:
6798 case TPACPI_FAN_WR_TPEC:
6799 /* never decrease fan level, scale is:
6800 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6802 * We expect the firmware to set either 7 or AUTO, but we
6803 * handle FULLSPEED out of paranoia.
6805 * So, we can safely only restore FULLSPEED or 7, anything
6806 * else could slow the fan. Restoring AUTO is useless, at
6807 * best that's exactly what the DSDT already set (it is the
6810 * Always keep in mind that the DSDT *will* have set the
6811 * fans to what the vendor supposes is the best level. We
6812 * muck with it only to speed the fan up.
6814 if (fan_control_resume_level != 7 &&
6815 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6818 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6819 (current_level != fan_control_resume_level);
6825 printk(TPACPI_NOTICE
6826 "restoring fan level to 0x%02x\n",
6827 fan_control_resume_level);
6828 rc = fan_set_level_safe(fan_control_resume_level);
6830 printk(TPACPI_NOTICE
6831 "failed to restore fan level: %d\n", rc);
6835 static int fan_read(char *p)
6840 unsigned int speed = 0;
6842 switch (fan_status_access_mode) {
6843 case TPACPI_FAN_RD_ACPI_GFAN:
6844 /* 570, 600e/x, 770e, 770x */
6845 rc = fan_get_status_safe(&status);
6849 len += sprintf(p + len, "status:\t\t%s\n"
6851 (status != 0) ? "enabled" : "disabled", status);
6854 case TPACPI_FAN_RD_TPEC:
6855 /* all except 570, 600e/x, 770e, 770x */
6856 rc = fan_get_status_safe(&status);
6860 len += sprintf(p + len, "status:\t\t%s\n",
6861 (status != 0) ? "enabled" : "disabled");
6863 rc = fan_get_speed(&speed);
6867 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6869 if (status & TP_EC_FAN_FULLSPEED)
6870 /* Disengaged mode takes precedence */
6871 len += sprintf(p + len, "level:\t\tdisengaged\n");
6872 else if (status & TP_EC_FAN_AUTO)
6873 len += sprintf(p + len, "level:\t\tauto\n");
6875 len += sprintf(p + len, "level:\t\t%d\n", status);
6878 case TPACPI_FAN_NONE:
6880 len += sprintf(p + len, "status:\t\tnot supported\n");
6883 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6884 len += sprintf(p + len, "commands:\tlevel <level>");
6886 switch (fan_control_access_mode) {
6887 case TPACPI_FAN_WR_ACPI_SFAN:
6888 len += sprintf(p + len, " (<level> is 0-7)\n");
6892 len += sprintf(p + len, " (<level> is 0-7, "
6893 "auto, disengaged, full-speed)\n");
6898 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6899 len += sprintf(p + len, "commands:\tenable, disable\n"
6900 "commands:\twatchdog <timeout> (<timeout> "
6901 "is 0 (off), 1-120 (seconds))\n");
6903 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6904 len += sprintf(p + len, "commands:\tspeed <speed>"
6905 " (<speed> is 0-65535)\n");
6910 static int fan_write_cmd_level(const char *cmd, int *rc)
6914 if (strlencmp(cmd, "level auto") == 0)
6915 level = TP_EC_FAN_AUTO;
6916 else if ((strlencmp(cmd, "level disengaged") == 0) |
6917 (strlencmp(cmd, "level full-speed") == 0))
6918 level = TP_EC_FAN_FULLSPEED;
6919 else if (sscanf(cmd, "level %d", &level) != 1)
6922 *rc = fan_set_level_safe(level);
6924 printk(TPACPI_ERR "level command accepted for unsupported "
6925 "access mode %d", fan_control_access_mode);
6930 static int fan_write_cmd_enable(const char *cmd, int *rc)
6932 if (strlencmp(cmd, "enable") != 0)
6935 *rc = fan_set_enable();
6937 printk(TPACPI_ERR "enable command accepted for unsupported "
6938 "access mode %d", fan_control_access_mode);
6943 static int fan_write_cmd_disable(const char *cmd, int *rc)
6945 if (strlencmp(cmd, "disable") != 0)
6948 *rc = fan_set_disable();
6950 printk(TPACPI_ERR "disable command accepted for unsupported "
6951 "access mode %d", fan_control_access_mode);
6956 static int fan_write_cmd_speed(const char *cmd, int *rc)
6961 * Support speed <low> <medium> <high> ? */
6963 if (sscanf(cmd, "speed %d", &speed) != 1)
6966 *rc = fan_set_speed(speed);
6968 printk(TPACPI_ERR "speed command accepted for unsupported "
6969 "access mode %d", fan_control_access_mode);
6974 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6978 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6981 if (interval < 0 || interval > 120)
6984 fan_watchdog_maxinterval = interval;
6989 static int fan_write(char *buf)
6994 while (!rc && (cmd = next_cmd(&buf))) {
6995 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6996 fan_write_cmd_level(cmd, &rc)) &&
6997 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6998 (fan_write_cmd_enable(cmd, &rc) ||
6999 fan_write_cmd_disable(cmd, &rc) ||
7000 fan_write_cmd_watchdog(cmd, &rc))) &&
7001 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7002 fan_write_cmd_speed(cmd, &rc))
7006 fan_watchdog_reset();
7012 static struct ibm_struct fan_driver_data = {
7017 .suspend = fan_suspend,
7018 .resume = fan_resume,
7021 /****************************************************************************
7022 ****************************************************************************
7026 ****************************************************************************
7027 ****************************************************************************/
7029 /* sysfs name ---------------------------------------------------------- */
7030 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7031 struct device_attribute *attr,
7034 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7037 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7038 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7040 /* --------------------------------------------------------------------- */
7043 static struct proc_dir_entry *proc_dir;
7046 * Module and infrastructure proble, init and exit handling
7049 static int force_load;
7051 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7052 static const char * __init str_supported(int is_supported)
7054 static char text_unsupported[] __initdata = "not supported";
7056 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7058 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7060 static void ibm_exit(struct ibm_struct *ibm)
7062 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7064 list_del_init(&ibm->all_drivers);
7066 if (ibm->flags.acpi_notify_installed) {
7067 dbg_printk(TPACPI_DBG_EXIT,
7068 "%s: acpi_remove_notify_handler\n", ibm->name);
7070 acpi_remove_notify_handler(*ibm->acpi->handle,
7072 dispatch_acpi_notify);
7073 ibm->flags.acpi_notify_installed = 0;
7074 ibm->flags.acpi_notify_installed = 0;
7077 if (ibm->flags.proc_created) {
7078 dbg_printk(TPACPI_DBG_EXIT,
7079 "%s: remove_proc_entry\n", ibm->name);
7080 remove_proc_entry(ibm->name, proc_dir);
7081 ibm->flags.proc_created = 0;
7084 if (ibm->flags.acpi_driver_registered) {
7085 dbg_printk(TPACPI_DBG_EXIT,
7086 "%s: acpi_bus_unregister_driver\n", ibm->name);
7088 acpi_bus_unregister_driver(ibm->acpi->driver);
7089 kfree(ibm->acpi->driver);
7090 ibm->acpi->driver = NULL;
7091 ibm->flags.acpi_driver_registered = 0;
7094 if (ibm->flags.init_called && ibm->exit) {
7096 ibm->flags.init_called = 0;
7099 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7102 static int __init ibm_init(struct ibm_init_struct *iibm)
7105 struct ibm_struct *ibm = iibm->data;
7106 struct proc_dir_entry *entry;
7108 BUG_ON(ibm == NULL);
7110 INIT_LIST_HEAD(&ibm->all_drivers);
7112 if (ibm->flags.experimental && !experimental)
7115 dbg_printk(TPACPI_DBG_INIT,
7116 "probing for %s\n", ibm->name);
7119 ret = iibm->init(iibm);
7121 return 0; /* probe failed */
7125 ibm->flags.init_called = 1;
7129 if (ibm->acpi->hid) {
7130 ret = register_tpacpi_subdriver(ibm);
7135 if (ibm->acpi->notify) {
7136 ret = setup_acpi_notify(ibm);
7137 if (ret == -ENODEV) {
7138 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7148 dbg_printk(TPACPI_DBG_INIT,
7149 "%s installed\n", ibm->name);
7152 entry = create_proc_entry(ibm->name,
7153 S_IFREG | S_IRUGO | S_IWUSR,
7156 printk(TPACPI_ERR "unable to create proc entry %s\n",
7161 entry->owner = THIS_MODULE;
7163 entry->read_proc = &dispatch_procfs_read;
7165 entry->write_proc = &dispatch_procfs_write;
7166 ibm->flags.proc_created = 1;
7169 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7174 dbg_printk(TPACPI_DBG_INIT,
7175 "%s: at error exit path with result %d\n",
7179 return (ret < 0)? ret : 0;
7184 /* returns 0 - probe ok, or < 0 - probe error.
7185 * Probe ok doesn't mean thinkpad found.
7186 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7187 static int __must_check __init get_thinkpad_model_data(
7188 struct thinkpad_id_data *tp)
7190 const struct dmi_device *dev = NULL;
7191 char ec_fw_string[18];
7197 memset(tp, 0, sizeof(*tp));
7199 if (dmi_name_in_vendors("IBM"))
7200 tp->vendor = PCI_VENDOR_ID_IBM;
7201 else if (dmi_name_in_vendors("LENOVO"))
7202 tp->vendor = PCI_VENDOR_ID_LENOVO;
7206 s = dmi_get_system_info(DMI_BIOS_VERSION);
7207 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7208 if (s && !tp->bios_version_str)
7210 if (!tp->bios_version_str)
7212 tp->bios_model = tp->bios_version_str[0]
7213 | (tp->bios_version_str[1] << 8);
7216 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7217 * X32 or newer, all Z series; Some models must have an
7218 * up-to-date BIOS or they will not be detected.
7220 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7222 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7223 if (sscanf(dev->name,
7224 "IBM ThinkPad Embedded Controller -[%17c",
7225 ec_fw_string) == 1) {
7226 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7227 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7229 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7230 if (!tp->ec_version_str)
7232 tp->ec_model = ec_fw_string[0]
7233 | (ec_fw_string[1] << 8);
7238 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7239 if (s && !strnicmp(s, "ThinkPad", 8)) {
7240 tp->model_str = kstrdup(s, GFP_KERNEL);
7245 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7246 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7247 if (s && !tp->nummodel_str)
7253 static int __init probe_for_thinkpad(void)
7261 * Non-ancient models have better DMI tagging, but very old models
7264 is_thinkpad = (thinkpad_id.model_str != NULL);
7266 /* ec is required because many other handles are relative to it */
7267 TPACPI_ACPIHANDLE_INIT(ec);
7271 "Not yet supported ThinkPad detected!\n");
7276 * Risks a regression on very old machines, but reduces potential
7277 * false positives a damn great deal
7280 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7282 if (!is_thinkpad && !force_load)
7289 /* Module init, exit, parameters */
7291 static struct ibm_init_struct ibms_init[] __initdata = {
7293 .init = thinkpad_acpi_driver_init,
7294 .data = &thinkpad_acpi_driver_data,
7297 .init = hotkey_init,
7298 .data = &hotkey_driver_data,
7301 .init = bluetooth_init,
7302 .data = &bluetooth_driver_data,
7306 .data = &wan_driver_data,
7310 .data = &uwb_driver_data,
7312 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7315 .data = &video_driver_data,
7320 .data = &light_driver_data,
7322 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7325 .data = &dock_driver_data[0],
7329 .data = &dock_driver_data[1],
7332 #ifdef CONFIG_THINKPAD_ACPI_BAY
7335 .data = &bay_driver_data,
7340 .data = &cmos_driver_data,
7344 .data = &led_driver_data,
7348 .data = &beep_driver_data,
7351 .init = thermal_init,
7352 .data = &thermal_driver_data,
7355 .data = &ecdump_driver_data,
7358 .init = brightness_init,
7359 .data = &brightness_driver_data,
7362 .data = &volume_driver_data,
7366 .data = &fan_driver_data,
7370 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7373 struct ibm_struct *ibm;
7375 if (!kp || !kp->name || !val)
7378 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7379 ibm = ibms_init[i].data;
7380 WARN_ON(ibm == NULL);
7382 if (!ibm || !ibm->name)
7385 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7386 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7388 strcpy(ibms_init[i].param, val);
7389 strcat(ibms_init[i].param, ",");
7397 module_param(experimental, int, 0);
7398 MODULE_PARM_DESC(experimental,
7399 "Enables experimental features when non-zero");
7401 module_param_named(debug, dbg_level, uint, 0);
7402 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7404 module_param(force_load, bool, 0);
7405 MODULE_PARM_DESC(force_load,
7406 "Attempts to load the driver even on a "
7407 "mis-identified ThinkPad when true");
7409 module_param_named(fan_control, fan_control_allowed, bool, 0);
7410 MODULE_PARM_DESC(fan_control,
7411 "Enables setting fan parameters features when true");
7413 module_param_named(brightness_mode, brightness_mode, int, 0);
7414 MODULE_PARM_DESC(brightness_mode,
7415 "Selects brightness control strategy: "
7416 "0=auto, 1=EC, 2=CMOS, 3=both");
7418 module_param(brightness_enable, uint, 0);
7419 MODULE_PARM_DESC(brightness_enable,
7420 "Enables backlight control when 1, disables when 0");
7422 module_param(hotkey_report_mode, uint, 0);
7423 MODULE_PARM_DESC(hotkey_report_mode,
7424 "used for backwards compatibility with userspace, "
7425 "see documentation");
7427 #define TPACPI_PARAM(feature) \
7428 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7429 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7430 "at module load, see documentation")
7432 TPACPI_PARAM(hotkey);
7433 TPACPI_PARAM(bluetooth);
7434 TPACPI_PARAM(video);
7435 TPACPI_PARAM(light);
7436 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7439 #ifdef CONFIG_THINKPAD_ACPI_BAY
7441 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7445 TPACPI_PARAM(ecdump);
7446 TPACPI_PARAM(brightness);
7447 TPACPI_PARAM(volume);
7450 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7451 module_param(dbg_wlswemul, uint, 0);
7452 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7453 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7454 MODULE_PARM_DESC(wlsw_state,
7455 "Initial state of the emulated WLSW switch");
7457 module_param(dbg_bluetoothemul, uint, 0);
7458 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7459 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7460 MODULE_PARM_DESC(bluetooth_state,
7461 "Initial state of the emulated bluetooth switch");
7463 module_param(dbg_wwanemul, uint, 0);
7464 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7465 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7466 MODULE_PARM_DESC(wwan_state,
7467 "Initial state of the emulated WWAN switch");
7469 module_param(dbg_uwbemul, uint, 0);
7470 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7471 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7472 MODULE_PARM_DESC(uwb_state,
7473 "Initial state of the emulated UWB switch");
7476 static void thinkpad_acpi_module_exit(void)
7478 struct ibm_struct *ibm, *itmp;
7480 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7482 list_for_each_entry_safe_reverse(ibm, itmp,
7483 &tpacpi_all_drivers,
7488 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7490 if (tpacpi_inputdev) {
7491 if (tp_features.input_device_registered)
7492 input_unregister_device(tpacpi_inputdev);
7494 input_free_device(tpacpi_inputdev);
7498 hwmon_device_unregister(tpacpi_hwmon);
7500 if (tp_features.sensors_pdev_attrs_registered)
7501 device_remove_file(&tpacpi_sensors_pdev->dev,
7502 &dev_attr_thinkpad_acpi_pdev_name);
7503 if (tpacpi_sensors_pdev)
7504 platform_device_unregister(tpacpi_sensors_pdev);
7506 platform_device_unregister(tpacpi_pdev);
7508 if (tp_features.sensors_pdrv_attrs_registered)
7509 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7510 if (tp_features.platform_drv_attrs_registered)
7511 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7513 if (tp_features.sensors_pdrv_registered)
7514 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7516 if (tp_features.platform_drv_registered)
7517 platform_driver_unregister(&tpacpi_pdriver);
7520 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7523 destroy_workqueue(tpacpi_wq);
7525 kfree(thinkpad_id.bios_version_str);
7526 kfree(thinkpad_id.ec_version_str);
7527 kfree(thinkpad_id.model_str);
7531 static int __init thinkpad_acpi_module_init(void)
7535 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7537 /* Parameter checking */
7538 if (hotkey_report_mode > 2)
7541 /* Driver-level probe */
7543 ret = get_thinkpad_model_data(&thinkpad_id);
7546 "unable to get DMI data: %d\n", ret);
7547 thinkpad_acpi_module_exit();
7550 ret = probe_for_thinkpad();
7552 thinkpad_acpi_module_exit();
7556 /* Driver initialization */
7558 TPACPI_ACPIHANDLE_INIT(ecrd);
7559 TPACPI_ACPIHANDLE_INIT(ecwr);
7561 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7563 thinkpad_acpi_module_exit();
7567 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7570 "unable to create proc dir " TPACPI_PROC_DIR);
7571 thinkpad_acpi_module_exit();
7574 proc_dir->owner = THIS_MODULE;
7576 ret = platform_driver_register(&tpacpi_pdriver);
7579 "unable to register main platform driver\n");
7580 thinkpad_acpi_module_exit();
7583 tp_features.platform_drv_registered = 1;
7585 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7588 "unable to register hwmon platform driver\n");
7589 thinkpad_acpi_module_exit();
7592 tp_features.sensors_pdrv_registered = 1;
7594 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7596 tp_features.platform_drv_attrs_registered = 1;
7597 ret = tpacpi_create_driver_attributes(
7598 &tpacpi_hwmon_pdriver.driver);
7602 "unable to create sysfs driver attributes\n");
7603 thinkpad_acpi_module_exit();
7606 tp_features.sensors_pdrv_attrs_registered = 1;
7609 /* Device initialization */
7610 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7612 if (IS_ERR(tpacpi_pdev)) {
7613 ret = PTR_ERR(tpacpi_pdev);
7615 printk(TPACPI_ERR "unable to register platform device\n");
7616 thinkpad_acpi_module_exit();
7619 tpacpi_sensors_pdev = platform_device_register_simple(
7620 TPACPI_HWMON_DRVR_NAME,
7622 if (IS_ERR(tpacpi_sensors_pdev)) {
7623 ret = PTR_ERR(tpacpi_sensors_pdev);
7624 tpacpi_sensors_pdev = NULL;
7626 "unable to register hwmon platform device\n");
7627 thinkpad_acpi_module_exit();
7630 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7631 &dev_attr_thinkpad_acpi_pdev_name);
7634 "unable to create sysfs hwmon device attributes\n");
7635 thinkpad_acpi_module_exit();
7638 tp_features.sensors_pdev_attrs_registered = 1;
7639 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7640 if (IS_ERR(tpacpi_hwmon)) {
7641 ret = PTR_ERR(tpacpi_hwmon);
7642 tpacpi_hwmon = NULL;
7643 printk(TPACPI_ERR "unable to register hwmon device\n");
7644 thinkpad_acpi_module_exit();
7647 mutex_init(&tpacpi_inputdev_send_mutex);
7648 tpacpi_inputdev = input_allocate_device();
7649 if (!tpacpi_inputdev) {
7650 printk(TPACPI_ERR "unable to allocate input device\n");
7651 thinkpad_acpi_module_exit();
7654 /* Prepare input device, but don't register */
7655 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7656 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7657 tpacpi_inputdev->id.bustype = BUS_HOST;
7658 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7659 thinkpad_id.vendor :
7661 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7662 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7664 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7665 ret = ibm_init(&ibms_init[i]);
7666 if (ret >= 0 && *ibms_init[i].param)
7667 ret = ibms_init[i].data->write(ibms_init[i].param);
7669 thinkpad_acpi_module_exit();
7673 ret = input_register_device(tpacpi_inputdev);
7675 printk(TPACPI_ERR "unable to register input device\n");
7676 thinkpad_acpi_module_exit();
7679 tp_features.input_device_registered = 1;
7682 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7686 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7689 * DMI matching for module autoloading
7691 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7692 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7694 * Only models listed in thinkwiki will be supported, so add yours
7695 * if it is not there yet.
7697 #define IBM_BIOS_MODULE_ALIAS(__type) \
7698 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7700 /* Non-ancient thinkpads */
7701 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7702 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7704 /* Ancient thinkpad BIOSes have to be identified by
7705 * BIOS type or model number, and there are far less
7706 * BIOS types than model numbers... */
7707 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7708 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7709 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7711 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7712 MODULE_DESCRIPTION(TPACPI_DESC);
7713 MODULE_VERSION(TPACPI_VERSION);
7714 MODULE_LICENSE("GPL");
7716 module_init(thinkpad_acpi_module_init);
7717 module_exit(thinkpad_acpi_module_exit);