ACPI: thinkpad-acpi: spring cleanup part 1
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob88447375bae1fed1353ba27a4530056b16069e6f
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2007 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
21 * 02110-1301, USA.
24 #define IBM_VERSION "0.18"
25 #define TPACPI_SYSFS_VERSION 0x020101
28 * Changelog:
29 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
30 * drivers/misc.
32 * 2006-11-22 0.13 new maintainer
33 * changelog now lives in git commit history, and will
34 * not be updated further in-file.
36 * 2005-08-17 0.12 fix compilation on 2.6.13-rc kernels
37 * 2005-03-17 0.11 support for 600e, 770x
38 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
39 * support for 770e, G41
40 * G40 and G41 don't have a thinklight
41 * temperatures no longer experimental
42 * experimental brightness control
43 * experimental volume control
44 * experimental fan enable/disable
45 * 2005-01-16 0.10 fix module loading on R30, R31
46 * 2005-01-16 0.9 support for 570, R30, R31
47 * ultrabay support on A22p, A3x
48 * limit arg for cmos, led, beep, drop experimental status
49 * more capable led control on A21e, A22p, T20-22, X20
50 * experimental temperatures and fan speed
51 * experimental embedded controller register dump
52 * mark more functions as __init, drop incorrect __exit
53 * use MODULE_VERSION
54 * thanks to Henrik Brix Andersen <brix@gentoo.org>
55 * fix parameter passing on module loading
56 * thanks to Rusty Russell <rusty@rustcorp.com.au>
57 * thanks to Jim Radford <radford@blackbean.org>
58 * 2004-11-08 0.8 fix init error case, don't return from a macro
59 * thanks to Chris Wright <chrisw@osdl.org>
60 * 2004-10-23 0.7 fix module loading on A21e, A22p, T20, T21, X20
61 * fix led control on A21e
62 * 2004-10-19 0.6 use acpi_bus_register_driver() to claim HKEY device
63 * 2004-10-18 0.5 thinklight support on A21e, G40, R32, T20, T21, X20
64 * proc file format changed
65 * video_switch command
66 * experimental cmos control
67 * experimental led control
68 * experimental acpi sounds
69 * 2004-09-16 0.4 support for module parameters
70 * hotkey mask can be prefixed by 0x
71 * video output switching
72 * video expansion control
73 * ultrabay eject support
74 * removed lcd brightness/on/off control, didn't work
75 * 2004-08-17 0.3 support for R40
76 * lcd off, brightness control
77 * thinklight on/off
78 * 2004-08-14 0.2 support for T series, X20
79 * bluetooth enable/disable
80 * hotkey events disabled by default
81 * removed fan control, currently useless
82 * 2004-08-09 0.1 initial release, support for X series
85 /* ==================================================== BEGIN HEADER */
87 #include <linux/kernel.h>
88 #include <linux/module.h>
89 #include <linux/init.h>
90 #include <linux/types.h>
91 #include <linux/string.h>
92 #include <linux/list.h>
93 #include <linux/mutex.h>
94 #include <linux/kthread.h>
95 #include <linux/freezer.h>
96 #include <linux/delay.h>
98 #include <linux/nvram.h>
99 #include <linux/proc_fs.h>
100 #include <linux/sysfs.h>
101 #include <linux/backlight.h>
102 #include <linux/fb.h>
103 #include <linux/platform_device.h>
104 #include <linux/hwmon.h>
105 #include <linux/hwmon-sysfs.h>
106 #include <linux/input.h>
107 #include <asm/uaccess.h>
109 #include <linux/dmi.h>
110 #include <linux/jiffies.h>
111 #include <linux/workqueue.h>
113 #include <acpi/acpi_drivers.h>
114 #include <acpi/acnamesp.h>
116 #include <linux/pci_ids.h>
118 /****************************************************************************
119 * Main driver
122 #define IBM_NAME "thinkpad"
123 #define IBM_DESC "ThinkPad ACPI Extras"
124 #define IBM_FILE IBM_NAME "_acpi"
125 #define IBM_URL "http://ibm-acpi.sf.net/"
126 #define IBM_MAIL "ibm-acpi-devel@lists.sourceforge.net"
128 #define IBM_PROC_DIR "ibm"
129 #define IBM_ACPI_EVENT_PREFIX "ibm"
130 #define IBM_DRVR_NAME IBM_FILE
131 #define IBM_HWMON_DRVR_NAME IBM_NAME "_hwmon"
133 #define IBM_LOG IBM_FILE ": "
134 #define IBM_ERR KERN_ERR IBM_LOG
135 #define IBM_NOTICE KERN_NOTICE IBM_LOG
136 #define IBM_INFO KERN_INFO IBM_LOG
137 #define IBM_DEBUG KERN_DEBUG IBM_LOG
139 #define IBM_MAX_ACPI_ARGS 3
141 /* ThinkPad CMOS commands */
142 #define TP_CMOS_VOLUME_DOWN 0
143 #define TP_CMOS_VOLUME_UP 1
144 #define TP_CMOS_VOLUME_MUTE 2
145 #define TP_CMOS_BRIGHTNESS_UP 4
146 #define TP_CMOS_BRIGHTNESS_DOWN 5
148 /* NVRAM Addresses */
149 enum tp_nvram_addr {
150 TP_NVRAM_ADDR_HK2 = 0x57,
151 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
152 TP_NVRAM_ADDR_VIDEO = 0x59,
153 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
154 TP_NVRAM_ADDR_MIXER = 0x60,
157 /* NVRAM bit masks */
158 enum {
159 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
160 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
161 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
162 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
163 TP_NVRAM_MASK_THINKLIGHT = 0x10,
164 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
165 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
166 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
167 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
168 TP_NVRAM_MASK_MUTE = 0x40,
169 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
170 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
171 TP_NVRAM_POS_LEVEL_VOLUME = 0,
174 #define onoff(status,bit) ((status) & (1 << (bit)) ? "on" : "off")
175 #define enabled(status,bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
176 #define strlencmp(a,b) (strncmp((a), (b), strlen(b)))
178 /* Debugging */
179 #define TPACPI_DBG_ALL 0xffff
180 #define TPACPI_DBG_ALL 0xffff
181 #define TPACPI_DBG_INIT 0x0001
182 #define TPACPI_DBG_EXIT 0x0002
183 #define dbg_printk(a_dbg_level, format, arg...) \
184 do { if (dbg_level & a_dbg_level) \
185 printk(IBM_DEBUG "%s: " format, __func__ , ## arg); } while (0)
186 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
187 #define vdbg_printk(a_dbg_level, format, arg...) \
188 dbg_printk(a_dbg_level, format, ## arg)
189 static const char *str_supported(int is_supported);
190 #else
191 #define vdbg_printk(a_dbg_level, format, arg...)
192 #endif
194 /* Input IDs */
195 #define TPACPI_HKEY_INPUT_VENDOR PCI_VENDOR_ID_IBM
196 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
197 #define TPACPI_HKEY_INPUT_VERSION 0x4101
199 /* ACPI HIDs */
200 #define IBM_HKEY_HID "IBM0068"
202 /* ACPI helpers */
203 static int __must_check acpi_evalf(acpi_handle handle,
204 void *res, char *method, char *fmt, ...);
205 static int __must_check acpi_ec_read(int i, u8 * p);
206 static int __must_check acpi_ec_write(int i, u8 v);
207 static int __must_check _sta(acpi_handle handle);
209 /* ACPI handles */
210 static acpi_handle root_handle; /* root namespace */
211 static acpi_handle ec_handle; /* EC */
212 static acpi_handle ecrd_handle, ecwr_handle; /* 570 EC access */
213 static acpi_handle cmos_handle, hkey_handle; /* basic thinkpad handles */
215 static void drv_acpi_handle_init(char *name,
216 acpi_handle *handle, acpi_handle parent,
217 char **paths, int num_paths, char **path);
218 #define IBM_ACPIHANDLE_INIT(object) \
219 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
220 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
222 /* ThinkPad ACPI helpers */
223 static int issue_thinkpad_cmos_command(int cmos_cmd);
225 /* procfs support */
226 static struct proc_dir_entry *proc_dir;
228 /* procfs helpers */
229 static int dispatch_procfs_read(char *page, char **start, off_t off,
230 int count, int *eof, void *data);
231 static int dispatch_procfs_write(struct file *file,
232 const char __user * userbuf,
233 unsigned long count, void *data);
234 static char *next_cmd(char **cmds);
236 /* sysfs support */
237 struct attribute_set {
238 unsigned int members, max_members;
239 struct attribute_group group;
242 static struct attribute_set *create_attr_set(unsigned int max_members,
243 const char* name);
244 #define destroy_attr_set(_set) \
245 kfree(_set);
246 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr);
247 static int add_many_to_attr_set(struct attribute_set* s,
248 struct attribute **attr,
249 unsigned int count);
250 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
251 sysfs_create_group(_kobj, &_attr_set->group)
252 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj);
254 static int parse_strtoul(const char *buf, unsigned long max,
255 unsigned long *value);
257 /* Device model */
258 static struct platform_device *tpacpi_pdev;
259 static struct platform_device *tpacpi_sensors_pdev;
260 static struct class_device *tpacpi_hwmon;
261 static struct platform_driver tpacpi_pdriver;
262 static struct input_dev *tpacpi_inputdev;
263 static int tpacpi_create_driver_attributes(struct device_driver *drv);
264 static void tpacpi_remove_driver_attributes(struct device_driver *drv);
266 /* Module */
267 static int experimental;
268 static u32 dbg_level;
269 static int force_load;
270 static unsigned int hotkey_report_mode;
272 static int thinkpad_acpi_module_init(void);
273 static void thinkpad_acpi_module_exit(void);
276 /****************************************************************************
277 * Subdrivers
280 struct ibm_struct;
282 struct tp_acpi_drv_struct {
283 char *hid;
284 struct acpi_driver *driver;
286 void (*notify) (struct ibm_struct *, u32);
287 acpi_handle *handle;
288 u32 type;
289 struct acpi_device *device;
292 struct ibm_struct {
293 char *name;
295 int (*read) (char *);
296 int (*write) (char *);
297 void (*exit) (void);
298 void (*resume) (void);
300 struct list_head all_drivers;
302 struct tp_acpi_drv_struct *acpi;
304 struct {
305 u8 acpi_driver_registered:1;
306 u8 acpi_notify_installed:1;
307 u8 proc_created:1;
308 u8 init_called:1;
309 u8 experimental:1;
310 } flags;
313 struct ibm_init_struct {
314 char param[32];
316 int (*init) (struct ibm_init_struct *);
317 struct ibm_struct *data;
320 static struct {
321 #ifdef CONFIG_THINKPAD_ACPI_BAY
322 u32 bay_status:1;
323 u32 bay_eject:1;
324 u32 bay_status2:1;
325 u32 bay_eject2:1;
326 #endif
327 u32 bluetooth:1;
328 u32 hotkey:1;
329 u32 hotkey_mask:1;
330 u32 hotkey_wlsw:1;
331 u32 light:1;
332 u32 light_status:1;
333 u32 bright_16levels:1;
334 u32 wan:1;
335 u32 fan_ctrl_status_undef:1;
336 u32 input_device_registered:1;
337 u32 hotkey_report_mode_locked:1;
338 u32 platform_drv_registered:1;
339 u32 platform_drv_attrs_registered:1;
340 u32 sensors_pdrv_registered:1;
341 u32 sensors_pdrv_attrs_registered:1;
342 u32 sensors_pdev_attrs_registered:1;
343 u32 hotkey_poll_active:1;
344 } tp_features;
346 struct thinkpad_id_data {
347 unsigned int vendor; /* ThinkPad vendor:
348 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
350 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
351 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
353 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
354 u16 ec_model;
356 char *model_str;
359 static struct thinkpad_id_data thinkpad_id;
361 static struct list_head tpacpi_all_drivers;
363 static struct ibm_init_struct ibms_init[];
364 static int set_ibm_param(const char *val, struct kernel_param *kp);
365 static int ibm_init(struct ibm_init_struct *iibm);
366 static void ibm_exit(struct ibm_struct *ibm);
370 * procfs master subdriver
372 static int thinkpad_acpi_driver_init(struct ibm_init_struct *iibm);
373 static int thinkpad_acpi_driver_read(char *p);
377 * Bay subdriver
380 #ifdef CONFIG_THINKPAD_ACPI_BAY
381 static acpi_handle bay_handle, bay_ej_handle;
382 static acpi_handle bay2_handle, bay2_ej_handle;
384 static int bay_init(struct ibm_init_struct *iibm);
385 static void bay_notify(struct ibm_struct *ibm, u32 event);
386 static int bay_read(char *p);
387 static int bay_write(char *buf);
388 #endif /* CONFIG_THINKPAD_ACPI_BAY */
392 * Beep subdriver
395 static acpi_handle beep_handle;
397 static int beep_read(char *p);
398 static int beep_write(char *buf);
402 * Bluetooth subdriver
405 enum {
406 /* ACPI GBDC/SBDC bits */
407 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
408 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
409 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
412 static int bluetooth_init(struct ibm_init_struct *iibm);
413 static int bluetooth_get_radiosw(void);
414 static int bluetooth_set_radiosw(int radio_on);
415 static int bluetooth_read(char *p);
416 static int bluetooth_write(char *buf);
420 * Brightness (backlight) subdriver
423 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
425 static struct backlight_device *ibm_backlight_device;
426 static int brightness_offset = 0x31;
427 static int brightness_mode;
428 static unsigned int brightness_enable; /* 0 = no, 1 = yes, 2 = auto */
430 static int brightness_init(struct ibm_init_struct *iibm);
431 static void brightness_exit(void);
432 static int brightness_get(struct backlight_device *bd);
433 static int brightness_set(int value);
434 static int brightness_update_status(struct backlight_device *bd);
435 static int brightness_read(char *p);
436 static int brightness_write(char *buf);
440 * CMOS subdriver
443 static int cmos_read(char *p);
444 static int cmos_write(char *buf);
448 * Dock subdriver
451 #ifdef CONFIG_THINKPAD_ACPI_DOCK
452 static acpi_handle pci_handle;
453 static acpi_handle dock_handle;
455 static void dock_notify(struct ibm_struct *ibm, u32 event);
456 static int dock_read(char *p);
457 static int dock_write(char *buf);
458 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
462 * EC dump subdriver
465 static int ecdump_read(char *p) ;
466 static int ecdump_write(char *buf);
470 * Fan subdriver
473 enum { /* Fan control constants */
474 fan_status_offset = 0x2f, /* EC register 0x2f */
475 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
476 * 0x84 must be read before 0x85 */
478 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
479 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
481 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
484 enum fan_status_access_mode {
485 TPACPI_FAN_NONE = 0, /* No fan status or control */
486 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
487 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
490 enum fan_control_access_mode {
491 TPACPI_FAN_WR_NONE = 0, /* No fan control */
492 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
493 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
494 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
497 enum fan_control_commands {
498 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
499 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
500 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
501 * and also watchdog cmd */
504 static int fan_control_allowed;
506 static enum fan_status_access_mode fan_status_access_mode;
507 static enum fan_control_access_mode fan_control_access_mode;
508 static enum fan_control_commands fan_control_commands;
509 static u8 fan_control_initial_status;
510 static u8 fan_control_desired_level;
511 static int fan_watchdog_maxinterval;
513 static struct mutex fan_mutex;
515 static acpi_handle fans_handle, gfan_handle, sfan_handle;
517 static int fan_init(struct ibm_init_struct *iibm);
518 static void fan_exit(void);
519 static int fan_get_status(u8 *status);
520 static int fan_get_status_safe(u8 *status);
521 static int fan_get_speed(unsigned int *speed);
522 static void fan_update_desired_level(u8 status);
523 static void fan_watchdog_fire(struct work_struct *ignored);
524 static void fan_watchdog_reset(void);
525 static int fan_set_level(int level);
526 static int fan_set_level_safe(int level);
527 static int fan_set_enable(void);
528 static int fan_set_disable(void);
529 static int fan_set_speed(int speed);
530 static int fan_read(char *p);
531 static int fan_write(char *buf);
532 static int fan_write_cmd_level(const char *cmd, int *rc);
533 static int fan_write_cmd_enable(const char *cmd, int *rc);
534 static int fan_write_cmd_disable(const char *cmd, int *rc);
535 static int fan_write_cmd_speed(const char *cmd, int *rc);
536 static int fan_write_cmd_watchdog(const char *cmd, int *rc);
540 * Hotkey subdriver
543 enum { /* hot key scan codes (derived from ACPI DSDT) */
544 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
545 TP_ACPI_HOTKEYSCAN_FNF2,
546 TP_ACPI_HOTKEYSCAN_FNF3,
547 TP_ACPI_HOTKEYSCAN_FNF4,
548 TP_ACPI_HOTKEYSCAN_FNF5,
549 TP_ACPI_HOTKEYSCAN_FNF6,
550 TP_ACPI_HOTKEYSCAN_FNF7,
551 TP_ACPI_HOTKEYSCAN_FNF8,
552 TP_ACPI_HOTKEYSCAN_FNF9,
553 TP_ACPI_HOTKEYSCAN_FNF10,
554 TP_ACPI_HOTKEYSCAN_FNF11,
555 TP_ACPI_HOTKEYSCAN_FNF12,
556 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
557 TP_ACPI_HOTKEYSCAN_FNINSERT,
558 TP_ACPI_HOTKEYSCAN_FNDELETE,
559 TP_ACPI_HOTKEYSCAN_FNHOME,
560 TP_ACPI_HOTKEYSCAN_FNEND,
561 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
562 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
563 TP_ACPI_HOTKEYSCAN_FNSPACE,
564 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
565 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
566 TP_ACPI_HOTKEYSCAN_MUTE,
567 TP_ACPI_HOTKEYSCAN_THINKPAD,
570 static int hotkey_orig_status;
571 static u32 hotkey_orig_mask;
573 static struct mutex hotkey_mutex;
575 static int hotkey_init(struct ibm_init_struct *iibm);
576 static void hotkey_exit(void);
577 static void hotkey_notify(struct ibm_struct *ibm, u32 event);
578 static int hotkey_read(char *p);
579 static int hotkey_write(char *buf);
583 * LED subdriver
586 enum led_access_mode {
587 TPACPI_LED_NONE = 0,
588 TPACPI_LED_570, /* 570 */
589 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
590 TPACPI_LED_NEW, /* all others */
593 enum { /* For TPACPI_LED_OLD */
594 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
595 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
596 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
599 static enum led_access_mode led_supported;
600 static acpi_handle led_handle;
602 static int led_init(struct ibm_init_struct *iibm);
603 static int led_read(char *p);
604 static int led_write(char *buf);
607 * Light (thinklight) subdriver
610 static acpi_handle lght_handle, ledb_handle;
612 static int light_init(struct ibm_init_struct *iibm);
613 static int light_read(char *p);
614 static int light_write(char *buf);
618 * Thermal subdriver
621 enum thermal_access_mode {
622 TPACPI_THERMAL_NONE = 0, /* No thermal support */
623 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
624 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
625 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
626 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
629 enum { /* TPACPI_THERMAL_TPEC_* */
630 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
631 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
632 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
635 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
636 struct ibm_thermal_sensors_struct {
637 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
640 static enum thermal_access_mode thermal_read_mode;
642 static int thermal_init(struct ibm_init_struct *iibm);
643 static int thermal_get_sensor(int idx, s32 *value);
644 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s);
645 static int thermal_read(char *p);
649 * Video subdriver
652 enum video_access_mode {
653 TPACPI_VIDEO_NONE = 0,
654 TPACPI_VIDEO_570, /* 570 */
655 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
656 TPACPI_VIDEO_NEW, /* all others */
659 enum { /* video status flags, based on VIDEO_570 */
660 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
661 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
662 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
665 enum { /* TPACPI_VIDEO_570 constants */
666 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
667 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
668 * video_status_flags */
669 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
670 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
673 static enum video_access_mode video_supported;
674 static int video_orig_autosw;
675 static acpi_handle vid_handle, vid2_handle;
677 static int video_init(struct ibm_init_struct *iibm);
678 static void video_exit(void);
679 static int video_outputsw_get(void);
680 static int video_outputsw_set(int status);
681 static int video_autosw_get(void);
682 static int video_autosw_set(int enable);
683 static int video_outputsw_cycle(void);
684 static int video_expand_toggle(void);
685 static int video_read(char *p);
686 static int video_write(char *buf);
690 * Volume subdriver
693 static int volume_offset = 0x30;
695 static int volume_read(char *p);
696 static int volume_write(char *buf);
700 * Wan subdriver
703 enum {
704 /* ACPI GWAN/SWAN bits */
705 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
706 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
707 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
710 static int wan_init(struct ibm_init_struct *iibm);
711 static int wan_get_radiosw(void);
712 static int wan_set_radiosw(int radio_on);
713 static int wan_read(char *p);
714 static int wan_write(char *buf);
716 /* ==================================================== END HEADER */
718 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
719 MODULE_DESCRIPTION(IBM_DESC);
720 MODULE_VERSION(IBM_VERSION);
721 MODULE_LICENSE("GPL");
723 /* Please remove this in year 2009 */
724 MODULE_ALIAS("ibm_acpi");
727 * DMI matching for module autoloading
729 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
730 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
732 * Only models listed in thinkwiki will be supported, so add yours
733 * if it is not there yet.
735 #define IBM_BIOS_MODULE_ALIAS(__type) \
736 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
738 /* Non-ancient thinkpads */
739 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
740 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
742 /* Ancient thinkpad BIOSes have to be identified by
743 * BIOS type or model number, and there are far less
744 * BIOS types than model numbers... */
745 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
746 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
747 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
749 #define __unused __attribute__ ((unused))
751 static enum {
752 TPACPI_LIFE_INIT = 0,
753 TPACPI_LIFE_RUNNING,
754 TPACPI_LIFE_EXITING,
755 } tpacpi_lifecycle;
757 /****************************************************************************
758 ****************************************************************************
760 * ACPI Helpers and device model
762 ****************************************************************************
763 ****************************************************************************/
765 /*************************************************************************
766 * ACPI basic handles
769 static acpi_handle root_handle;
771 #define IBM_HANDLE(object, parent, paths...) \
772 static acpi_handle object##_handle; \
773 static acpi_handle *object##_parent = &parent##_handle; \
774 static char *object##_path; \
775 static char *object##_paths[] = { paths }
777 IBM_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
778 "\\_SB.PCI.ISA.EC", /* 570 */
779 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
780 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
781 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
782 "\\_SB.PCI0.ICH3.EC0", /* R31 */
783 "\\_SB.PCI0.LPC.EC", /* all others */
786 IBM_HANDLE(ecrd, ec, "ECRD"); /* 570 */
787 IBM_HANDLE(ecwr, ec, "ECWR"); /* 570 */
790 /*************************************************************************
791 * Misc ACPI handles
794 IBM_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, T4x, X31, X40 */
795 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
796 "\\CMS", /* R40, R40e */
797 ); /* all others */
799 IBM_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
800 "^HKEY", /* R30, R31 */
801 "HKEY", /* all others */
802 ); /* 570 */
805 /*************************************************************************
806 * ACPI helpers
809 static int acpi_evalf(acpi_handle handle,
810 void *res, char *method, char *fmt, ...)
812 char *fmt0 = fmt;
813 struct acpi_object_list params;
814 union acpi_object in_objs[IBM_MAX_ACPI_ARGS];
815 struct acpi_buffer result, *resultp;
816 union acpi_object out_obj;
817 acpi_status status;
818 va_list ap;
819 char res_type;
820 int success;
821 int quiet;
823 if (!*fmt) {
824 printk(IBM_ERR "acpi_evalf() called with empty format\n");
825 return 0;
828 if (*fmt == 'q') {
829 quiet = 1;
830 fmt++;
831 } else
832 quiet = 0;
834 res_type = *(fmt++);
836 params.count = 0;
837 params.pointer = &in_objs[0];
839 va_start(ap, fmt);
840 while (*fmt) {
841 char c = *(fmt++);
842 switch (c) {
843 case 'd': /* int */
844 in_objs[params.count].integer.value = va_arg(ap, int);
845 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
846 break;
847 /* add more types as needed */
848 default:
849 printk(IBM_ERR "acpi_evalf() called "
850 "with invalid format character '%c'\n", c);
851 return 0;
854 va_end(ap);
856 if (res_type != 'v') {
857 result.length = sizeof(out_obj);
858 result.pointer = &out_obj;
859 resultp = &result;
860 } else
861 resultp = NULL;
863 status = acpi_evaluate_object(handle, method, &params, resultp);
865 switch (res_type) {
866 case 'd': /* int */
867 if (res)
868 *(int *)res = out_obj.integer.value;
869 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
870 break;
871 case 'v': /* void */
872 success = status == AE_OK;
873 break;
874 /* add more types as needed */
875 default:
876 printk(IBM_ERR "acpi_evalf() called "
877 "with invalid format character '%c'\n", res_type);
878 return 0;
881 if (!success && !quiet)
882 printk(IBM_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
883 method, fmt0, status);
885 return success;
888 static void __unused acpi_print_int(acpi_handle handle, char *method)
890 int i;
892 if (acpi_evalf(handle, &i, method, "d"))
893 printk(IBM_INFO "%s = 0x%x\n", method, i);
894 else
895 printk(IBM_ERR "error calling %s\n", method);
898 static int acpi_ec_read(int i, u8 * p)
900 int v;
902 if (ecrd_handle) {
903 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
904 return 0;
905 *p = v;
906 } else {
907 if (ec_read(i, p) < 0)
908 return 0;
911 return 1;
914 static int acpi_ec_write(int i, u8 v)
916 if (ecwr_handle) {
917 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
918 return 0;
919 } else {
920 if (ec_write(i, v) < 0)
921 return 0;
924 return 1;
927 static int _sta(acpi_handle handle)
929 int status;
931 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
932 status = 0;
934 return status;
937 static int issue_thinkpad_cmos_command(int cmos_cmd)
939 if (!cmos_handle)
940 return -ENXIO;
942 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
943 return -EIO;
945 return 0;
948 /*************************************************************************
949 * ACPI device model
952 static void drv_acpi_handle_init(char *name,
953 acpi_handle *handle, acpi_handle parent,
954 char **paths, int num_paths, char **path)
956 int i;
957 acpi_status status;
959 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
960 name);
962 for (i = 0; i < num_paths; i++) {
963 status = acpi_get_handle(parent, paths[i], handle);
964 if (ACPI_SUCCESS(status)) {
965 *path = paths[i];
966 dbg_printk(TPACPI_DBG_INIT,
967 "Found ACPI handle %s for %s\n",
968 *path, name);
969 return;
973 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
974 name);
975 *handle = NULL;
978 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
980 struct ibm_struct *ibm = data;
982 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
983 return;
985 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
986 return;
988 ibm->acpi->notify(ibm, event);
991 static int __init setup_acpi_notify(struct ibm_struct *ibm)
993 acpi_status status;
994 int rc;
996 BUG_ON(!ibm->acpi);
998 if (!*ibm->acpi->handle)
999 return 0;
1001 vdbg_printk(TPACPI_DBG_INIT,
1002 "setting up ACPI notify for %s\n", ibm->name);
1004 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
1005 if (rc < 0) {
1006 printk(IBM_ERR "acpi_bus_get_device(%s) failed: %d\n",
1007 ibm->name, rc);
1008 return -ENODEV;
1011 acpi_driver_data(ibm->acpi->device) = ibm;
1012 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
1013 IBM_ACPI_EVENT_PREFIX,
1014 ibm->name);
1016 status = acpi_install_notify_handler(*ibm->acpi->handle,
1017 ibm->acpi->type, dispatch_acpi_notify, ibm);
1018 if (ACPI_FAILURE(status)) {
1019 if (status == AE_ALREADY_EXISTS) {
1020 printk(IBM_NOTICE "another device driver is already handling %s events\n",
1021 ibm->name);
1022 } else {
1023 printk(IBM_ERR "acpi_install_notify_handler(%s) failed: %d\n",
1024 ibm->name, status);
1026 return -ENODEV;
1028 ibm->flags.acpi_notify_installed = 1;
1029 return 0;
1032 static int __init tpacpi_device_add(struct acpi_device *device)
1034 return 0;
1037 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
1039 int rc;
1041 dbg_printk(TPACPI_DBG_INIT,
1042 "registering %s as an ACPI driver\n", ibm->name);
1044 BUG_ON(!ibm->acpi);
1046 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
1047 if (!ibm->acpi->driver) {
1048 printk(IBM_ERR "kzalloc(ibm->driver) failed\n");
1049 return -ENOMEM;
1052 sprintf(ibm->acpi->driver->name, "%s_%s", IBM_NAME, ibm->name);
1053 ibm->acpi->driver->ids = ibm->acpi->hid;
1054 ibm->acpi->driver->ops.add = &tpacpi_device_add;
1056 rc = acpi_bus_register_driver(ibm->acpi->driver);
1057 if (rc < 0) {
1058 printk(IBM_ERR "acpi_bus_register_driver(%s) failed: %d\n",
1059 ibm->acpi->hid, rc);
1060 kfree(ibm->acpi->driver);
1061 ibm->acpi->driver = NULL;
1062 } else if (!rc)
1063 ibm->flags.acpi_driver_registered = 1;
1065 return rc;
1069 /****************************************************************************
1070 ****************************************************************************
1072 * Procfs Helpers
1074 ****************************************************************************
1075 ****************************************************************************/
1077 static int dispatch_procfs_read(char *page, char **start, off_t off,
1078 int count, int *eof, void *data)
1080 struct ibm_struct *ibm = data;
1081 int len;
1083 if (!ibm || !ibm->read)
1084 return -EINVAL;
1086 len = ibm->read(page);
1087 if (len < 0)
1088 return len;
1090 if (len <= off + count)
1091 *eof = 1;
1092 *start = page + off;
1093 len -= off;
1094 if (len > count)
1095 len = count;
1096 if (len < 0)
1097 len = 0;
1099 return len;
1102 static int dispatch_procfs_write(struct file *file,
1103 const char __user * userbuf,
1104 unsigned long count, void *data)
1106 struct ibm_struct *ibm = data;
1107 char *kernbuf;
1108 int ret;
1110 if (!ibm || !ibm->write)
1111 return -EINVAL;
1113 kernbuf = kmalloc(count + 2, GFP_KERNEL);
1114 if (!kernbuf)
1115 return -ENOMEM;
1117 if (copy_from_user(kernbuf, userbuf, count)) {
1118 kfree(kernbuf);
1119 return -EFAULT;
1122 kernbuf[count] = 0;
1123 strcat(kernbuf, ",");
1124 ret = ibm->write(kernbuf);
1125 if (ret == 0)
1126 ret = count;
1128 kfree(kernbuf);
1130 return ret;
1133 static char *next_cmd(char **cmds)
1135 char *start = *cmds;
1136 char *end;
1138 while ((end = strchr(start, ',')) && end == start)
1139 start = end + 1;
1141 if (!end)
1142 return NULL;
1144 *end = 0;
1145 *cmds = end + 1;
1146 return start;
1150 /****************************************************************************
1151 ****************************************************************************
1153 * Device model: input, hwmon and platform
1155 ****************************************************************************
1156 ****************************************************************************/
1158 static struct platform_device *tpacpi_pdev;
1159 static struct platform_device *tpacpi_sensors_pdev;
1160 static struct class_device *tpacpi_hwmon;
1161 static struct input_dev *tpacpi_inputdev;
1162 static struct mutex tpacpi_inputdev_send_mutex;
1165 static int tpacpi_resume_handler(struct platform_device *pdev)
1167 struct ibm_struct *ibm, *itmp;
1169 list_for_each_entry_safe(ibm, itmp,
1170 &tpacpi_all_drivers,
1171 all_drivers) {
1172 if (ibm->resume)
1173 (ibm->resume)();
1176 return 0;
1179 static struct platform_driver tpacpi_pdriver = {
1180 .driver = {
1181 .name = IBM_DRVR_NAME,
1182 .owner = THIS_MODULE,
1184 .resume = tpacpi_resume_handler,
1187 static struct platform_driver tpacpi_hwmon_pdriver = {
1188 .driver = {
1189 .name = IBM_HWMON_DRVR_NAME,
1190 .owner = THIS_MODULE,
1194 /*************************************************************************
1195 * thinkpad-acpi driver attributes
1198 /* interface_version --------------------------------------------------- */
1199 static ssize_t tpacpi_driver_interface_version_show(
1200 struct device_driver *drv,
1201 char *buf)
1203 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1206 static DRIVER_ATTR(interface_version, S_IRUGO,
1207 tpacpi_driver_interface_version_show, NULL);
1209 /* debug_level --------------------------------------------------------- */
1210 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1211 char *buf)
1213 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1216 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1217 const char *buf, size_t count)
1219 unsigned long t;
1221 if (parse_strtoul(buf, 0xffff, &t))
1222 return -EINVAL;
1224 dbg_level = t;
1226 return count;
1229 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1230 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1232 /* version ------------------------------------------------------------- */
1233 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1234 char *buf)
1236 return snprintf(buf, PAGE_SIZE, "%s v%s\n", IBM_DESC, IBM_VERSION);
1239 static DRIVER_ATTR(version, S_IRUGO,
1240 tpacpi_driver_version_show, NULL);
1242 /* --------------------------------------------------------------------- */
1244 static struct driver_attribute* tpacpi_driver_attributes[] = {
1245 &driver_attr_debug_level, &driver_attr_version,
1246 &driver_attr_interface_version,
1249 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1251 int i, res;
1253 i = 0;
1254 res = 0;
1255 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1256 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1257 i++;
1260 return res;
1263 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1265 int i;
1267 for(i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1268 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1271 /*************************************************************************
1272 * sysfs support helpers
1275 struct attribute_set_obj {
1276 struct attribute_set s;
1277 struct attribute *a;
1278 } __attribute__((packed));
1280 static struct attribute_set *create_attr_set(unsigned int max_members,
1281 const char* name)
1283 struct attribute_set_obj *sobj;
1285 if (max_members == 0)
1286 return NULL;
1288 /* Allocates space for implicit NULL at the end too */
1289 sobj = kzalloc(sizeof(struct attribute_set_obj) +
1290 max_members * sizeof(struct attribute *),
1291 GFP_KERNEL);
1292 if (!sobj)
1293 return NULL;
1294 sobj->s.max_members = max_members;
1295 sobj->s.group.attrs = &sobj->a;
1296 sobj->s.group.name = name;
1298 return &sobj->s;
1301 /* not multi-threaded safe, use it in a single thread per set */
1302 static int add_to_attr_set(struct attribute_set* s, struct attribute *attr)
1304 if (!s || !attr)
1305 return -EINVAL;
1307 if (s->members >= s->max_members)
1308 return -ENOMEM;
1310 s->group.attrs[s->members] = attr;
1311 s->members++;
1313 return 0;
1316 static int add_many_to_attr_set(struct attribute_set* s,
1317 struct attribute **attr,
1318 unsigned int count)
1320 int i, res;
1322 for (i = 0; i < count; i++) {
1323 res = add_to_attr_set(s, attr[i]);
1324 if (res)
1325 return res;
1328 return 0;
1331 static void delete_attr_set(struct attribute_set* s, struct kobject *kobj)
1333 sysfs_remove_group(kobj, &s->group);
1334 destroy_attr_set(s);
1337 static int parse_strtoul(const char *buf,
1338 unsigned long max, unsigned long *value)
1340 char *endp;
1342 while (*buf && isspace(*buf))
1343 buf++;
1344 *value = simple_strtoul(buf, &endp, 0);
1345 while (*endp && isspace(*endp))
1346 endp++;
1347 if (*endp || *value > max)
1348 return -EINVAL;
1350 return 0;
1353 /****************************************************************************
1354 ****************************************************************************
1356 * Subdrivers
1358 ****************************************************************************
1359 ****************************************************************************/
1361 /*************************************************************************
1362 * thinkpad-acpi init subdriver
1365 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1367 printk(IBM_INFO "%s v%s\n", IBM_DESC, IBM_VERSION);
1368 printk(IBM_INFO "%s\n", IBM_URL);
1370 printk(IBM_INFO "ThinkPad BIOS %s, EC %s\n",
1371 (thinkpad_id.bios_version_str) ?
1372 thinkpad_id.bios_version_str : "unknown",
1373 (thinkpad_id.ec_version_str) ?
1374 thinkpad_id.ec_version_str : "unknown");
1376 if (thinkpad_id.vendor && thinkpad_id.model_str)
1377 printk(IBM_INFO "%s %s\n",
1378 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1379 "IBM" : ((thinkpad_id.vendor ==
1380 PCI_VENDOR_ID_LENOVO) ?
1381 "Lenovo" : "Unknown vendor"),
1382 thinkpad_id.model_str);
1384 return 0;
1387 static int thinkpad_acpi_driver_read(char *p)
1389 int len = 0;
1391 len += sprintf(p + len, "driver:\t\t%s\n", IBM_DESC);
1392 len += sprintf(p + len, "version:\t%s\n", IBM_VERSION);
1394 return len;
1397 static struct ibm_struct thinkpad_acpi_driver_data = {
1398 .name = "driver",
1399 .read = thinkpad_acpi_driver_read,
1402 /*************************************************************************
1403 * Hotkey subdriver
1406 enum { /* Keys available through NVRAM polling */
1407 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1408 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1411 enum { /* Positions of some of the keys in hotkey masks */
1412 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1413 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1414 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1415 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1416 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1417 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1418 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1419 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1420 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1421 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1422 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1425 enum { /* NVRAM to ACPI HKEY group map */
1426 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1427 TP_ACPI_HKEY_ZOOM_MASK |
1428 TP_ACPI_HKEY_DISPSWTCH_MASK |
1429 TP_ACPI_HKEY_HIBERNATE_MASK,
1430 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1431 TP_ACPI_HKEY_BRGHTDWN_MASK,
1432 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1433 TP_ACPI_HKEY_VOLDWN_MASK |
1434 TP_ACPI_HKEY_MUTE_MASK,
1437 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1438 struct tp_nvram_state {
1439 u16 thinkpad_toggle:1;
1440 u16 zoom_toggle:1;
1441 u16 display_toggle:1;
1442 u16 thinklight_toggle:1;
1443 u16 hibernate_toggle:1;
1444 u16 displayexp_toggle:1;
1445 u16 display_state:1;
1446 u16 brightness_toggle:1;
1447 u16 volume_toggle:1;
1448 u16 mute:1;
1450 u8 brightness_level;
1451 u8 volume_level;
1454 static struct task_struct *tpacpi_hotkey_task;
1455 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1456 static int hotkey_poll_freq = 10; /* Hz */
1457 static struct mutex hotkey_thread_mutex;
1458 static struct mutex hotkey_thread_data_mutex;
1459 static unsigned int hotkey_config_change;
1461 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1463 #define hotkey_source_mask 0U
1465 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1467 static int hotkey_orig_status;
1468 static u32 hotkey_orig_mask;
1469 static u32 hotkey_all_mask;
1470 static u32 hotkey_reserved_mask;
1471 static u32 hotkey_mask;
1473 static u16 *hotkey_keycode_map;
1475 static struct attribute_set *hotkey_dev_attributes;
1477 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1478 #define HOTKEY_CONFIG_CRITICAL_START \
1479 mutex_lock(&hotkey_thread_data_mutex); \
1480 hotkey_config_change++;
1481 #define HOTKEY_CONFIG_CRITICAL_END \
1482 mutex_unlock(&hotkey_thread_data_mutex);
1483 #else
1484 #define HOTKEY_CONFIG_CRITICAL_START
1485 #define HOTKEY_CONFIG_CRITICAL_END
1486 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1488 static int hotkey_get_wlsw(int *status)
1490 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1491 return -EIO;
1492 return 0;
1496 * Call with hotkey_mutex held
1498 static int hotkey_mask_get(void)
1500 u32 m = 0;
1502 if (tp_features.hotkey_mask) {
1503 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1504 return -EIO;
1506 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1508 return 0;
1512 * Call with hotkey_mutex held
1514 static int hotkey_mask_set(u32 mask)
1516 int i;
1517 int rc = 0;
1519 if (tp_features.hotkey_mask) {
1520 HOTKEY_CONFIG_CRITICAL_START
1521 for (i = 0; i < 32; i++) {
1522 u32 m = 1 << 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))) {
1530 rc = -EIO;
1531 break;
1532 } else {
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(IBM_NOTICE
1543 "requested hot key mask 0x%08x, but "
1544 "firmware forced it to 0x%08x\n",
1545 mask, hotkey_mask);
1547 } else {
1548 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1549 HOTKEY_CONFIG_CRITICAL_START
1550 hotkey_mask = mask & hotkey_source_mask;
1551 HOTKEY_CONFIG_CRITICAL_END
1552 hotkey_mask_get();
1553 if (hotkey_mask != mask) {
1554 printk(IBM_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);
1560 #else
1561 hotkey_mask_get();
1562 rc = -ENXIO;
1563 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1566 return rc;
1569 static int hotkey_status_get(int *status)
1571 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1572 return -EIO;
1574 return 0;
1577 static int hotkey_status_set(int status)
1579 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1580 return -EIO;
1582 return 0;
1585 static void tpacpi_input_send_radiosw(void)
1587 int wlsw;
1589 mutex_lock(&tpacpi_inputdev_send_mutex);
1591 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1592 input_report_switch(tpacpi_inputdev,
1593 SW_RADIO, !!wlsw);
1594 input_sync(tpacpi_inputdev);
1597 mutex_unlock(&tpacpi_inputdev_send_mutex);
1600 static void tpacpi_input_send_key(unsigned int scancode)
1602 unsigned int keycode;
1604 keycode = hotkey_keycode_map[scancode];
1606 if (keycode != KEY_RESERVED) {
1607 mutex_lock(&tpacpi_inputdev_send_mutex);
1609 input_report_key(tpacpi_inputdev, keycode, 1);
1610 if (keycode == KEY_UNKNOWN)
1611 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1612 scancode);
1613 input_sync(tpacpi_inputdev);
1615 input_report_key(tpacpi_inputdev, keycode, 0);
1616 if (keycode == KEY_UNKNOWN)
1617 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1618 scancode);
1619 input_sync(tpacpi_inputdev);
1621 mutex_unlock(&tpacpi_inputdev_send_mutex);
1625 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1626 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1628 static void tpacpi_hotkey_send_key(unsigned int scancode)
1630 tpacpi_input_send_key(scancode);
1631 if (hotkey_report_mode < 2) {
1632 acpi_bus_generate_event(ibm_hotkey_acpidriver.device,
1633 0x80, 0x1001 + scancode);
1637 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1639 u8 d;
1641 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1642 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1643 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1644 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1645 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1646 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1648 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1649 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1650 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1652 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1653 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1654 n->displayexp_toggle =
1655 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1657 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1658 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1659 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1660 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1661 n->brightness_toggle =
1662 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1664 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1665 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1666 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1667 >> TP_NVRAM_POS_LEVEL_VOLUME;
1668 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1669 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1673 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1674 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1675 tpacpi_hotkey_send_key(__scancode); } while (0)
1677 #define TPACPI_MAY_SEND_KEY(__scancode) \
1678 do { if (mask & (1 << __scancode)) \
1679 tpacpi_hotkey_send_key(__scancode); } while (0)
1681 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1682 struct tp_nvram_state *newn,
1683 u32 mask)
1685 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1686 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1687 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1688 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1690 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1694 /* handle volume */
1695 if (oldn->volume_toggle != newn->volume_toggle) {
1696 if (oldn->mute != newn->mute) {
1697 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1699 if (oldn->volume_level > newn->volume_level) {
1700 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1701 } else if (oldn->volume_level < newn->volume_level) {
1702 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1703 } else if (oldn->mute == newn->mute) {
1704 /* repeated key presses that didn't change state */
1705 if (newn->mute) {
1706 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1707 } else if (newn->volume_level != 0) {
1708 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1709 } else {
1710 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1715 /* handle brightness */
1716 if (oldn->brightness_toggle != newn->brightness_toggle) {
1717 if (oldn->brightness_level < newn->brightness_level) {
1718 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1719 } else if (oldn->brightness_level > newn->brightness_level) {
1720 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1721 } else {
1722 /* repeated key presses that didn't change state */
1723 if (newn->brightness_level != 0) {
1724 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1725 } else {
1726 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1732 #undef TPACPI_COMPARE_KEY
1733 #undef TPACPI_MAY_SEND_KEY
1735 static int hotkey_kthread(void *data)
1737 struct tp_nvram_state s[2];
1738 u32 mask;
1739 unsigned int si, so;
1740 unsigned long t;
1741 unsigned int change_detector, must_reset;
1743 mutex_lock(&hotkey_thread_mutex);
1745 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1746 goto exit;
1748 so = 0;
1749 si = 1;
1750 t = 0;
1752 /* Initial state for compares */
1753 mutex_lock(&hotkey_thread_data_mutex);
1754 change_detector = hotkey_config_change;
1755 mask = hotkey_source_mask & hotkey_mask;
1756 mutex_unlock(&hotkey_thread_data_mutex);
1757 hotkey_read_nvram(&s[so], mask);
1759 while (!kthread_should_stop() && hotkey_poll_freq) {
1760 if (t == 0)
1761 t = 1000/hotkey_poll_freq;
1762 t = msleep_interruptible(t);
1763 if (unlikely(kthread_should_stop()))
1764 break;
1765 must_reset = try_to_freeze();
1766 if (t > 0 && !must_reset)
1767 continue;
1769 mutex_lock(&hotkey_thread_data_mutex);
1770 if (must_reset || hotkey_config_change != change_detector) {
1771 /* forget old state on thaw or config change */
1772 si = so;
1773 t = 0;
1774 change_detector = hotkey_config_change;
1776 mask = hotkey_source_mask & hotkey_mask;
1777 mutex_unlock(&hotkey_thread_data_mutex);
1779 if (likely(mask)) {
1780 hotkey_read_nvram(&s[si], mask);
1781 if (likely(si != so)) {
1782 hotkey_compare_and_issue_event(&s[so], &s[si],
1783 mask);
1787 so = si;
1788 si ^= 1;
1791 exit:
1792 mutex_unlock(&hotkey_thread_mutex);
1793 return 0;
1796 static void hotkey_poll_stop_sync(void)
1798 if (tpacpi_hotkey_task) {
1799 if (frozen(tpacpi_hotkey_task) ||
1800 freezing(tpacpi_hotkey_task))
1801 thaw_process(tpacpi_hotkey_task);
1803 kthread_stop(tpacpi_hotkey_task);
1804 tpacpi_hotkey_task = NULL;
1805 mutex_lock(&hotkey_thread_mutex);
1806 /* at this point, the thread did exit */
1807 mutex_unlock(&hotkey_thread_mutex);
1811 /* call with hotkey_mutex held */
1812 static void hotkey_poll_setup(int may_warn)
1814 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1815 hotkey_poll_freq > 0 &&
1816 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1817 if (!tpacpi_hotkey_task) {
1818 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1819 NULL, IBM_FILE "d");
1820 if (IS_ERR(tpacpi_hotkey_task)) {
1821 tpacpi_hotkey_task = NULL;
1822 printk(IBM_ERR "could not create kernel thread "
1823 "for hotkey polling\n");
1826 } else {
1827 hotkey_poll_stop_sync();
1828 if (may_warn &&
1829 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1830 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1831 "which is currently disabled\n",
1832 hotkey_source_mask);
1837 static void hotkey_poll_setup_safe(int may_warn)
1839 mutex_lock(&hotkey_mutex);
1840 hotkey_poll_setup(may_warn);
1841 mutex_unlock(&hotkey_mutex);
1844 static int hotkey_inputdev_open(struct input_dev *dev)
1846 switch (tpacpi_lifecycle) {
1847 case TPACPI_LIFE_INIT:
1849 * hotkey_init will call hotkey_poll_setup_safe
1850 * at the appropriate moment
1852 return 0;
1853 case TPACPI_LIFE_EXITING:
1854 return -EBUSY;
1855 case TPACPI_LIFE_RUNNING:
1856 hotkey_poll_setup_safe(0);
1857 return 0;
1860 /* Should only happen if tpacpi_lifecycle is corrupt */
1861 BUG();
1862 return -EBUSY;
1865 static void hotkey_inputdev_close(struct input_dev *dev)
1867 /* disable hotkey polling when possible */
1868 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1869 hotkey_poll_setup_safe(0);
1871 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1873 /* sysfs hotkey enable ------------------------------------------------- */
1874 static ssize_t hotkey_enable_show(struct device *dev,
1875 struct device_attribute *attr,
1876 char *buf)
1878 int res, status;
1880 res = hotkey_status_get(&status);
1881 if (res)
1882 return res;
1884 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1887 static ssize_t hotkey_enable_store(struct device *dev,
1888 struct device_attribute *attr,
1889 const char *buf, size_t count)
1891 unsigned long t;
1892 int res;
1894 if (parse_strtoul(buf, 1, &t))
1895 return -EINVAL;
1897 res = hotkey_status_set(t);
1899 return (res) ? res : count;
1902 static struct device_attribute dev_attr_hotkey_enable =
1903 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1904 hotkey_enable_show, hotkey_enable_store);
1906 /* sysfs hotkey mask --------------------------------------------------- */
1907 static ssize_t hotkey_mask_show(struct device *dev,
1908 struct device_attribute *attr,
1909 char *buf)
1911 int res;
1913 if (mutex_lock_interruptible(&hotkey_mutex))
1914 return -ERESTARTSYS;
1915 res = hotkey_mask_get();
1916 mutex_unlock(&hotkey_mutex);
1918 return (res)?
1919 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1922 static ssize_t hotkey_mask_store(struct device *dev,
1923 struct device_attribute *attr,
1924 const char *buf, size_t count)
1926 unsigned long t;
1927 int res;
1929 if (parse_strtoul(buf, 0xffffffffUL, &t))
1930 return -EINVAL;
1932 if (mutex_lock_interruptible(&hotkey_mutex))
1933 return -ERESTARTSYS;
1935 res = hotkey_mask_set(t);
1937 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1938 hotkey_poll_setup(1);
1939 #endif
1941 mutex_unlock(&hotkey_mutex);
1943 return (res) ? res : count;
1946 static struct device_attribute dev_attr_hotkey_mask =
1947 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1948 hotkey_mask_show, hotkey_mask_store);
1950 /* sysfs hotkey bios_enabled ------------------------------------------- */
1951 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1952 struct device_attribute *attr,
1953 char *buf)
1955 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1958 static struct device_attribute dev_attr_hotkey_bios_enabled =
1959 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1961 /* sysfs hotkey bios_mask ---------------------------------------------- */
1962 static ssize_t hotkey_bios_mask_show(struct device *dev,
1963 struct device_attribute *attr,
1964 char *buf)
1966 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1969 static struct device_attribute dev_attr_hotkey_bios_mask =
1970 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1972 /* sysfs hotkey all_mask ----------------------------------------------- */
1973 static ssize_t hotkey_all_mask_show(struct device *dev,
1974 struct device_attribute *attr,
1975 char *buf)
1977 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1978 hotkey_all_mask | hotkey_source_mask);
1981 static struct device_attribute dev_attr_hotkey_all_mask =
1982 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1984 /* sysfs hotkey recommended_mask --------------------------------------- */
1985 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1986 struct device_attribute *attr,
1987 char *buf)
1989 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1990 (hotkey_all_mask | hotkey_source_mask)
1991 & ~hotkey_reserved_mask);
1994 static struct device_attribute dev_attr_hotkey_recommended_mask =
1995 __ATTR(hotkey_recommended_mask, S_IRUGO,
1996 hotkey_recommended_mask_show, NULL);
1998 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2000 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2001 static ssize_t hotkey_source_mask_show(struct device *dev,
2002 struct device_attribute *attr,
2003 char *buf)
2005 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2008 static ssize_t hotkey_source_mask_store(struct device *dev,
2009 struct device_attribute *attr,
2010 const char *buf, size_t count)
2012 unsigned long t;
2014 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2015 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2016 return -EINVAL;
2018 if (mutex_lock_interruptible(&hotkey_mutex))
2019 return -ERESTARTSYS;
2021 HOTKEY_CONFIG_CRITICAL_START
2022 hotkey_source_mask = t;
2023 HOTKEY_CONFIG_CRITICAL_END
2025 hotkey_poll_setup(1);
2027 mutex_unlock(&hotkey_mutex);
2029 return count;
2032 static struct device_attribute dev_attr_hotkey_source_mask =
2033 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2034 hotkey_source_mask_show, hotkey_source_mask_store);
2036 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2037 static ssize_t hotkey_poll_freq_show(struct device *dev,
2038 struct device_attribute *attr,
2039 char *buf)
2041 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2044 static ssize_t hotkey_poll_freq_store(struct device *dev,
2045 struct device_attribute *attr,
2046 const char *buf, size_t count)
2048 unsigned long t;
2050 if (parse_strtoul(buf, 25, &t))
2051 return -EINVAL;
2053 if (mutex_lock_interruptible(&hotkey_mutex))
2054 return -ERESTARTSYS;
2056 hotkey_poll_freq = t;
2058 hotkey_poll_setup(1);
2059 mutex_unlock(&hotkey_mutex);
2061 return count;
2064 static struct device_attribute dev_attr_hotkey_poll_freq =
2065 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2066 hotkey_poll_freq_show, hotkey_poll_freq_store);
2068 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2070 /* sysfs hotkey radio_sw ----------------------------------------------- */
2071 static ssize_t hotkey_radio_sw_show(struct device *dev,
2072 struct device_attribute *attr,
2073 char *buf)
2075 int res, s;
2076 res = hotkey_get_wlsw(&s);
2077 if (res < 0)
2078 return res;
2080 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2083 static struct device_attribute dev_attr_hotkey_radio_sw =
2084 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2086 /* sysfs hotkey report_mode -------------------------------------------- */
2087 static ssize_t hotkey_report_mode_show(struct device *dev,
2088 struct device_attribute *attr,
2089 char *buf)
2091 return snprintf(buf, PAGE_SIZE, "%d\n",
2092 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2095 static ssize_t hotkey_report_mode_store(struct device *dev,
2096 struct device_attribute *attr,
2097 const char *buf, size_t count)
2099 unsigned long t;
2100 int res = 0;
2102 if (parse_strtoul(buf, 2, &t) || (t < 1))
2103 return -EINVAL;
2105 if (t != hotkey_report_mode) {
2106 if (tp_features.hotkey_report_mode_locked)
2107 return -EPERM;
2108 hotkey_report_mode = t;
2109 printk(IBM_NOTICE "hot key report mode set to %d\n",
2110 hotkey_report_mode);
2113 return (res) ? res : count;
2116 static struct device_attribute dev_attr_hotkey_report_mode =
2117 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
2118 hotkey_report_mode_show, hotkey_report_mode_store);
2120 /* --------------------------------------------------------------------- */
2122 static struct attribute *hotkey_attributes[] __initdata = {
2123 &dev_attr_hotkey_enable.attr,
2124 &dev_attr_hotkey_bios_enabled.attr,
2125 &dev_attr_hotkey_report_mode.attr,
2126 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2127 &dev_attr_hotkey_mask.attr,
2128 &dev_attr_hotkey_all_mask.attr,
2129 &dev_attr_hotkey_recommended_mask.attr,
2130 &dev_attr_hotkey_source_mask.attr,
2131 &dev_attr_hotkey_poll_freq.attr,
2132 #endif
2135 static struct attribute *hotkey_mask_attributes[] __initdata = {
2136 &dev_attr_hotkey_bios_mask.attr,
2137 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2138 &dev_attr_hotkey_mask.attr,
2139 &dev_attr_hotkey_all_mask.attr,
2140 &dev_attr_hotkey_recommended_mask.attr,
2141 #endif
2144 static int __init hotkey_init(struct ibm_init_struct *iibm)
2146 /* Requirements for changing the default keymaps:
2148 * 1. Many of the keys are mapped to KEY_RESERVED for very
2149 * good reasons. Do not change them unless you have deep
2150 * knowledge on the IBM and Lenovo ThinkPad firmware for
2151 * the various ThinkPad models. The driver behaves
2152 * differently for KEY_RESERVED: such keys have their
2153 * hot key mask *unset* in mask_recommended, and also
2154 * in the initial hot key mask programmed into the
2155 * firmware at driver load time, which means the firm-
2156 * ware may react very differently if you change them to
2157 * something else;
2159 * 2. You must be subscribed to the linux-thinkpad and
2160 * ibm-acpi-devel mailing lists, and you should read the
2161 * list archives since 2007 if you want to change the
2162 * keymaps. This requirement exists so that you will
2163 * know the past history of problems with the thinkpad-
2164 * acpi driver keymaps, and also that you will be
2165 * listening to any bug reports;
2167 * 3. Do not send thinkpad-acpi specific patches directly to
2168 * for merging, *ever*. Send them to the linux-acpi
2169 * mailinglist for comments. Merging is to be done only
2170 * through acpi-test and the ACPI maintainer.
2172 * If the above is too much to ask, don't change the keymap.
2173 * Ask the thinkpad-acpi maintainer to do it, instead.
2175 static u16 ibm_keycode_map[] __initdata = {
2176 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2177 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2178 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2179 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2181 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2182 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2183 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2184 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2186 /* brightness: firmware always reacts to them, unless
2187 * X.org did some tricks in the radeon BIOS scratch
2188 * registers of *some* models */
2189 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2190 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2192 /* Thinklight: firmware always react to it */
2193 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2195 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2196 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2198 /* Volume: firmware always react to it and reprograms
2199 * the built-in *extra* mixer. Never map it to control
2200 * another mixer by default. */
2201 KEY_RESERVED, /* 0x14: VOLUME UP */
2202 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2203 KEY_RESERVED, /* 0x16: MUTE */
2205 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2207 /* (assignments unknown, please report if found) */
2208 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2209 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2211 static u16 lenovo_keycode_map[] __initdata = {
2212 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2213 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2214 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2215 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2217 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2218 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2219 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2220 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2222 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2223 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2225 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2227 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2228 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2230 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2231 * react to it and reprograms the built-in *extra* mixer.
2232 * Never map it to control another mixer by default.
2234 * T60?, T61, R60?, R61: firmware and EC tries to send
2235 * these over the regular keyboard, so these are no-ops,
2236 * but there are still weird bugs re. MUTE, so do not
2237 * change unless you get test reports from all Lenovo
2238 * models. May cause the BIOS to interfere with the
2239 * HDA mixer.
2241 KEY_RESERVED, /* 0x14: VOLUME UP */
2242 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2243 KEY_RESERVED, /* 0x16: MUTE */
2245 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2247 /* (assignments unknown, please report if found) */
2248 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2249 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2252 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2253 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2254 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2256 int res, i;
2257 int status;
2258 int hkeyv;
2260 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2262 BUG_ON(!tpacpi_inputdev);
2263 BUG_ON(tpacpi_inputdev->open != NULL ||
2264 tpacpi_inputdev->close != NULL);
2266 IBM_ACPIHANDLE_INIT(hkey);
2267 mutex_init(&hotkey_mutex);
2269 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2270 mutex_init(&hotkey_thread_mutex);
2271 mutex_init(&hotkey_thread_data_mutex);
2272 #endif
2274 /* hotkey not supported on 570 */
2275 tp_features.hotkey = hkey_handle != NULL;
2277 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2278 str_supported(tp_features.hotkey));
2280 if (tp_features.hotkey) {
2281 hotkey_dev_attributes = create_attr_set(10, NULL);
2282 if (!hotkey_dev_attributes)
2283 return -ENOMEM;
2284 res = add_many_to_attr_set(hotkey_dev_attributes,
2285 hotkey_attributes,
2286 ARRAY_SIZE(hotkey_attributes));
2287 if (res)
2288 return res;
2290 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2291 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2292 for HKEY interface version 0x100 */
2293 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2294 if ((hkeyv >> 8) != 1) {
2295 printk(IBM_ERR "unknown version of the "
2296 "HKEY interface: 0x%x\n", hkeyv);
2297 printk(IBM_ERR "please report this to %s\n",
2298 IBM_MAIL);
2299 } else {
2301 * MHKV 0x100 in A31, R40, R40e,
2302 * T4x, X31, and later
2304 tp_features.hotkey_mask = 1;
2308 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2309 str_supported(tp_features.hotkey_mask));
2311 if (tp_features.hotkey_mask) {
2312 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2313 "MHKA", "qd")) {
2314 printk(IBM_ERR
2315 "missing MHKA handler, "
2316 "please report this to %s\n",
2317 IBM_MAIL);
2318 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2322 /* hotkey_source_mask *must* be zero for
2323 * the first hotkey_mask_get */
2324 res = hotkey_status_get(&hotkey_orig_status);
2325 if (!res && tp_features.hotkey_mask) {
2326 res = hotkey_mask_get();
2327 hotkey_orig_mask = hotkey_mask;
2328 if (!res) {
2329 res = add_many_to_attr_set(
2330 hotkey_dev_attributes,
2331 hotkey_mask_attributes,
2332 ARRAY_SIZE(hotkey_mask_attributes));
2336 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2337 if (tp_features.hotkey_mask) {
2338 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2339 & ~hotkey_all_mask;
2340 } else {
2341 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2344 vdbg_printk(TPACPI_DBG_INIT,
2345 "hotkey source mask 0x%08x, polling freq %d\n",
2346 hotkey_source_mask, hotkey_poll_freq);
2347 #endif
2349 /* Not all thinkpads have a hardware radio switch */
2350 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2351 tp_features.hotkey_wlsw = 1;
2352 printk(IBM_INFO
2353 "radio switch found; radios are %s\n",
2354 enabled(status, 0));
2355 res = add_to_attr_set(hotkey_dev_attributes,
2356 &dev_attr_hotkey_radio_sw.attr);
2359 if (!res)
2360 res = register_attr_set_with_sysfs(
2361 hotkey_dev_attributes,
2362 &tpacpi_pdev->dev.kobj);
2363 if (res)
2364 return res;
2366 /* Set up key map */
2368 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2369 GFP_KERNEL);
2370 if (!hotkey_keycode_map) {
2371 printk(IBM_ERR "failed to allocate memory for key map\n");
2372 return -ENOMEM;
2375 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2376 dbg_printk(TPACPI_DBG_INIT,
2377 "using Lenovo default hot key map\n");
2378 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2379 TPACPI_HOTKEY_MAP_SIZE);
2380 } else {
2381 dbg_printk(TPACPI_DBG_INIT,
2382 "using IBM default hot key map\n");
2383 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2384 TPACPI_HOTKEY_MAP_SIZE);
2387 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2388 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2389 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2390 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2391 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2392 tpacpi_inputdev->keycode = hotkey_keycode_map;
2393 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2394 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2395 set_bit(hotkey_keycode_map[i],
2396 tpacpi_inputdev->keybit);
2397 } else {
2398 if (i < sizeof(hotkey_reserved_mask)*8)
2399 hotkey_reserved_mask |= 1 << i;
2403 if (tp_features.hotkey_wlsw) {
2404 set_bit(EV_SW, tpacpi_inputdev->evbit);
2405 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2408 dbg_printk(TPACPI_DBG_INIT,
2409 "enabling hot key handling\n");
2410 res = hotkey_status_set(1);
2411 if (res)
2412 return res;
2413 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2414 & ~hotkey_reserved_mask)
2415 | hotkey_orig_mask);
2416 if (res < 0 && res != -ENXIO)
2417 return res;
2419 if (hotkey_report_mode > 0) {
2420 tp_features.hotkey_report_mode_locked = 1;
2421 } else {
2422 hotkey_report_mode = 1;
2423 vdbg_printk(TPACPI_DBG_INIT,
2424 "hot key reporting mode can be "
2425 "changed at runtime\n");
2428 dbg_printk(TPACPI_DBG_INIT,
2429 "legacy hot key reporting over procfs %s\n",
2430 (hotkey_report_mode < 2) ?
2431 "enabled" : "disabled");
2433 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2434 tpacpi_inputdev->open = &hotkey_inputdev_open;
2435 tpacpi_inputdev->close = &hotkey_inputdev_close;
2437 hotkey_poll_setup_safe(1);
2438 #endif
2441 return (tp_features.hotkey)? 0 : 1;
2444 static void hotkey_exit(void)
2446 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2447 hotkey_poll_stop_sync();
2448 #endif
2450 if (tp_features.hotkey) {
2451 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
2452 /* no short-circuit boolean operator below! */
2453 if ((hotkey_mask_set(hotkey_orig_mask) |
2454 hotkey_status_set(hotkey_orig_status)) != 0)
2455 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
2458 if (hotkey_dev_attributes) {
2459 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2460 hotkey_dev_attributes = NULL;
2464 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2466 u32 hkey;
2467 unsigned int scancode;
2468 int send_acpi_ev;
2469 int ignore_acpi_ev;
2471 if (event != 0x80) {
2472 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
2473 /* forward it to userspace, maybe it knows how to handle it */
2474 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2476 return;
2479 while (1) {
2480 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2481 printk(IBM_ERR "failed to retrieve HKEY event\n");
2482 return;
2485 if (hkey == 0) {
2486 /* queue empty */
2487 return;
2490 send_acpi_ev = 0;
2491 ignore_acpi_ev = 0;
2493 switch (hkey >> 12) {
2494 case 1:
2495 /* 0x1000-0x1FFF: key presses */
2496 scancode = hkey & 0xfff;
2497 if (scancode > 0 && scancode < 0x21) {
2498 scancode--;
2499 if (!(hotkey_source_mask & (1 << scancode))) {
2500 tpacpi_input_send_key(scancode);
2501 } else {
2502 ignore_acpi_ev = 1;
2504 } else {
2505 printk(IBM_ERR
2506 "hotkey 0x%04x out of range for keyboard map\n",
2507 hkey);
2508 send_acpi_ev = 1;
2510 break;
2511 case 5:
2512 /* 0x5000-0x5FFF: LID */
2513 /* we don't handle it through this path, just
2514 * eat up known LID events */
2515 if (hkey != 0x5001 && hkey != 0x5002) {
2516 printk(IBM_ERR
2517 "unknown LID-related HKEY event: 0x%04x\n",
2518 hkey);
2519 send_acpi_ev = 1;
2520 } else {
2521 ignore_acpi_ev = 1;
2523 break;
2524 case 7:
2525 /* 0x7000-0x7FFF: misc */
2526 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2527 tpacpi_input_send_radiosw();
2528 break;
2530 /* fallthrough to default */
2531 default:
2532 /* case 2: dock-related */
2533 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
2534 /* case 3: ultra-bay related. maybe bay in dock? */
2535 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
2536 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
2537 send_acpi_ev = 1;
2540 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
2541 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
2546 static void hotkey_resume(void)
2548 if (hotkey_mask_get())
2549 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
2550 tpacpi_input_send_radiosw();
2551 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2552 hotkey_poll_setup_safe(0);
2553 #endif
2556 /* procfs -------------------------------------------------------------- */
2557 static int hotkey_read(char *p)
2559 int res, status;
2560 int len = 0;
2562 if (!tp_features.hotkey) {
2563 len += sprintf(p + len, "status:\t\tnot supported\n");
2564 return len;
2567 if (mutex_lock_interruptible(&hotkey_mutex))
2568 return -ERESTARTSYS;
2569 res = hotkey_status_get(&status);
2570 if (!res)
2571 res = hotkey_mask_get();
2572 mutex_unlock(&hotkey_mutex);
2573 if (res)
2574 return res;
2576 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2577 if (tp_features.hotkey_mask) {
2578 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2579 len += sprintf(p + len,
2580 "commands:\tenable, disable, reset, <mask>\n");
2581 } else {
2582 len += sprintf(p + len, "mask:\t\tnot supported\n");
2583 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2586 return len;
2589 static int hotkey_write(char *buf)
2591 int res, status;
2592 u32 mask;
2593 char *cmd;
2595 if (!tp_features.hotkey)
2596 return -ENODEV;
2598 if (mutex_lock_interruptible(&hotkey_mutex))
2599 return -ERESTARTSYS;
2601 status = -1;
2602 mask = hotkey_mask;
2604 res = 0;
2605 while ((cmd = next_cmd(&buf))) {
2606 if (strlencmp(cmd, "enable") == 0) {
2607 status = 1;
2608 } else if (strlencmp(cmd, "disable") == 0) {
2609 status = 0;
2610 } else if (strlencmp(cmd, "reset") == 0) {
2611 status = hotkey_orig_status;
2612 mask = hotkey_orig_mask;
2613 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2614 /* mask set */
2615 } else if (sscanf(cmd, "%x", &mask) == 1) {
2616 /* mask set */
2617 } else {
2618 res = -EINVAL;
2619 goto errexit;
2622 if (status != -1)
2623 res = hotkey_status_set(status);
2625 if (!res && mask != hotkey_mask)
2626 res = hotkey_mask_set(mask);
2628 errexit:
2629 mutex_unlock(&hotkey_mutex);
2630 return res;
2633 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2634 .hid = IBM_HKEY_HID,
2635 .notify = hotkey_notify,
2636 .handle = &hkey_handle,
2637 .type = ACPI_DEVICE_NOTIFY,
2640 static struct ibm_struct hotkey_driver_data = {
2641 .name = "hotkey",
2642 .read = hotkey_read,
2643 .write = hotkey_write,
2644 .exit = hotkey_exit,
2645 .resume = hotkey_resume,
2646 .acpi = &ibm_hotkey_acpidriver,
2649 /*************************************************************************
2650 * Bluetooth subdriver
2653 /* sysfs bluetooth enable ---------------------------------------------- */
2654 static ssize_t bluetooth_enable_show(struct device *dev,
2655 struct device_attribute *attr,
2656 char *buf)
2658 int status;
2660 status = bluetooth_get_radiosw();
2661 if (status < 0)
2662 return status;
2664 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2667 static ssize_t bluetooth_enable_store(struct device *dev,
2668 struct device_attribute *attr,
2669 const char *buf, size_t count)
2671 unsigned long t;
2672 int res;
2674 if (parse_strtoul(buf, 1, &t))
2675 return -EINVAL;
2677 res = bluetooth_set_radiosw(t);
2679 return (res) ? res : count;
2682 static struct device_attribute dev_attr_bluetooth_enable =
2683 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2684 bluetooth_enable_show, bluetooth_enable_store);
2686 /* --------------------------------------------------------------------- */
2688 static struct attribute *bluetooth_attributes[] = {
2689 &dev_attr_bluetooth_enable.attr,
2690 NULL
2693 static const struct attribute_group bluetooth_attr_group = {
2694 .attrs = bluetooth_attributes,
2697 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2699 int res;
2700 int status = 0;
2702 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2704 IBM_ACPIHANDLE_INIT(hkey);
2706 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2707 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2708 tp_features.bluetooth = hkey_handle &&
2709 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2711 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2712 str_supported(tp_features.bluetooth),
2713 status);
2715 if (tp_features.bluetooth) {
2716 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2717 /* no bluetooth hardware present in system */
2718 tp_features.bluetooth = 0;
2719 dbg_printk(TPACPI_DBG_INIT,
2720 "bluetooth hardware not installed\n");
2721 } else {
2722 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2723 &bluetooth_attr_group);
2724 if (res)
2725 return res;
2729 return (tp_features.bluetooth)? 0 : 1;
2732 static void bluetooth_exit(void)
2734 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2735 &bluetooth_attr_group);
2738 static int bluetooth_get_radiosw(void)
2740 int status;
2742 if (!tp_features.bluetooth)
2743 return -ENODEV;
2745 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2746 return -EIO;
2748 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2751 static int bluetooth_set_radiosw(int radio_on)
2753 int status;
2755 if (!tp_features.bluetooth)
2756 return -ENODEV;
2758 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2759 return -EIO;
2760 if (radio_on)
2761 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2762 else
2763 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2764 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2765 return -EIO;
2767 return 0;
2770 /* procfs -------------------------------------------------------------- */
2771 static int bluetooth_read(char *p)
2773 int len = 0;
2774 int status = bluetooth_get_radiosw();
2776 if (!tp_features.bluetooth)
2777 len += sprintf(p + len, "status:\t\tnot supported\n");
2778 else {
2779 len += sprintf(p + len, "status:\t\t%s\n",
2780 (status)? "enabled" : "disabled");
2781 len += sprintf(p + len, "commands:\tenable, disable\n");
2784 return len;
2787 static int bluetooth_write(char *buf)
2789 char *cmd;
2791 if (!tp_features.bluetooth)
2792 return -ENODEV;
2794 while ((cmd = next_cmd(&buf))) {
2795 if (strlencmp(cmd, "enable") == 0) {
2796 bluetooth_set_radiosw(1);
2797 } else if (strlencmp(cmd, "disable") == 0) {
2798 bluetooth_set_radiosw(0);
2799 } else
2800 return -EINVAL;
2803 return 0;
2806 static struct ibm_struct bluetooth_driver_data = {
2807 .name = "bluetooth",
2808 .read = bluetooth_read,
2809 .write = bluetooth_write,
2810 .exit = bluetooth_exit,
2813 /*************************************************************************
2814 * Wan subdriver
2817 /* sysfs wan enable ---------------------------------------------------- */
2818 static ssize_t wan_enable_show(struct device *dev,
2819 struct device_attribute *attr,
2820 char *buf)
2822 int status;
2824 status = wan_get_radiosw();
2825 if (status < 0)
2826 return status;
2828 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2831 static ssize_t wan_enable_store(struct device *dev,
2832 struct device_attribute *attr,
2833 const char *buf, size_t count)
2835 unsigned long t;
2836 int res;
2838 if (parse_strtoul(buf, 1, &t))
2839 return -EINVAL;
2841 res = wan_set_radiosw(t);
2843 return (res) ? res : count;
2846 static struct device_attribute dev_attr_wan_enable =
2847 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2848 wan_enable_show, wan_enable_store);
2850 /* --------------------------------------------------------------------- */
2852 static struct attribute *wan_attributes[] = {
2853 &dev_attr_wan_enable.attr,
2854 NULL
2857 static const struct attribute_group wan_attr_group = {
2858 .attrs = wan_attributes,
2861 static int __init wan_init(struct ibm_init_struct *iibm)
2863 int res;
2864 int status = 0;
2866 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2868 IBM_ACPIHANDLE_INIT(hkey);
2870 tp_features.wan = hkey_handle &&
2871 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2873 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2874 str_supported(tp_features.wan),
2875 status);
2877 if (tp_features.wan) {
2878 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2879 /* no wan hardware present in system */
2880 tp_features.wan = 0;
2881 dbg_printk(TPACPI_DBG_INIT,
2882 "wan hardware not installed\n");
2883 } else {
2884 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2885 &wan_attr_group);
2886 if (res)
2887 return res;
2891 return (tp_features.wan)? 0 : 1;
2894 static void wan_exit(void)
2896 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2897 &wan_attr_group);
2900 static int wan_get_radiosw(void)
2902 int status;
2904 if (!tp_features.wan)
2905 return -ENODEV;
2907 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2908 return -EIO;
2910 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2913 static int wan_set_radiosw(int radio_on)
2915 int status;
2917 if (!tp_features.wan)
2918 return -ENODEV;
2920 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2921 return -EIO;
2922 if (radio_on)
2923 status |= TP_ACPI_WANCARD_RADIOSSW;
2924 else
2925 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2926 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2927 return -EIO;
2929 return 0;
2932 /* procfs -------------------------------------------------------------- */
2933 static int wan_read(char *p)
2935 int len = 0;
2936 int status = wan_get_radiosw();
2938 if (!tp_features.wan)
2939 len += sprintf(p + len, "status:\t\tnot supported\n");
2940 else {
2941 len += sprintf(p + len, "status:\t\t%s\n",
2942 (status)? "enabled" : "disabled");
2943 len += sprintf(p + len, "commands:\tenable, disable\n");
2946 return len;
2949 static int wan_write(char *buf)
2951 char *cmd;
2953 if (!tp_features.wan)
2954 return -ENODEV;
2956 while ((cmd = next_cmd(&buf))) {
2957 if (strlencmp(cmd, "enable") == 0) {
2958 wan_set_radiosw(1);
2959 } else if (strlencmp(cmd, "disable") == 0) {
2960 wan_set_radiosw(0);
2961 } else
2962 return -EINVAL;
2965 return 0;
2968 static struct ibm_struct wan_driver_data = {
2969 .name = "wan",
2970 .read = wan_read,
2971 .write = wan_write,
2972 .exit = wan_exit,
2973 .flags.experimental = 1,
2976 /*************************************************************************
2977 * Video subdriver
2980 static enum video_access_mode video_supported;
2981 static int video_orig_autosw;
2983 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2984 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2985 "\\_SB.PCI0.VID0", /* 770e */
2986 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2987 "\\_SB.PCI0.AGP.VID", /* all others */
2988 ); /* R30, R31 */
2990 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2992 static int __init video_init(struct ibm_init_struct *iibm)
2994 int ivga;
2996 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2998 IBM_ACPIHANDLE_INIT(vid);
2999 IBM_ACPIHANDLE_INIT(vid2);
3001 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3002 /* G41, assume IVGA doesn't change */
3003 vid_handle = vid2_handle;
3005 if (!vid_handle)
3006 /* video switching not supported on R30, R31 */
3007 video_supported = TPACPI_VIDEO_NONE;
3008 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3009 /* 570 */
3010 video_supported = TPACPI_VIDEO_570;
3011 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3012 /* 600e/x, 770e, 770x */
3013 video_supported = TPACPI_VIDEO_770;
3014 else
3015 /* all others */
3016 video_supported = TPACPI_VIDEO_NEW;
3018 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3019 str_supported(video_supported != TPACPI_VIDEO_NONE),
3020 video_supported);
3022 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3025 static void video_exit(void)
3027 dbg_printk(TPACPI_DBG_EXIT,
3028 "restoring original video autoswitch mode\n");
3029 if (video_autosw_set(video_orig_autosw))
3030 printk(IBM_ERR "error while trying to restore original "
3031 "video autoswitch mode\n");
3034 static int video_outputsw_get(void)
3036 int status = 0;
3037 int i;
3039 switch (video_supported) {
3040 case TPACPI_VIDEO_570:
3041 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3042 TP_ACPI_VIDEO_570_PHSCMD))
3043 return -EIO;
3044 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3045 break;
3046 case TPACPI_VIDEO_770:
3047 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3048 return -EIO;
3049 if (i)
3050 status |= TP_ACPI_VIDEO_S_LCD;
3051 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3052 return -EIO;
3053 if (i)
3054 status |= TP_ACPI_VIDEO_S_CRT;
3055 break;
3056 case TPACPI_VIDEO_NEW:
3057 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3058 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3059 return -EIO;
3060 if (i)
3061 status |= TP_ACPI_VIDEO_S_CRT;
3063 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3064 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3065 return -EIO;
3066 if (i)
3067 status |= TP_ACPI_VIDEO_S_LCD;
3068 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3069 return -EIO;
3070 if (i)
3071 status |= TP_ACPI_VIDEO_S_DVI;
3072 break;
3073 default:
3074 return -ENOSYS;
3077 return status;
3080 static int video_outputsw_set(int status)
3082 int autosw;
3083 int res = 0;
3085 switch (video_supported) {
3086 case TPACPI_VIDEO_570:
3087 res = acpi_evalf(NULL, NULL,
3088 "\\_SB.PHS2", "vdd",
3089 TP_ACPI_VIDEO_570_PHS2CMD,
3090 status | TP_ACPI_VIDEO_570_PHS2SET);
3091 break;
3092 case TPACPI_VIDEO_770:
3093 autosw = video_autosw_get();
3094 if (autosw < 0)
3095 return autosw;
3097 res = video_autosw_set(1);
3098 if (res)
3099 return res;
3100 res = acpi_evalf(vid_handle, NULL,
3101 "ASWT", "vdd", status * 0x100, 0);
3102 if (!autosw && video_autosw_set(autosw)) {
3103 printk(IBM_ERR "video auto-switch left enabled due to error\n");
3104 return -EIO;
3106 break;
3107 case TPACPI_VIDEO_NEW:
3108 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3109 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3110 break;
3111 default:
3112 return -ENOSYS;
3115 return (res)? 0 : -EIO;
3118 static int video_autosw_get(void)
3120 int autosw = 0;
3122 switch (video_supported) {
3123 case TPACPI_VIDEO_570:
3124 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3125 return -EIO;
3126 break;
3127 case TPACPI_VIDEO_770:
3128 case TPACPI_VIDEO_NEW:
3129 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3130 return -EIO;
3131 break;
3132 default:
3133 return -ENOSYS;
3136 return autosw & 1;
3139 static int video_autosw_set(int enable)
3141 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3142 return -EIO;
3143 return 0;
3146 static int video_outputsw_cycle(void)
3148 int autosw = video_autosw_get();
3149 int res;
3151 if (autosw < 0)
3152 return autosw;
3154 switch (video_supported) {
3155 case TPACPI_VIDEO_570:
3156 res = video_autosw_set(1);
3157 if (res)
3158 return res;
3159 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3160 break;
3161 case TPACPI_VIDEO_770:
3162 case TPACPI_VIDEO_NEW:
3163 res = video_autosw_set(1);
3164 if (res)
3165 return res;
3166 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3167 break;
3168 default:
3169 return -ENOSYS;
3171 if (!autosw && video_autosw_set(autosw)) {
3172 printk(IBM_ERR "video auto-switch left enabled due to error\n");
3173 return -EIO;
3176 return (res)? 0 : -EIO;
3179 static int video_expand_toggle(void)
3181 switch (video_supported) {
3182 case TPACPI_VIDEO_570:
3183 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3184 0 : -EIO;
3185 case TPACPI_VIDEO_770:
3186 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3187 0 : -EIO;
3188 case TPACPI_VIDEO_NEW:
3189 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3190 0 : -EIO;
3191 default:
3192 return -ENOSYS;
3194 /* not reached */
3197 static int video_read(char *p)
3199 int status, autosw;
3200 int len = 0;
3202 if (video_supported == TPACPI_VIDEO_NONE) {
3203 len += sprintf(p + len, "status:\t\tnot supported\n");
3204 return len;
3207 status = video_outputsw_get();
3208 if (status < 0)
3209 return status;
3211 autosw = video_autosw_get();
3212 if (autosw < 0)
3213 return autosw;
3215 len += sprintf(p + len, "status:\t\tsupported\n");
3216 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3217 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3218 if (video_supported == TPACPI_VIDEO_NEW)
3219 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3220 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3221 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3222 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3223 if (video_supported == TPACPI_VIDEO_NEW)
3224 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3225 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3226 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3228 return len;
3231 static int video_write(char *buf)
3233 char *cmd;
3234 int enable, disable, status;
3235 int res;
3237 if (video_supported == TPACPI_VIDEO_NONE)
3238 return -ENODEV;
3240 enable = 0;
3241 disable = 0;
3243 while ((cmd = next_cmd(&buf))) {
3244 if (strlencmp(cmd, "lcd_enable") == 0) {
3245 enable |= TP_ACPI_VIDEO_S_LCD;
3246 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3247 disable |= TP_ACPI_VIDEO_S_LCD;
3248 } else if (strlencmp(cmd, "crt_enable") == 0) {
3249 enable |= TP_ACPI_VIDEO_S_CRT;
3250 } else if (strlencmp(cmd, "crt_disable") == 0) {
3251 disable |= TP_ACPI_VIDEO_S_CRT;
3252 } else if (video_supported == TPACPI_VIDEO_NEW &&
3253 strlencmp(cmd, "dvi_enable") == 0) {
3254 enable |= TP_ACPI_VIDEO_S_DVI;
3255 } else if (video_supported == TPACPI_VIDEO_NEW &&
3256 strlencmp(cmd, "dvi_disable") == 0) {
3257 disable |= TP_ACPI_VIDEO_S_DVI;
3258 } else if (strlencmp(cmd, "auto_enable") == 0) {
3259 res = video_autosw_set(1);
3260 if (res)
3261 return res;
3262 } else if (strlencmp(cmd, "auto_disable") == 0) {
3263 res = video_autosw_set(0);
3264 if (res)
3265 return res;
3266 } else if (strlencmp(cmd, "video_switch") == 0) {
3267 res = video_outputsw_cycle();
3268 if (res)
3269 return res;
3270 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3271 res = video_expand_toggle();
3272 if (res)
3273 return res;
3274 } else
3275 return -EINVAL;
3278 if (enable || disable) {
3279 status = video_outputsw_get();
3280 if (status < 0)
3281 return status;
3282 res = video_outputsw_set((status & ~disable) | enable);
3283 if (res)
3284 return res;
3287 return 0;
3290 static struct ibm_struct video_driver_data = {
3291 .name = "video",
3292 .read = video_read,
3293 .write = video_write,
3294 .exit = video_exit,
3297 /*************************************************************************
3298 * Light (thinklight) subdriver
3301 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3302 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
3304 static int __init light_init(struct ibm_init_struct *iibm)
3306 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3308 IBM_ACPIHANDLE_INIT(ledb);
3309 IBM_ACPIHANDLE_INIT(lght);
3310 IBM_ACPIHANDLE_INIT(cmos);
3312 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3313 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3315 if (tp_features.light)
3316 /* light status not supported on
3317 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3318 tp_features.light_status =
3319 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3321 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3322 str_supported(tp_features.light));
3324 return (tp_features.light)? 0 : 1;
3327 static int light_read(char *p)
3329 int len = 0;
3330 int status = 0;
3332 if (!tp_features.light) {
3333 len += sprintf(p + len, "status:\t\tnot supported\n");
3334 } else if (!tp_features.light_status) {
3335 len += sprintf(p + len, "status:\t\tunknown\n");
3336 len += sprintf(p + len, "commands:\ton, off\n");
3337 } else {
3338 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3339 return -EIO;
3340 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3341 len += sprintf(p + len, "commands:\ton, off\n");
3344 return len;
3347 static int light_write(char *buf)
3349 int cmos_cmd, lght_cmd;
3350 char *cmd;
3351 int success;
3353 if (!tp_features.light)
3354 return -ENODEV;
3356 while ((cmd = next_cmd(&buf))) {
3357 if (strlencmp(cmd, "on") == 0) {
3358 cmos_cmd = 0x0c;
3359 lght_cmd = 1;
3360 } else if (strlencmp(cmd, "off") == 0) {
3361 cmos_cmd = 0x0d;
3362 lght_cmd = 0;
3363 } else
3364 return -EINVAL;
3366 success = cmos_handle ?
3367 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3368 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3369 if (!success)
3370 return -EIO;
3373 return 0;
3376 static struct ibm_struct light_driver_data = {
3377 .name = "light",
3378 .read = light_read,
3379 .write = light_write,
3382 /*************************************************************************
3383 * Dock subdriver
3386 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3388 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3389 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3390 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3391 "\\_SB.PCI.ISA.SLCE", /* 570 */
3392 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3394 /* don't list other alternatives as we install a notify handler on the 570 */
3395 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3397 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3399 .notify = dock_notify,
3400 .handle = &dock_handle,
3401 .type = ACPI_SYSTEM_NOTIFY,
3404 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3405 * We just use it to get notifications of dock hotplug
3406 * in very old thinkpads */
3407 .hid = PCI_ROOT_HID_STRING,
3408 .notify = dock_notify,
3409 .handle = &pci_handle,
3410 .type = ACPI_SYSTEM_NOTIFY,
3414 static struct ibm_struct dock_driver_data[2] = {
3416 .name = "dock",
3417 .read = dock_read,
3418 .write = dock_write,
3419 .acpi = &ibm_dock_acpidriver[0],
3422 .name = "dock",
3423 .acpi = &ibm_dock_acpidriver[1],
3427 #define dock_docked() (_sta(dock_handle) & 1)
3429 static int __init dock_init(struct ibm_init_struct *iibm)
3431 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3433 IBM_ACPIHANDLE_INIT(dock);
3435 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3436 str_supported(dock_handle != NULL));
3438 return (dock_handle)? 0 : 1;
3441 static int __init dock_init2(struct ibm_init_struct *iibm)
3443 int dock2_needed;
3445 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3447 if (dock_driver_data[0].flags.acpi_driver_registered &&
3448 dock_driver_data[0].flags.acpi_notify_installed) {
3449 IBM_ACPIHANDLE_INIT(pci);
3450 dock2_needed = (pci_handle != NULL);
3451 vdbg_printk(TPACPI_DBG_INIT,
3452 "dock PCI handler for the TP 570 is %s\n",
3453 str_supported(dock2_needed));
3454 } else {
3455 vdbg_printk(TPACPI_DBG_INIT,
3456 "dock subdriver part 2 not required\n");
3457 dock2_needed = 0;
3460 return (dock2_needed)? 0 : 1;
3463 static void dock_notify(struct ibm_struct *ibm, u32 event)
3465 int docked = dock_docked();
3466 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
3468 if (event == 1 && !pci) /* 570 */
3469 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3470 else if (event == 1 && pci) /* 570 */
3471 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3472 else if (event == 3 && docked)
3473 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3474 else if (event == 3 && !docked)
3475 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
3476 else if (event == 0 && docked)
3477 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3478 else {
3479 printk(IBM_ERR "unknown dock event %d, status %d\n",
3480 event, _sta(dock_handle));
3481 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
3485 static int dock_read(char *p)
3487 int len = 0;
3488 int docked = dock_docked();
3490 if (!dock_handle)
3491 len += sprintf(p + len, "status:\t\tnot supported\n");
3492 else if (!docked)
3493 len += sprintf(p + len, "status:\t\tundocked\n");
3494 else {
3495 len += sprintf(p + len, "status:\t\tdocked\n");
3496 len += sprintf(p + len, "commands:\tdock, undock\n");
3499 return len;
3502 static int dock_write(char *buf)
3504 char *cmd;
3506 if (!dock_docked())
3507 return -ENODEV;
3509 while ((cmd = next_cmd(&buf))) {
3510 if (strlencmp(cmd, "undock") == 0) {
3511 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3512 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3513 return -EIO;
3514 } else if (strlencmp(cmd, "dock") == 0) {
3515 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3516 return -EIO;
3517 } else
3518 return -EINVAL;
3521 return 0;
3524 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3526 /*************************************************************************
3527 * Bay subdriver
3530 #ifdef CONFIG_THINKPAD_ACPI_BAY
3531 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3532 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3533 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3534 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3535 ); /* A21e, R30, R31 */
3536 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3537 "_EJ0", /* all others */
3538 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3539 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3540 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3541 ); /* all others */
3542 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3543 "_EJ0", /* 770x */
3544 ); /* all others */
3546 static int __init bay_init(struct ibm_init_struct *iibm)
3548 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3550 IBM_ACPIHANDLE_INIT(bay);
3551 if (bay_handle)
3552 IBM_ACPIHANDLE_INIT(bay_ej);
3553 IBM_ACPIHANDLE_INIT(bay2);
3554 if (bay2_handle)
3555 IBM_ACPIHANDLE_INIT(bay2_ej);
3557 tp_features.bay_status = bay_handle &&
3558 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3559 tp_features.bay_status2 = bay2_handle &&
3560 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3562 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3563 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3564 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3565 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3567 vdbg_printk(TPACPI_DBG_INIT,
3568 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3569 str_supported(tp_features.bay_status),
3570 str_supported(tp_features.bay_eject),
3571 str_supported(tp_features.bay_status2),
3572 str_supported(tp_features.bay_eject2));
3574 return (tp_features.bay_status || tp_features.bay_eject ||
3575 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3578 static void bay_notify(struct ibm_struct *ibm, u32 event)
3580 acpi_bus_generate_event(ibm->acpi->device, event, 0);
3583 #define bay_occupied(b) (_sta(b##_handle) & 1)
3585 static int bay_read(char *p)
3587 int len = 0;
3588 int occupied = bay_occupied(bay);
3589 int occupied2 = bay_occupied(bay2);
3590 int eject, eject2;
3592 len += sprintf(p + len, "status:\t\t%s\n",
3593 tp_features.bay_status ?
3594 (occupied ? "occupied" : "unoccupied") :
3595 "not supported");
3596 if (tp_features.bay_status2)
3597 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3598 "occupied" : "unoccupied");
3600 eject = tp_features.bay_eject && occupied;
3601 eject2 = tp_features.bay_eject2 && occupied2;
3603 if (eject && eject2)
3604 len += sprintf(p + len, "commands:\teject, eject2\n");
3605 else if (eject)
3606 len += sprintf(p + len, "commands:\teject\n");
3607 else if (eject2)
3608 len += sprintf(p + len, "commands:\teject2\n");
3610 return len;
3613 static int bay_write(char *buf)
3615 char *cmd;
3617 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3618 return -ENODEV;
3620 while ((cmd = next_cmd(&buf))) {
3621 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3622 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3623 return -EIO;
3624 } else if (tp_features.bay_eject2 &&
3625 strlencmp(cmd, "eject2") == 0) {
3626 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3627 return -EIO;
3628 } else
3629 return -EINVAL;
3632 return 0;
3635 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3636 .notify = bay_notify,
3637 .handle = &bay_handle,
3638 .type = ACPI_SYSTEM_NOTIFY,
3641 static struct ibm_struct bay_driver_data = {
3642 .name = "bay",
3643 .read = bay_read,
3644 .write = bay_write,
3645 .acpi = &ibm_bay_acpidriver,
3648 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3650 /*************************************************************************
3651 * CMOS subdriver
3654 /* sysfs cmos_command -------------------------------------------------- */
3655 static ssize_t cmos_command_store(struct device *dev,
3656 struct device_attribute *attr,
3657 const char *buf, size_t count)
3659 unsigned long cmos_cmd;
3660 int res;
3662 if (parse_strtoul(buf, 21, &cmos_cmd))
3663 return -EINVAL;
3665 res = issue_thinkpad_cmos_command(cmos_cmd);
3666 return (res)? res : count;
3669 static struct device_attribute dev_attr_cmos_command =
3670 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3672 /* --------------------------------------------------------------------- */
3674 static int __init cmos_init(struct ibm_init_struct *iibm)
3676 int res;
3678 vdbg_printk(TPACPI_DBG_INIT,
3679 "initializing cmos commands subdriver\n");
3681 IBM_ACPIHANDLE_INIT(cmos);
3683 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3684 str_supported(cmos_handle != NULL));
3686 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3687 if (res)
3688 return res;
3690 return (cmos_handle)? 0 : 1;
3693 static void cmos_exit(void)
3695 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3698 static int cmos_read(char *p)
3700 int len = 0;
3702 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3703 R30, R31, T20-22, X20-21 */
3704 if (!cmos_handle)
3705 len += sprintf(p + len, "status:\t\tnot supported\n");
3706 else {
3707 len += sprintf(p + len, "status:\t\tsupported\n");
3708 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3711 return len;
3714 static int cmos_write(char *buf)
3716 char *cmd;
3717 int cmos_cmd, res;
3719 while ((cmd = next_cmd(&buf))) {
3720 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3721 cmos_cmd >= 0 && cmos_cmd <= 21) {
3722 /* cmos_cmd set */
3723 } else
3724 return -EINVAL;
3726 res = issue_thinkpad_cmos_command(cmos_cmd);
3727 if (res)
3728 return res;
3731 return 0;
3734 static struct ibm_struct cmos_driver_data = {
3735 .name = "cmos",
3736 .read = cmos_read,
3737 .write = cmos_write,
3738 .exit = cmos_exit,
3741 /*************************************************************************
3742 * LED subdriver
3745 static enum led_access_mode led_supported;
3747 IBM_HANDLE(led, ec, "SLED", /* 570 */
3748 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3749 "LED", /* all others */
3750 ); /* R30, R31 */
3752 static int __init led_init(struct ibm_init_struct *iibm)
3754 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3756 IBM_ACPIHANDLE_INIT(led);
3758 if (!led_handle)
3759 /* led not supported on R30, R31 */
3760 led_supported = TPACPI_LED_NONE;
3761 else if (strlencmp(led_path, "SLED") == 0)
3762 /* 570 */
3763 led_supported = TPACPI_LED_570;
3764 else if (strlencmp(led_path, "SYSL") == 0)
3765 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3766 led_supported = TPACPI_LED_OLD;
3767 else
3768 /* all others */
3769 led_supported = TPACPI_LED_NEW;
3771 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3772 str_supported(led_supported), led_supported);
3774 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3777 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3779 static int led_read(char *p)
3781 int len = 0;
3783 if (!led_supported) {
3784 len += sprintf(p + len, "status:\t\tnot supported\n");
3785 return len;
3787 len += sprintf(p + len, "status:\t\tsupported\n");
3789 if (led_supported == TPACPI_LED_570) {
3790 /* 570 */
3791 int i, status;
3792 for (i = 0; i < 8; i++) {
3793 if (!acpi_evalf(ec_handle,
3794 &status, "GLED", "dd", 1 << i))
3795 return -EIO;
3796 len += sprintf(p + len, "%d:\t\t%s\n",
3797 i, led_status(status));
3801 len += sprintf(p + len, "commands:\t"
3802 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3804 return len;
3807 /* off, on, blink */
3808 static const int led_sled_arg1[] = { 0, 1, 3 };
3809 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3810 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3811 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3813 static int led_write(char *buf)
3815 char *cmd;
3816 int led, ind, ret;
3818 if (!led_supported)
3819 return -ENODEV;
3821 while ((cmd = next_cmd(&buf))) {
3822 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3823 return -EINVAL;
3825 if (strstr(cmd, "off")) {
3826 ind = 0;
3827 } else if (strstr(cmd, "on")) {
3828 ind = 1;
3829 } else if (strstr(cmd, "blink")) {
3830 ind = 2;
3831 } else
3832 return -EINVAL;
3834 if (led_supported == TPACPI_LED_570) {
3835 /* 570 */
3836 led = 1 << led;
3837 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3838 led, led_sled_arg1[ind]))
3839 return -EIO;
3840 } else if (led_supported == TPACPI_LED_OLD) {
3841 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3842 led = 1 << led;
3843 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3844 if (ret >= 0)
3845 ret =
3846 ec_write(TPACPI_LED_EC_HLBL,
3847 led * led_exp_hlbl[ind]);
3848 if (ret >= 0)
3849 ret =
3850 ec_write(TPACPI_LED_EC_HLCL,
3851 led * led_exp_hlcl[ind]);
3852 if (ret < 0)
3853 return ret;
3854 } else {
3855 /* all others */
3856 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3857 led, led_led_arg1[ind]))
3858 return -EIO;
3862 return 0;
3865 static struct ibm_struct led_driver_data = {
3866 .name = "led",
3867 .read = led_read,
3868 .write = led_write,
3871 /*************************************************************************
3872 * Beep subdriver
3875 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3877 static int __init beep_init(struct ibm_init_struct *iibm)
3879 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3881 IBM_ACPIHANDLE_INIT(beep);
3883 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3884 str_supported(beep_handle != NULL));
3886 return (beep_handle)? 0 : 1;
3889 static int beep_read(char *p)
3891 int len = 0;
3893 if (!beep_handle)
3894 len += sprintf(p + len, "status:\t\tnot supported\n");
3895 else {
3896 len += sprintf(p + len, "status:\t\tsupported\n");
3897 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3900 return len;
3903 static int beep_write(char *buf)
3905 char *cmd;
3906 int beep_cmd;
3908 if (!beep_handle)
3909 return -ENODEV;
3911 while ((cmd = next_cmd(&buf))) {
3912 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3913 beep_cmd >= 0 && beep_cmd <= 17) {
3914 /* beep_cmd set */
3915 } else
3916 return -EINVAL;
3917 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3918 return -EIO;
3921 return 0;
3924 static struct ibm_struct beep_driver_data = {
3925 .name = "beep",
3926 .read = beep_read,
3927 .write = beep_write,
3930 /*************************************************************************
3931 * Thermal subdriver
3934 static enum thermal_access_mode thermal_read_mode;
3936 /* sysfs temp##_input -------------------------------------------------- */
3938 static ssize_t thermal_temp_input_show(struct device *dev,
3939 struct device_attribute *attr,
3940 char *buf)
3942 struct sensor_device_attribute *sensor_attr =
3943 to_sensor_dev_attr(attr);
3944 int idx = sensor_attr->index;
3945 s32 value;
3946 int res;
3948 res = thermal_get_sensor(idx, &value);
3949 if (res)
3950 return res;
3951 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3952 return -ENXIO;
3954 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3957 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3958 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3960 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3961 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3962 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3963 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3964 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3965 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3966 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3967 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3968 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3969 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3970 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3971 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3972 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3973 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3974 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3975 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3976 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3979 #define THERMAL_ATTRS(X) \
3980 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3982 static struct attribute *thermal_temp_input_attr[] = {
3983 THERMAL_ATTRS(8),
3984 THERMAL_ATTRS(9),
3985 THERMAL_ATTRS(10),
3986 THERMAL_ATTRS(11),
3987 THERMAL_ATTRS(12),
3988 THERMAL_ATTRS(13),
3989 THERMAL_ATTRS(14),
3990 THERMAL_ATTRS(15),
3991 THERMAL_ATTRS(0),
3992 THERMAL_ATTRS(1),
3993 THERMAL_ATTRS(2),
3994 THERMAL_ATTRS(3),
3995 THERMAL_ATTRS(4),
3996 THERMAL_ATTRS(5),
3997 THERMAL_ATTRS(6),
3998 THERMAL_ATTRS(7),
3999 NULL
4002 static const struct attribute_group thermal_temp_input16_group = {
4003 .attrs = thermal_temp_input_attr
4006 static const struct attribute_group thermal_temp_input8_group = {
4007 .attrs = &thermal_temp_input_attr[8]
4010 #undef THERMAL_SENSOR_ATTR_TEMP
4011 #undef THERMAL_ATTRS
4013 /* --------------------------------------------------------------------- */
4015 static int __init thermal_init(struct ibm_init_struct *iibm)
4017 u8 t, ta1, ta2;
4018 int i;
4019 int acpi_tmp7;
4020 int res;
4022 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4024 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4026 if (thinkpad_id.ec_model) {
4028 * Direct EC access mode: sensors at registers
4029 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4030 * non-implemented, thermal sensors return 0x80 when
4031 * not available
4034 ta1 = ta2 = 0;
4035 for (i = 0; i < 8; i++) {
4036 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4037 ta1 |= t;
4038 } else {
4039 ta1 = 0;
4040 break;
4042 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4043 ta2 |= t;
4044 } else {
4045 ta1 = 0;
4046 break;
4049 if (ta1 == 0) {
4050 /* This is sheer paranoia, but we handle it anyway */
4051 if (acpi_tmp7) {
4052 printk(IBM_ERR
4053 "ThinkPad ACPI EC access misbehaving, "
4054 "falling back to ACPI TMPx access mode\n");
4055 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4056 } else {
4057 printk(IBM_ERR
4058 "ThinkPad ACPI EC access misbehaving, "
4059 "disabling thermal sensors access\n");
4060 thermal_read_mode = TPACPI_THERMAL_NONE;
4062 } else {
4063 thermal_read_mode =
4064 (ta2 != 0) ?
4065 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4067 } else if (acpi_tmp7) {
4068 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4069 /* 600e/x, 770e, 770x */
4070 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4071 } else {
4072 /* Standard ACPI TMPx access, max 8 sensors */
4073 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4075 } else {
4076 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4077 thermal_read_mode = TPACPI_THERMAL_NONE;
4080 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4081 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4082 thermal_read_mode);
4084 switch(thermal_read_mode) {
4085 case TPACPI_THERMAL_TPEC_16:
4086 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4087 &thermal_temp_input16_group);
4088 if (res)
4089 return res;
4090 break;
4091 case TPACPI_THERMAL_TPEC_8:
4092 case TPACPI_THERMAL_ACPI_TMP07:
4093 case TPACPI_THERMAL_ACPI_UPDT:
4094 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4095 &thermal_temp_input8_group);
4096 if (res)
4097 return res;
4098 break;
4099 case TPACPI_THERMAL_NONE:
4100 default:
4101 return 1;
4104 return 0;
4107 static void thermal_exit(void)
4109 switch(thermal_read_mode) {
4110 case TPACPI_THERMAL_TPEC_16:
4111 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4112 &thermal_temp_input16_group);
4113 break;
4114 case TPACPI_THERMAL_TPEC_8:
4115 case TPACPI_THERMAL_ACPI_TMP07:
4116 case TPACPI_THERMAL_ACPI_UPDT:
4117 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4118 &thermal_temp_input16_group);
4119 break;
4120 case TPACPI_THERMAL_NONE:
4121 default:
4122 break;
4126 /* idx is zero-based */
4127 static int thermal_get_sensor(int idx, s32 *value)
4129 int t;
4130 s8 tmp;
4131 char tmpi[5];
4133 t = TP_EC_THERMAL_TMP0;
4135 switch (thermal_read_mode) {
4136 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4137 case TPACPI_THERMAL_TPEC_16:
4138 if (idx >= 8 && idx <= 15) {
4139 t = TP_EC_THERMAL_TMP8;
4140 idx -= 8;
4142 /* fallthrough */
4143 #endif
4144 case TPACPI_THERMAL_TPEC_8:
4145 if (idx <= 7) {
4146 if (!acpi_ec_read(t + idx, &tmp))
4147 return -EIO;
4148 *value = tmp * 1000;
4149 return 0;
4151 break;
4153 case TPACPI_THERMAL_ACPI_UPDT:
4154 if (idx <= 7) {
4155 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4156 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4157 return -EIO;
4158 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4159 return -EIO;
4160 *value = (t - 2732) * 100;
4161 return 0;
4163 break;
4165 case TPACPI_THERMAL_ACPI_TMP07:
4166 if (idx <= 7) {
4167 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4168 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4169 return -EIO;
4170 if (t > 127 || t < -127)
4171 t = TP_EC_THERMAL_TMP_NA;
4172 *value = t * 1000;
4173 return 0;
4175 break;
4177 case TPACPI_THERMAL_NONE:
4178 default:
4179 return -ENOSYS;
4182 return -EINVAL;
4185 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4187 int res, i;
4188 int n;
4190 n = 8;
4191 i = 0;
4193 if (!s)
4194 return -EINVAL;
4196 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4197 n = 16;
4199 for(i = 0 ; i < n; i++) {
4200 res = thermal_get_sensor(i, &s->temp[i]);
4201 if (res)
4202 return res;
4205 return n;
4208 static int thermal_read(char *p)
4210 int len = 0;
4211 int n, i;
4212 struct ibm_thermal_sensors_struct t;
4214 n = thermal_get_sensors(&t);
4215 if (unlikely(n < 0))
4216 return n;
4218 len += sprintf(p + len, "temperatures:\t");
4220 if (n > 0) {
4221 for (i = 0; i < (n - 1); i++)
4222 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4223 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4224 } else
4225 len += sprintf(p + len, "not supported\n");
4227 return len;
4230 static struct ibm_struct thermal_driver_data = {
4231 .name = "thermal",
4232 .read = thermal_read,
4233 .exit = thermal_exit,
4236 /*************************************************************************
4237 * EC Dump subdriver
4240 static u8 ecdump_regs[256];
4242 static int ecdump_read(char *p)
4244 int len = 0;
4245 int i, j;
4246 u8 v;
4248 len += sprintf(p + len, "EC "
4249 " +00 +01 +02 +03 +04 +05 +06 +07"
4250 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4251 for (i = 0; i < 256; i += 16) {
4252 len += sprintf(p + len, "EC 0x%02x:", i);
4253 for (j = 0; j < 16; j++) {
4254 if (!acpi_ec_read(i + j, &v))
4255 break;
4256 if (v != ecdump_regs[i + j])
4257 len += sprintf(p + len, " *%02x", v);
4258 else
4259 len += sprintf(p + len, " %02x", v);
4260 ecdump_regs[i + j] = v;
4262 len += sprintf(p + len, "\n");
4263 if (j != 16)
4264 break;
4267 /* These are way too dangerous to advertise openly... */
4268 #if 0
4269 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4270 " (<offset> is 00-ff, <value> is 00-ff)\n");
4271 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4272 " (<offset> is 00-ff, <value> is 0-255)\n");
4273 #endif
4274 return len;
4277 static int ecdump_write(char *buf)
4279 char *cmd;
4280 int i, v;
4282 while ((cmd = next_cmd(&buf))) {
4283 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4284 /* i and v set */
4285 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4286 /* i and v set */
4287 } else
4288 return -EINVAL;
4289 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4290 if (!acpi_ec_write(i, v))
4291 return -EIO;
4292 } else
4293 return -EINVAL;
4296 return 0;
4299 static struct ibm_struct ecdump_driver_data = {
4300 .name = "ecdump",
4301 .read = ecdump_read,
4302 .write = ecdump_write,
4303 .flags.experimental = 1,
4306 /*************************************************************************
4307 * Backlight/brightness subdriver
4310 static struct backlight_device *ibm_backlight_device;
4312 static struct backlight_properties ibm_backlight_data = {
4313 .owner = THIS_MODULE,
4314 .get_brightness = brightness_get,
4315 .update_status = brightness_update_status,
4318 static struct mutex brightness_mutex;
4320 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4322 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4323 union acpi_object *obj;
4324 int rc;
4326 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4327 obj = (union acpi_object *)buffer.pointer;
4328 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4329 printk(IBM_ERR "Unknown BCLL data, "
4330 "please report this to %s\n", IBM_MAIL);
4331 rc = 0;
4332 } else {
4333 rc = obj->package.count;
4335 } else {
4336 return 0;
4339 kfree(buffer.pointer);
4340 return rc;
4343 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4344 void *context, void **rv)
4346 char name[ACPI_PATH_SEGMENT_LENGTH];
4347 struct acpi_buffer buffer = { sizeof(name), &name };
4349 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4350 !strncmp("BCLL", name, sizeof(name) - 1)) {
4351 if (tpacpi_query_bcll_levels(handle) == 16) {
4352 *rv = handle;
4353 return AE_CTRL_TERMINATE;
4354 } else {
4355 return AE_OK;
4357 } else {
4358 return AE_OK;
4362 static int __init brightness_check_levels(void)
4364 int status;
4365 void *found_node = NULL;
4367 if (!vid_handle) {
4368 IBM_ACPIHANDLE_INIT(vid);
4370 if (!vid_handle)
4371 return 0;
4373 /* Search for a BCLL package with 16 levels */
4374 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4375 brightness_find_bcll, NULL, &found_node);
4377 return (ACPI_SUCCESS(status) && found_node != NULL);
4380 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4381 void *context, void **rv)
4383 char name[ACPI_PATH_SEGMENT_LENGTH];
4384 struct acpi_buffer buffer = { sizeof(name), &name };
4386 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4387 !strncmp("_BCL", name, sizeof(name) - 1)) {
4388 *rv = handle;
4389 return AE_CTRL_TERMINATE;
4390 } else {
4391 return AE_OK;
4395 static int __init brightness_check_std_acpi_support(void)
4397 int status;
4398 void *found_node = NULL;
4400 if (!vid_handle) {
4401 IBM_ACPIHANDLE_INIT(vid);
4403 if (!vid_handle)
4404 return 0;
4406 /* Search for a _BCL method, but don't execute it */
4407 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4408 brightness_find_bcl, NULL, &found_node);
4410 return (ACPI_SUCCESS(status) && found_node != NULL);
4413 static int __init brightness_init(struct ibm_init_struct *iibm)
4415 int b;
4417 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4419 mutex_init(&brightness_mutex);
4421 if (!brightness_enable) {
4422 dbg_printk(TPACPI_DBG_INIT,
4423 "brightness support disabled by module parameter\n");
4424 return 1;
4425 } else if (brightness_enable > 1) {
4426 if (brightness_check_std_acpi_support()) {
4427 printk(IBM_NOTICE
4428 "standard ACPI backlight interface available, not loading native one...\n");
4429 return 1;
4433 if (!brightness_mode) {
4434 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4435 brightness_mode = 2;
4436 else
4437 brightness_mode = 3;
4439 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4440 brightness_mode);
4443 if (brightness_mode > 3)
4444 return -EINVAL;
4446 tp_features.bright_16levels =
4447 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4448 brightness_check_levels();
4450 b = brightness_get(NULL);
4451 if (b < 0)
4452 return 1;
4454 if (tp_features.bright_16levels)
4455 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
4457 ibm_backlight_device = backlight_device_register(
4458 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4459 &ibm_backlight_data);
4460 if (IS_ERR(ibm_backlight_device)) {
4461 printk(IBM_ERR "Could not register backlight device\n");
4462 return PTR_ERR(ibm_backlight_device);
4464 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4466 ibm_backlight_device->props->max_brightness =
4467 (tp_features.bright_16levels)? 15 : 7;
4468 ibm_backlight_device->props->brightness = b;
4469 brightness_update_status(ibm_backlight_device);
4471 return 0;
4474 static void brightness_exit(void)
4476 if (ibm_backlight_device) {
4477 vdbg_printk(TPACPI_DBG_EXIT,
4478 "calling backlight_device_unregister()\n");
4479 backlight_device_unregister(ibm_backlight_device);
4480 ibm_backlight_device = NULL;
4484 static int brightness_update_status(struct backlight_device *bd)
4486 /* it is the backlight class's job (caller) to handle
4487 * EINTR and other errors properly */
4488 return brightness_set(
4489 (bd->props->fb_blank == FB_BLANK_UNBLANK &&
4490 bd->props->power == FB_BLANK_UNBLANK) ?
4491 bd->props->brightness : 0);
4495 * ThinkPads can read brightness from two places: EC 0x31, or
4496 * CMOS NVRAM byte 0x5E, bits 0-3.
4498 static int brightness_get(struct backlight_device *bd)
4500 u8 lec = 0, lcmos = 0, level = 0;
4502 if (brightness_mode & 1) {
4503 if (!acpi_ec_read(brightness_offset, &lec))
4504 return -EIO;
4505 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4506 level = lec;
4508 if (brightness_mode & 2) {
4509 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4510 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4511 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4512 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4513 level = lcmos;
4516 if (brightness_mode == 3 && lec != lcmos) {
4517 printk(IBM_ERR
4518 "CMOS NVRAM (%u) and EC (%u) do not agree "
4519 "on display brightness level\n",
4520 (unsigned int) lcmos,
4521 (unsigned int) lec);
4522 return -EIO;
4525 return level;
4528 /* May return EINTR which can always be mapped to ERESTARTSYS */
4529 static int brightness_set(int value)
4531 int cmos_cmd, inc, i, res;
4532 int current_value;
4534 if (value > ((tp_features.bright_16levels)? 15 : 7))
4535 return -EINVAL;
4537 res = mutex_lock_interruptible(&brightness_mutex);
4538 if (res < 0)
4539 return res;
4541 current_value = brightness_get(NULL);
4542 if (current_value < 0) {
4543 res = current_value;
4544 goto errout;
4547 cmos_cmd = value > current_value ?
4548 TP_CMOS_BRIGHTNESS_UP :
4549 TP_CMOS_BRIGHTNESS_DOWN;
4550 inc = (value > current_value)? 1 : -1;
4552 res = 0;
4553 for (i = current_value; i != value; i += inc) {
4554 if ((brightness_mode & 2) &&
4555 issue_thinkpad_cmos_command(cmos_cmd)) {
4556 res = -EIO;
4557 goto errout;
4559 if ((brightness_mode & 1) &&
4560 !acpi_ec_write(brightness_offset, i + inc)) {
4561 res = -EIO;
4562 goto errout;;
4566 errout:
4567 mutex_unlock(&brightness_mutex);
4568 return res;
4571 static int brightness_read(char *p)
4573 int len = 0;
4574 int level;
4576 if ((level = brightness_get(NULL)) < 0) {
4577 len += sprintf(p + len, "level:\t\tunreadable\n");
4578 } else {
4579 len += sprintf(p + len, "level:\t\t%d\n", level);
4580 len += sprintf(p + len, "commands:\tup, down\n");
4581 len += sprintf(p + len, "commands:\tlevel <level>"
4582 " (<level> is 0-%d)\n",
4583 (tp_features.bright_16levels) ? 15 : 7);
4586 return len;
4589 static int brightness_write(char *buf)
4591 int level;
4592 int rc;
4593 char *cmd;
4594 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4596 level = brightness_get(NULL);
4597 if (level < 0)
4598 return level;
4600 while ((cmd = next_cmd(&buf))) {
4601 if (strlencmp(cmd, "up") == 0) {
4602 if (level < max_level)
4603 level++;
4604 } else if (strlencmp(cmd, "down") == 0) {
4605 if (level > 0)
4606 level--;
4607 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4608 level >= 0 && level <= max_level) {
4609 /* new level set */
4610 } else
4611 return -EINVAL;
4615 * Now we know what the final level should be, so we try to set it.
4616 * Doing it this way makes the syscall restartable in case of EINTR
4618 rc = brightness_set(level);
4619 return (rc == -EINTR)? ERESTARTSYS : rc;
4622 static struct ibm_struct brightness_driver_data = {
4623 .name = "brightness",
4624 .read = brightness_read,
4625 .write = brightness_write,
4626 .exit = brightness_exit,
4629 /*************************************************************************
4630 * Volume subdriver
4633 static int volume_read(char *p)
4635 int len = 0;
4636 u8 level;
4638 if (!acpi_ec_read(volume_offset, &level)) {
4639 len += sprintf(p + len, "level:\t\tunreadable\n");
4640 } else {
4641 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4642 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4643 len += sprintf(p + len, "commands:\tup, down, mute\n");
4644 len += sprintf(p + len, "commands:\tlevel <level>"
4645 " (<level> is 0-15)\n");
4648 return len;
4651 static int volume_write(char *buf)
4653 int cmos_cmd, inc, i;
4654 u8 level, mute;
4655 int new_level, new_mute;
4656 char *cmd;
4658 while ((cmd = next_cmd(&buf))) {
4659 if (!acpi_ec_read(volume_offset, &level))
4660 return -EIO;
4661 new_mute = mute = level & 0x40;
4662 new_level = level = level & 0xf;
4664 if (strlencmp(cmd, "up") == 0) {
4665 if (mute)
4666 new_mute = 0;
4667 else
4668 new_level = level == 15 ? 15 : level + 1;
4669 } else if (strlencmp(cmd, "down") == 0) {
4670 if (mute)
4671 new_mute = 0;
4672 else
4673 new_level = level == 0 ? 0 : level - 1;
4674 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4675 new_level >= 0 && new_level <= 15) {
4676 /* new_level set */
4677 } else if (strlencmp(cmd, "mute") == 0) {
4678 new_mute = 0x40;
4679 } else
4680 return -EINVAL;
4682 if (new_level != level) { /* mute doesn't change */
4683 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4684 inc = new_level > level ? 1 : -1;
4686 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4687 !acpi_ec_write(volume_offset, level)))
4688 return -EIO;
4690 for (i = level; i != new_level; i += inc)
4691 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4692 !acpi_ec_write(volume_offset, i + inc))
4693 return -EIO;
4695 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4696 !acpi_ec_write(volume_offset,
4697 new_level + mute)))
4698 return -EIO;
4701 if (new_mute != mute) { /* level doesn't change */
4702 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4704 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4705 !acpi_ec_write(volume_offset, level + new_mute))
4706 return -EIO;
4710 return 0;
4713 static struct ibm_struct volume_driver_data = {
4714 .name = "volume",
4715 .read = volume_read,
4716 .write = volume_write,
4719 /*************************************************************************
4720 * Fan subdriver
4724 * FAN ACCESS MODES
4726 * TPACPI_FAN_RD_ACPI_GFAN:
4727 * ACPI GFAN method: returns fan level
4729 * see TPACPI_FAN_WR_ACPI_SFAN
4730 * EC 0x2f (HFSP) not available if GFAN exists
4732 * TPACPI_FAN_WR_ACPI_SFAN:
4733 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4735 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4736 * it for writing.
4738 * TPACPI_FAN_WR_TPEC:
4739 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4740 * Supported on almost all ThinkPads
4742 * Fan speed changes of any sort (including those caused by the
4743 * disengaged mode) are usually done slowly by the firmware as the
4744 * maximum ammount of fan duty cycle change per second seems to be
4745 * limited.
4747 * Reading is not available if GFAN exists.
4748 * Writing is not available if SFAN exists.
4750 * Bits
4751 * 7 automatic mode engaged;
4752 * (default operation mode of the ThinkPad)
4753 * fan level is ignored in this mode.
4754 * 6 full speed mode (takes precedence over bit 7);
4755 * not available on all thinkpads. May disable
4756 * the tachometer while the fan controller ramps up
4757 * the speed (which can take up to a few *minutes*).
4758 * Speeds up fan to 100% duty-cycle, which is far above
4759 * the standard RPM levels. It is not impossible that
4760 * it could cause hardware damage.
4761 * 5-3 unused in some models. Extra bits for fan level
4762 * in others, but still useless as all values above
4763 * 7 map to the same speed as level 7 in these models.
4764 * 2-0 fan level (0..7 usually)
4765 * 0x00 = stop
4766 * 0x07 = max (set when temperatures critical)
4767 * Some ThinkPads may have other levels, see
4768 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4770 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4771 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4772 * does so, its initial value is meaningless (0x07).
4774 * For firmware bugs, refer to:
4775 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4777 * ----
4779 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4780 * Main fan tachometer reading (in RPM)
4782 * This register is present on all ThinkPads with a new-style EC, and
4783 * it is known not to be present on the A21m/e, and T22, as there is
4784 * something else in offset 0x84 according to the ACPI DSDT. Other
4785 * ThinkPads from this same time period (and earlier) probably lack the
4786 * tachometer as well.
4788 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4789 * was never fixed by IBM to report the EC firmware version string
4790 * probably support the tachometer (like the early X models), so
4791 * detecting it is quite hard. We need more data to know for sure.
4793 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4794 * might result.
4796 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4797 * mode.
4799 * For firmware bugs, refer to:
4800 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4802 * TPACPI_FAN_WR_ACPI_FANS:
4803 * ThinkPad X31, X40, X41. Not available in the X60.
4805 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4806 * high speed. ACPI DSDT seems to map these three speeds to levels
4807 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4808 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4810 * The speeds are stored on handles
4811 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4813 * There are three default speed sets, acessible as handles:
4814 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4816 * ACPI DSDT switches which set is in use depending on various
4817 * factors.
4819 * TPACPI_FAN_WR_TPEC is also available and should be used to
4820 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4821 * but the ACPI tables just mention level 7.
4824 static enum fan_status_access_mode fan_status_access_mode;
4825 static enum fan_control_access_mode fan_control_access_mode;
4826 static enum fan_control_commands fan_control_commands;
4828 static u8 fan_control_initial_status;
4829 static u8 fan_control_desired_level;
4831 static void fan_watchdog_fire(struct work_struct *ignored);
4832 static int fan_watchdog_maxinterval;
4833 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4835 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4836 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
4837 "\\FSPD", /* 600e/x, 770e, 770x */
4838 ); /* all others */
4839 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
4840 "JFNS", /* 770x-JL */
4841 ); /* all others */
4844 * SYSFS fan layout: hwmon compatible (device)
4846 * pwm*_enable:
4847 * 0: "disengaged" mode
4848 * 1: manual mode
4849 * 2: native EC "auto" mode (recommended, hardware default)
4851 * pwm*: set speed in manual mode, ignored otherwise.
4852 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4853 * interpolation.
4855 * fan*_input: tachometer reading, RPM
4858 * SYSFS fan layout: extensions
4860 * fan_watchdog (driver):
4861 * fan watchdog interval in seconds, 0 disables (default), max 120
4864 /* sysfs fan pwm1_enable ----------------------------------------------- */
4865 static ssize_t fan_pwm1_enable_show(struct device *dev,
4866 struct device_attribute *attr,
4867 char *buf)
4869 int res, mode;
4870 u8 status;
4872 res = fan_get_status_safe(&status);
4873 if (res)
4874 return res;
4876 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4877 if (status != fan_control_initial_status) {
4878 tp_features.fan_ctrl_status_undef = 0;
4879 } else {
4880 /* Return most likely status. In fact, it
4881 * might be the only possible status */
4882 status = TP_EC_FAN_AUTO;
4886 if (status & TP_EC_FAN_FULLSPEED) {
4887 mode = 0;
4888 } else if (status & TP_EC_FAN_AUTO) {
4889 mode = 2;
4890 } else
4891 mode = 1;
4893 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4896 static ssize_t fan_pwm1_enable_store(struct device *dev,
4897 struct device_attribute *attr,
4898 const char *buf, size_t count)
4900 unsigned long t;
4901 int res, level;
4903 if (parse_strtoul(buf, 2, &t))
4904 return -EINVAL;
4906 switch (t) {
4907 case 0:
4908 level = TP_EC_FAN_FULLSPEED;
4909 break;
4910 case 1:
4911 level = TPACPI_FAN_LAST_LEVEL;
4912 break;
4913 case 2:
4914 level = TP_EC_FAN_AUTO;
4915 break;
4916 case 3:
4917 /* reserved for software-controlled auto mode */
4918 return -ENOSYS;
4919 default:
4920 return -EINVAL;
4923 res = fan_set_level_safe(level);
4924 if (res == -ENXIO)
4925 return -EINVAL;
4926 else if (res < 0)
4927 return res;
4929 fan_watchdog_reset();
4931 return count;
4934 static struct device_attribute dev_attr_fan_pwm1_enable =
4935 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4936 fan_pwm1_enable_show, fan_pwm1_enable_store);
4938 /* sysfs fan pwm1 ------------------------------------------------------ */
4939 static ssize_t fan_pwm1_show(struct device *dev,
4940 struct device_attribute *attr,
4941 char *buf)
4943 int res;
4944 u8 status;
4946 res = fan_get_status_safe(&status);
4947 if (res)
4948 return res;
4950 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4951 if (status != fan_control_initial_status) {
4952 tp_features.fan_ctrl_status_undef = 0;
4953 } else {
4954 status = TP_EC_FAN_AUTO;
4958 if ((status &
4959 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4960 status = fan_control_desired_level;
4962 if (status > 7)
4963 status = 7;
4965 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4968 static ssize_t fan_pwm1_store(struct device *dev,
4969 struct device_attribute *attr,
4970 const char *buf, size_t count)
4972 unsigned long s;
4973 int rc;
4974 u8 status, newlevel;
4976 if (parse_strtoul(buf, 255, &s))
4977 return -EINVAL;
4979 /* scale down from 0-255 to 0-7 */
4980 newlevel = (s >> 5) & 0x07;
4982 if (mutex_lock_interruptible(&fan_mutex))
4983 return -ERESTARTSYS;
4985 rc = fan_get_status(&status);
4986 if (!rc && (status &
4987 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4988 rc = fan_set_level(newlevel);
4989 if (rc == -ENXIO)
4990 rc = -EINVAL;
4991 else if (!rc) {
4992 fan_update_desired_level(newlevel);
4993 fan_watchdog_reset();
4997 mutex_unlock(&fan_mutex);
4998 return (rc)? rc : count;
5001 static struct device_attribute dev_attr_fan_pwm1 =
5002 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5003 fan_pwm1_show, fan_pwm1_store);
5005 /* sysfs fan fan1_input ------------------------------------------------ */
5006 static ssize_t fan_fan1_input_show(struct device *dev,
5007 struct device_attribute *attr,
5008 char *buf)
5010 int res;
5011 unsigned int speed;
5013 res = fan_get_speed(&speed);
5014 if (res < 0)
5015 return res;
5017 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5020 static struct device_attribute dev_attr_fan_fan1_input =
5021 __ATTR(fan1_input, S_IRUGO,
5022 fan_fan1_input_show, NULL);
5024 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5025 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5026 char *buf)
5028 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5031 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5032 const char *buf, size_t count)
5034 unsigned long t;
5036 if (parse_strtoul(buf, 120, &t))
5037 return -EINVAL;
5039 if (!fan_control_allowed)
5040 return -EPERM;
5042 fan_watchdog_maxinterval = t;
5043 fan_watchdog_reset();
5045 return count;
5048 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5049 fan_fan_watchdog_show, fan_fan_watchdog_store);
5051 /* --------------------------------------------------------------------- */
5052 static struct attribute *fan_attributes[] = {
5053 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5054 &dev_attr_fan_fan1_input.attr,
5055 NULL
5058 static const struct attribute_group fan_attr_group = {
5059 .attrs = fan_attributes,
5062 static int __init fan_init(struct ibm_init_struct *iibm)
5064 int rc;
5066 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5068 mutex_init(&fan_mutex);
5069 fan_status_access_mode = TPACPI_FAN_NONE;
5070 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5071 fan_control_commands = 0;
5072 fan_watchdog_maxinterval = 0;
5073 tp_features.fan_ctrl_status_undef = 0;
5074 fan_control_desired_level = 7;
5076 IBM_ACPIHANDLE_INIT(fans);
5077 IBM_ACPIHANDLE_INIT(gfan);
5078 IBM_ACPIHANDLE_INIT(sfan);
5080 if (gfan_handle) {
5081 /* 570, 600e/x, 770e, 770x */
5082 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5083 } else {
5084 /* all other ThinkPads: note that even old-style
5085 * ThinkPad ECs supports the fan control register */
5086 if (likely(acpi_ec_read(fan_status_offset,
5087 &fan_control_initial_status))) {
5088 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5090 /* In some ThinkPads, neither the EC nor the ACPI
5091 * DSDT initialize the fan status, and it ends up
5092 * being set to 0x07 when it *could* be either
5093 * 0x07 or 0x80.
5095 * Enable for TP-1Y (T43), TP-78 (R51e),
5096 * TP-76 (R52), TP-70 (T43, R52), which are known
5097 * to be buggy. */
5098 if (fan_control_initial_status == 0x07) {
5099 switch (thinkpad_id.ec_model) {
5100 case 0x5931: /* TP-1Y */
5101 case 0x3837: /* TP-78 */
5102 case 0x3637: /* TP-76 */
5103 case 0x3037: /* TP-70 */
5104 printk(IBM_NOTICE
5105 "fan_init: initial fan status is "
5106 "unknown, assuming it is in auto "
5107 "mode\n");
5108 tp_features.fan_ctrl_status_undef = 1;
5112 } else {
5113 printk(IBM_ERR
5114 "ThinkPad ACPI EC access misbehaving, "
5115 "fan status and control unavailable\n");
5116 return 1;
5120 if (sfan_handle) {
5121 /* 570, 770x-JL */
5122 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5123 fan_control_commands |=
5124 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5125 } else {
5126 if (!gfan_handle) {
5127 /* gfan without sfan means no fan control */
5128 /* all other models implement TP EC 0x2f control */
5130 if (fans_handle) {
5131 /* X31, X40, X41 */
5132 fan_control_access_mode =
5133 TPACPI_FAN_WR_ACPI_FANS;
5134 fan_control_commands |=
5135 TPACPI_FAN_CMD_SPEED |
5136 TPACPI_FAN_CMD_LEVEL |
5137 TPACPI_FAN_CMD_ENABLE;
5138 } else {
5139 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5140 fan_control_commands |=
5141 TPACPI_FAN_CMD_LEVEL |
5142 TPACPI_FAN_CMD_ENABLE;
5147 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5148 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5149 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5150 fan_status_access_mode, fan_control_access_mode);
5152 /* fan control master switch */
5153 if (!fan_control_allowed) {
5154 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5155 fan_control_commands = 0;
5156 dbg_printk(TPACPI_DBG_INIT,
5157 "fan control features disabled by parameter\n");
5160 /* update fan_control_desired_level */
5161 if (fan_status_access_mode != TPACPI_FAN_NONE)
5162 fan_get_status_safe(NULL);
5164 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5165 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5166 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5167 &fan_attr_group);
5168 if (!(rc < 0))
5169 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5170 &driver_attr_fan_watchdog);
5171 if (rc < 0)
5172 return rc;
5173 return 0;
5174 } else
5175 return 1;
5179 * Call with fan_mutex held
5181 static void fan_update_desired_level(u8 status)
5183 if ((status &
5184 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5185 if (status > 7)
5186 fan_control_desired_level = 7;
5187 else
5188 fan_control_desired_level = status;
5192 static int fan_get_status(u8 *status)
5194 u8 s;
5196 /* TODO:
5197 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5199 switch (fan_status_access_mode) {
5200 case TPACPI_FAN_RD_ACPI_GFAN:
5201 /* 570, 600e/x, 770e, 770x */
5203 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5204 return -EIO;
5206 if (likely(status))
5207 *status = s & 0x07;
5209 break;
5211 case TPACPI_FAN_RD_TPEC:
5212 /* all except 570, 600e/x, 770e, 770x */
5213 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5214 return -EIO;
5216 if (likely(status))
5217 *status = s;
5219 break;
5221 default:
5222 return -ENXIO;
5225 return 0;
5228 static int fan_get_status_safe(u8 *status)
5230 int rc;
5231 u8 s;
5233 if (mutex_lock_interruptible(&fan_mutex))
5234 return -ERESTARTSYS;
5235 rc = fan_get_status(&s);
5236 if (!rc)
5237 fan_update_desired_level(s);
5238 mutex_unlock(&fan_mutex);
5240 if (status)
5241 *status = s;
5243 return rc;
5246 static void fan_exit(void)
5248 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
5250 /* FIXME: can we really do this unconditionally? */
5251 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5252 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
5254 cancel_delayed_work(&fan_watchdog_task);
5255 flush_scheduled_work();
5258 static int fan_get_speed(unsigned int *speed)
5260 u8 hi, lo;
5262 switch (fan_status_access_mode) {
5263 case TPACPI_FAN_RD_TPEC:
5264 /* all except 570, 600e/x, 770e, 770x */
5265 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5266 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5267 return -EIO;
5269 if (likely(speed))
5270 *speed = (hi << 8) | lo;
5272 break;
5274 default:
5275 return -ENXIO;
5278 return 0;
5281 static void fan_watchdog_fire(struct work_struct *ignored)
5283 int rc;
5285 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5286 return;
5288 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
5289 rc = fan_set_enable();
5290 if (rc < 0) {
5291 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
5292 "will try again later...\n", -rc);
5293 /* reschedule for later */
5294 fan_watchdog_reset();
5298 static void fan_watchdog_reset(void)
5300 static int fan_watchdog_active;
5302 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5303 return;
5305 if (fan_watchdog_active)
5306 cancel_delayed_work(&fan_watchdog_task);
5308 if (fan_watchdog_maxinterval > 0 &&
5309 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5310 fan_watchdog_active = 1;
5311 if (!schedule_delayed_work(&fan_watchdog_task,
5312 msecs_to_jiffies(fan_watchdog_maxinterval
5313 * 1000))) {
5314 printk(IBM_ERR "failed to schedule the fan watchdog, "
5315 "watchdog will not trigger\n");
5317 } else
5318 fan_watchdog_active = 0;
5321 static int fan_set_level(int level)
5323 if (!fan_control_allowed)
5324 return -EPERM;
5326 switch (fan_control_access_mode) {
5327 case TPACPI_FAN_WR_ACPI_SFAN:
5328 if (level >= 0 && level <= 7) {
5329 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5330 return -EIO;
5331 } else
5332 return -EINVAL;
5333 break;
5335 case TPACPI_FAN_WR_ACPI_FANS:
5336 case TPACPI_FAN_WR_TPEC:
5337 if ((level != TP_EC_FAN_AUTO) &&
5338 (level != TP_EC_FAN_FULLSPEED) &&
5339 ((level < 0) || (level > 7)))
5340 return -EINVAL;
5342 /* safety net should the EC not support AUTO
5343 * or FULLSPEED mode bits and just ignore them */
5344 if (level & TP_EC_FAN_FULLSPEED)
5345 level |= 7; /* safety min speed 7 */
5346 else if (level & TP_EC_FAN_FULLSPEED)
5347 level |= 4; /* safety min speed 4 */
5349 if (!acpi_ec_write(fan_status_offset, level))
5350 return -EIO;
5351 else
5352 tp_features.fan_ctrl_status_undef = 0;
5353 break;
5355 default:
5356 return -ENXIO;
5358 return 0;
5361 static int fan_set_level_safe(int level)
5363 int rc;
5365 if (!fan_control_allowed)
5366 return -EPERM;
5368 if (mutex_lock_interruptible(&fan_mutex))
5369 return -ERESTARTSYS;
5371 if (level == TPACPI_FAN_LAST_LEVEL)
5372 level = fan_control_desired_level;
5374 rc = fan_set_level(level);
5375 if (!rc)
5376 fan_update_desired_level(level);
5378 mutex_unlock(&fan_mutex);
5379 return rc;
5382 static int fan_set_enable(void)
5384 u8 s;
5385 int rc;
5387 if (!fan_control_allowed)
5388 return -EPERM;
5390 if (mutex_lock_interruptible(&fan_mutex))
5391 return -ERESTARTSYS;
5393 switch (fan_control_access_mode) {
5394 case TPACPI_FAN_WR_ACPI_FANS:
5395 case TPACPI_FAN_WR_TPEC:
5396 rc = fan_get_status(&s);
5397 if (rc < 0)
5398 break;
5400 /* Don't go out of emergency fan mode */
5401 if (s != 7) {
5402 s &= 0x07;
5403 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5406 if (!acpi_ec_write(fan_status_offset, s))
5407 rc = -EIO;
5408 else {
5409 tp_features.fan_ctrl_status_undef = 0;
5410 rc = 0;
5412 break;
5414 case TPACPI_FAN_WR_ACPI_SFAN:
5415 rc = fan_get_status(&s);
5416 if (rc < 0)
5417 break;
5419 s &= 0x07;
5421 /* Set fan to at least level 4 */
5422 s |= 4;
5424 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5425 rc= -EIO;
5426 else
5427 rc = 0;
5428 break;
5430 default:
5431 rc = -ENXIO;
5434 mutex_unlock(&fan_mutex);
5435 return rc;
5438 static int fan_set_disable(void)
5440 int rc;
5442 if (!fan_control_allowed)
5443 return -EPERM;
5445 if (mutex_lock_interruptible(&fan_mutex))
5446 return -ERESTARTSYS;
5448 rc = 0;
5449 switch (fan_control_access_mode) {
5450 case TPACPI_FAN_WR_ACPI_FANS:
5451 case TPACPI_FAN_WR_TPEC:
5452 if (!acpi_ec_write(fan_status_offset, 0x00))
5453 rc = -EIO;
5454 else {
5455 fan_control_desired_level = 0;
5456 tp_features.fan_ctrl_status_undef = 0;
5458 break;
5460 case TPACPI_FAN_WR_ACPI_SFAN:
5461 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5462 rc = -EIO;
5463 else
5464 fan_control_desired_level = 0;
5465 break;
5467 default:
5468 rc = -ENXIO;
5472 mutex_unlock(&fan_mutex);
5473 return rc;
5476 static int fan_set_speed(int speed)
5478 int rc;
5480 if (!fan_control_allowed)
5481 return -EPERM;
5483 if (mutex_lock_interruptible(&fan_mutex))
5484 return -ERESTARTSYS;
5486 rc = 0;
5487 switch (fan_control_access_mode) {
5488 case TPACPI_FAN_WR_ACPI_FANS:
5489 if (speed >= 0 && speed <= 65535) {
5490 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5491 speed, speed, speed))
5492 rc = -EIO;
5493 } else
5494 rc = -EINVAL;
5495 break;
5497 default:
5498 rc = -ENXIO;
5501 mutex_unlock(&fan_mutex);
5502 return rc;
5505 static int fan_read(char *p)
5507 int len = 0;
5508 int rc;
5509 u8 status;
5510 unsigned int speed = 0;
5512 switch (fan_status_access_mode) {
5513 case TPACPI_FAN_RD_ACPI_GFAN:
5514 /* 570, 600e/x, 770e, 770x */
5515 if ((rc = fan_get_status_safe(&status)) < 0)
5516 return rc;
5518 len += sprintf(p + len, "status:\t\t%s\n"
5519 "level:\t\t%d\n",
5520 (status != 0) ? "enabled" : "disabled", status);
5521 break;
5523 case TPACPI_FAN_RD_TPEC:
5524 /* all except 570, 600e/x, 770e, 770x */
5525 if ((rc = fan_get_status_safe(&status)) < 0)
5526 return rc;
5528 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5529 if (status != fan_control_initial_status)
5530 tp_features.fan_ctrl_status_undef = 0;
5531 else
5532 /* Return most likely status. In fact, it
5533 * might be the only possible status */
5534 status = TP_EC_FAN_AUTO;
5537 len += sprintf(p + len, "status:\t\t%s\n",
5538 (status != 0) ? "enabled" : "disabled");
5540 if ((rc = fan_get_speed(&speed)) < 0)
5541 return rc;
5543 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5545 if (status & TP_EC_FAN_FULLSPEED)
5546 /* Disengaged mode takes precedence */
5547 len += sprintf(p + len, "level:\t\tdisengaged\n");
5548 else if (status & TP_EC_FAN_AUTO)
5549 len += sprintf(p + len, "level:\t\tauto\n");
5550 else
5551 len += sprintf(p + len, "level:\t\t%d\n", status);
5552 break;
5554 case TPACPI_FAN_NONE:
5555 default:
5556 len += sprintf(p + len, "status:\t\tnot supported\n");
5559 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5560 len += sprintf(p + len, "commands:\tlevel <level>");
5562 switch (fan_control_access_mode) {
5563 case TPACPI_FAN_WR_ACPI_SFAN:
5564 len += sprintf(p + len, " (<level> is 0-7)\n");
5565 break;
5567 default:
5568 len += sprintf(p + len, " (<level> is 0-7, "
5569 "auto, disengaged, full-speed)\n");
5570 break;
5574 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5575 len += sprintf(p + len, "commands:\tenable, disable\n"
5576 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
5577 "1-120 (seconds))\n");
5579 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5580 len += sprintf(p + len, "commands:\tspeed <speed>"
5581 " (<speed> is 0-65535)\n");
5583 return len;
5586 static int fan_write_cmd_level(const char *cmd, int *rc)
5588 int level;
5590 if (strlencmp(cmd, "level auto") == 0)
5591 level = TP_EC_FAN_AUTO;
5592 else if ((strlencmp(cmd, "level disengaged") == 0) |
5593 (strlencmp(cmd, "level full-speed") == 0))
5594 level = TP_EC_FAN_FULLSPEED;
5595 else if (sscanf(cmd, "level %d", &level) != 1)
5596 return 0;
5598 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
5599 printk(IBM_ERR "level command accepted for unsupported "
5600 "access mode %d", fan_control_access_mode);
5602 return 1;
5605 static int fan_write_cmd_enable(const char *cmd, int *rc)
5607 if (strlencmp(cmd, "enable") != 0)
5608 return 0;
5610 if ((*rc = fan_set_enable()) == -ENXIO)
5611 printk(IBM_ERR "enable command accepted for unsupported "
5612 "access mode %d", fan_control_access_mode);
5614 return 1;
5617 static int fan_write_cmd_disable(const char *cmd, int *rc)
5619 if (strlencmp(cmd, "disable") != 0)
5620 return 0;
5622 if ((*rc = fan_set_disable()) == -ENXIO)
5623 printk(IBM_ERR "disable command accepted for unsupported "
5624 "access mode %d", fan_control_access_mode);
5626 return 1;
5629 static int fan_write_cmd_speed(const char *cmd, int *rc)
5631 int speed;
5633 /* TODO:
5634 * Support speed <low> <medium> <high> ? */
5636 if (sscanf(cmd, "speed %d", &speed) != 1)
5637 return 0;
5639 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5640 printk(IBM_ERR "speed command accepted for unsupported "
5641 "access mode %d", fan_control_access_mode);
5643 return 1;
5646 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5648 int interval;
5650 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5651 return 0;
5653 if (interval < 0 || interval > 120)
5654 *rc = -EINVAL;
5655 else
5656 fan_watchdog_maxinterval = interval;
5658 return 1;
5661 static int fan_write(char *buf)
5663 char *cmd;
5664 int rc = 0;
5666 while (!rc && (cmd = next_cmd(&buf))) {
5667 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5668 fan_write_cmd_level(cmd, &rc)) &&
5669 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5670 (fan_write_cmd_enable(cmd, &rc) ||
5671 fan_write_cmd_disable(cmd, &rc) ||
5672 fan_write_cmd_watchdog(cmd, &rc))) &&
5673 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5674 fan_write_cmd_speed(cmd, &rc))
5676 rc = -EINVAL;
5677 else if (!rc)
5678 fan_watchdog_reset();
5681 return rc;
5684 static struct ibm_struct fan_driver_data = {
5685 .name = "fan",
5686 .read = fan_read,
5687 .write = fan_write,
5688 .exit = fan_exit,
5691 /****************************************************************************
5692 ****************************************************************************
5694 * Infrastructure
5696 ****************************************************************************
5697 ****************************************************************************/
5699 /* sysfs name ---------------------------------------------------------- */
5700 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5701 struct device_attribute *attr,
5702 char *buf)
5704 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
5707 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5708 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5710 /* --------------------------------------------------------------------- */
5712 /* /proc support */
5713 static struct proc_dir_entry *proc_dir;
5715 /* Subdriver registry */
5716 static LIST_HEAD(tpacpi_all_drivers);
5720 * Module and infrastructure proble, init and exit handling
5723 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5724 static const char * __init str_supported(int is_supported)
5726 static char text_unsupported[] __initdata = "not supported";
5728 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5730 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5732 static int __init ibm_init(struct ibm_init_struct *iibm)
5734 int ret;
5735 struct ibm_struct *ibm = iibm->data;
5736 struct proc_dir_entry *entry;
5738 BUG_ON(ibm == NULL);
5740 INIT_LIST_HEAD(&ibm->all_drivers);
5742 if (ibm->flags.experimental && !experimental)
5743 return 0;
5745 dbg_printk(TPACPI_DBG_INIT,
5746 "probing for %s\n", ibm->name);
5748 if (iibm->init) {
5749 ret = iibm->init(iibm);
5750 if (ret > 0)
5751 return 0; /* probe failed */
5752 if (ret)
5753 return ret;
5755 ibm->flags.init_called = 1;
5758 if (ibm->acpi) {
5759 if (ibm->acpi->hid) {
5760 ret = register_tpacpi_subdriver(ibm);
5761 if (ret)
5762 goto err_out;
5765 if (ibm->acpi->notify) {
5766 ret = setup_acpi_notify(ibm);
5767 if (ret == -ENODEV) {
5768 printk(IBM_NOTICE "disabling subdriver %s\n",
5769 ibm->name);
5770 ret = 0;
5771 goto err_out;
5773 if (ret < 0)
5774 goto err_out;
5778 dbg_printk(TPACPI_DBG_INIT,
5779 "%s installed\n", ibm->name);
5781 if (ibm->read) {
5782 entry = create_proc_entry(ibm->name,
5783 S_IFREG | S_IRUGO | S_IWUSR,
5784 proc_dir);
5785 if (!entry) {
5786 printk(IBM_ERR "unable to create proc entry %s\n",
5787 ibm->name);
5788 ret = -ENODEV;
5789 goto err_out;
5791 entry->owner = THIS_MODULE;
5792 entry->data = ibm;
5793 entry->read_proc = &dispatch_procfs_read;
5794 if (ibm->write)
5795 entry->write_proc = &dispatch_procfs_write;
5796 ibm->flags.proc_created = 1;
5799 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5801 return 0;
5803 err_out:
5804 dbg_printk(TPACPI_DBG_INIT,
5805 "%s: at error exit path with result %d\n",
5806 ibm->name, ret);
5808 ibm_exit(ibm);
5809 return (ret < 0)? ret : 0;
5812 static void ibm_exit(struct ibm_struct *ibm)
5814 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5816 list_del_init(&ibm->all_drivers);
5818 if (ibm->flags.acpi_notify_installed) {
5819 dbg_printk(TPACPI_DBG_EXIT,
5820 "%s: acpi_remove_notify_handler\n", ibm->name);
5821 BUG_ON(!ibm->acpi);
5822 acpi_remove_notify_handler(*ibm->acpi->handle,
5823 ibm->acpi->type,
5824 dispatch_acpi_notify);
5825 ibm->flags.acpi_notify_installed = 0;
5826 ibm->flags.acpi_notify_installed = 0;
5829 if (ibm->flags.proc_created) {
5830 dbg_printk(TPACPI_DBG_EXIT,
5831 "%s: remove_proc_entry\n", ibm->name);
5832 remove_proc_entry(ibm->name, proc_dir);
5833 ibm->flags.proc_created = 0;
5836 if (ibm->flags.acpi_driver_registered) {
5837 dbg_printk(TPACPI_DBG_EXIT,
5838 "%s: acpi_bus_unregister_driver\n", ibm->name);
5839 BUG_ON(!ibm->acpi);
5840 acpi_bus_unregister_driver(ibm->acpi->driver);
5841 kfree(ibm->acpi->driver);
5842 ibm->acpi->driver = NULL;
5843 ibm->flags.acpi_driver_registered = 0;
5846 if (ibm->flags.init_called && ibm->exit) {
5847 ibm->exit();
5848 ibm->flags.init_called = 0;
5851 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5854 /* Probing */
5856 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5858 struct dmi_device *dev = NULL;
5859 char ec_fw_string[18];
5861 if (!tp)
5862 return;
5864 memset(tp, 0, sizeof(*tp));
5866 if (dmi_name_in_vendors("IBM"))
5867 tp->vendor = PCI_VENDOR_ID_IBM;
5868 else if (dmi_name_in_vendors("LENOVO"))
5869 tp->vendor = PCI_VENDOR_ID_LENOVO;
5870 else
5871 return;
5873 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5874 GFP_KERNEL);
5875 if (!tp->bios_version_str)
5876 return;
5877 tp->bios_model = tp->bios_version_str[0]
5878 | (tp->bios_version_str[1] << 8);
5881 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5882 * X32 or newer, all Z series; Some models must have an
5883 * up-to-date BIOS or they will not be detected.
5885 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5887 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5888 if (sscanf(dev->name,
5889 "IBM ThinkPad Embedded Controller -[%17c",
5890 ec_fw_string) == 1) {
5891 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5892 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5894 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5895 tp->ec_model = ec_fw_string[0]
5896 | (ec_fw_string[1] << 8);
5897 break;
5901 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5902 GFP_KERNEL);
5903 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5904 kfree(tp->model_str);
5905 tp->model_str = NULL;
5909 static int __init probe_for_thinkpad(void)
5911 int is_thinkpad;
5913 if (acpi_disabled)
5914 return -ENODEV;
5917 * Non-ancient models have better DMI tagging, but very old models
5918 * don't.
5920 is_thinkpad = (thinkpad_id.model_str != NULL);
5922 /* ec is required because many other handles are relative to it */
5923 IBM_ACPIHANDLE_INIT(ec);
5924 if (!ec_handle) {
5925 if (is_thinkpad)
5926 printk(IBM_ERR
5927 "Not yet supported ThinkPad detected!\n");
5928 return -ENODEV;
5932 * Risks a regression on very old machines, but reduces potential
5933 * false positives a damn great deal
5935 if (!is_thinkpad)
5936 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5938 if (!is_thinkpad && !force_load)
5939 return -ENODEV;
5941 return 0;
5945 /* Module init, exit, parameters */
5947 static struct ibm_init_struct ibms_init[] __initdata = {
5949 .init = thinkpad_acpi_driver_init,
5950 .data = &thinkpad_acpi_driver_data,
5953 .init = hotkey_init,
5954 .data = &hotkey_driver_data,
5957 .init = bluetooth_init,
5958 .data = &bluetooth_driver_data,
5961 .init = wan_init,
5962 .data = &wan_driver_data,
5965 .init = video_init,
5966 .data = &video_driver_data,
5969 .init = light_init,
5970 .data = &light_driver_data,
5972 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5974 .init = dock_init,
5975 .data = &dock_driver_data[0],
5978 .init = dock_init2,
5979 .data = &dock_driver_data[1],
5981 #endif
5982 #ifdef CONFIG_THINKPAD_ACPI_BAY
5984 .init = bay_init,
5985 .data = &bay_driver_data,
5987 #endif
5989 .init = cmos_init,
5990 .data = &cmos_driver_data,
5993 .init = led_init,
5994 .data = &led_driver_data,
5997 .init = beep_init,
5998 .data = &beep_driver_data,
6001 .init = thermal_init,
6002 .data = &thermal_driver_data,
6005 .data = &ecdump_driver_data,
6008 .init = brightness_init,
6009 .data = &brightness_driver_data,
6012 .data = &volume_driver_data,
6015 .init = fan_init,
6016 .data = &fan_driver_data,
6020 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6022 unsigned int i;
6023 struct ibm_struct *ibm;
6025 if (!kp || !kp->name || !val)
6026 return -EINVAL;
6028 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6029 ibm = ibms_init[i].data;
6030 WARN_ON(ibm == NULL);
6032 if (!ibm || !ibm->name)
6033 continue;
6035 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6036 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6037 return -ENOSPC;
6038 strcpy(ibms_init[i].param, val);
6039 strcat(ibms_init[i].param, ",");
6040 return 0;
6044 return -EINVAL;
6047 static int experimental;
6048 module_param(experimental, int, 0);
6050 static u32 dbg_level;
6051 module_param_named(debug, dbg_level, uint, 0);
6053 static int force_load;
6054 module_param(force_load, bool, 0);
6056 static int fan_control_allowed;
6057 module_param_named(fan_control, fan_control_allowed, bool, 0);
6059 static int brightness_mode;
6060 module_param_named(brightness_mode, brightness_mode, int, 0);
6062 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
6063 module_param(brightness_enable, uint, 0);
6065 static unsigned int hotkey_report_mode;
6066 module_param(hotkey_report_mode, uint, 0);
6068 #define IBM_PARAM(feature) \
6069 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
6071 IBM_PARAM(hotkey);
6072 IBM_PARAM(bluetooth);
6073 IBM_PARAM(video);
6074 IBM_PARAM(light);
6075 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6076 IBM_PARAM(dock);
6077 #endif
6078 #ifdef CONFIG_THINKPAD_ACPI_BAY
6079 IBM_PARAM(bay);
6080 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6081 IBM_PARAM(cmos);
6082 IBM_PARAM(led);
6083 IBM_PARAM(beep);
6084 IBM_PARAM(ecdump);
6085 IBM_PARAM(brightness);
6086 IBM_PARAM(volume);
6087 IBM_PARAM(fan);
6089 static int __init thinkpad_acpi_module_init(void)
6091 int ret, i;
6093 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6095 /* Parameter checking */
6096 if (hotkey_report_mode > 2)
6097 return -EINVAL;
6099 /* Driver-level probe */
6101 get_thinkpad_model_data(&thinkpad_id);
6102 ret = probe_for_thinkpad();
6103 if (ret) {
6104 thinkpad_acpi_module_exit();
6105 return ret;
6108 /* Driver initialization */
6110 IBM_ACPIHANDLE_INIT(ecrd);
6111 IBM_ACPIHANDLE_INIT(ecwr);
6113 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
6114 if (!proc_dir) {
6115 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
6116 thinkpad_acpi_module_exit();
6117 return -ENODEV;
6119 proc_dir->owner = THIS_MODULE;
6121 ret = platform_driver_register(&tpacpi_pdriver);
6122 if (ret) {
6123 printk(IBM_ERR "unable to register main platform driver\n");
6124 thinkpad_acpi_module_exit();
6125 return ret;
6127 tp_features.platform_drv_registered = 1;
6129 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6130 if (ret) {
6131 printk(IBM_ERR "unable to register hwmon platform driver\n");
6132 thinkpad_acpi_module_exit();
6133 return ret;
6135 tp_features.sensors_pdrv_registered = 1;
6137 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6138 if (!ret) {
6139 tp_features.platform_drv_attrs_registered = 1;
6140 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6142 if (ret) {
6143 printk(IBM_ERR "unable to create sysfs driver attributes\n");
6144 thinkpad_acpi_module_exit();
6145 return ret;
6147 tp_features.sensors_pdrv_attrs_registered = 1;
6150 /* Device initialization */
6151 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
6152 NULL, 0);
6153 if (IS_ERR(tpacpi_pdev)) {
6154 ret = PTR_ERR(tpacpi_pdev);
6155 tpacpi_pdev = NULL;
6156 printk(IBM_ERR "unable to register platform device\n");
6157 thinkpad_acpi_module_exit();
6158 return ret;
6160 tpacpi_sensors_pdev = platform_device_register_simple(
6161 IBM_HWMON_DRVR_NAME,
6162 -1, NULL, 0);
6163 if (IS_ERR(tpacpi_sensors_pdev)) {
6164 ret = PTR_ERR(tpacpi_sensors_pdev);
6165 tpacpi_sensors_pdev = NULL;
6166 printk(IBM_ERR "unable to register hwmon platform device\n");
6167 thinkpad_acpi_module_exit();
6168 return ret;
6170 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6171 &dev_attr_thinkpad_acpi_pdev_name);
6172 if (ret) {
6173 printk(IBM_ERR
6174 "unable to create sysfs hwmon device attributes\n");
6175 thinkpad_acpi_module_exit();
6176 return ret;
6178 tp_features.sensors_pdev_attrs_registered = 1;
6179 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6180 if (IS_ERR(tpacpi_hwmon)) {
6181 ret = PTR_ERR(tpacpi_hwmon);
6182 tpacpi_hwmon = NULL;
6183 printk(IBM_ERR "unable to register hwmon device\n");
6184 thinkpad_acpi_module_exit();
6185 return ret;
6187 mutex_init(&tpacpi_inputdev_send_mutex);
6188 tpacpi_inputdev = input_allocate_device();
6189 if (!tpacpi_inputdev) {
6190 printk(IBM_ERR "unable to allocate input device\n");
6191 thinkpad_acpi_module_exit();
6192 return -ENOMEM;
6193 } else {
6194 /* Prepare input device, but don't register */
6195 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6196 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
6197 tpacpi_inputdev->id.bustype = BUS_HOST;
6198 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6199 thinkpad_id.vendor :
6200 PCI_VENDOR_ID_IBM;
6201 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6202 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6204 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6205 ret = ibm_init(&ibms_init[i]);
6206 if (ret >= 0 && *ibms_init[i].param)
6207 ret = ibms_init[i].data->write(ibms_init[i].param);
6208 if (ret < 0) {
6209 thinkpad_acpi_module_exit();
6210 return ret;
6213 ret = input_register_device(tpacpi_inputdev);
6214 if (ret < 0) {
6215 printk(IBM_ERR "unable to register input device\n");
6216 thinkpad_acpi_module_exit();
6217 return ret;
6218 } else {
6219 tp_features.input_device_registered = 1;
6222 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6223 return 0;
6226 static void thinkpad_acpi_module_exit(void)
6228 struct ibm_struct *ibm, *itmp;
6230 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6232 list_for_each_entry_safe_reverse(ibm, itmp,
6233 &tpacpi_all_drivers,
6234 all_drivers) {
6235 ibm_exit(ibm);
6238 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6240 if (tpacpi_inputdev) {
6241 if (tp_features.input_device_registered)
6242 input_unregister_device(tpacpi_inputdev);
6243 else
6244 input_free_device(tpacpi_inputdev);
6247 if (tpacpi_hwmon)
6248 hwmon_device_unregister(tpacpi_hwmon);
6250 if (tp_features.sensors_pdev_attrs_registered)
6251 device_remove_file(&tpacpi_sensors_pdev->dev,
6252 &dev_attr_thinkpad_acpi_pdev_name);
6253 if (tpacpi_sensors_pdev)
6254 platform_device_unregister(tpacpi_sensors_pdev);
6255 if (tpacpi_pdev)
6256 platform_device_unregister(tpacpi_pdev);
6258 if (tp_features.sensors_pdrv_attrs_registered)
6259 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6260 if (tp_features.platform_drv_attrs_registered)
6261 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6263 if (tp_features.sensors_pdrv_registered)
6264 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6266 if (tp_features.platform_drv_registered)
6267 platform_driver_unregister(&tpacpi_pdriver);
6269 if (proc_dir)
6270 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
6272 kfree(thinkpad_id.bios_version_str);
6273 kfree(thinkpad_id.ec_version_str);
6274 kfree(thinkpad_id.model_str);
6277 module_init(thinkpad_acpi_module_init);
6278 module_exit(thinkpad_acpi_module_exit);