ACPI: thinkpad-acpi: spring cleanup part 1
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob5869a5a17887fcce121d53745fb75190caf515f2
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 0x020100
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;
1502 if (tp_features.hotkey_mask) {
1503 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1504 return -EIO;
1505 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 if (!acpi_evalf(hkey_handle,
1524 NULL, "MHKM", "vdd", i + 1,
1525 !!(mask & m))) {
1526 rc = -EIO;
1527 break;
1528 } else {
1529 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1532 HOTKEY_CONFIG_CRITICAL_END
1534 /* hotkey_mask_get must be called unconditionally below */
1535 if (!hotkey_mask_get() && !rc &&
1536 (hotkey_mask & ~hotkey_source_mask) !=
1537 (mask & ~hotkey_source_mask)) {
1538 printk(IBM_NOTICE
1539 "requested hot key mask 0x%08x, but "
1540 "firmware forced it to 0x%08x\n",
1541 mask, hotkey_mask);
1545 return rc;
1548 static int hotkey_status_get(int *status)
1550 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1551 return -EIO;
1553 return 0;
1556 static int hotkey_status_set(int status)
1558 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1559 return -EIO;
1561 return 0;
1564 static void tpacpi_input_send_radiosw(void)
1566 int wlsw;
1568 mutex_lock(&tpacpi_inputdev_send_mutex);
1570 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1571 input_report_switch(tpacpi_inputdev,
1572 SW_RADIO, !!wlsw);
1573 input_sync(tpacpi_inputdev);
1576 mutex_unlock(&tpacpi_inputdev_send_mutex);
1579 static void tpacpi_input_send_key(unsigned int scancode)
1581 unsigned int keycode;
1583 keycode = hotkey_keycode_map[scancode];
1585 if (keycode != KEY_RESERVED) {
1586 mutex_lock(&tpacpi_inputdev_send_mutex);
1588 input_report_key(tpacpi_inputdev, keycode, 1);
1589 if (keycode == KEY_UNKNOWN)
1590 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1591 scancode);
1592 input_sync(tpacpi_inputdev);
1594 input_report_key(tpacpi_inputdev, keycode, 0);
1595 if (keycode == KEY_UNKNOWN)
1596 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1597 scancode);
1598 input_sync(tpacpi_inputdev);
1600 mutex_unlock(&tpacpi_inputdev_send_mutex);
1604 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1605 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1607 static void tpacpi_hotkey_send_key(unsigned int scancode)
1609 tpacpi_input_send_key(scancode);
1610 if (hotkey_report_mode < 2) {
1611 acpi_bus_generate_event(ibm_hotkey_acpidriver.device,
1612 0x80, 0x1001 + scancode);
1616 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1618 u8 d;
1620 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1621 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1622 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1623 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1624 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1625 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1627 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1628 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1629 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1631 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1632 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1633 n->displayexp_toggle =
1634 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1636 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1637 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1638 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1639 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1640 n->brightness_toggle =
1641 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1643 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1644 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1645 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1646 >> TP_NVRAM_POS_LEVEL_VOLUME;
1647 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1648 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1652 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1653 do { if ((mask & (1 << __scancode)) && oldn->__member != newn->__member) \
1654 tpacpi_hotkey_send_key(__scancode); } while (0)
1656 #define TPACPI_MAY_SEND_KEY(__scancode) \
1657 do { if (mask & (1 << __scancode)) \
1658 tpacpi_hotkey_send_key(__scancode); } while (0)
1660 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1661 struct tp_nvram_state *newn,
1662 u32 mask)
1664 if (oldn == newn)
1665 return;
1667 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1668 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1669 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1670 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1672 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1674 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1676 /* handle volume */
1677 if (oldn->volume_toggle != newn->volume_toggle) {
1678 if (oldn->mute != newn->mute) {
1679 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1681 if (oldn->volume_level > newn->volume_level) {
1682 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1683 } else if (oldn->volume_level < newn->volume_level) {
1684 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1685 } else if (oldn->mute == newn->mute) {
1686 /* repeated key presses that didn't change state */
1687 if (newn->mute) {
1688 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1689 } else if (newn->volume_level != 0) {
1690 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1691 } else {
1692 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1697 /* handle brightness */
1698 if (oldn->brightness_toggle != newn->brightness_toggle) {
1699 if (oldn->brightness_level < newn->brightness_level) {
1700 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1701 } else if (oldn->brightness_level > newn->brightness_level) {
1702 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1703 } else {
1704 /* repeated key presses that didn't change state */
1705 if (newn->brightness_level != 0) {
1706 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1707 } else {
1708 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1714 #undef TPACPI_COMPARE_KEY
1715 #undef TPACPI_MAY_SEND_KEY
1717 static int hotkey_kthread(void *data)
1719 struct tp_nvram_state s[2];
1720 u32 mask;
1721 unsigned int si, so;
1722 unsigned long t;
1723 unsigned int change_detector, must_reset;
1725 mutex_lock(&hotkey_thread_mutex);
1727 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1728 goto exit;
1730 so = 0;
1731 si = 1;
1732 t = 0;
1734 /* Initial state for compares */
1735 mutex_lock(&hotkey_thread_data_mutex);
1736 change_detector = hotkey_config_change;
1737 mask = hotkey_source_mask & hotkey_mask;
1738 mutex_unlock(&hotkey_thread_data_mutex);
1739 hotkey_read_nvram(&s[so], mask);
1741 while (!kthread_should_stop() && hotkey_poll_freq) {
1742 if (t == 0)
1743 t = 1000/hotkey_poll_freq;
1744 t = msleep_interruptible(t);
1745 if (unlikely(kthread_should_stop()))
1746 break;
1747 must_reset = try_to_freeze();
1748 if (t > 0 && !must_reset)
1749 continue;
1751 mutex_lock(&hotkey_thread_data_mutex);
1752 if (must_reset || hotkey_config_change != change_detector) {
1753 /* forget old state on thaw or config change */
1754 si = so;
1755 t = 0;
1756 change_detector = hotkey_config_change;
1758 mask = hotkey_source_mask & hotkey_mask;
1759 mutex_unlock(&hotkey_thread_data_mutex);
1761 hotkey_read_nvram(&s[si], mask);
1762 hotkey_compare_and_issue_event(&s[so], &s[si], mask);
1764 so = si;
1765 si ^= 1;
1768 exit:
1769 mutex_unlock(&hotkey_thread_mutex);
1770 return 0;
1773 static void hotkey_poll_stop_sync(void)
1775 if (tpacpi_hotkey_task) {
1776 if (frozen(tpacpi_hotkey_task) ||
1777 freezing(tpacpi_hotkey_task))
1778 thaw_process(tpacpi_hotkey_task);
1780 kthread_stop(tpacpi_hotkey_task);
1781 tpacpi_hotkey_task = NULL;
1782 mutex_lock(&hotkey_thread_mutex);
1783 /* at this point, the thread did exit */
1784 mutex_unlock(&hotkey_thread_mutex);
1788 /* call with hotkey_mutex held */
1789 static void hotkey_poll_setup(int may_warn)
1791 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1792 hotkey_poll_freq > 0 &&
1793 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1794 if (!tpacpi_hotkey_task) {
1795 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1796 NULL, IBM_FILE "d");
1797 if (IS_ERR(tpacpi_hotkey_task)) {
1798 tpacpi_hotkey_task = NULL;
1799 printk(IBM_ERR "could not create kernel thread "
1800 "for hotkey polling\n");
1803 } else {
1804 hotkey_poll_stop_sync();
1805 if (may_warn &&
1806 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1807 printk(IBM_NOTICE "hot keys 0x%08x require polling, "
1808 "which is currently disabled\n",
1809 hotkey_source_mask);
1814 static void hotkey_poll_setup_safe(int may_warn)
1816 mutex_lock(&hotkey_mutex);
1817 hotkey_poll_setup(may_warn);
1818 mutex_unlock(&hotkey_mutex);
1821 static int hotkey_inputdev_open(struct input_dev *dev)
1823 switch (tpacpi_lifecycle) {
1824 case TPACPI_LIFE_INIT:
1826 * hotkey_init will call hotkey_poll_setup_safe
1827 * at the appropriate moment
1829 return 0;
1830 case TPACPI_LIFE_EXITING:
1831 return -EBUSY;
1832 case TPACPI_LIFE_RUNNING:
1833 hotkey_poll_setup_safe(0);
1834 return 0;
1837 /* Should only happen if tpacpi_lifecycle is corrupt */
1838 BUG();
1839 return -EBUSY;
1842 static void hotkey_inputdev_close(struct input_dev *dev)
1844 /* disable hotkey polling when possible */
1845 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1846 hotkey_poll_setup_safe(0);
1848 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1850 /* sysfs hotkey enable ------------------------------------------------- */
1851 static ssize_t hotkey_enable_show(struct device *dev,
1852 struct device_attribute *attr,
1853 char *buf)
1855 int res, status;
1857 res = hotkey_status_get(&status);
1858 if (res)
1859 return res;
1861 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1864 static ssize_t hotkey_enable_store(struct device *dev,
1865 struct device_attribute *attr,
1866 const char *buf, size_t count)
1868 unsigned long t;
1869 int res;
1871 if (parse_strtoul(buf, 1, &t))
1872 return -EINVAL;
1874 res = hotkey_status_set(t);
1876 return (res) ? res : count;
1879 static struct device_attribute dev_attr_hotkey_enable =
1880 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1881 hotkey_enable_show, hotkey_enable_store);
1883 /* sysfs hotkey mask --------------------------------------------------- */
1884 static ssize_t hotkey_mask_show(struct device *dev,
1885 struct device_attribute *attr,
1886 char *buf)
1888 int res;
1890 if (mutex_lock_interruptible(&hotkey_mutex))
1891 return -ERESTARTSYS;
1892 res = hotkey_mask_get();
1893 mutex_unlock(&hotkey_mutex);
1895 return (res)?
1896 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1899 static ssize_t hotkey_mask_store(struct device *dev,
1900 struct device_attribute *attr,
1901 const char *buf, size_t count)
1903 unsigned long t;
1904 int res;
1906 if (parse_strtoul(buf, 0xffffffffUL, &t))
1907 return -EINVAL;
1909 if (mutex_lock_interruptible(&hotkey_mutex))
1910 return -ERESTARTSYS;
1912 res = hotkey_mask_set(t);
1914 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 hotkey_poll_setup(1);
1916 #endif
1918 mutex_unlock(&hotkey_mutex);
1920 return (res) ? res : count;
1923 static struct device_attribute dev_attr_hotkey_mask =
1924 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1925 hotkey_mask_show, hotkey_mask_store);
1927 /* sysfs hotkey bios_enabled ------------------------------------------- */
1928 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1929 struct device_attribute *attr,
1930 char *buf)
1932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1935 static struct device_attribute dev_attr_hotkey_bios_enabled =
1936 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1938 /* sysfs hotkey bios_mask ---------------------------------------------- */
1939 static ssize_t hotkey_bios_mask_show(struct device *dev,
1940 struct device_attribute *attr,
1941 char *buf)
1943 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1946 static struct device_attribute dev_attr_hotkey_bios_mask =
1947 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1949 /* sysfs hotkey all_mask ----------------------------------------------- */
1950 static ssize_t hotkey_all_mask_show(struct device *dev,
1951 struct device_attribute *attr,
1952 char *buf)
1954 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1955 hotkey_all_mask | hotkey_source_mask);
1958 static struct device_attribute dev_attr_hotkey_all_mask =
1959 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1961 /* sysfs hotkey recommended_mask --------------------------------------- */
1962 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1963 struct device_attribute *attr,
1964 char *buf)
1966 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1967 (hotkey_all_mask | hotkey_source_mask)
1968 & ~hotkey_reserved_mask);
1971 static struct device_attribute dev_attr_hotkey_recommended_mask =
1972 __ATTR(hotkey_recommended_mask, S_IRUGO,
1973 hotkey_recommended_mask_show, NULL);
1975 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1977 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1978 static ssize_t hotkey_source_mask_show(struct device *dev,
1979 struct device_attribute *attr,
1980 char *buf)
1982 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1985 static ssize_t hotkey_source_mask_store(struct device *dev,
1986 struct device_attribute *attr,
1987 const char *buf, size_t count)
1989 unsigned long t;
1991 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1992 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1993 return -EINVAL;
1995 if (mutex_lock_interruptible(&hotkey_mutex))
1996 return -ERESTARTSYS;
1998 HOTKEY_CONFIG_CRITICAL_START
1999 hotkey_source_mask = t;
2000 HOTKEY_CONFIG_CRITICAL_END
2002 hotkey_poll_setup(1);
2004 mutex_unlock(&hotkey_mutex);
2006 return count;
2009 static struct device_attribute dev_attr_hotkey_source_mask =
2010 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2011 hotkey_source_mask_show, hotkey_source_mask_store);
2013 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2014 static ssize_t hotkey_poll_freq_show(struct device *dev,
2015 struct device_attribute *attr,
2016 char *buf)
2018 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2021 static ssize_t hotkey_poll_freq_store(struct device *dev,
2022 struct device_attribute *attr,
2023 const char *buf, size_t count)
2025 unsigned long t;
2027 if (parse_strtoul(buf, 25, &t))
2028 return -EINVAL;
2030 if (mutex_lock_interruptible(&hotkey_mutex))
2031 return -ERESTARTSYS;
2033 hotkey_poll_freq = t;
2035 hotkey_poll_setup(1);
2036 mutex_unlock(&hotkey_mutex);
2038 return count;
2041 static struct device_attribute dev_attr_hotkey_poll_freq =
2042 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2043 hotkey_poll_freq_show, hotkey_poll_freq_store);
2045 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2047 /* sysfs hotkey radio_sw ----------------------------------------------- */
2048 static ssize_t hotkey_radio_sw_show(struct device *dev,
2049 struct device_attribute *attr,
2050 char *buf)
2052 int res, s;
2053 res = hotkey_get_wlsw(&s);
2054 if (res < 0)
2055 return res;
2057 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2060 static struct device_attribute dev_attr_hotkey_radio_sw =
2061 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2063 /* sysfs hotkey report_mode -------------------------------------------- */
2064 static ssize_t hotkey_report_mode_show(struct device *dev,
2065 struct device_attribute *attr,
2066 char *buf)
2068 return snprintf(buf, PAGE_SIZE, "%d\n",
2069 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2072 static ssize_t hotkey_report_mode_store(struct device *dev,
2073 struct device_attribute *attr,
2074 const char *buf, size_t count)
2076 unsigned long t;
2077 int res = 0;
2079 if (parse_strtoul(buf, 2, &t) || (t < 1))
2080 return -EINVAL;
2082 if (t != hotkey_report_mode) {
2083 if (tp_features.hotkey_report_mode_locked)
2084 return -EPERM;
2085 hotkey_report_mode = t;
2086 printk(IBM_NOTICE "hot key report mode set to %d\n",
2087 hotkey_report_mode);
2090 return (res) ? res : count;
2093 static struct device_attribute dev_attr_hotkey_report_mode =
2094 __ATTR(hotkey_report_mode, S_IWUSR | S_IRUGO,
2095 hotkey_report_mode_show, hotkey_report_mode_store);
2097 /* --------------------------------------------------------------------- */
2099 static struct attribute *hotkey_attributes[] __initdata = {
2100 &dev_attr_hotkey_enable.attr,
2101 &dev_attr_hotkey_report_mode.attr,
2104 static struct attribute *hotkey_mask_attributes[] __initdata = {
2105 &dev_attr_hotkey_mask.attr,
2106 &dev_attr_hotkey_bios_enabled.attr,
2107 &dev_attr_hotkey_bios_mask.attr,
2108 &dev_attr_hotkey_all_mask.attr,
2109 &dev_attr_hotkey_recommended_mask.attr,
2110 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2111 &dev_attr_hotkey_source_mask.attr,
2112 &dev_attr_hotkey_poll_freq.attr,
2113 #endif
2116 static int __init hotkey_init(struct ibm_init_struct *iibm)
2119 static u16 ibm_keycode_map[] __initdata = {
2120 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2121 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2122 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2123 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2124 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
2125 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2126 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2127 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2128 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2129 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
2130 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2131 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2132 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2133 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2134 KEY_RESERVED, /* 0x14: VOLUME UP */
2135 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2136 KEY_RESERVED, /* 0x16: MUTE */
2137 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2138 /* (assignments unknown, please report if found) */
2139 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2140 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2142 static u16 lenovo_keycode_map[] __initdata = {
2143 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2144 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2145 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2146 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2147 /* Scan codes 0x0C to 0x0F: Other ACPI HKEY hot keys */
2148 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2149 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2150 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2151 KEY_BRIGHTNESSUP, /* 0x0F: FN+HOME (brightness up) */
2152 /* Scan codes 0x10 to 0x1F: Extended ACPI HKEY hot keys */
2153 KEY_BRIGHTNESSDOWN, /* 0x10: FN+END (brightness down) */
2154 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2155 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2156 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2157 KEY_RESERVED, /* 0x14: VOLUME UP */
2158 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2159 KEY_RESERVED, /* 0x16: MUTE */
2160 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2161 /* (assignments unknown, please report if found) */
2162 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2163 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2166 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2167 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2168 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2170 int res, i;
2171 int status;
2172 int hkeyv;
2174 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2176 BUG_ON(!tpacpi_inputdev);
2177 BUG_ON(tpacpi_inputdev->open != NULL ||
2178 tpacpi_inputdev->close != NULL);
2180 IBM_ACPIHANDLE_INIT(hkey);
2181 mutex_init(&hotkey_mutex);
2183 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2184 mutex_init(&hotkey_thread_mutex);
2185 mutex_init(&hotkey_thread_data_mutex);
2186 #endif
2188 /* hotkey not supported on 570 */
2189 tp_features.hotkey = hkey_handle != NULL;
2191 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2192 str_supported(tp_features.hotkey));
2194 if (tp_features.hotkey) {
2195 hotkey_dev_attributes = create_attr_set(10, NULL);
2196 if (!hotkey_dev_attributes)
2197 return -ENOMEM;
2198 res = add_many_to_attr_set(hotkey_dev_attributes,
2199 hotkey_attributes,
2200 ARRAY_SIZE(hotkey_attributes));
2201 if (res)
2202 return res;
2204 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2205 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2206 for HKEY interface version 0x100 */
2207 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2208 if ((hkeyv >> 8) != 1) {
2209 printk(IBM_ERR "unknown version of the "
2210 "HKEY interface: 0x%x\n", hkeyv);
2211 printk(IBM_ERR "please report this to %s\n",
2212 IBM_MAIL);
2213 } else {
2215 * MHKV 0x100 in A31, R40, R40e,
2216 * T4x, X31, and later
2218 tp_features.hotkey_mask = 1;
2222 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2223 str_supported(tp_features.hotkey_mask));
2225 if (tp_features.hotkey_mask) {
2226 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2227 "MHKA", "qd")) {
2228 printk(IBM_ERR
2229 "missing MHKA handler, "
2230 "please report this to %s\n",
2231 IBM_MAIL);
2232 hotkey_all_mask = 0x080cU; /* FN+F12, FN+F4, FN+F3 */
2236 /* hotkey_source_mask *must* be zero for
2237 * the first hotkey_mask_get */
2238 res = hotkey_status_get(&hotkey_orig_status);
2239 if (!res && tp_features.hotkey_mask) {
2240 res = hotkey_mask_get();
2241 hotkey_orig_mask = hotkey_mask;
2242 if (!res) {
2243 res = add_many_to_attr_set(
2244 hotkey_dev_attributes,
2245 hotkey_mask_attributes,
2246 ARRAY_SIZE(hotkey_mask_attributes));
2250 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2251 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2252 & ~hotkey_all_mask;
2254 vdbg_printk(TPACPI_DBG_INIT,
2255 "hotkey source mask 0x%08x, polling freq %d\n",
2256 hotkey_source_mask, hotkey_poll_freq);
2257 #endif
2259 /* Not all thinkpads have a hardware radio switch */
2260 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2261 tp_features.hotkey_wlsw = 1;
2262 printk(IBM_INFO
2263 "radio switch found; radios are %s\n",
2264 enabled(status, 0));
2265 res = add_to_attr_set(hotkey_dev_attributes,
2266 &dev_attr_hotkey_radio_sw.attr);
2269 if (!res)
2270 res = register_attr_set_with_sysfs(
2271 hotkey_dev_attributes,
2272 &tpacpi_pdev->dev.kobj);
2273 if (res)
2274 return res;
2276 /* Set up key map */
2278 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2279 GFP_KERNEL);
2280 if (!hotkey_keycode_map) {
2281 printk(IBM_ERR "failed to allocate memory for key map\n");
2282 return -ENOMEM;
2285 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2286 dbg_printk(TPACPI_DBG_INIT,
2287 "using Lenovo default hot key map\n");
2288 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2289 TPACPI_HOTKEY_MAP_SIZE);
2290 } else {
2291 dbg_printk(TPACPI_DBG_INIT,
2292 "using IBM default hot key map\n");
2293 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2294 TPACPI_HOTKEY_MAP_SIZE);
2297 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2298 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2299 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2300 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2301 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2302 tpacpi_inputdev->keycode = hotkey_keycode_map;
2303 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2304 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2305 set_bit(hotkey_keycode_map[i],
2306 tpacpi_inputdev->keybit);
2307 } else {
2308 if (i < sizeof(hotkey_reserved_mask)*8)
2309 hotkey_reserved_mask |= 1 << i;
2313 if (tp_features.hotkey_wlsw) {
2314 set_bit(EV_SW, tpacpi_inputdev->evbit);
2315 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2318 dbg_printk(TPACPI_DBG_INIT,
2319 "enabling hot key handling\n");
2320 res = hotkey_status_set(1);
2321 if (res)
2322 return res;
2323 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2324 & ~hotkey_reserved_mask)
2325 | hotkey_orig_mask);
2326 if (res)
2327 return res;
2329 if (hotkey_report_mode > 0) {
2330 tp_features.hotkey_report_mode_locked = 1;
2331 } else {
2332 hotkey_report_mode = 1;
2333 vdbg_printk(TPACPI_DBG_INIT,
2334 "hot key reporting mode can be "
2335 "changed at runtime\n");
2338 dbg_printk(TPACPI_DBG_INIT,
2339 "legacy hot key reporting over procfs %s\n",
2340 (hotkey_report_mode < 2) ?
2341 "enabled" : "disabled");
2343 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2344 tpacpi_inputdev->open = &hotkey_inputdev_open;
2345 tpacpi_inputdev->close = &hotkey_inputdev_close;
2347 hotkey_poll_setup_safe(1);
2348 #endif
2351 return (tp_features.hotkey)? 0 : 1;
2354 static void hotkey_exit(void)
2356 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2357 hotkey_poll_stop_sync();
2358 #endif
2360 if (tp_features.hotkey) {
2361 dbg_printk(TPACPI_DBG_EXIT, "restoring original hot key mask\n");
2362 /* no short-circuit boolean operator below! */
2363 if ((hotkey_mask_set(hotkey_orig_mask) |
2364 hotkey_status_set(hotkey_orig_status)) != 0)
2365 printk(IBM_ERR "failed to restore hot key mask to BIOS defaults\n");
2368 if (hotkey_dev_attributes) {
2369 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2370 hotkey_dev_attributes = NULL;
2374 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2376 u32 hkey;
2377 unsigned int scancode;
2378 int send_acpi_ev;
2379 int ignore_acpi_ev;
2381 if (event != 0x80) {
2382 printk(IBM_ERR "unknown HKEY notification event %d\n", event);
2383 /* forward it to userspace, maybe it knows how to handle it */
2384 acpi_bus_generate_event(ibm->acpi->device, event, 0);
2386 return;
2389 while (1) {
2390 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2391 printk(IBM_ERR "failed to retrieve HKEY event\n");
2392 return;
2395 if (hkey == 0) {
2396 /* queue empty */
2397 return;
2400 send_acpi_ev = 0;
2401 ignore_acpi_ev = 0;
2403 switch (hkey >> 12) {
2404 case 1:
2405 /* 0x1000-0x1FFF: key presses */
2406 scancode = hkey & 0xfff;
2407 if (scancode > 0 && scancode < 0x21) {
2408 scancode--;
2409 if (!(hotkey_source_mask & (1 << scancode))) {
2410 tpacpi_input_send_key(scancode);
2411 } else {
2412 ignore_acpi_ev = 1;
2414 } else {
2415 printk(IBM_ERR
2416 "hotkey 0x%04x out of range for keyboard map\n",
2417 hkey);
2418 send_acpi_ev = 1;
2420 break;
2421 case 5:
2422 /* 0x5000-0x5FFF: LID */
2423 /* we don't handle it through this path, just
2424 * eat up known LID events */
2425 if (hkey != 0x5001 && hkey != 0x5002) {
2426 printk(IBM_ERR
2427 "unknown LID-related HKEY event: 0x%04x\n",
2428 hkey);
2429 send_acpi_ev = 1;
2430 } else {
2431 ignore_acpi_ev = 1;
2433 break;
2434 case 7:
2435 /* 0x7000-0x7FFF: misc */
2436 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2437 tpacpi_input_send_radiosw();
2438 break;
2440 /* fallthrough to default */
2441 default:
2442 /* case 2: dock-related */
2443 /* 0x2305 - T43 waking up due to bay lever eject while aslept */
2444 /* case 3: ultra-bay related. maybe bay in dock? */
2445 /* 0x3003 - T43 after wake up by bay lever eject (0x2305) */
2446 printk(IBM_NOTICE "unhandled HKEY event 0x%04x\n", hkey);
2447 send_acpi_ev = 1;
2450 if (!ignore_acpi_ev && (send_acpi_ev || hotkey_report_mode < 2)) {
2451 acpi_bus_generate_event(ibm->acpi->device, event, hkey);
2456 static void hotkey_resume(void)
2458 if (hotkey_mask_get())
2459 printk(IBM_ERR "error while trying to read hot key mask from firmware\n");
2460 tpacpi_input_send_radiosw();
2461 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2462 hotkey_poll_setup_safe(0);
2463 #endif
2466 /* procfs -------------------------------------------------------------- */
2467 static int hotkey_read(char *p)
2469 int res, status;
2470 int len = 0;
2472 if (!tp_features.hotkey) {
2473 len += sprintf(p + len, "status:\t\tnot supported\n");
2474 return len;
2477 if (mutex_lock_interruptible(&hotkey_mutex))
2478 return -ERESTARTSYS;
2479 res = hotkey_status_get(&status);
2480 if (!res)
2481 res = hotkey_mask_get();
2482 mutex_unlock(&hotkey_mutex);
2483 if (res)
2484 return res;
2486 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2487 if (tp_features.hotkey_mask) {
2488 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2489 len += sprintf(p + len,
2490 "commands:\tenable, disable, reset, <mask>\n");
2491 } else {
2492 len += sprintf(p + len, "mask:\t\tnot supported\n");
2493 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2496 return len;
2499 static int hotkey_write(char *buf)
2501 int res, status;
2502 u32 mask;
2503 char *cmd;
2505 if (!tp_features.hotkey)
2506 return -ENODEV;
2508 if (mutex_lock_interruptible(&hotkey_mutex))
2509 return -ERESTARTSYS;
2511 status = -1;
2512 mask = hotkey_mask;
2514 res = 0;
2515 while ((cmd = next_cmd(&buf))) {
2516 if (strlencmp(cmd, "enable") == 0) {
2517 status = 1;
2518 } else if (strlencmp(cmd, "disable") == 0) {
2519 status = 0;
2520 } else if (strlencmp(cmd, "reset") == 0) {
2521 status = hotkey_orig_status;
2522 mask = hotkey_orig_mask;
2523 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2524 /* mask set */
2525 } else if (sscanf(cmd, "%x", &mask) == 1) {
2526 /* mask set */
2527 } else {
2528 res = -EINVAL;
2529 goto errexit;
2532 if (status != -1)
2533 res = hotkey_status_set(status);
2535 if (!res && mask != hotkey_mask)
2536 res = hotkey_mask_set(mask);
2538 errexit:
2539 mutex_unlock(&hotkey_mutex);
2540 return res;
2543 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2544 .hid = IBM_HKEY_HID,
2545 .notify = hotkey_notify,
2546 .handle = &hkey_handle,
2547 .type = ACPI_DEVICE_NOTIFY,
2550 static struct ibm_struct hotkey_driver_data = {
2551 .name = "hotkey",
2552 .read = hotkey_read,
2553 .write = hotkey_write,
2554 .exit = hotkey_exit,
2555 .resume = hotkey_resume,
2556 .acpi = &ibm_hotkey_acpidriver,
2559 /*************************************************************************
2560 * Bluetooth subdriver
2563 /* sysfs bluetooth enable ---------------------------------------------- */
2564 static ssize_t bluetooth_enable_show(struct device *dev,
2565 struct device_attribute *attr,
2566 char *buf)
2568 int status;
2570 status = bluetooth_get_radiosw();
2571 if (status < 0)
2572 return status;
2574 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2577 static ssize_t bluetooth_enable_store(struct device *dev,
2578 struct device_attribute *attr,
2579 const char *buf, size_t count)
2581 unsigned long t;
2582 int res;
2584 if (parse_strtoul(buf, 1, &t))
2585 return -EINVAL;
2587 res = bluetooth_set_radiosw(t);
2589 return (res) ? res : count;
2592 static struct device_attribute dev_attr_bluetooth_enable =
2593 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2594 bluetooth_enable_show, bluetooth_enable_store);
2596 /* --------------------------------------------------------------------- */
2598 static struct attribute *bluetooth_attributes[] = {
2599 &dev_attr_bluetooth_enable.attr,
2600 NULL
2603 static const struct attribute_group bluetooth_attr_group = {
2604 .attrs = bluetooth_attributes,
2607 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2609 int res;
2610 int status = 0;
2612 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2614 IBM_ACPIHANDLE_INIT(hkey);
2616 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2617 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2618 tp_features.bluetooth = hkey_handle &&
2619 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2621 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2622 str_supported(tp_features.bluetooth),
2623 status);
2625 if (tp_features.bluetooth) {
2626 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2627 /* no bluetooth hardware present in system */
2628 tp_features.bluetooth = 0;
2629 dbg_printk(TPACPI_DBG_INIT,
2630 "bluetooth hardware not installed\n");
2631 } else {
2632 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2633 &bluetooth_attr_group);
2634 if (res)
2635 return res;
2639 return (tp_features.bluetooth)? 0 : 1;
2642 static void bluetooth_exit(void)
2644 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2645 &bluetooth_attr_group);
2648 static int bluetooth_get_radiosw(void)
2650 int status;
2652 if (!tp_features.bluetooth)
2653 return -ENODEV;
2655 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2656 return -EIO;
2658 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2661 static int bluetooth_set_radiosw(int radio_on)
2663 int status;
2665 if (!tp_features.bluetooth)
2666 return -ENODEV;
2668 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2669 return -EIO;
2670 if (radio_on)
2671 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2672 else
2673 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2674 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2675 return -EIO;
2677 return 0;
2680 /* procfs -------------------------------------------------------------- */
2681 static int bluetooth_read(char *p)
2683 int len = 0;
2684 int status = bluetooth_get_radiosw();
2686 if (!tp_features.bluetooth)
2687 len += sprintf(p + len, "status:\t\tnot supported\n");
2688 else {
2689 len += sprintf(p + len, "status:\t\t%s\n",
2690 (status)? "enabled" : "disabled");
2691 len += sprintf(p + len, "commands:\tenable, disable\n");
2694 return len;
2697 static int bluetooth_write(char *buf)
2699 char *cmd;
2701 if (!tp_features.bluetooth)
2702 return -ENODEV;
2704 while ((cmd = next_cmd(&buf))) {
2705 if (strlencmp(cmd, "enable") == 0) {
2706 bluetooth_set_radiosw(1);
2707 } else if (strlencmp(cmd, "disable") == 0) {
2708 bluetooth_set_radiosw(0);
2709 } else
2710 return -EINVAL;
2713 return 0;
2716 static struct ibm_struct bluetooth_driver_data = {
2717 .name = "bluetooth",
2718 .read = bluetooth_read,
2719 .write = bluetooth_write,
2720 .exit = bluetooth_exit,
2723 /*************************************************************************
2724 * Wan subdriver
2727 /* sysfs wan enable ---------------------------------------------------- */
2728 static ssize_t wan_enable_show(struct device *dev,
2729 struct device_attribute *attr,
2730 char *buf)
2732 int status;
2734 status = wan_get_radiosw();
2735 if (status < 0)
2736 return status;
2738 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2741 static ssize_t wan_enable_store(struct device *dev,
2742 struct device_attribute *attr,
2743 const char *buf, size_t count)
2745 unsigned long t;
2746 int res;
2748 if (parse_strtoul(buf, 1, &t))
2749 return -EINVAL;
2751 res = wan_set_radiosw(t);
2753 return (res) ? res : count;
2756 static struct device_attribute dev_attr_wan_enable =
2757 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2758 wan_enable_show, wan_enable_store);
2760 /* --------------------------------------------------------------------- */
2762 static struct attribute *wan_attributes[] = {
2763 &dev_attr_wan_enable.attr,
2764 NULL
2767 static const struct attribute_group wan_attr_group = {
2768 .attrs = wan_attributes,
2771 static int __init wan_init(struct ibm_init_struct *iibm)
2773 int res;
2774 int status = 0;
2776 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2778 IBM_ACPIHANDLE_INIT(hkey);
2780 tp_features.wan = hkey_handle &&
2781 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
2783 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2784 str_supported(tp_features.wan),
2785 status);
2787 if (tp_features.wan) {
2788 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2789 /* no wan hardware present in system */
2790 tp_features.wan = 0;
2791 dbg_printk(TPACPI_DBG_INIT,
2792 "wan hardware not installed\n");
2793 } else {
2794 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2795 &wan_attr_group);
2796 if (res)
2797 return res;
2801 return (tp_features.wan)? 0 : 1;
2804 static void wan_exit(void)
2806 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2807 &wan_attr_group);
2810 static int wan_get_radiosw(void)
2812 int status;
2814 if (!tp_features.wan)
2815 return -ENODEV;
2817 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2818 return -EIO;
2820 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2823 static int wan_set_radiosw(int radio_on)
2825 int status;
2827 if (!tp_features.wan)
2828 return -ENODEV;
2830 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2831 return -EIO;
2832 if (radio_on)
2833 status |= TP_ACPI_WANCARD_RADIOSSW;
2834 else
2835 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2836 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2837 return -EIO;
2839 return 0;
2842 /* procfs -------------------------------------------------------------- */
2843 static int wan_read(char *p)
2845 int len = 0;
2846 int status = wan_get_radiosw();
2848 if (!tp_features.wan)
2849 len += sprintf(p + len, "status:\t\tnot supported\n");
2850 else {
2851 len += sprintf(p + len, "status:\t\t%s\n",
2852 (status)? "enabled" : "disabled");
2853 len += sprintf(p + len, "commands:\tenable, disable\n");
2856 return len;
2859 static int wan_write(char *buf)
2861 char *cmd;
2863 if (!tp_features.wan)
2864 return -ENODEV;
2866 while ((cmd = next_cmd(&buf))) {
2867 if (strlencmp(cmd, "enable") == 0) {
2868 wan_set_radiosw(1);
2869 } else if (strlencmp(cmd, "disable") == 0) {
2870 wan_set_radiosw(0);
2871 } else
2872 return -EINVAL;
2875 return 0;
2878 static struct ibm_struct wan_driver_data = {
2879 .name = "wan",
2880 .read = wan_read,
2881 .write = wan_write,
2882 .exit = wan_exit,
2883 .flags.experimental = 1,
2886 /*************************************************************************
2887 * Video subdriver
2890 static enum video_access_mode video_supported;
2891 static int video_orig_autosw;
2893 IBM_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
2894 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
2895 "\\_SB.PCI0.VID0", /* 770e */
2896 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
2897 "\\_SB.PCI0.AGP.VID", /* all others */
2898 ); /* R30, R31 */
2900 IBM_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
2902 static int __init video_init(struct ibm_init_struct *iibm)
2904 int ivga;
2906 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2908 IBM_ACPIHANDLE_INIT(vid);
2909 IBM_ACPIHANDLE_INIT(vid2);
2911 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2912 /* G41, assume IVGA doesn't change */
2913 vid_handle = vid2_handle;
2915 if (!vid_handle)
2916 /* video switching not supported on R30, R31 */
2917 video_supported = TPACPI_VIDEO_NONE;
2918 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2919 /* 570 */
2920 video_supported = TPACPI_VIDEO_570;
2921 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2922 /* 600e/x, 770e, 770x */
2923 video_supported = TPACPI_VIDEO_770;
2924 else
2925 /* all others */
2926 video_supported = TPACPI_VIDEO_NEW;
2928 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2929 str_supported(video_supported != TPACPI_VIDEO_NONE),
2930 video_supported);
2932 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
2935 static void video_exit(void)
2937 dbg_printk(TPACPI_DBG_EXIT,
2938 "restoring original video autoswitch mode\n");
2939 if (video_autosw_set(video_orig_autosw))
2940 printk(IBM_ERR "error while trying to restore original "
2941 "video autoswitch mode\n");
2944 static int video_outputsw_get(void)
2946 int status = 0;
2947 int i;
2949 switch (video_supported) {
2950 case TPACPI_VIDEO_570:
2951 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2952 TP_ACPI_VIDEO_570_PHSCMD))
2953 return -EIO;
2954 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2955 break;
2956 case TPACPI_VIDEO_770:
2957 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2958 return -EIO;
2959 if (i)
2960 status |= TP_ACPI_VIDEO_S_LCD;
2961 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2962 return -EIO;
2963 if (i)
2964 status |= TP_ACPI_VIDEO_S_CRT;
2965 break;
2966 case TPACPI_VIDEO_NEW:
2967 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
2968 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
2969 return -EIO;
2970 if (i)
2971 status |= TP_ACPI_VIDEO_S_CRT;
2973 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
2974 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
2975 return -EIO;
2976 if (i)
2977 status |= TP_ACPI_VIDEO_S_LCD;
2978 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
2979 return -EIO;
2980 if (i)
2981 status |= TP_ACPI_VIDEO_S_DVI;
2982 break;
2983 default:
2984 return -ENOSYS;
2987 return status;
2990 static int video_outputsw_set(int status)
2992 int autosw;
2993 int res = 0;
2995 switch (video_supported) {
2996 case TPACPI_VIDEO_570:
2997 res = acpi_evalf(NULL, NULL,
2998 "\\_SB.PHS2", "vdd",
2999 TP_ACPI_VIDEO_570_PHS2CMD,
3000 status | TP_ACPI_VIDEO_570_PHS2SET);
3001 break;
3002 case TPACPI_VIDEO_770:
3003 autosw = video_autosw_get();
3004 if (autosw < 0)
3005 return autosw;
3007 res = video_autosw_set(1);
3008 if (res)
3009 return res;
3010 res = acpi_evalf(vid_handle, NULL,
3011 "ASWT", "vdd", status * 0x100, 0);
3012 if (!autosw && video_autosw_set(autosw)) {
3013 printk(IBM_ERR "video auto-switch left enabled due to error\n");
3014 return -EIO;
3016 break;
3017 case TPACPI_VIDEO_NEW:
3018 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3019 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3020 break;
3021 default:
3022 return -ENOSYS;
3025 return (res)? 0 : -EIO;
3028 static int video_autosw_get(void)
3030 int autosw = 0;
3032 switch (video_supported) {
3033 case TPACPI_VIDEO_570:
3034 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3035 return -EIO;
3036 break;
3037 case TPACPI_VIDEO_770:
3038 case TPACPI_VIDEO_NEW:
3039 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3040 return -EIO;
3041 break;
3042 default:
3043 return -ENOSYS;
3046 return autosw & 1;
3049 static int video_autosw_set(int enable)
3051 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3052 return -EIO;
3053 return 0;
3056 static int video_outputsw_cycle(void)
3058 int autosw = video_autosw_get();
3059 int res;
3061 if (autosw < 0)
3062 return autosw;
3064 switch (video_supported) {
3065 case TPACPI_VIDEO_570:
3066 res = video_autosw_set(1);
3067 if (res)
3068 return res;
3069 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3070 break;
3071 case TPACPI_VIDEO_770:
3072 case TPACPI_VIDEO_NEW:
3073 res = video_autosw_set(1);
3074 if (res)
3075 return res;
3076 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3077 break;
3078 default:
3079 return -ENOSYS;
3081 if (!autosw && video_autosw_set(autosw)) {
3082 printk(IBM_ERR "video auto-switch left enabled due to error\n");
3083 return -EIO;
3086 return (res)? 0 : -EIO;
3089 static int video_expand_toggle(void)
3091 switch (video_supported) {
3092 case TPACPI_VIDEO_570:
3093 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3094 0 : -EIO;
3095 case TPACPI_VIDEO_770:
3096 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3097 0 : -EIO;
3098 case TPACPI_VIDEO_NEW:
3099 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3100 0 : -EIO;
3101 default:
3102 return -ENOSYS;
3104 /* not reached */
3107 static int video_read(char *p)
3109 int status, autosw;
3110 int len = 0;
3112 if (video_supported == TPACPI_VIDEO_NONE) {
3113 len += sprintf(p + len, "status:\t\tnot supported\n");
3114 return len;
3117 status = video_outputsw_get();
3118 if (status < 0)
3119 return status;
3121 autosw = video_autosw_get();
3122 if (autosw < 0)
3123 return autosw;
3125 len += sprintf(p + len, "status:\t\tsupported\n");
3126 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3127 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3128 if (video_supported == TPACPI_VIDEO_NEW)
3129 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3130 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3131 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3132 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3133 if (video_supported == TPACPI_VIDEO_NEW)
3134 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3135 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3136 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3138 return len;
3141 static int video_write(char *buf)
3143 char *cmd;
3144 int enable, disable, status;
3145 int res;
3147 if (video_supported == TPACPI_VIDEO_NONE)
3148 return -ENODEV;
3150 enable = 0;
3151 disable = 0;
3153 while ((cmd = next_cmd(&buf))) {
3154 if (strlencmp(cmd, "lcd_enable") == 0) {
3155 enable |= TP_ACPI_VIDEO_S_LCD;
3156 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3157 disable |= TP_ACPI_VIDEO_S_LCD;
3158 } else if (strlencmp(cmd, "crt_enable") == 0) {
3159 enable |= TP_ACPI_VIDEO_S_CRT;
3160 } else if (strlencmp(cmd, "crt_disable") == 0) {
3161 disable |= TP_ACPI_VIDEO_S_CRT;
3162 } else if (video_supported == TPACPI_VIDEO_NEW &&
3163 strlencmp(cmd, "dvi_enable") == 0) {
3164 enable |= TP_ACPI_VIDEO_S_DVI;
3165 } else if (video_supported == TPACPI_VIDEO_NEW &&
3166 strlencmp(cmd, "dvi_disable") == 0) {
3167 disable |= TP_ACPI_VIDEO_S_DVI;
3168 } else if (strlencmp(cmd, "auto_enable") == 0) {
3169 res = video_autosw_set(1);
3170 if (res)
3171 return res;
3172 } else if (strlencmp(cmd, "auto_disable") == 0) {
3173 res = video_autosw_set(0);
3174 if (res)
3175 return res;
3176 } else if (strlencmp(cmd, "video_switch") == 0) {
3177 res = video_outputsw_cycle();
3178 if (res)
3179 return res;
3180 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3181 res = video_expand_toggle();
3182 if (res)
3183 return res;
3184 } else
3185 return -EINVAL;
3188 if (enable || disable) {
3189 status = video_outputsw_get();
3190 if (status < 0)
3191 return status;
3192 res = video_outputsw_set((status & ~disable) | enable);
3193 if (res)
3194 return res;
3197 return 0;
3200 static struct ibm_struct video_driver_data = {
3201 .name = "video",
3202 .read = video_read,
3203 .write = video_write,
3204 .exit = video_exit,
3207 /*************************************************************************
3208 * Light (thinklight) subdriver
3211 IBM_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3212 IBM_HANDLE(ledb, ec, "LEDB"); /* G4x */
3214 static int __init light_init(struct ibm_init_struct *iibm)
3216 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3218 IBM_ACPIHANDLE_INIT(ledb);
3219 IBM_ACPIHANDLE_INIT(lght);
3220 IBM_ACPIHANDLE_INIT(cmos);
3222 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3223 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3225 if (tp_features.light)
3226 /* light status not supported on
3227 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3228 tp_features.light_status =
3229 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3231 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
3232 str_supported(tp_features.light));
3234 return (tp_features.light)? 0 : 1;
3237 static int light_read(char *p)
3239 int len = 0;
3240 int status = 0;
3242 if (!tp_features.light) {
3243 len += sprintf(p + len, "status:\t\tnot supported\n");
3244 } else if (!tp_features.light_status) {
3245 len += sprintf(p + len, "status:\t\tunknown\n");
3246 len += sprintf(p + len, "commands:\ton, off\n");
3247 } else {
3248 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3249 return -EIO;
3250 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3251 len += sprintf(p + len, "commands:\ton, off\n");
3254 return len;
3257 static int light_write(char *buf)
3259 int cmos_cmd, lght_cmd;
3260 char *cmd;
3261 int success;
3263 if (!tp_features.light)
3264 return -ENODEV;
3266 while ((cmd = next_cmd(&buf))) {
3267 if (strlencmp(cmd, "on") == 0) {
3268 cmos_cmd = 0x0c;
3269 lght_cmd = 1;
3270 } else if (strlencmp(cmd, "off") == 0) {
3271 cmos_cmd = 0x0d;
3272 lght_cmd = 0;
3273 } else
3274 return -EINVAL;
3276 success = cmos_handle ?
3277 acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd) :
3278 acpi_evalf(lght_handle, NULL, NULL, "vd", lght_cmd);
3279 if (!success)
3280 return -EIO;
3283 return 0;
3286 static struct ibm_struct light_driver_data = {
3287 .name = "light",
3288 .read = light_read,
3289 .write = light_write,
3292 /*************************************************************************
3293 * Dock subdriver
3296 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3298 IBM_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3299 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3300 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3301 "\\_SB.PCI.ISA.SLCE", /* 570 */
3302 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3304 /* don't list other alternatives as we install a notify handler on the 570 */
3305 IBM_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3307 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3309 .notify = dock_notify,
3310 .handle = &dock_handle,
3311 .type = ACPI_SYSTEM_NOTIFY,
3314 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3315 * We just use it to get notifications of dock hotplug
3316 * in very old thinkpads */
3317 .hid = PCI_ROOT_HID_STRING,
3318 .notify = dock_notify,
3319 .handle = &pci_handle,
3320 .type = ACPI_SYSTEM_NOTIFY,
3324 static struct ibm_struct dock_driver_data[2] = {
3326 .name = "dock",
3327 .read = dock_read,
3328 .write = dock_write,
3329 .acpi = &ibm_dock_acpidriver[0],
3332 .name = "dock",
3333 .acpi = &ibm_dock_acpidriver[1],
3337 #define dock_docked() (_sta(dock_handle) & 1)
3339 static int __init dock_init(struct ibm_init_struct *iibm)
3341 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3343 IBM_ACPIHANDLE_INIT(dock);
3345 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3346 str_supported(dock_handle != NULL));
3348 return (dock_handle)? 0 : 1;
3351 static int __init dock_init2(struct ibm_init_struct *iibm)
3353 int dock2_needed;
3355 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3357 if (dock_driver_data[0].flags.acpi_driver_registered &&
3358 dock_driver_data[0].flags.acpi_notify_installed) {
3359 IBM_ACPIHANDLE_INIT(pci);
3360 dock2_needed = (pci_handle != NULL);
3361 vdbg_printk(TPACPI_DBG_INIT,
3362 "dock PCI handler for the TP 570 is %s\n",
3363 str_supported(dock2_needed));
3364 } else {
3365 vdbg_printk(TPACPI_DBG_INIT,
3366 "dock subdriver part 2 not required\n");
3367 dock2_needed = 0;
3370 return (dock2_needed)? 0 : 1;
3373 static void dock_notify(struct ibm_struct *ibm, u32 event)
3375 int docked = dock_docked();
3376 int pci = ibm->acpi->hid && strstr(ibm->acpi->hid, PCI_ROOT_HID_STRING);
3378 if (event == 1 && !pci) /* 570 */
3379 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3380 else if (event == 1 && pci) /* 570 */
3381 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3382 else if (event == 3 && docked)
3383 acpi_bus_generate_event(ibm->acpi->device, event, 1); /* button */
3384 else if (event == 3 && !docked)
3385 acpi_bus_generate_event(ibm->acpi->device, event, 2); /* undock */
3386 else if (event == 0 && docked)
3387 acpi_bus_generate_event(ibm->acpi->device, event, 3); /* dock */
3388 else {
3389 printk(IBM_ERR "unknown dock event %d, status %d\n",
3390 event, _sta(dock_handle));
3391 acpi_bus_generate_event(ibm->acpi->device, event, 0); /* unknown */
3395 static int dock_read(char *p)
3397 int len = 0;
3398 int docked = dock_docked();
3400 if (!dock_handle)
3401 len += sprintf(p + len, "status:\t\tnot supported\n");
3402 else if (!docked)
3403 len += sprintf(p + len, "status:\t\tundocked\n");
3404 else {
3405 len += sprintf(p + len, "status:\t\tdocked\n");
3406 len += sprintf(p + len, "commands:\tdock, undock\n");
3409 return len;
3412 static int dock_write(char *buf)
3414 char *cmd;
3416 if (!dock_docked())
3417 return -ENODEV;
3419 while ((cmd = next_cmd(&buf))) {
3420 if (strlencmp(cmd, "undock") == 0) {
3421 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3422 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3423 return -EIO;
3424 } else if (strlencmp(cmd, "dock") == 0) {
3425 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3426 return -EIO;
3427 } else
3428 return -EINVAL;
3431 return 0;
3434 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3436 /*************************************************************************
3437 * Bay subdriver
3440 #ifdef CONFIG_THINKPAD_ACPI_BAY
3441 IBM_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3442 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3443 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3444 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3445 ); /* A21e, R30, R31 */
3446 IBM_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3447 "_EJ0", /* all others */
3448 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3449 IBM_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3450 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3451 ); /* all others */
3452 IBM_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3453 "_EJ0", /* 770x */
3454 ); /* all others */
3456 static int __init bay_init(struct ibm_init_struct *iibm)
3458 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3460 IBM_ACPIHANDLE_INIT(bay);
3461 if (bay_handle)
3462 IBM_ACPIHANDLE_INIT(bay_ej);
3463 IBM_ACPIHANDLE_INIT(bay2);
3464 if (bay2_handle)
3465 IBM_ACPIHANDLE_INIT(bay2_ej);
3467 tp_features.bay_status = bay_handle &&
3468 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3469 tp_features.bay_status2 = bay2_handle &&
3470 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3472 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3473 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3474 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3475 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3477 vdbg_printk(TPACPI_DBG_INIT,
3478 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3479 str_supported(tp_features.bay_status),
3480 str_supported(tp_features.bay_eject),
3481 str_supported(tp_features.bay_status2),
3482 str_supported(tp_features.bay_eject2));
3484 return (tp_features.bay_status || tp_features.bay_eject ||
3485 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3488 static void bay_notify(struct ibm_struct *ibm, u32 event)
3490 acpi_bus_generate_event(ibm->acpi->device, event, 0);
3493 #define bay_occupied(b) (_sta(b##_handle) & 1)
3495 static int bay_read(char *p)
3497 int len = 0;
3498 int occupied = bay_occupied(bay);
3499 int occupied2 = bay_occupied(bay2);
3500 int eject, eject2;
3502 len += sprintf(p + len, "status:\t\t%s\n",
3503 tp_features.bay_status ?
3504 (occupied ? "occupied" : "unoccupied") :
3505 "not supported");
3506 if (tp_features.bay_status2)
3507 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3508 "occupied" : "unoccupied");
3510 eject = tp_features.bay_eject && occupied;
3511 eject2 = tp_features.bay_eject2 && occupied2;
3513 if (eject && eject2)
3514 len += sprintf(p + len, "commands:\teject, eject2\n");
3515 else if (eject)
3516 len += sprintf(p + len, "commands:\teject\n");
3517 else if (eject2)
3518 len += sprintf(p + len, "commands:\teject2\n");
3520 return len;
3523 static int bay_write(char *buf)
3525 char *cmd;
3527 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3528 return -ENODEV;
3530 while ((cmd = next_cmd(&buf))) {
3531 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3532 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3533 return -EIO;
3534 } else if (tp_features.bay_eject2 &&
3535 strlencmp(cmd, "eject2") == 0) {
3536 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3537 return -EIO;
3538 } else
3539 return -EINVAL;
3542 return 0;
3545 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3546 .notify = bay_notify,
3547 .handle = &bay_handle,
3548 .type = ACPI_SYSTEM_NOTIFY,
3551 static struct ibm_struct bay_driver_data = {
3552 .name = "bay",
3553 .read = bay_read,
3554 .write = bay_write,
3555 .acpi = &ibm_bay_acpidriver,
3558 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3560 /*************************************************************************
3561 * CMOS subdriver
3564 /* sysfs cmos_command -------------------------------------------------- */
3565 static ssize_t cmos_command_store(struct device *dev,
3566 struct device_attribute *attr,
3567 const char *buf, size_t count)
3569 unsigned long cmos_cmd;
3570 int res;
3572 if (parse_strtoul(buf, 21, &cmos_cmd))
3573 return -EINVAL;
3575 res = issue_thinkpad_cmos_command(cmos_cmd);
3576 return (res)? res : count;
3579 static struct device_attribute dev_attr_cmos_command =
3580 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3582 /* --------------------------------------------------------------------- */
3584 static int __init cmos_init(struct ibm_init_struct *iibm)
3586 int res;
3588 vdbg_printk(TPACPI_DBG_INIT,
3589 "initializing cmos commands subdriver\n");
3591 IBM_ACPIHANDLE_INIT(cmos);
3593 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3594 str_supported(cmos_handle != NULL));
3596 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3597 if (res)
3598 return res;
3600 return (cmos_handle)? 0 : 1;
3603 static void cmos_exit(void)
3605 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3608 static int cmos_read(char *p)
3610 int len = 0;
3612 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3613 R30, R31, T20-22, X20-21 */
3614 if (!cmos_handle)
3615 len += sprintf(p + len, "status:\t\tnot supported\n");
3616 else {
3617 len += sprintf(p + len, "status:\t\tsupported\n");
3618 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3621 return len;
3624 static int cmos_write(char *buf)
3626 char *cmd;
3627 int cmos_cmd, res;
3629 while ((cmd = next_cmd(&buf))) {
3630 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3631 cmos_cmd >= 0 && cmos_cmd <= 21) {
3632 /* cmos_cmd set */
3633 } else
3634 return -EINVAL;
3636 res = issue_thinkpad_cmos_command(cmos_cmd);
3637 if (res)
3638 return res;
3641 return 0;
3644 static struct ibm_struct cmos_driver_data = {
3645 .name = "cmos",
3646 .read = cmos_read,
3647 .write = cmos_write,
3648 .exit = cmos_exit,
3651 /*************************************************************************
3652 * LED subdriver
3655 static enum led_access_mode led_supported;
3657 IBM_HANDLE(led, ec, "SLED", /* 570 */
3658 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3659 "LED", /* all others */
3660 ); /* R30, R31 */
3662 static int __init led_init(struct ibm_init_struct *iibm)
3664 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3666 IBM_ACPIHANDLE_INIT(led);
3668 if (!led_handle)
3669 /* led not supported on R30, R31 */
3670 led_supported = TPACPI_LED_NONE;
3671 else if (strlencmp(led_path, "SLED") == 0)
3672 /* 570 */
3673 led_supported = TPACPI_LED_570;
3674 else if (strlencmp(led_path, "SYSL") == 0)
3675 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3676 led_supported = TPACPI_LED_OLD;
3677 else
3678 /* all others */
3679 led_supported = TPACPI_LED_NEW;
3681 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
3682 str_supported(led_supported), led_supported);
3684 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
3687 #define led_status(s) ((s) == 0 ? "off" : ((s) == 1 ? "on" : "blinking"))
3689 static int led_read(char *p)
3691 int len = 0;
3693 if (!led_supported) {
3694 len += sprintf(p + len, "status:\t\tnot supported\n");
3695 return len;
3697 len += sprintf(p + len, "status:\t\tsupported\n");
3699 if (led_supported == TPACPI_LED_570) {
3700 /* 570 */
3701 int i, status;
3702 for (i = 0; i < 8; i++) {
3703 if (!acpi_evalf(ec_handle,
3704 &status, "GLED", "dd", 1 << i))
3705 return -EIO;
3706 len += sprintf(p + len, "%d:\t\t%s\n",
3707 i, led_status(status));
3711 len += sprintf(p + len, "commands:\t"
3712 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
3714 return len;
3717 /* off, on, blink */
3718 static const int led_sled_arg1[] = { 0, 1, 3 };
3719 static const int led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3720 static const int led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3721 static const int led_led_arg1[] = { 0, 0x80, 0xc0 };
3723 static int led_write(char *buf)
3725 char *cmd;
3726 int led, ind, ret;
3728 if (!led_supported)
3729 return -ENODEV;
3731 while ((cmd = next_cmd(&buf))) {
3732 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
3733 return -EINVAL;
3735 if (strstr(cmd, "off")) {
3736 ind = 0;
3737 } else if (strstr(cmd, "on")) {
3738 ind = 1;
3739 } else if (strstr(cmd, "blink")) {
3740 ind = 2;
3741 } else
3742 return -EINVAL;
3744 if (led_supported == TPACPI_LED_570) {
3745 /* 570 */
3746 led = 1 << led;
3747 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3748 led, led_sled_arg1[ind]))
3749 return -EIO;
3750 } else if (led_supported == TPACPI_LED_OLD) {
3751 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3752 led = 1 << led;
3753 ret = ec_write(TPACPI_LED_EC_HLMS, led);
3754 if (ret >= 0)
3755 ret =
3756 ec_write(TPACPI_LED_EC_HLBL,
3757 led * led_exp_hlbl[ind]);
3758 if (ret >= 0)
3759 ret =
3760 ec_write(TPACPI_LED_EC_HLCL,
3761 led * led_exp_hlcl[ind]);
3762 if (ret < 0)
3763 return ret;
3764 } else {
3765 /* all others */
3766 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3767 led, led_led_arg1[ind]))
3768 return -EIO;
3772 return 0;
3775 static struct ibm_struct led_driver_data = {
3776 .name = "led",
3777 .read = led_read,
3778 .write = led_write,
3781 /*************************************************************************
3782 * Beep subdriver
3785 IBM_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
3787 static int __init beep_init(struct ibm_init_struct *iibm)
3789 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
3791 IBM_ACPIHANDLE_INIT(beep);
3793 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
3794 str_supported(beep_handle != NULL));
3796 return (beep_handle)? 0 : 1;
3799 static int beep_read(char *p)
3801 int len = 0;
3803 if (!beep_handle)
3804 len += sprintf(p + len, "status:\t\tnot supported\n");
3805 else {
3806 len += sprintf(p + len, "status:\t\tsupported\n");
3807 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
3810 return len;
3813 static int beep_write(char *buf)
3815 char *cmd;
3816 int beep_cmd;
3818 if (!beep_handle)
3819 return -ENODEV;
3821 while ((cmd = next_cmd(&buf))) {
3822 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
3823 beep_cmd >= 0 && beep_cmd <= 17) {
3824 /* beep_cmd set */
3825 } else
3826 return -EINVAL;
3827 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
3828 return -EIO;
3831 return 0;
3834 static struct ibm_struct beep_driver_data = {
3835 .name = "beep",
3836 .read = beep_read,
3837 .write = beep_write,
3840 /*************************************************************************
3841 * Thermal subdriver
3844 static enum thermal_access_mode thermal_read_mode;
3846 /* sysfs temp##_input -------------------------------------------------- */
3848 static ssize_t thermal_temp_input_show(struct device *dev,
3849 struct device_attribute *attr,
3850 char *buf)
3852 struct sensor_device_attribute *sensor_attr =
3853 to_sensor_dev_attr(attr);
3854 int idx = sensor_attr->index;
3855 s32 value;
3856 int res;
3858 res = thermal_get_sensor(idx, &value);
3859 if (res)
3860 return res;
3861 if (value == TP_EC_THERMAL_TMP_NA * 1000)
3862 return -ENXIO;
3864 return snprintf(buf, PAGE_SIZE, "%d\n", value);
3867 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
3868 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, thermal_temp_input_show, NULL, _idxB)
3870 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
3871 THERMAL_SENSOR_ATTR_TEMP(1, 0),
3872 THERMAL_SENSOR_ATTR_TEMP(2, 1),
3873 THERMAL_SENSOR_ATTR_TEMP(3, 2),
3874 THERMAL_SENSOR_ATTR_TEMP(4, 3),
3875 THERMAL_SENSOR_ATTR_TEMP(5, 4),
3876 THERMAL_SENSOR_ATTR_TEMP(6, 5),
3877 THERMAL_SENSOR_ATTR_TEMP(7, 6),
3878 THERMAL_SENSOR_ATTR_TEMP(8, 7),
3879 THERMAL_SENSOR_ATTR_TEMP(9, 8),
3880 THERMAL_SENSOR_ATTR_TEMP(10, 9),
3881 THERMAL_SENSOR_ATTR_TEMP(11, 10),
3882 THERMAL_SENSOR_ATTR_TEMP(12, 11),
3883 THERMAL_SENSOR_ATTR_TEMP(13, 12),
3884 THERMAL_SENSOR_ATTR_TEMP(14, 13),
3885 THERMAL_SENSOR_ATTR_TEMP(15, 14),
3886 THERMAL_SENSOR_ATTR_TEMP(16, 15),
3889 #define THERMAL_ATTRS(X) \
3890 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
3892 static struct attribute *thermal_temp_input_attr[] = {
3893 THERMAL_ATTRS(8),
3894 THERMAL_ATTRS(9),
3895 THERMAL_ATTRS(10),
3896 THERMAL_ATTRS(11),
3897 THERMAL_ATTRS(12),
3898 THERMAL_ATTRS(13),
3899 THERMAL_ATTRS(14),
3900 THERMAL_ATTRS(15),
3901 THERMAL_ATTRS(0),
3902 THERMAL_ATTRS(1),
3903 THERMAL_ATTRS(2),
3904 THERMAL_ATTRS(3),
3905 THERMAL_ATTRS(4),
3906 THERMAL_ATTRS(5),
3907 THERMAL_ATTRS(6),
3908 THERMAL_ATTRS(7),
3909 NULL
3912 static const struct attribute_group thermal_temp_input16_group = {
3913 .attrs = thermal_temp_input_attr
3916 static const struct attribute_group thermal_temp_input8_group = {
3917 .attrs = &thermal_temp_input_attr[8]
3920 #undef THERMAL_SENSOR_ATTR_TEMP
3921 #undef THERMAL_ATTRS
3923 /* --------------------------------------------------------------------- */
3925 static int __init thermal_init(struct ibm_init_struct *iibm)
3927 u8 t, ta1, ta2;
3928 int i;
3929 int acpi_tmp7;
3930 int res;
3932 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
3934 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
3936 if (thinkpad_id.ec_model) {
3938 * Direct EC access mode: sensors at registers
3939 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
3940 * non-implemented, thermal sensors return 0x80 when
3941 * not available
3944 ta1 = ta2 = 0;
3945 for (i = 0; i < 8; i++) {
3946 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
3947 ta1 |= t;
3948 } else {
3949 ta1 = 0;
3950 break;
3952 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
3953 ta2 |= t;
3954 } else {
3955 ta1 = 0;
3956 break;
3959 if (ta1 == 0) {
3960 /* This is sheer paranoia, but we handle it anyway */
3961 if (acpi_tmp7) {
3962 printk(IBM_ERR
3963 "ThinkPad ACPI EC access misbehaving, "
3964 "falling back to ACPI TMPx access mode\n");
3965 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3966 } else {
3967 printk(IBM_ERR
3968 "ThinkPad ACPI EC access misbehaving, "
3969 "disabling thermal sensors access\n");
3970 thermal_read_mode = TPACPI_THERMAL_NONE;
3972 } else {
3973 thermal_read_mode =
3974 (ta2 != 0) ?
3975 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
3977 } else if (acpi_tmp7) {
3978 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
3979 /* 600e/x, 770e, 770x */
3980 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
3981 } else {
3982 /* Standard ACPI TMPx access, max 8 sensors */
3983 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
3985 } else {
3986 /* temperatures not supported on 570, G4x, R30, R31, R32 */
3987 thermal_read_mode = TPACPI_THERMAL_NONE;
3990 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
3991 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
3992 thermal_read_mode);
3994 switch(thermal_read_mode) {
3995 case TPACPI_THERMAL_TPEC_16:
3996 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
3997 &thermal_temp_input16_group);
3998 if (res)
3999 return res;
4000 break;
4001 case TPACPI_THERMAL_TPEC_8:
4002 case TPACPI_THERMAL_ACPI_TMP07:
4003 case TPACPI_THERMAL_ACPI_UPDT:
4004 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4005 &thermal_temp_input8_group);
4006 if (res)
4007 return res;
4008 break;
4009 case TPACPI_THERMAL_NONE:
4010 default:
4011 return 1;
4014 return 0;
4017 static void thermal_exit(void)
4019 switch(thermal_read_mode) {
4020 case TPACPI_THERMAL_TPEC_16:
4021 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4022 &thermal_temp_input16_group);
4023 break;
4024 case TPACPI_THERMAL_TPEC_8:
4025 case TPACPI_THERMAL_ACPI_TMP07:
4026 case TPACPI_THERMAL_ACPI_UPDT:
4027 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4028 &thermal_temp_input16_group);
4029 break;
4030 case TPACPI_THERMAL_NONE:
4031 default:
4032 break;
4036 /* idx is zero-based */
4037 static int thermal_get_sensor(int idx, s32 *value)
4039 int t;
4040 s8 tmp;
4041 char tmpi[5];
4043 t = TP_EC_THERMAL_TMP0;
4045 switch (thermal_read_mode) {
4046 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4047 case TPACPI_THERMAL_TPEC_16:
4048 if (idx >= 8 && idx <= 15) {
4049 t = TP_EC_THERMAL_TMP8;
4050 idx -= 8;
4052 /* fallthrough */
4053 #endif
4054 case TPACPI_THERMAL_TPEC_8:
4055 if (idx <= 7) {
4056 if (!acpi_ec_read(t + idx, &tmp))
4057 return -EIO;
4058 *value = tmp * 1000;
4059 return 0;
4061 break;
4063 case TPACPI_THERMAL_ACPI_UPDT:
4064 if (idx <= 7) {
4065 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4066 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4067 return -EIO;
4068 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4069 return -EIO;
4070 *value = (t - 2732) * 100;
4071 return 0;
4073 break;
4075 case TPACPI_THERMAL_ACPI_TMP07:
4076 if (idx <= 7) {
4077 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4078 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4079 return -EIO;
4080 if (t > 127 || t < -127)
4081 t = TP_EC_THERMAL_TMP_NA;
4082 *value = t * 1000;
4083 return 0;
4085 break;
4087 case TPACPI_THERMAL_NONE:
4088 default:
4089 return -ENOSYS;
4092 return -EINVAL;
4095 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4097 int res, i;
4098 int n;
4100 n = 8;
4101 i = 0;
4103 if (!s)
4104 return -EINVAL;
4106 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4107 n = 16;
4109 for(i = 0 ; i < n; i++) {
4110 res = thermal_get_sensor(i, &s->temp[i]);
4111 if (res)
4112 return res;
4115 return n;
4118 static int thermal_read(char *p)
4120 int len = 0;
4121 int n, i;
4122 struct ibm_thermal_sensors_struct t;
4124 n = thermal_get_sensors(&t);
4125 if (unlikely(n < 0))
4126 return n;
4128 len += sprintf(p + len, "temperatures:\t");
4130 if (n > 0) {
4131 for (i = 0; i < (n - 1); i++)
4132 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4133 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4134 } else
4135 len += sprintf(p + len, "not supported\n");
4137 return len;
4140 static struct ibm_struct thermal_driver_data = {
4141 .name = "thermal",
4142 .read = thermal_read,
4143 .exit = thermal_exit,
4146 /*************************************************************************
4147 * EC Dump subdriver
4150 static u8 ecdump_regs[256];
4152 static int ecdump_read(char *p)
4154 int len = 0;
4155 int i, j;
4156 u8 v;
4158 len += sprintf(p + len, "EC "
4159 " +00 +01 +02 +03 +04 +05 +06 +07"
4160 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4161 for (i = 0; i < 256; i += 16) {
4162 len += sprintf(p + len, "EC 0x%02x:", i);
4163 for (j = 0; j < 16; j++) {
4164 if (!acpi_ec_read(i + j, &v))
4165 break;
4166 if (v != ecdump_regs[i + j])
4167 len += sprintf(p + len, " *%02x", v);
4168 else
4169 len += sprintf(p + len, " %02x", v);
4170 ecdump_regs[i + j] = v;
4172 len += sprintf(p + len, "\n");
4173 if (j != 16)
4174 break;
4177 /* These are way too dangerous to advertise openly... */
4178 #if 0
4179 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4180 " (<offset> is 00-ff, <value> is 00-ff)\n");
4181 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4182 " (<offset> is 00-ff, <value> is 0-255)\n");
4183 #endif
4184 return len;
4187 static int ecdump_write(char *buf)
4189 char *cmd;
4190 int i, v;
4192 while ((cmd = next_cmd(&buf))) {
4193 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4194 /* i and v set */
4195 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4196 /* i and v set */
4197 } else
4198 return -EINVAL;
4199 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4200 if (!acpi_ec_write(i, v))
4201 return -EIO;
4202 } else
4203 return -EINVAL;
4206 return 0;
4209 static struct ibm_struct ecdump_driver_data = {
4210 .name = "ecdump",
4211 .read = ecdump_read,
4212 .write = ecdump_write,
4213 .flags.experimental = 1,
4216 /*************************************************************************
4217 * Backlight/brightness subdriver
4220 static struct backlight_device *ibm_backlight_device;
4222 static struct backlight_ops ibm_backlight_data = {
4223 .get_brightness = brightness_get,
4224 .update_status = brightness_update_status,
4227 static struct mutex brightness_mutex;
4229 static int __init tpacpi_query_bcll_levels(acpi_handle handle)
4231 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
4232 union acpi_object *obj;
4233 int rc;
4235 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
4236 obj = (union acpi_object *)buffer.pointer;
4237 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
4238 printk(IBM_ERR "Unknown BCLL data, "
4239 "please report this to %s\n", IBM_MAIL);
4240 rc = 0;
4241 } else {
4242 rc = obj->package.count;
4244 } else {
4245 return 0;
4248 kfree(buffer.pointer);
4249 return rc;
4252 static acpi_status __init brightness_find_bcll(acpi_handle handle, u32 lvl,
4253 void *context, void **rv)
4255 char name[ACPI_PATH_SEGMENT_LENGTH];
4256 struct acpi_buffer buffer = { sizeof(name), &name };
4258 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4259 !strncmp("BCLL", name, sizeof(name) - 1)) {
4260 if (tpacpi_query_bcll_levels(handle) == 16) {
4261 *rv = handle;
4262 return AE_CTRL_TERMINATE;
4263 } else {
4264 return AE_OK;
4266 } else {
4267 return AE_OK;
4271 static int __init brightness_check_levels(void)
4273 int status;
4274 void *found_node = NULL;
4276 if (!vid_handle) {
4277 IBM_ACPIHANDLE_INIT(vid);
4279 if (!vid_handle)
4280 return 0;
4282 /* Search for a BCLL package with 16 levels */
4283 status = acpi_walk_namespace(ACPI_TYPE_PACKAGE, vid_handle, 3,
4284 brightness_find_bcll, NULL, &found_node);
4286 return (ACPI_SUCCESS(status) && found_node != NULL);
4289 static acpi_status __init brightness_find_bcl(acpi_handle handle, u32 lvl,
4290 void *context, void **rv)
4292 char name[ACPI_PATH_SEGMENT_LENGTH];
4293 struct acpi_buffer buffer = { sizeof(name), &name };
4295 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
4296 !strncmp("_BCL", name, sizeof(name) - 1)) {
4297 *rv = handle;
4298 return AE_CTRL_TERMINATE;
4299 } else {
4300 return AE_OK;
4304 static int __init brightness_check_std_acpi_support(void)
4306 int status;
4307 void *found_node = NULL;
4309 if (!vid_handle) {
4310 IBM_ACPIHANDLE_INIT(vid);
4312 if (!vid_handle)
4313 return 0;
4315 /* Search for a _BCL method, but don't execute it */
4316 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
4317 brightness_find_bcl, NULL, &found_node);
4319 return (ACPI_SUCCESS(status) && found_node != NULL);
4322 static int __init brightness_init(struct ibm_init_struct *iibm)
4324 int b;
4326 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4328 mutex_init(&brightness_mutex);
4330 if (!brightness_enable) {
4331 dbg_printk(TPACPI_DBG_INIT,
4332 "brightness support disabled by module parameter\n");
4333 return 1;
4334 } else if (brightness_enable > 1) {
4335 if (brightness_check_std_acpi_support()) {
4336 printk(IBM_NOTICE
4337 "standard ACPI backlight interface available, not loading native one...\n");
4338 return 1;
4342 if (!brightness_mode) {
4343 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4344 brightness_mode = 2;
4345 else
4346 brightness_mode = 3;
4348 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4349 brightness_mode);
4352 if (brightness_mode > 3)
4353 return -EINVAL;
4355 tp_features.bright_16levels =
4356 thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO &&
4357 brightness_check_levels();
4359 b = brightness_get(NULL);
4360 if (b < 0)
4361 return 1;
4363 if (tp_features.bright_16levels)
4364 printk(IBM_INFO "detected a 16-level brightness capable ThinkPad\n");
4366 ibm_backlight_device = backlight_device_register(
4367 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4368 &ibm_backlight_data);
4369 if (IS_ERR(ibm_backlight_device)) {
4370 printk(IBM_ERR "Could not register backlight device\n");
4371 return PTR_ERR(ibm_backlight_device);
4373 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4375 ibm_backlight_device->props.max_brightness =
4376 (tp_features.bright_16levels)? 15 : 7;
4377 ibm_backlight_device->props.brightness = b;
4378 backlight_update_status(ibm_backlight_device);
4380 return 0;
4383 static void brightness_exit(void)
4385 if (ibm_backlight_device) {
4386 vdbg_printk(TPACPI_DBG_EXIT,
4387 "calling backlight_device_unregister()\n");
4388 backlight_device_unregister(ibm_backlight_device);
4389 ibm_backlight_device = NULL;
4393 static int brightness_update_status(struct backlight_device *bd)
4395 /* it is the backlight class's job (caller) to handle
4396 * EINTR and other errors properly */
4397 return brightness_set(
4398 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4399 bd->props.power == FB_BLANK_UNBLANK) ?
4400 bd->props.brightness : 0);
4404 * ThinkPads can read brightness from two places: EC 0x31, or
4405 * CMOS NVRAM byte 0x5E, bits 0-3.
4407 static int brightness_get(struct backlight_device *bd)
4409 u8 lec = 0, lcmos = 0, level = 0;
4411 if (brightness_mode & 1) {
4412 if (!acpi_ec_read(brightness_offset, &lec))
4413 return -EIO;
4414 lec &= (tp_features.bright_16levels)? 0x0f : 0x07;
4415 level = lec;
4417 if (brightness_mode & 2) {
4418 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4419 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4420 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4421 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4422 level = lcmos;
4425 if (brightness_mode == 3 && lec != lcmos) {
4426 printk(IBM_ERR
4427 "CMOS NVRAM (%u) and EC (%u) do not agree "
4428 "on display brightness level\n",
4429 (unsigned int) lcmos,
4430 (unsigned int) lec);
4431 return -EIO;
4434 return level;
4437 /* May return EINTR which can always be mapped to ERESTARTSYS */
4438 static int brightness_set(int value)
4440 int cmos_cmd, inc, i, res;
4441 int current_value;
4443 if (value > ((tp_features.bright_16levels)? 15 : 7))
4444 return -EINVAL;
4446 res = mutex_lock_interruptible(&brightness_mutex);
4447 if (res < 0)
4448 return res;
4450 current_value = brightness_get(NULL);
4451 if (current_value < 0) {
4452 res = current_value;
4453 goto errout;
4456 cmos_cmd = value > current_value ?
4457 TP_CMOS_BRIGHTNESS_UP :
4458 TP_CMOS_BRIGHTNESS_DOWN;
4459 inc = (value > current_value)? 1 : -1;
4461 res = 0;
4462 for (i = current_value; i != value; i += inc) {
4463 if ((brightness_mode & 2) &&
4464 issue_thinkpad_cmos_command(cmos_cmd)) {
4465 res = -EIO;
4466 goto errout;
4468 if ((brightness_mode & 1) &&
4469 !acpi_ec_write(brightness_offset, i + inc)) {
4470 res = -EIO;
4471 goto errout;;
4475 errout:
4476 mutex_unlock(&brightness_mutex);
4477 return res;
4480 static int brightness_read(char *p)
4482 int len = 0;
4483 int level;
4485 if ((level = brightness_get(NULL)) < 0) {
4486 len += sprintf(p + len, "level:\t\tunreadable\n");
4487 } else {
4488 len += sprintf(p + len, "level:\t\t%d\n", level);
4489 len += sprintf(p + len, "commands:\tup, down\n");
4490 len += sprintf(p + len, "commands:\tlevel <level>"
4491 " (<level> is 0-%d)\n",
4492 (tp_features.bright_16levels) ? 15 : 7);
4495 return len;
4498 static int brightness_write(char *buf)
4500 int level;
4501 int rc;
4502 char *cmd;
4503 int max_level = (tp_features.bright_16levels) ? 15 : 7;
4505 level = brightness_get(NULL);
4506 if (level < 0)
4507 return level;
4509 while ((cmd = next_cmd(&buf))) {
4510 if (strlencmp(cmd, "up") == 0) {
4511 if (level < max_level)
4512 level++;
4513 } else if (strlencmp(cmd, "down") == 0) {
4514 if (level > 0)
4515 level--;
4516 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4517 level >= 0 && level <= max_level) {
4518 /* new level set */
4519 } else
4520 return -EINVAL;
4524 * Now we know what the final level should be, so we try to set it.
4525 * Doing it this way makes the syscall restartable in case of EINTR
4527 rc = brightness_set(level);
4528 return (rc == -EINTR)? ERESTARTSYS : rc;
4531 static struct ibm_struct brightness_driver_data = {
4532 .name = "brightness",
4533 .read = brightness_read,
4534 .write = brightness_write,
4535 .exit = brightness_exit,
4538 /*************************************************************************
4539 * Volume subdriver
4542 static int volume_read(char *p)
4544 int len = 0;
4545 u8 level;
4547 if (!acpi_ec_read(volume_offset, &level)) {
4548 len += sprintf(p + len, "level:\t\tunreadable\n");
4549 } else {
4550 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4551 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4552 len += sprintf(p + len, "commands:\tup, down, mute\n");
4553 len += sprintf(p + len, "commands:\tlevel <level>"
4554 " (<level> is 0-15)\n");
4557 return len;
4560 static int volume_write(char *buf)
4562 int cmos_cmd, inc, i;
4563 u8 level, mute;
4564 int new_level, new_mute;
4565 char *cmd;
4567 while ((cmd = next_cmd(&buf))) {
4568 if (!acpi_ec_read(volume_offset, &level))
4569 return -EIO;
4570 new_mute = mute = level & 0x40;
4571 new_level = level = level & 0xf;
4573 if (strlencmp(cmd, "up") == 0) {
4574 if (mute)
4575 new_mute = 0;
4576 else
4577 new_level = level == 15 ? 15 : level + 1;
4578 } else if (strlencmp(cmd, "down") == 0) {
4579 if (mute)
4580 new_mute = 0;
4581 else
4582 new_level = level == 0 ? 0 : level - 1;
4583 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4584 new_level >= 0 && new_level <= 15) {
4585 /* new_level set */
4586 } else if (strlencmp(cmd, "mute") == 0) {
4587 new_mute = 0x40;
4588 } else
4589 return -EINVAL;
4591 if (new_level != level) { /* mute doesn't change */
4592 cmos_cmd = new_level > level ? TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
4593 inc = new_level > level ? 1 : -1;
4595 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
4596 !acpi_ec_write(volume_offset, level)))
4597 return -EIO;
4599 for (i = level; i != new_level; i += inc)
4600 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4601 !acpi_ec_write(volume_offset, i + inc))
4602 return -EIO;
4604 if (mute && (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4605 !acpi_ec_write(volume_offset,
4606 new_level + mute)))
4607 return -EIO;
4610 if (new_mute != mute) { /* level doesn't change */
4611 cmos_cmd = new_mute ? TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
4613 if (issue_thinkpad_cmos_command(cmos_cmd) ||
4614 !acpi_ec_write(volume_offset, level + new_mute))
4615 return -EIO;
4619 return 0;
4622 static struct ibm_struct volume_driver_data = {
4623 .name = "volume",
4624 .read = volume_read,
4625 .write = volume_write,
4628 /*************************************************************************
4629 * Fan subdriver
4633 * FAN ACCESS MODES
4635 * TPACPI_FAN_RD_ACPI_GFAN:
4636 * ACPI GFAN method: returns fan level
4638 * see TPACPI_FAN_WR_ACPI_SFAN
4639 * EC 0x2f (HFSP) not available if GFAN exists
4641 * TPACPI_FAN_WR_ACPI_SFAN:
4642 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4644 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4645 * it for writing.
4647 * TPACPI_FAN_WR_TPEC:
4648 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4649 * Supported on almost all ThinkPads
4651 * Fan speed changes of any sort (including those caused by the
4652 * disengaged mode) are usually done slowly by the firmware as the
4653 * maximum ammount of fan duty cycle change per second seems to be
4654 * limited.
4656 * Reading is not available if GFAN exists.
4657 * Writing is not available if SFAN exists.
4659 * Bits
4660 * 7 automatic mode engaged;
4661 * (default operation mode of the ThinkPad)
4662 * fan level is ignored in this mode.
4663 * 6 full speed mode (takes precedence over bit 7);
4664 * not available on all thinkpads. May disable
4665 * the tachometer while the fan controller ramps up
4666 * the speed (which can take up to a few *minutes*).
4667 * Speeds up fan to 100% duty-cycle, which is far above
4668 * the standard RPM levels. It is not impossible that
4669 * it could cause hardware damage.
4670 * 5-3 unused in some models. Extra bits for fan level
4671 * in others, but still useless as all values above
4672 * 7 map to the same speed as level 7 in these models.
4673 * 2-0 fan level (0..7 usually)
4674 * 0x00 = stop
4675 * 0x07 = max (set when temperatures critical)
4676 * Some ThinkPads may have other levels, see
4677 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
4679 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
4680 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
4681 * does so, its initial value is meaningless (0x07).
4683 * For firmware bugs, refer to:
4684 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4686 * ----
4688 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
4689 * Main fan tachometer reading (in RPM)
4691 * This register is present on all ThinkPads with a new-style EC, and
4692 * it is known not to be present on the A21m/e, and T22, as there is
4693 * something else in offset 0x84 according to the ACPI DSDT. Other
4694 * ThinkPads from this same time period (and earlier) probably lack the
4695 * tachometer as well.
4697 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
4698 * was never fixed by IBM to report the EC firmware version string
4699 * probably support the tachometer (like the early X models), so
4700 * detecting it is quite hard. We need more data to know for sure.
4702 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
4703 * might result.
4705 * FIRMWARE BUG: may go stale while the EC is switching to full speed
4706 * mode.
4708 * For firmware bugs, refer to:
4709 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
4711 * TPACPI_FAN_WR_ACPI_FANS:
4712 * ThinkPad X31, X40, X41. Not available in the X60.
4714 * FANS ACPI handle: takes three arguments: low speed, medium speed,
4715 * high speed. ACPI DSDT seems to map these three speeds to levels
4716 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
4717 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
4719 * The speeds are stored on handles
4720 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
4722 * There are three default speed sets, acessible as handles:
4723 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
4725 * ACPI DSDT switches which set is in use depending on various
4726 * factors.
4728 * TPACPI_FAN_WR_TPEC is also available and should be used to
4729 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
4730 * but the ACPI tables just mention level 7.
4733 static enum fan_status_access_mode fan_status_access_mode;
4734 static enum fan_control_access_mode fan_control_access_mode;
4735 static enum fan_control_commands fan_control_commands;
4737 static u8 fan_control_initial_status;
4738 static u8 fan_control_desired_level;
4740 static void fan_watchdog_fire(struct work_struct *ignored);
4741 static int fan_watchdog_maxinterval;
4742 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
4744 IBM_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
4745 IBM_HANDLE(gfan, ec, "GFAN", /* 570 */
4746 "\\FSPD", /* 600e/x, 770e, 770x */
4747 ); /* all others */
4748 IBM_HANDLE(sfan, ec, "SFAN", /* 570 */
4749 "JFNS", /* 770x-JL */
4750 ); /* all others */
4753 * SYSFS fan layout: hwmon compatible (device)
4755 * pwm*_enable:
4756 * 0: "disengaged" mode
4757 * 1: manual mode
4758 * 2: native EC "auto" mode (recommended, hardware default)
4760 * pwm*: set speed in manual mode, ignored otherwise.
4761 * 0 is level 0; 255 is level 7. Intermediate points done with linear
4762 * interpolation.
4764 * fan*_input: tachometer reading, RPM
4767 * SYSFS fan layout: extensions
4769 * fan_watchdog (driver):
4770 * fan watchdog interval in seconds, 0 disables (default), max 120
4773 /* sysfs fan pwm1_enable ----------------------------------------------- */
4774 static ssize_t fan_pwm1_enable_show(struct device *dev,
4775 struct device_attribute *attr,
4776 char *buf)
4778 int res, mode;
4779 u8 status;
4781 res = fan_get_status_safe(&status);
4782 if (res)
4783 return res;
4785 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4786 if (status != fan_control_initial_status) {
4787 tp_features.fan_ctrl_status_undef = 0;
4788 } else {
4789 /* Return most likely status. In fact, it
4790 * might be the only possible status */
4791 status = TP_EC_FAN_AUTO;
4795 if (status & TP_EC_FAN_FULLSPEED) {
4796 mode = 0;
4797 } else if (status & TP_EC_FAN_AUTO) {
4798 mode = 2;
4799 } else
4800 mode = 1;
4802 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
4805 static ssize_t fan_pwm1_enable_store(struct device *dev,
4806 struct device_attribute *attr,
4807 const char *buf, size_t count)
4809 unsigned long t;
4810 int res, level;
4812 if (parse_strtoul(buf, 2, &t))
4813 return -EINVAL;
4815 switch (t) {
4816 case 0:
4817 level = TP_EC_FAN_FULLSPEED;
4818 break;
4819 case 1:
4820 level = TPACPI_FAN_LAST_LEVEL;
4821 break;
4822 case 2:
4823 level = TP_EC_FAN_AUTO;
4824 break;
4825 case 3:
4826 /* reserved for software-controlled auto mode */
4827 return -ENOSYS;
4828 default:
4829 return -EINVAL;
4832 res = fan_set_level_safe(level);
4833 if (res == -ENXIO)
4834 return -EINVAL;
4835 else if (res < 0)
4836 return res;
4838 fan_watchdog_reset();
4840 return count;
4843 static struct device_attribute dev_attr_fan_pwm1_enable =
4844 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
4845 fan_pwm1_enable_show, fan_pwm1_enable_store);
4847 /* sysfs fan pwm1 ------------------------------------------------------ */
4848 static ssize_t fan_pwm1_show(struct device *dev,
4849 struct device_attribute *attr,
4850 char *buf)
4852 int res;
4853 u8 status;
4855 res = fan_get_status_safe(&status);
4856 if (res)
4857 return res;
4859 if (unlikely(tp_features.fan_ctrl_status_undef)) {
4860 if (status != fan_control_initial_status) {
4861 tp_features.fan_ctrl_status_undef = 0;
4862 } else {
4863 status = TP_EC_FAN_AUTO;
4867 if ((status &
4868 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
4869 status = fan_control_desired_level;
4871 if (status > 7)
4872 status = 7;
4874 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
4877 static ssize_t fan_pwm1_store(struct device *dev,
4878 struct device_attribute *attr,
4879 const char *buf, size_t count)
4881 unsigned long s;
4882 int rc;
4883 u8 status, newlevel;
4885 if (parse_strtoul(buf, 255, &s))
4886 return -EINVAL;
4888 /* scale down from 0-255 to 0-7 */
4889 newlevel = (s >> 5) & 0x07;
4891 if (mutex_lock_interruptible(&fan_mutex))
4892 return -ERESTARTSYS;
4894 rc = fan_get_status(&status);
4895 if (!rc && (status &
4896 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
4897 rc = fan_set_level(newlevel);
4898 if (rc == -ENXIO)
4899 rc = -EINVAL;
4900 else if (!rc) {
4901 fan_update_desired_level(newlevel);
4902 fan_watchdog_reset();
4906 mutex_unlock(&fan_mutex);
4907 return (rc)? rc : count;
4910 static struct device_attribute dev_attr_fan_pwm1 =
4911 __ATTR(pwm1, S_IWUSR | S_IRUGO,
4912 fan_pwm1_show, fan_pwm1_store);
4914 /* sysfs fan fan1_input ------------------------------------------------ */
4915 static ssize_t fan_fan1_input_show(struct device *dev,
4916 struct device_attribute *attr,
4917 char *buf)
4919 int res;
4920 unsigned int speed;
4922 res = fan_get_speed(&speed);
4923 if (res < 0)
4924 return res;
4926 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
4929 static struct device_attribute dev_attr_fan_fan1_input =
4930 __ATTR(fan1_input, S_IRUGO,
4931 fan_fan1_input_show, NULL);
4933 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
4934 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
4935 char *buf)
4937 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
4940 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
4941 const char *buf, size_t count)
4943 unsigned long t;
4945 if (parse_strtoul(buf, 120, &t))
4946 return -EINVAL;
4948 if (!fan_control_allowed)
4949 return -EPERM;
4951 fan_watchdog_maxinterval = t;
4952 fan_watchdog_reset();
4954 return count;
4957 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
4958 fan_fan_watchdog_show, fan_fan_watchdog_store);
4960 /* --------------------------------------------------------------------- */
4961 static struct attribute *fan_attributes[] = {
4962 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
4963 &dev_attr_fan_fan1_input.attr,
4964 NULL
4967 static const struct attribute_group fan_attr_group = {
4968 .attrs = fan_attributes,
4971 static int __init fan_init(struct ibm_init_struct *iibm)
4973 int rc;
4975 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
4977 mutex_init(&fan_mutex);
4978 fan_status_access_mode = TPACPI_FAN_NONE;
4979 fan_control_access_mode = TPACPI_FAN_WR_NONE;
4980 fan_control_commands = 0;
4981 fan_watchdog_maxinterval = 0;
4982 tp_features.fan_ctrl_status_undef = 0;
4983 fan_control_desired_level = 7;
4985 IBM_ACPIHANDLE_INIT(fans);
4986 IBM_ACPIHANDLE_INIT(gfan);
4987 IBM_ACPIHANDLE_INIT(sfan);
4989 if (gfan_handle) {
4990 /* 570, 600e/x, 770e, 770x */
4991 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
4992 } else {
4993 /* all other ThinkPads: note that even old-style
4994 * ThinkPad ECs supports the fan control register */
4995 if (likely(acpi_ec_read(fan_status_offset,
4996 &fan_control_initial_status))) {
4997 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
4999 /* In some ThinkPads, neither the EC nor the ACPI
5000 * DSDT initialize the fan status, and it ends up
5001 * being set to 0x07 when it *could* be either
5002 * 0x07 or 0x80.
5004 * Enable for TP-1Y (T43), TP-78 (R51e),
5005 * TP-76 (R52), TP-70 (T43, R52), which are known
5006 * to be buggy. */
5007 if (fan_control_initial_status == 0x07) {
5008 switch (thinkpad_id.ec_model) {
5009 case 0x5931: /* TP-1Y */
5010 case 0x3837: /* TP-78 */
5011 case 0x3637: /* TP-76 */
5012 case 0x3037: /* TP-70 */
5013 printk(IBM_NOTICE
5014 "fan_init: initial fan status is "
5015 "unknown, assuming it is in auto "
5016 "mode\n");
5017 tp_features.fan_ctrl_status_undef = 1;
5021 } else {
5022 printk(IBM_ERR
5023 "ThinkPad ACPI EC access misbehaving, "
5024 "fan status and control unavailable\n");
5025 return 1;
5029 if (sfan_handle) {
5030 /* 570, 770x-JL */
5031 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5032 fan_control_commands |=
5033 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5034 } else {
5035 if (!gfan_handle) {
5036 /* gfan without sfan means no fan control */
5037 /* all other models implement TP EC 0x2f control */
5039 if (fans_handle) {
5040 /* X31, X40, X41 */
5041 fan_control_access_mode =
5042 TPACPI_FAN_WR_ACPI_FANS;
5043 fan_control_commands |=
5044 TPACPI_FAN_CMD_SPEED |
5045 TPACPI_FAN_CMD_LEVEL |
5046 TPACPI_FAN_CMD_ENABLE;
5047 } else {
5048 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5049 fan_control_commands |=
5050 TPACPI_FAN_CMD_LEVEL |
5051 TPACPI_FAN_CMD_ENABLE;
5056 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5057 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5058 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5059 fan_status_access_mode, fan_control_access_mode);
5061 /* fan control master switch */
5062 if (!fan_control_allowed) {
5063 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5064 fan_control_commands = 0;
5065 dbg_printk(TPACPI_DBG_INIT,
5066 "fan control features disabled by parameter\n");
5069 /* update fan_control_desired_level */
5070 if (fan_status_access_mode != TPACPI_FAN_NONE)
5071 fan_get_status_safe(NULL);
5073 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5074 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5075 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5076 &fan_attr_group);
5077 if (!(rc < 0))
5078 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5079 &driver_attr_fan_watchdog);
5080 if (rc < 0)
5081 return rc;
5082 return 0;
5083 } else
5084 return 1;
5088 * Call with fan_mutex held
5090 static void fan_update_desired_level(u8 status)
5092 if ((status &
5093 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5094 if (status > 7)
5095 fan_control_desired_level = 7;
5096 else
5097 fan_control_desired_level = status;
5101 static int fan_get_status(u8 *status)
5103 u8 s;
5105 /* TODO:
5106 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5108 switch (fan_status_access_mode) {
5109 case TPACPI_FAN_RD_ACPI_GFAN:
5110 /* 570, 600e/x, 770e, 770x */
5112 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5113 return -EIO;
5115 if (likely(status))
5116 *status = s & 0x07;
5118 break;
5120 case TPACPI_FAN_RD_TPEC:
5121 /* all except 570, 600e/x, 770e, 770x */
5122 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5123 return -EIO;
5125 if (likely(status))
5126 *status = s;
5128 break;
5130 default:
5131 return -ENXIO;
5134 return 0;
5137 static int fan_get_status_safe(u8 *status)
5139 int rc;
5140 u8 s;
5142 if (mutex_lock_interruptible(&fan_mutex))
5143 return -ERESTARTSYS;
5144 rc = fan_get_status(&s);
5145 if (!rc)
5146 fan_update_desired_level(s);
5147 mutex_unlock(&fan_mutex);
5149 if (status)
5150 *status = s;
5152 return rc;
5155 static void fan_exit(void)
5157 vdbg_printk(TPACPI_DBG_EXIT, "cancelling any pending fan watchdog tasks\n");
5159 /* FIXME: can we really do this unconditionally? */
5160 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5161 driver_remove_file(&tpacpi_hwmon_pdriver.driver, &driver_attr_fan_watchdog);
5163 cancel_delayed_work(&fan_watchdog_task);
5164 flush_scheduled_work();
5167 static int fan_get_speed(unsigned int *speed)
5169 u8 hi, lo;
5171 switch (fan_status_access_mode) {
5172 case TPACPI_FAN_RD_TPEC:
5173 /* all except 570, 600e/x, 770e, 770x */
5174 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5175 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5176 return -EIO;
5178 if (likely(speed))
5179 *speed = (hi << 8) | lo;
5181 break;
5183 default:
5184 return -ENXIO;
5187 return 0;
5190 static void fan_watchdog_fire(struct work_struct *ignored)
5192 int rc;
5194 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5195 return;
5197 printk(IBM_NOTICE "fan watchdog: enabling fan\n");
5198 rc = fan_set_enable();
5199 if (rc < 0) {
5200 printk(IBM_ERR "fan watchdog: error %d while enabling fan, "
5201 "will try again later...\n", -rc);
5202 /* reschedule for later */
5203 fan_watchdog_reset();
5207 static void fan_watchdog_reset(void)
5209 static int fan_watchdog_active;
5211 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5212 return;
5214 if (fan_watchdog_active)
5215 cancel_delayed_work(&fan_watchdog_task);
5217 if (fan_watchdog_maxinterval > 0 &&
5218 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5219 fan_watchdog_active = 1;
5220 if (!schedule_delayed_work(&fan_watchdog_task,
5221 msecs_to_jiffies(fan_watchdog_maxinterval
5222 * 1000))) {
5223 printk(IBM_ERR "failed to schedule the fan watchdog, "
5224 "watchdog will not trigger\n");
5226 } else
5227 fan_watchdog_active = 0;
5230 static int fan_set_level(int level)
5232 if (!fan_control_allowed)
5233 return -EPERM;
5235 switch (fan_control_access_mode) {
5236 case TPACPI_FAN_WR_ACPI_SFAN:
5237 if (level >= 0 && level <= 7) {
5238 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5239 return -EIO;
5240 } else
5241 return -EINVAL;
5242 break;
5244 case TPACPI_FAN_WR_ACPI_FANS:
5245 case TPACPI_FAN_WR_TPEC:
5246 if ((level != TP_EC_FAN_AUTO) &&
5247 (level != TP_EC_FAN_FULLSPEED) &&
5248 ((level < 0) || (level > 7)))
5249 return -EINVAL;
5251 /* safety net should the EC not support AUTO
5252 * or FULLSPEED mode bits and just ignore them */
5253 if (level & TP_EC_FAN_FULLSPEED)
5254 level |= 7; /* safety min speed 7 */
5255 else if (level & TP_EC_FAN_FULLSPEED)
5256 level |= 4; /* safety min speed 4 */
5258 if (!acpi_ec_write(fan_status_offset, level))
5259 return -EIO;
5260 else
5261 tp_features.fan_ctrl_status_undef = 0;
5262 break;
5264 default:
5265 return -ENXIO;
5267 return 0;
5270 static int fan_set_level_safe(int level)
5272 int rc;
5274 if (!fan_control_allowed)
5275 return -EPERM;
5277 if (mutex_lock_interruptible(&fan_mutex))
5278 return -ERESTARTSYS;
5280 if (level == TPACPI_FAN_LAST_LEVEL)
5281 level = fan_control_desired_level;
5283 rc = fan_set_level(level);
5284 if (!rc)
5285 fan_update_desired_level(level);
5287 mutex_unlock(&fan_mutex);
5288 return rc;
5291 static int fan_set_enable(void)
5293 u8 s;
5294 int rc;
5296 if (!fan_control_allowed)
5297 return -EPERM;
5299 if (mutex_lock_interruptible(&fan_mutex))
5300 return -ERESTARTSYS;
5302 switch (fan_control_access_mode) {
5303 case TPACPI_FAN_WR_ACPI_FANS:
5304 case TPACPI_FAN_WR_TPEC:
5305 rc = fan_get_status(&s);
5306 if (rc < 0)
5307 break;
5309 /* Don't go out of emergency fan mode */
5310 if (s != 7) {
5311 s &= 0x07;
5312 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5315 if (!acpi_ec_write(fan_status_offset, s))
5316 rc = -EIO;
5317 else {
5318 tp_features.fan_ctrl_status_undef = 0;
5319 rc = 0;
5321 break;
5323 case TPACPI_FAN_WR_ACPI_SFAN:
5324 rc = fan_get_status(&s);
5325 if (rc < 0)
5326 break;
5328 s &= 0x07;
5330 /* Set fan to at least level 4 */
5331 s |= 4;
5333 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5334 rc= -EIO;
5335 else
5336 rc = 0;
5337 break;
5339 default:
5340 rc = -ENXIO;
5343 mutex_unlock(&fan_mutex);
5344 return rc;
5347 static int fan_set_disable(void)
5349 int rc;
5351 if (!fan_control_allowed)
5352 return -EPERM;
5354 if (mutex_lock_interruptible(&fan_mutex))
5355 return -ERESTARTSYS;
5357 rc = 0;
5358 switch (fan_control_access_mode) {
5359 case TPACPI_FAN_WR_ACPI_FANS:
5360 case TPACPI_FAN_WR_TPEC:
5361 if (!acpi_ec_write(fan_status_offset, 0x00))
5362 rc = -EIO;
5363 else {
5364 fan_control_desired_level = 0;
5365 tp_features.fan_ctrl_status_undef = 0;
5367 break;
5369 case TPACPI_FAN_WR_ACPI_SFAN:
5370 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5371 rc = -EIO;
5372 else
5373 fan_control_desired_level = 0;
5374 break;
5376 default:
5377 rc = -ENXIO;
5381 mutex_unlock(&fan_mutex);
5382 return rc;
5385 static int fan_set_speed(int speed)
5387 int rc;
5389 if (!fan_control_allowed)
5390 return -EPERM;
5392 if (mutex_lock_interruptible(&fan_mutex))
5393 return -ERESTARTSYS;
5395 rc = 0;
5396 switch (fan_control_access_mode) {
5397 case TPACPI_FAN_WR_ACPI_FANS:
5398 if (speed >= 0 && speed <= 65535) {
5399 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5400 speed, speed, speed))
5401 rc = -EIO;
5402 } else
5403 rc = -EINVAL;
5404 break;
5406 default:
5407 rc = -ENXIO;
5410 mutex_unlock(&fan_mutex);
5411 return rc;
5414 static int fan_read(char *p)
5416 int len = 0;
5417 int rc;
5418 u8 status;
5419 unsigned int speed = 0;
5421 switch (fan_status_access_mode) {
5422 case TPACPI_FAN_RD_ACPI_GFAN:
5423 /* 570, 600e/x, 770e, 770x */
5424 if ((rc = fan_get_status_safe(&status)) < 0)
5425 return rc;
5427 len += sprintf(p + len, "status:\t\t%s\n"
5428 "level:\t\t%d\n",
5429 (status != 0) ? "enabled" : "disabled", status);
5430 break;
5432 case TPACPI_FAN_RD_TPEC:
5433 /* all except 570, 600e/x, 770e, 770x */
5434 if ((rc = fan_get_status_safe(&status)) < 0)
5435 return rc;
5437 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5438 if (status != fan_control_initial_status)
5439 tp_features.fan_ctrl_status_undef = 0;
5440 else
5441 /* Return most likely status. In fact, it
5442 * might be the only possible status */
5443 status = TP_EC_FAN_AUTO;
5446 len += sprintf(p + len, "status:\t\t%s\n",
5447 (status != 0) ? "enabled" : "disabled");
5449 if ((rc = fan_get_speed(&speed)) < 0)
5450 return rc;
5452 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5454 if (status & TP_EC_FAN_FULLSPEED)
5455 /* Disengaged mode takes precedence */
5456 len += sprintf(p + len, "level:\t\tdisengaged\n");
5457 else if (status & TP_EC_FAN_AUTO)
5458 len += sprintf(p + len, "level:\t\tauto\n");
5459 else
5460 len += sprintf(p + len, "level:\t\t%d\n", status);
5461 break;
5463 case TPACPI_FAN_NONE:
5464 default:
5465 len += sprintf(p + len, "status:\t\tnot supported\n");
5468 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
5469 len += sprintf(p + len, "commands:\tlevel <level>");
5471 switch (fan_control_access_mode) {
5472 case TPACPI_FAN_WR_ACPI_SFAN:
5473 len += sprintf(p + len, " (<level> is 0-7)\n");
5474 break;
5476 default:
5477 len += sprintf(p + len, " (<level> is 0-7, "
5478 "auto, disengaged, full-speed)\n");
5479 break;
5483 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
5484 len += sprintf(p + len, "commands:\tenable, disable\n"
5485 "commands:\twatchdog <timeout> (<timeout> is 0 (off), "
5486 "1-120 (seconds))\n");
5488 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
5489 len += sprintf(p + len, "commands:\tspeed <speed>"
5490 " (<speed> is 0-65535)\n");
5492 return len;
5495 static int fan_write_cmd_level(const char *cmd, int *rc)
5497 int level;
5499 if (strlencmp(cmd, "level auto") == 0)
5500 level = TP_EC_FAN_AUTO;
5501 else if ((strlencmp(cmd, "level disengaged") == 0) |
5502 (strlencmp(cmd, "level full-speed") == 0))
5503 level = TP_EC_FAN_FULLSPEED;
5504 else if (sscanf(cmd, "level %d", &level) != 1)
5505 return 0;
5507 if ((*rc = fan_set_level_safe(level)) == -ENXIO)
5508 printk(IBM_ERR "level command accepted for unsupported "
5509 "access mode %d", fan_control_access_mode);
5511 return 1;
5514 static int fan_write_cmd_enable(const char *cmd, int *rc)
5516 if (strlencmp(cmd, "enable") != 0)
5517 return 0;
5519 if ((*rc = fan_set_enable()) == -ENXIO)
5520 printk(IBM_ERR "enable command accepted for unsupported "
5521 "access mode %d", fan_control_access_mode);
5523 return 1;
5526 static int fan_write_cmd_disable(const char *cmd, int *rc)
5528 if (strlencmp(cmd, "disable") != 0)
5529 return 0;
5531 if ((*rc = fan_set_disable()) == -ENXIO)
5532 printk(IBM_ERR "disable command accepted for unsupported "
5533 "access mode %d", fan_control_access_mode);
5535 return 1;
5538 static int fan_write_cmd_speed(const char *cmd, int *rc)
5540 int speed;
5542 /* TODO:
5543 * Support speed <low> <medium> <high> ? */
5545 if (sscanf(cmd, "speed %d", &speed) != 1)
5546 return 0;
5548 if ((*rc = fan_set_speed(speed)) == -ENXIO)
5549 printk(IBM_ERR "speed command accepted for unsupported "
5550 "access mode %d", fan_control_access_mode);
5552 return 1;
5555 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5557 int interval;
5559 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5560 return 0;
5562 if (interval < 0 || interval > 120)
5563 *rc = -EINVAL;
5564 else
5565 fan_watchdog_maxinterval = interval;
5567 return 1;
5570 static int fan_write(char *buf)
5572 char *cmd;
5573 int rc = 0;
5575 while (!rc && (cmd = next_cmd(&buf))) {
5576 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
5577 fan_write_cmd_level(cmd, &rc)) &&
5578 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
5579 (fan_write_cmd_enable(cmd, &rc) ||
5580 fan_write_cmd_disable(cmd, &rc) ||
5581 fan_write_cmd_watchdog(cmd, &rc))) &&
5582 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
5583 fan_write_cmd_speed(cmd, &rc))
5585 rc = -EINVAL;
5586 else if (!rc)
5587 fan_watchdog_reset();
5590 return rc;
5593 static struct ibm_struct fan_driver_data = {
5594 .name = "fan",
5595 .read = fan_read,
5596 .write = fan_write,
5597 .exit = fan_exit,
5600 /****************************************************************************
5601 ****************************************************************************
5603 * Infrastructure
5605 ****************************************************************************
5606 ****************************************************************************/
5608 /* sysfs name ---------------------------------------------------------- */
5609 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5610 struct device_attribute *attr,
5611 char *buf)
5613 return snprintf(buf, PAGE_SIZE, "%s\n", IBM_NAME);
5616 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
5617 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
5619 /* --------------------------------------------------------------------- */
5621 /* /proc support */
5622 static struct proc_dir_entry *proc_dir;
5624 /* Subdriver registry */
5625 static LIST_HEAD(tpacpi_all_drivers);
5629 * Module and infrastructure proble, init and exit handling
5632 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
5633 static const char * __init str_supported(int is_supported)
5635 static char text_unsupported[] __initdata = "not supported";
5637 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
5639 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
5641 static int __init ibm_init(struct ibm_init_struct *iibm)
5643 int ret;
5644 struct ibm_struct *ibm = iibm->data;
5645 struct proc_dir_entry *entry;
5647 BUG_ON(ibm == NULL);
5649 INIT_LIST_HEAD(&ibm->all_drivers);
5651 if (ibm->flags.experimental && !experimental)
5652 return 0;
5654 dbg_printk(TPACPI_DBG_INIT,
5655 "probing for %s\n", ibm->name);
5657 if (iibm->init) {
5658 ret = iibm->init(iibm);
5659 if (ret > 0)
5660 return 0; /* probe failed */
5661 if (ret)
5662 return ret;
5664 ibm->flags.init_called = 1;
5667 if (ibm->acpi) {
5668 if (ibm->acpi->hid) {
5669 ret = register_tpacpi_subdriver(ibm);
5670 if (ret)
5671 goto err_out;
5674 if (ibm->acpi->notify) {
5675 ret = setup_acpi_notify(ibm);
5676 if (ret == -ENODEV) {
5677 printk(IBM_NOTICE "disabling subdriver %s\n",
5678 ibm->name);
5679 ret = 0;
5680 goto err_out;
5682 if (ret < 0)
5683 goto err_out;
5687 dbg_printk(TPACPI_DBG_INIT,
5688 "%s installed\n", ibm->name);
5690 if (ibm->read) {
5691 entry = create_proc_entry(ibm->name,
5692 S_IFREG | S_IRUGO | S_IWUSR,
5693 proc_dir);
5694 if (!entry) {
5695 printk(IBM_ERR "unable to create proc entry %s\n",
5696 ibm->name);
5697 ret = -ENODEV;
5698 goto err_out;
5700 entry->owner = THIS_MODULE;
5701 entry->data = ibm;
5702 entry->read_proc = &dispatch_procfs_read;
5703 if (ibm->write)
5704 entry->write_proc = &dispatch_procfs_write;
5705 ibm->flags.proc_created = 1;
5708 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
5710 return 0;
5712 err_out:
5713 dbg_printk(TPACPI_DBG_INIT,
5714 "%s: at error exit path with result %d\n",
5715 ibm->name, ret);
5717 ibm_exit(ibm);
5718 return (ret < 0)? ret : 0;
5721 static void ibm_exit(struct ibm_struct *ibm)
5723 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
5725 list_del_init(&ibm->all_drivers);
5727 if (ibm->flags.acpi_notify_installed) {
5728 dbg_printk(TPACPI_DBG_EXIT,
5729 "%s: acpi_remove_notify_handler\n", ibm->name);
5730 BUG_ON(!ibm->acpi);
5731 acpi_remove_notify_handler(*ibm->acpi->handle,
5732 ibm->acpi->type,
5733 dispatch_acpi_notify);
5734 ibm->flags.acpi_notify_installed = 0;
5735 ibm->flags.acpi_notify_installed = 0;
5738 if (ibm->flags.proc_created) {
5739 dbg_printk(TPACPI_DBG_EXIT,
5740 "%s: remove_proc_entry\n", ibm->name);
5741 remove_proc_entry(ibm->name, proc_dir);
5742 ibm->flags.proc_created = 0;
5745 if (ibm->flags.acpi_driver_registered) {
5746 dbg_printk(TPACPI_DBG_EXIT,
5747 "%s: acpi_bus_unregister_driver\n", ibm->name);
5748 BUG_ON(!ibm->acpi);
5749 acpi_bus_unregister_driver(ibm->acpi->driver);
5750 kfree(ibm->acpi->driver);
5751 ibm->acpi->driver = NULL;
5752 ibm->flags.acpi_driver_registered = 0;
5755 if (ibm->flags.init_called && ibm->exit) {
5756 ibm->exit();
5757 ibm->flags.init_called = 0;
5760 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
5763 /* Probing */
5765 static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
5767 struct dmi_device *dev = NULL;
5768 char ec_fw_string[18];
5770 if (!tp)
5771 return;
5773 memset(tp, 0, sizeof(*tp));
5775 if (dmi_name_in_vendors("IBM"))
5776 tp->vendor = PCI_VENDOR_ID_IBM;
5777 else if (dmi_name_in_vendors("LENOVO"))
5778 tp->vendor = PCI_VENDOR_ID_LENOVO;
5779 else
5780 return;
5782 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
5783 GFP_KERNEL);
5784 if (!tp->bios_version_str)
5785 return;
5786 tp->bios_model = tp->bios_version_str[0]
5787 | (tp->bios_version_str[1] << 8);
5790 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
5791 * X32 or newer, all Z series; Some models must have an
5792 * up-to-date BIOS or they will not be detected.
5794 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
5796 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
5797 if (sscanf(dev->name,
5798 "IBM ThinkPad Embedded Controller -[%17c",
5799 ec_fw_string) == 1) {
5800 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
5801 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
5803 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
5804 tp->ec_model = ec_fw_string[0]
5805 | (ec_fw_string[1] << 8);
5806 break;
5810 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
5811 GFP_KERNEL);
5812 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
5813 kfree(tp->model_str);
5814 tp->model_str = NULL;
5818 static int __init probe_for_thinkpad(void)
5820 int is_thinkpad;
5822 if (acpi_disabled)
5823 return -ENODEV;
5826 * Non-ancient models have better DMI tagging, but very old models
5827 * don't.
5829 is_thinkpad = (thinkpad_id.model_str != NULL);
5831 /* ec is required because many other handles are relative to it */
5832 IBM_ACPIHANDLE_INIT(ec);
5833 if (!ec_handle) {
5834 if (is_thinkpad)
5835 printk(IBM_ERR
5836 "Not yet supported ThinkPad detected!\n");
5837 return -ENODEV;
5841 * Risks a regression on very old machines, but reduces potential
5842 * false positives a damn great deal
5844 if (!is_thinkpad)
5845 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
5847 if (!is_thinkpad && !force_load)
5848 return -ENODEV;
5850 return 0;
5854 /* Module init, exit, parameters */
5856 static struct ibm_init_struct ibms_init[] __initdata = {
5858 .init = thinkpad_acpi_driver_init,
5859 .data = &thinkpad_acpi_driver_data,
5862 .init = hotkey_init,
5863 .data = &hotkey_driver_data,
5866 .init = bluetooth_init,
5867 .data = &bluetooth_driver_data,
5870 .init = wan_init,
5871 .data = &wan_driver_data,
5874 .init = video_init,
5875 .data = &video_driver_data,
5878 .init = light_init,
5879 .data = &light_driver_data,
5881 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5883 .init = dock_init,
5884 .data = &dock_driver_data[0],
5887 .init = dock_init2,
5888 .data = &dock_driver_data[1],
5890 #endif
5891 #ifdef CONFIG_THINKPAD_ACPI_BAY
5893 .init = bay_init,
5894 .data = &bay_driver_data,
5896 #endif
5898 .init = cmos_init,
5899 .data = &cmos_driver_data,
5902 .init = led_init,
5903 .data = &led_driver_data,
5906 .init = beep_init,
5907 .data = &beep_driver_data,
5910 .init = thermal_init,
5911 .data = &thermal_driver_data,
5914 .data = &ecdump_driver_data,
5917 .init = brightness_init,
5918 .data = &brightness_driver_data,
5921 .data = &volume_driver_data,
5924 .init = fan_init,
5925 .data = &fan_driver_data,
5929 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
5931 unsigned int i;
5932 struct ibm_struct *ibm;
5934 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
5935 ibm = ibms_init[i].data;
5936 BUG_ON(ibm == NULL);
5938 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
5939 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
5940 return -ENOSPC;
5941 strcpy(ibms_init[i].param, val);
5942 strcat(ibms_init[i].param, ",");
5943 return 0;
5947 return -EINVAL;
5950 static int experimental;
5951 module_param(experimental, int, 0);
5953 static u32 dbg_level;
5954 module_param_named(debug, dbg_level, uint, 0);
5956 static int force_load;
5957 module_param(force_load, bool, 0);
5959 static int fan_control_allowed;
5960 module_param_named(fan_control, fan_control_allowed, bool, 0);
5962 static int brightness_mode;
5963 module_param_named(brightness_mode, brightness_mode, int, 0);
5965 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5966 module_param(brightness_enable, uint, 0);
5968 static unsigned int hotkey_report_mode;
5969 module_param(hotkey_report_mode, uint, 0);
5971 #define IBM_PARAM(feature) \
5972 module_param_call(feature, set_ibm_param, NULL, NULL, 0)
5974 IBM_PARAM(hotkey);
5975 IBM_PARAM(bluetooth);
5976 IBM_PARAM(video);
5977 IBM_PARAM(light);
5978 #ifdef CONFIG_THINKPAD_ACPI_DOCK
5979 IBM_PARAM(dock);
5980 #endif
5981 #ifdef CONFIG_THINKPAD_ACPI_BAY
5982 IBM_PARAM(bay);
5983 #endif /* CONFIG_THINKPAD_ACPI_BAY */
5984 IBM_PARAM(cmos);
5985 IBM_PARAM(led);
5986 IBM_PARAM(beep);
5987 IBM_PARAM(ecdump);
5988 IBM_PARAM(brightness);
5989 IBM_PARAM(volume);
5990 IBM_PARAM(fan);
5992 static int __init thinkpad_acpi_module_init(void)
5994 int ret, i;
5996 tpacpi_lifecycle = TPACPI_LIFE_INIT;
5998 /* Parameter checking */
5999 if (hotkey_report_mode > 2)
6000 return -EINVAL;
6002 /* Driver-level probe */
6004 get_thinkpad_model_data(&thinkpad_id);
6005 ret = probe_for_thinkpad();
6006 if (ret) {
6007 thinkpad_acpi_module_exit();
6008 return ret;
6011 /* Driver initialization */
6013 IBM_ACPIHANDLE_INIT(ecrd);
6014 IBM_ACPIHANDLE_INIT(ecwr);
6016 proc_dir = proc_mkdir(IBM_PROC_DIR, acpi_root_dir);
6017 if (!proc_dir) {
6018 printk(IBM_ERR "unable to create proc dir " IBM_PROC_DIR);
6019 thinkpad_acpi_module_exit();
6020 return -ENODEV;
6022 proc_dir->owner = THIS_MODULE;
6024 ret = platform_driver_register(&tpacpi_pdriver);
6025 if (ret) {
6026 printk(IBM_ERR "unable to register main platform driver\n");
6027 thinkpad_acpi_module_exit();
6028 return ret;
6030 tp_features.platform_drv_registered = 1;
6032 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6033 if (ret) {
6034 printk(IBM_ERR "unable to register hwmon platform driver\n");
6035 thinkpad_acpi_module_exit();
6036 return ret;
6038 tp_features.sensors_pdrv_registered = 1;
6040 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6041 if (!ret) {
6042 tp_features.platform_drv_attrs_registered = 1;
6043 ret = tpacpi_create_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6045 if (ret) {
6046 printk(IBM_ERR "unable to create sysfs driver attributes\n");
6047 thinkpad_acpi_module_exit();
6048 return ret;
6050 tp_features.sensors_pdrv_attrs_registered = 1;
6053 /* Device initialization */
6054 tpacpi_pdev = platform_device_register_simple(IBM_DRVR_NAME, -1,
6055 NULL, 0);
6056 if (IS_ERR(tpacpi_pdev)) {
6057 ret = PTR_ERR(tpacpi_pdev);
6058 tpacpi_pdev = NULL;
6059 printk(IBM_ERR "unable to register platform device\n");
6060 thinkpad_acpi_module_exit();
6061 return ret;
6063 tpacpi_sensors_pdev = platform_device_register_simple(
6064 IBM_HWMON_DRVR_NAME,
6065 -1, NULL, 0);
6066 if (IS_ERR(tpacpi_sensors_pdev)) {
6067 ret = PTR_ERR(tpacpi_sensors_pdev);
6068 tpacpi_sensors_pdev = NULL;
6069 printk(IBM_ERR "unable to register hwmon platform device\n");
6070 thinkpad_acpi_module_exit();
6071 return ret;
6073 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6074 &dev_attr_thinkpad_acpi_pdev_name);
6075 if (ret) {
6076 printk(IBM_ERR
6077 "unable to create sysfs hwmon device attributes\n");
6078 thinkpad_acpi_module_exit();
6079 return ret;
6081 tp_features.sensors_pdev_attrs_registered = 1;
6082 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6083 if (IS_ERR(tpacpi_hwmon)) {
6084 ret = PTR_ERR(tpacpi_hwmon);
6085 tpacpi_hwmon = NULL;
6086 printk(IBM_ERR "unable to register hwmon device\n");
6087 thinkpad_acpi_module_exit();
6088 return ret;
6090 mutex_init(&tpacpi_inputdev_send_mutex);
6091 tpacpi_inputdev = input_allocate_device();
6092 if (!tpacpi_inputdev) {
6093 printk(IBM_ERR "unable to allocate input device\n");
6094 thinkpad_acpi_module_exit();
6095 return -ENOMEM;
6096 } else {
6097 /* Prepare input device, but don't register */
6098 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6099 tpacpi_inputdev->phys = IBM_DRVR_NAME "/input0";
6100 tpacpi_inputdev->id.bustype = BUS_HOST;
6101 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6102 thinkpad_id.vendor :
6103 PCI_VENDOR_ID_IBM;
6104 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6105 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6107 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6108 ret = ibm_init(&ibms_init[i]);
6109 if (ret >= 0 && *ibms_init[i].param)
6110 ret = ibms_init[i].data->write(ibms_init[i].param);
6111 if (ret < 0) {
6112 thinkpad_acpi_module_exit();
6113 return ret;
6116 ret = input_register_device(tpacpi_inputdev);
6117 if (ret < 0) {
6118 printk(IBM_ERR "unable to register input device\n");
6119 thinkpad_acpi_module_exit();
6120 return ret;
6121 } else {
6122 tp_features.input_device_registered = 1;
6125 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6126 return 0;
6129 static void thinkpad_acpi_module_exit(void)
6131 struct ibm_struct *ibm, *itmp;
6133 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6135 list_for_each_entry_safe_reverse(ibm, itmp,
6136 &tpacpi_all_drivers,
6137 all_drivers) {
6138 ibm_exit(ibm);
6141 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6143 if (tpacpi_inputdev) {
6144 if (tp_features.input_device_registered)
6145 input_unregister_device(tpacpi_inputdev);
6146 else
6147 input_free_device(tpacpi_inputdev);
6150 if (tpacpi_hwmon)
6151 hwmon_device_unregister(tpacpi_hwmon);
6153 if (tp_features.sensors_pdev_attrs_registered)
6154 device_remove_file(&tpacpi_sensors_pdev->dev,
6155 &dev_attr_thinkpad_acpi_pdev_name);
6156 if (tpacpi_sensors_pdev)
6157 platform_device_unregister(tpacpi_sensors_pdev);
6158 if (tpacpi_pdev)
6159 platform_device_unregister(tpacpi_pdev);
6161 if (tp_features.sensors_pdrv_attrs_registered)
6162 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6163 if (tp_features.platform_drv_attrs_registered)
6164 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6166 if (tp_features.sensors_pdrv_registered)
6167 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6169 if (tp_features.platform_drv_registered)
6170 platform_driver_unregister(&tpacpi_pdriver);
6172 if (proc_dir)
6173 remove_proc_entry(IBM_PROC_DIR, acpi_root_dir);
6175 kfree(thinkpad_id.bios_version_str);
6176 kfree(thinkpad_id.ec_version_str);
6177 kfree(thinkpad_id.model_str);
6180 module_init(thinkpad_acpi_module_init);
6181 module_exit(thinkpad_acpi_module_exit);