thinkpad-acpi: enhanced debugging messages for the hotkey subdriver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob5e15b73a5f48dcc0d3e2f30a6de0bb6ee22d404d
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.22"
25 #define TPACPI_SYSFS_VERSION 0x020300
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_RFKILL 0x0004
193 #define TPACPI_DBG_HKEY 0x0008
195 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
196 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
197 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
200 /****************************************************************************
201 * Driver-wide structs and misc. variables
204 struct ibm_struct;
206 struct tp_acpi_drv_struct {
207 const struct acpi_device_id *hid;
208 struct acpi_driver *driver;
210 void (*notify) (struct ibm_struct *, u32);
211 acpi_handle *handle;
212 u32 type;
213 struct acpi_device *device;
216 struct ibm_struct {
217 char *name;
219 int (*read) (char *);
220 int (*write) (char *);
221 void (*exit) (void);
222 void (*resume) (void);
223 void (*suspend) (pm_message_t state);
224 void (*shutdown) (void);
226 struct list_head all_drivers;
228 struct tp_acpi_drv_struct *acpi;
230 struct {
231 u8 acpi_driver_registered:1;
232 u8 acpi_notify_installed:1;
233 u8 proc_created:1;
234 u8 init_called:1;
235 u8 experimental:1;
236 } flags;
239 struct ibm_init_struct {
240 char param[32];
242 int (*init) (struct ibm_init_struct *);
243 struct ibm_struct *data;
246 static struct {
247 #ifdef CONFIG_THINKPAD_ACPI_BAY
248 u32 bay_status:1;
249 u32 bay_eject:1;
250 u32 bay_status2:1;
251 u32 bay_eject2:1;
252 #endif
253 u32 bluetooth:1;
254 u32 hotkey:1;
255 u32 hotkey_mask:1;
256 u32 hotkey_wlsw:1;
257 u32 hotkey_tablet:1;
258 u32 light:1;
259 u32 light_status:1;
260 u32 bright_16levels:1;
261 u32 bright_acpimode:1;
262 u32 wan:1;
263 u32 uwb:1;
264 u32 fan_ctrl_status_undef:1;
265 u32 input_device_registered:1;
266 u32 platform_drv_registered:1;
267 u32 platform_drv_attrs_registered:1;
268 u32 sensors_pdrv_registered:1;
269 u32 sensors_pdrv_attrs_registered:1;
270 u32 sensors_pdev_attrs_registered:1;
271 u32 hotkey_poll_active:1;
272 } tp_features;
274 static struct {
275 u16 hotkey_mask_ff:1;
276 u16 bright_cmos_ec_unsync:1;
277 } tp_warned;
279 struct thinkpad_id_data {
280 unsigned int vendor; /* ThinkPad vendor:
281 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
283 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
284 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
286 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
287 u16 ec_model;
289 char *model_str; /* ThinkPad T43 */
290 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
292 static struct thinkpad_id_data thinkpad_id;
294 static enum {
295 TPACPI_LIFE_INIT = 0,
296 TPACPI_LIFE_RUNNING,
297 TPACPI_LIFE_EXITING,
298 } tpacpi_lifecycle;
300 static int experimental;
301 static u32 dbg_level;
303 static struct workqueue_struct *tpacpi_wq;
305 enum led_status_t {
306 TPACPI_LED_OFF = 0,
307 TPACPI_LED_ON,
308 TPACPI_LED_BLINK,
311 /* Special LED class that can defer work */
312 struct tpacpi_led_classdev {
313 struct led_classdev led_classdev;
314 struct work_struct work;
315 enum led_status_t new_state;
316 unsigned int led;
319 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
320 static int dbg_wlswemul;
321 static int tpacpi_wlsw_emulstate;
322 static int dbg_bluetoothemul;
323 static int tpacpi_bluetooth_emulstate;
324 static int dbg_wwanemul;
325 static int tpacpi_wwan_emulstate;
326 static int dbg_uwbemul;
327 static int tpacpi_uwb_emulstate;
328 #endif
331 /*************************************************************************
332 * Debugging helpers
335 #define dbg_printk(a_dbg_level, format, arg...) \
336 do { if (dbg_level & (a_dbg_level)) \
337 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
338 } while (0)
340 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
341 #define vdbg_printk dbg_printk
342 static const char *str_supported(int is_supported);
343 #else
344 #define vdbg_printk(a_dbg_level, format, arg...) \
345 do { } while (0)
346 #endif
348 static void tpacpi_log_usertask(const char * const what)
350 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
351 what, task_tgid_vnr(current));
354 #define tpacpi_disclose_usertask(what, format, arg...) \
355 do { \
356 if (unlikely( \
357 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
358 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
359 printk(TPACPI_DEBUG "%s: PID %d: " format, \
360 what, task_tgid_vnr(current), ## arg); \
362 } while (0)
364 /****************************************************************************
365 ****************************************************************************
367 * ACPI Helpers and device model
369 ****************************************************************************
370 ****************************************************************************/
372 /*************************************************************************
373 * ACPI basic handles
376 static acpi_handle root_handle;
378 #define TPACPI_HANDLE(object, parent, paths...) \
379 static acpi_handle object##_handle; \
380 static acpi_handle *object##_parent = &parent##_handle; \
381 static char *object##_path; \
382 static char *object##_paths[] = { paths }
384 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
385 "\\_SB.PCI.ISA.EC", /* 570 */
386 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
387 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
388 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
389 "\\_SB.PCI0.ICH3.EC0", /* R31 */
390 "\\_SB.PCI0.LPC.EC", /* all others */
393 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
394 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
396 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
397 /* T4x, X31, X40 */
398 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
399 "\\CMS", /* R40, R40e */
400 ); /* all others */
402 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
403 "^HKEY", /* R30, R31 */
404 "HKEY", /* all others */
405 ); /* 570 */
407 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
408 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
409 "\\_SB.PCI0.VID0", /* 770e */
410 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
411 "\\_SB.PCI0.AGP.VID", /* all others */
412 ); /* R30, R31 */
415 /*************************************************************************
416 * ACPI helpers
419 static int acpi_evalf(acpi_handle handle,
420 void *res, char *method, char *fmt, ...)
422 char *fmt0 = fmt;
423 struct acpi_object_list params;
424 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
425 struct acpi_buffer result, *resultp;
426 union acpi_object out_obj;
427 acpi_status status;
428 va_list ap;
429 char res_type;
430 int success;
431 int quiet;
433 if (!*fmt) {
434 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
435 return 0;
438 if (*fmt == 'q') {
439 quiet = 1;
440 fmt++;
441 } else
442 quiet = 0;
444 res_type = *(fmt++);
446 params.count = 0;
447 params.pointer = &in_objs[0];
449 va_start(ap, fmt);
450 while (*fmt) {
451 char c = *(fmt++);
452 switch (c) {
453 case 'd': /* int */
454 in_objs[params.count].integer.value = va_arg(ap, int);
455 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
456 break;
457 /* add more types as needed */
458 default:
459 printk(TPACPI_ERR "acpi_evalf() called "
460 "with invalid format character '%c'\n", c);
461 return 0;
464 va_end(ap);
466 if (res_type != 'v') {
467 result.length = sizeof(out_obj);
468 result.pointer = &out_obj;
469 resultp = &result;
470 } else
471 resultp = NULL;
473 status = acpi_evaluate_object(handle, method, &params, resultp);
475 switch (res_type) {
476 case 'd': /* int */
477 if (res)
478 *(int *)res = out_obj.integer.value;
479 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
480 break;
481 case 'v': /* void */
482 success = status == AE_OK;
483 break;
484 /* add more types as needed */
485 default:
486 printk(TPACPI_ERR "acpi_evalf() called "
487 "with invalid format character '%c'\n", res_type);
488 return 0;
491 if (!success && !quiet)
492 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
493 method, fmt0, status);
495 return success;
498 static int acpi_ec_read(int i, u8 *p)
500 int v;
502 if (ecrd_handle) {
503 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
504 return 0;
505 *p = v;
506 } else {
507 if (ec_read(i, p) < 0)
508 return 0;
511 return 1;
514 static int acpi_ec_write(int i, u8 v)
516 if (ecwr_handle) {
517 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
518 return 0;
519 } else {
520 if (ec_write(i, v) < 0)
521 return 0;
524 return 1;
527 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
528 static int _sta(acpi_handle handle)
530 int status;
532 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
533 status = 0;
535 return status;
537 #endif
539 static int issue_thinkpad_cmos_command(int cmos_cmd)
541 if (!cmos_handle)
542 return -ENXIO;
544 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
545 return -EIO;
547 return 0;
550 /*************************************************************************
551 * ACPI device model
554 #define TPACPI_ACPIHANDLE_INIT(object) \
555 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
556 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
558 static void drv_acpi_handle_init(char *name,
559 acpi_handle *handle, acpi_handle parent,
560 char **paths, int num_paths, char **path)
562 int i;
563 acpi_status status;
565 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
566 name);
568 for (i = 0; i < num_paths; i++) {
569 status = acpi_get_handle(parent, paths[i], handle);
570 if (ACPI_SUCCESS(status)) {
571 *path = paths[i];
572 dbg_printk(TPACPI_DBG_INIT,
573 "Found ACPI handle %s for %s\n",
574 *path, name);
575 return;
579 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
580 name);
581 *handle = NULL;
584 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
586 struct ibm_struct *ibm = data;
588 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
589 return;
591 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
592 return;
594 ibm->acpi->notify(ibm, event);
597 static int __init setup_acpi_notify(struct ibm_struct *ibm)
599 acpi_status status;
600 int rc;
602 BUG_ON(!ibm->acpi);
604 if (!*ibm->acpi->handle)
605 return 0;
607 vdbg_printk(TPACPI_DBG_INIT,
608 "setting up ACPI notify for %s\n", ibm->name);
610 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
611 if (rc < 0) {
612 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
613 ibm->name, rc);
614 return -ENODEV;
617 acpi_driver_data(ibm->acpi->device) = ibm;
618 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
619 TPACPI_ACPI_EVENT_PREFIX,
620 ibm->name);
622 status = acpi_install_notify_handler(*ibm->acpi->handle,
623 ibm->acpi->type, dispatch_acpi_notify, ibm);
624 if (ACPI_FAILURE(status)) {
625 if (status == AE_ALREADY_EXISTS) {
626 printk(TPACPI_NOTICE
627 "another device driver is already "
628 "handling %s events\n", ibm->name);
629 } else {
630 printk(TPACPI_ERR
631 "acpi_install_notify_handler(%s) failed: %d\n",
632 ibm->name, status);
634 return -ENODEV;
636 ibm->flags.acpi_notify_installed = 1;
637 return 0;
640 static int __init tpacpi_device_add(struct acpi_device *device)
642 return 0;
645 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
647 int rc;
649 dbg_printk(TPACPI_DBG_INIT,
650 "registering %s as an ACPI driver\n", ibm->name);
652 BUG_ON(!ibm->acpi);
654 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
655 if (!ibm->acpi->driver) {
656 printk(TPACPI_ERR
657 "failed to allocate memory for ibm->acpi->driver\n");
658 return -ENOMEM;
661 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
662 ibm->acpi->driver->ids = ibm->acpi->hid;
664 ibm->acpi->driver->ops.add = &tpacpi_device_add;
666 rc = acpi_bus_register_driver(ibm->acpi->driver);
667 if (rc < 0) {
668 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
669 ibm->name, rc);
670 kfree(ibm->acpi->driver);
671 ibm->acpi->driver = NULL;
672 } else if (!rc)
673 ibm->flags.acpi_driver_registered = 1;
675 return rc;
679 /****************************************************************************
680 ****************************************************************************
682 * Procfs Helpers
684 ****************************************************************************
685 ****************************************************************************/
687 static int dispatch_procfs_read(char *page, char **start, off_t off,
688 int count, int *eof, void *data)
690 struct ibm_struct *ibm = data;
691 int len;
693 if (!ibm || !ibm->read)
694 return -EINVAL;
696 len = ibm->read(page);
697 if (len < 0)
698 return len;
700 if (len <= off + count)
701 *eof = 1;
702 *start = page + off;
703 len -= off;
704 if (len > count)
705 len = count;
706 if (len < 0)
707 len = 0;
709 return len;
712 static int dispatch_procfs_write(struct file *file,
713 const char __user *userbuf,
714 unsigned long count, void *data)
716 struct ibm_struct *ibm = data;
717 char *kernbuf;
718 int ret;
720 if (!ibm || !ibm->write)
721 return -EINVAL;
723 kernbuf = kmalloc(count + 2, GFP_KERNEL);
724 if (!kernbuf)
725 return -ENOMEM;
727 if (copy_from_user(kernbuf, userbuf, count)) {
728 kfree(kernbuf);
729 return -EFAULT;
732 kernbuf[count] = 0;
733 strcat(kernbuf, ",");
734 ret = ibm->write(kernbuf);
735 if (ret == 0)
736 ret = count;
738 kfree(kernbuf);
740 return ret;
743 static char *next_cmd(char **cmds)
745 char *start = *cmds;
746 char *end;
748 while ((end = strchr(start, ',')) && end == start)
749 start = end + 1;
751 if (!end)
752 return NULL;
754 *end = 0;
755 *cmds = end + 1;
756 return start;
760 /****************************************************************************
761 ****************************************************************************
763 * Device model: input, hwmon and platform
765 ****************************************************************************
766 ****************************************************************************/
768 static struct platform_device *tpacpi_pdev;
769 static struct platform_device *tpacpi_sensors_pdev;
770 static struct device *tpacpi_hwmon;
771 static struct input_dev *tpacpi_inputdev;
772 static struct mutex tpacpi_inputdev_send_mutex;
773 static LIST_HEAD(tpacpi_all_drivers);
775 static int tpacpi_suspend_handler(struct platform_device *pdev,
776 pm_message_t state)
778 struct ibm_struct *ibm, *itmp;
780 list_for_each_entry_safe(ibm, itmp,
781 &tpacpi_all_drivers,
782 all_drivers) {
783 if (ibm->suspend)
784 (ibm->suspend)(state);
787 return 0;
790 static int tpacpi_resume_handler(struct platform_device *pdev)
792 struct ibm_struct *ibm, *itmp;
794 list_for_each_entry_safe(ibm, itmp,
795 &tpacpi_all_drivers,
796 all_drivers) {
797 if (ibm->resume)
798 (ibm->resume)();
801 return 0;
804 static void tpacpi_shutdown_handler(struct platform_device *pdev)
806 struct ibm_struct *ibm, *itmp;
808 list_for_each_entry_safe(ibm, itmp,
809 &tpacpi_all_drivers,
810 all_drivers) {
811 if (ibm->shutdown)
812 (ibm->shutdown)();
816 static struct platform_driver tpacpi_pdriver = {
817 .driver = {
818 .name = TPACPI_DRVR_NAME,
819 .owner = THIS_MODULE,
821 .suspend = tpacpi_suspend_handler,
822 .resume = tpacpi_resume_handler,
823 .shutdown = tpacpi_shutdown_handler,
826 static struct platform_driver tpacpi_hwmon_pdriver = {
827 .driver = {
828 .name = TPACPI_HWMON_DRVR_NAME,
829 .owner = THIS_MODULE,
833 /*************************************************************************
834 * sysfs support helpers
837 struct attribute_set {
838 unsigned int members, max_members;
839 struct attribute_group group;
842 struct attribute_set_obj {
843 struct attribute_set s;
844 struct attribute *a;
845 } __attribute__((packed));
847 static struct attribute_set *create_attr_set(unsigned int max_members,
848 const char *name)
850 struct attribute_set_obj *sobj;
852 if (max_members == 0)
853 return NULL;
855 /* Allocates space for implicit NULL at the end too */
856 sobj = kzalloc(sizeof(struct attribute_set_obj) +
857 max_members * sizeof(struct attribute *),
858 GFP_KERNEL);
859 if (!sobj)
860 return NULL;
861 sobj->s.max_members = max_members;
862 sobj->s.group.attrs = &sobj->a;
863 sobj->s.group.name = name;
865 return &sobj->s;
868 #define destroy_attr_set(_set) \
869 kfree(_set);
871 /* not multi-threaded safe, use it in a single thread per set */
872 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
874 if (!s || !attr)
875 return -EINVAL;
877 if (s->members >= s->max_members)
878 return -ENOMEM;
880 s->group.attrs[s->members] = attr;
881 s->members++;
883 return 0;
886 static int add_many_to_attr_set(struct attribute_set *s,
887 struct attribute **attr,
888 unsigned int count)
890 int i, res;
892 for (i = 0; i < count; i++) {
893 res = add_to_attr_set(s, attr[i]);
894 if (res)
895 return res;
898 return 0;
901 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
903 sysfs_remove_group(kobj, &s->group);
904 destroy_attr_set(s);
907 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
908 sysfs_create_group(_kobj, &_attr_set->group)
910 static int parse_strtoul(const char *buf,
911 unsigned long max, unsigned long *value)
913 char *endp;
915 while (*buf && isspace(*buf))
916 buf++;
917 *value = simple_strtoul(buf, &endp, 0);
918 while (*endp && isspace(*endp))
919 endp++;
920 if (*endp || *value > max)
921 return -EINVAL;
923 return 0;
926 static void tpacpi_disable_brightness_delay(void)
928 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
929 printk(TPACPI_NOTICE
930 "ACPI backlight control delay disabled\n");
933 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
935 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
936 union acpi_object *obj;
937 int rc;
939 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
940 obj = (union acpi_object *)buffer.pointer;
941 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
942 printk(TPACPI_ERR "Unknown _BCL data, "
943 "please report this to %s\n", TPACPI_MAIL);
944 rc = 0;
945 } else {
946 rc = obj->package.count;
948 } else {
949 return 0;
952 kfree(buffer.pointer);
953 return rc;
956 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
957 u32 lvl, void *context, void **rv)
959 char name[ACPI_PATH_SEGMENT_LENGTH];
960 struct acpi_buffer buffer = { sizeof(name), &name };
962 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
963 !strncmp("_BCL", name, sizeof(name) - 1)) {
964 BUG_ON(!rv || !*rv);
965 **(int **)rv = tpacpi_query_bcl_levels(handle);
966 return AE_CTRL_TERMINATE;
967 } else {
968 return AE_OK;
973 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
975 static int __init tpacpi_check_std_acpi_brightness_support(void)
977 int status;
978 int bcl_levels = 0;
979 void *bcl_ptr = &bcl_levels;
981 if (!vid_handle) {
982 TPACPI_ACPIHANDLE_INIT(vid);
984 if (!vid_handle)
985 return 0;
988 * Search for a _BCL method, and execute it. This is safe on all
989 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
990 * BIOS in ACPI backlight control mode. We do NOT have to care
991 * about calling the _BCL method in an enabled video device, any
992 * will do for our purposes.
995 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
996 tpacpi_acpi_walk_find_bcl, NULL,
997 &bcl_ptr);
999 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
1000 tp_features.bright_acpimode = 1;
1001 return (bcl_levels - 2);
1004 return 0;
1007 static int __init tpacpi_new_rfkill(const unsigned int id,
1008 struct rfkill **rfk,
1009 const enum rfkill_type rfktype,
1010 const char *name,
1011 const bool set_default,
1012 int (*toggle_radio)(void *, enum rfkill_state),
1013 int (*get_state)(void *, enum rfkill_state *))
1015 int res;
1016 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1018 res = get_state(NULL, &initial_state);
1019 if (res < 0) {
1020 printk(TPACPI_ERR
1021 "failed to read initial state for %s, error %d; "
1022 "will turn radio off\n", name, res);
1023 } else if (set_default) {
1024 /* try to set the initial state as the default for the rfkill
1025 * type, since we ask the firmware to preserve it across S5 in
1026 * NVRAM */
1027 if (rfkill_set_default(rfktype,
1028 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1029 RFKILL_STATE_UNBLOCKED :
1030 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1031 vdbg_printk(TPACPI_DBG_RFKILL,
1032 "Default state for %s cannot be changed\n",
1033 name);
1036 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1037 if (!*rfk) {
1038 printk(TPACPI_ERR
1039 "failed to allocate memory for rfkill class\n");
1040 return -ENOMEM;
1043 (*rfk)->name = name;
1044 (*rfk)->get_state = get_state;
1045 (*rfk)->toggle_radio = toggle_radio;
1046 (*rfk)->state = initial_state;
1048 res = rfkill_register(*rfk);
1049 if (res < 0) {
1050 printk(TPACPI_ERR
1051 "failed to register %s rfkill switch: %d\n",
1052 name, res);
1053 rfkill_free(*rfk);
1054 *rfk = NULL;
1055 return res;
1058 return 0;
1061 static void printk_deprecated_attribute(const char * const what,
1062 const char * const details)
1064 tpacpi_log_usertask("deprecated sysfs attribute");
1065 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1066 "will be removed. %s\n",
1067 what, details);
1070 static void printk_deprecated_rfkill_attribute(const char * const what)
1072 printk_deprecated_attribute(what,
1073 "Please switch to generic rfkill before year 2010");
1076 /*************************************************************************
1077 * thinkpad-acpi driver attributes
1080 /* interface_version --------------------------------------------------- */
1081 static ssize_t tpacpi_driver_interface_version_show(
1082 struct device_driver *drv,
1083 char *buf)
1085 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1088 static DRIVER_ATTR(interface_version, S_IRUGO,
1089 tpacpi_driver_interface_version_show, NULL);
1091 /* debug_level --------------------------------------------------------- */
1092 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1093 char *buf)
1095 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1098 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1099 const char *buf, size_t count)
1101 unsigned long t;
1103 if (parse_strtoul(buf, 0xffff, &t))
1104 return -EINVAL;
1106 dbg_level = t;
1108 return count;
1111 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1112 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1114 /* version ------------------------------------------------------------- */
1115 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1116 char *buf)
1118 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1119 TPACPI_DESC, TPACPI_VERSION);
1122 static DRIVER_ATTR(version, S_IRUGO,
1123 tpacpi_driver_version_show, NULL);
1125 /* --------------------------------------------------------------------- */
1127 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1129 static void tpacpi_send_radiosw_update(void);
1131 /* wlsw_emulstate ------------------------------------------------------ */
1132 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1133 char *buf)
1135 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1138 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1139 const char *buf, size_t count)
1141 unsigned long t;
1143 if (parse_strtoul(buf, 1, &t))
1144 return -EINVAL;
1146 if (tpacpi_wlsw_emulstate != t) {
1147 tpacpi_wlsw_emulstate = !!t;
1148 tpacpi_send_radiosw_update();
1149 } else
1150 tpacpi_wlsw_emulstate = !!t;
1152 return count;
1155 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1156 tpacpi_driver_wlsw_emulstate_show,
1157 tpacpi_driver_wlsw_emulstate_store);
1159 /* bluetooth_emulstate ------------------------------------------------- */
1160 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1161 struct device_driver *drv,
1162 char *buf)
1164 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1167 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1168 struct device_driver *drv,
1169 const char *buf, size_t count)
1171 unsigned long t;
1173 if (parse_strtoul(buf, 1, &t))
1174 return -EINVAL;
1176 tpacpi_bluetooth_emulstate = !!t;
1178 return count;
1181 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1182 tpacpi_driver_bluetooth_emulstate_show,
1183 tpacpi_driver_bluetooth_emulstate_store);
1185 /* wwan_emulstate ------------------------------------------------- */
1186 static ssize_t tpacpi_driver_wwan_emulstate_show(
1187 struct device_driver *drv,
1188 char *buf)
1190 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1193 static ssize_t tpacpi_driver_wwan_emulstate_store(
1194 struct device_driver *drv,
1195 const char *buf, size_t count)
1197 unsigned long t;
1199 if (parse_strtoul(buf, 1, &t))
1200 return -EINVAL;
1202 tpacpi_wwan_emulstate = !!t;
1204 return count;
1207 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1208 tpacpi_driver_wwan_emulstate_show,
1209 tpacpi_driver_wwan_emulstate_store);
1211 /* uwb_emulstate ------------------------------------------------- */
1212 static ssize_t tpacpi_driver_uwb_emulstate_show(
1213 struct device_driver *drv,
1214 char *buf)
1216 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1219 static ssize_t tpacpi_driver_uwb_emulstate_store(
1220 struct device_driver *drv,
1221 const char *buf, size_t count)
1223 unsigned long t;
1225 if (parse_strtoul(buf, 1, &t))
1226 return -EINVAL;
1228 tpacpi_uwb_emulstate = !!t;
1230 return count;
1233 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1234 tpacpi_driver_uwb_emulstate_show,
1235 tpacpi_driver_uwb_emulstate_store);
1236 #endif
1238 /* --------------------------------------------------------------------- */
1240 static struct driver_attribute *tpacpi_driver_attributes[] = {
1241 &driver_attr_debug_level, &driver_attr_version,
1242 &driver_attr_interface_version,
1245 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1247 int i, res;
1249 i = 0;
1250 res = 0;
1251 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1252 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1253 i++;
1256 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1257 if (!res && dbg_wlswemul)
1258 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1259 if (!res && dbg_bluetoothemul)
1260 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1261 if (!res && dbg_wwanemul)
1262 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1263 if (!res && dbg_uwbemul)
1264 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1265 #endif
1267 return res;
1270 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1272 int i;
1274 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1275 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1277 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1278 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1279 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1280 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1281 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1282 #endif
1285 /****************************************************************************
1286 ****************************************************************************
1288 * Subdrivers
1290 ****************************************************************************
1291 ****************************************************************************/
1293 /*************************************************************************
1294 * thinkpad-acpi init subdriver
1297 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1299 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1300 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1302 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1303 (thinkpad_id.bios_version_str) ?
1304 thinkpad_id.bios_version_str : "unknown",
1305 (thinkpad_id.ec_version_str) ?
1306 thinkpad_id.ec_version_str : "unknown");
1308 if (thinkpad_id.vendor && thinkpad_id.model_str)
1309 printk(TPACPI_INFO "%s %s, model %s\n",
1310 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1311 "IBM" : ((thinkpad_id.vendor ==
1312 PCI_VENDOR_ID_LENOVO) ?
1313 "Lenovo" : "Unknown vendor"),
1314 thinkpad_id.model_str,
1315 (thinkpad_id.nummodel_str) ?
1316 thinkpad_id.nummodel_str : "unknown");
1318 return 0;
1321 static int thinkpad_acpi_driver_read(char *p)
1323 int len = 0;
1325 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1326 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1328 return len;
1331 static struct ibm_struct thinkpad_acpi_driver_data = {
1332 .name = "driver",
1333 .read = thinkpad_acpi_driver_read,
1336 /*************************************************************************
1337 * Hotkey subdriver
1340 enum { /* hot key scan codes (derived from ACPI DSDT) */
1341 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1342 TP_ACPI_HOTKEYSCAN_FNF2,
1343 TP_ACPI_HOTKEYSCAN_FNF3,
1344 TP_ACPI_HOTKEYSCAN_FNF4,
1345 TP_ACPI_HOTKEYSCAN_FNF5,
1346 TP_ACPI_HOTKEYSCAN_FNF6,
1347 TP_ACPI_HOTKEYSCAN_FNF7,
1348 TP_ACPI_HOTKEYSCAN_FNF8,
1349 TP_ACPI_HOTKEYSCAN_FNF9,
1350 TP_ACPI_HOTKEYSCAN_FNF10,
1351 TP_ACPI_HOTKEYSCAN_FNF11,
1352 TP_ACPI_HOTKEYSCAN_FNF12,
1353 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1354 TP_ACPI_HOTKEYSCAN_FNINSERT,
1355 TP_ACPI_HOTKEYSCAN_FNDELETE,
1356 TP_ACPI_HOTKEYSCAN_FNHOME,
1357 TP_ACPI_HOTKEYSCAN_FNEND,
1358 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1359 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1360 TP_ACPI_HOTKEYSCAN_FNSPACE,
1361 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1362 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1363 TP_ACPI_HOTKEYSCAN_MUTE,
1364 TP_ACPI_HOTKEYSCAN_THINKPAD,
1367 enum { /* Keys available through NVRAM polling */
1368 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1369 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1372 enum { /* Positions of some of the keys in hotkey masks */
1373 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1374 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1375 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1376 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1377 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1378 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1379 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1380 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1381 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1382 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1383 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1386 enum { /* NVRAM to ACPI HKEY group map */
1387 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1388 TP_ACPI_HKEY_ZOOM_MASK |
1389 TP_ACPI_HKEY_DISPSWTCH_MASK |
1390 TP_ACPI_HKEY_HIBERNATE_MASK,
1391 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1392 TP_ACPI_HKEY_BRGHTDWN_MASK,
1393 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1394 TP_ACPI_HKEY_VOLDWN_MASK |
1395 TP_ACPI_HKEY_MUTE_MASK,
1398 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1399 struct tp_nvram_state {
1400 u16 thinkpad_toggle:1;
1401 u16 zoom_toggle:1;
1402 u16 display_toggle:1;
1403 u16 thinklight_toggle:1;
1404 u16 hibernate_toggle:1;
1405 u16 displayexp_toggle:1;
1406 u16 display_state:1;
1407 u16 brightness_toggle:1;
1408 u16 volume_toggle:1;
1409 u16 mute:1;
1411 u8 brightness_level;
1412 u8 volume_level;
1415 static struct task_struct *tpacpi_hotkey_task;
1416 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1417 static int hotkey_poll_freq = 10; /* Hz */
1418 static struct mutex hotkey_thread_mutex;
1419 static struct mutex hotkey_thread_data_mutex;
1420 static unsigned int hotkey_config_change;
1422 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1424 #define hotkey_source_mask 0U
1426 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1428 static struct mutex hotkey_mutex;
1430 static enum { /* Reasons for waking up */
1431 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1432 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1433 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1434 } hotkey_wakeup_reason;
1436 static int hotkey_autosleep_ack;
1438 static u32 hotkey_orig_mask;
1439 static u32 hotkey_all_mask;
1440 static u32 hotkey_reserved_mask;
1441 static u32 hotkey_mask;
1443 static unsigned int hotkey_report_mode;
1445 static u16 *hotkey_keycode_map;
1447 static struct attribute_set *hotkey_dev_attributes;
1449 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1450 #define HOTKEY_CONFIG_CRITICAL_START \
1451 do { \
1452 mutex_lock(&hotkey_thread_data_mutex); \
1453 hotkey_config_change++; \
1454 } while (0);
1455 #define HOTKEY_CONFIG_CRITICAL_END \
1456 mutex_unlock(&hotkey_thread_data_mutex);
1457 #else
1458 #define HOTKEY_CONFIG_CRITICAL_START
1459 #define HOTKEY_CONFIG_CRITICAL_END
1460 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1462 /* HKEY.MHKG() return bits */
1463 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1465 static int hotkey_get_wlsw(int *status)
1467 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1468 if (dbg_wlswemul) {
1469 *status = !!tpacpi_wlsw_emulstate;
1470 return 0;
1472 #endif
1473 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1474 return -EIO;
1475 return 0;
1478 static int hotkey_get_tablet_mode(int *status)
1480 int s;
1482 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1483 return -EIO;
1485 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1486 return 0;
1490 * Call with hotkey_mutex held
1492 static int hotkey_mask_get(void)
1494 u32 m = 0;
1496 if (tp_features.hotkey_mask) {
1497 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1498 return -EIO;
1500 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1502 return 0;
1506 * Call with hotkey_mutex held
1508 static int hotkey_mask_set(u32 mask)
1510 int i;
1511 int rc = 0;
1513 if (tp_features.hotkey_mask) {
1514 if (!tp_warned.hotkey_mask_ff &&
1515 (mask == 0xffff || mask == 0xffffff ||
1516 mask == 0xffffffff)) {
1517 tp_warned.hotkey_mask_ff = 1;
1518 printk(TPACPI_NOTICE
1519 "setting the hotkey mask to 0x%08x is likely "
1520 "not the best way to go about it\n", mask);
1521 printk(TPACPI_NOTICE
1522 "please consider using the driver defaults, "
1523 "and refer to up-to-date thinkpad-acpi "
1524 "documentation\n");
1527 HOTKEY_CONFIG_CRITICAL_START
1528 for (i = 0; i < 32; i++) {
1529 u32 m = 1 << i;
1530 /* enable in firmware mask only keys not in NVRAM
1531 * mode, but enable the key in the cached hotkey_mask
1532 * regardless of mode, or the key will end up
1533 * disabled by hotkey_mask_get() */
1534 if (!acpi_evalf(hkey_handle,
1535 NULL, "MHKM", "vdd", i + 1,
1536 !!((mask & ~hotkey_source_mask) & m))) {
1537 rc = -EIO;
1538 break;
1539 } else {
1540 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1543 HOTKEY_CONFIG_CRITICAL_END
1545 /* hotkey_mask_get must be called unconditionally below */
1546 if (!hotkey_mask_get() && !rc &&
1547 (hotkey_mask & ~hotkey_source_mask) !=
1548 (mask & ~hotkey_source_mask)) {
1549 printk(TPACPI_NOTICE
1550 "requested hot key mask 0x%08x, but "
1551 "firmware forced it to 0x%08x\n",
1552 mask, hotkey_mask);
1554 } else {
1555 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1556 HOTKEY_CONFIG_CRITICAL_START
1557 hotkey_mask = mask & hotkey_source_mask;
1558 HOTKEY_CONFIG_CRITICAL_END
1559 hotkey_mask_get();
1560 if (hotkey_mask != mask) {
1561 printk(TPACPI_NOTICE
1562 "requested hot key mask 0x%08x, "
1563 "forced to 0x%08x (NVRAM poll mask is "
1564 "0x%08x): no firmware mask support\n",
1565 mask, hotkey_mask, hotkey_source_mask);
1567 #else
1568 hotkey_mask_get();
1569 rc = -ENXIO;
1570 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1573 return rc;
1576 static int hotkey_status_get(int *status)
1578 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1579 return -EIO;
1581 return 0;
1584 static int hotkey_status_set(bool enable)
1586 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1587 return -EIO;
1589 return 0;
1592 static void tpacpi_input_send_tabletsw(void)
1594 int state;
1596 if (tp_features.hotkey_tablet &&
1597 !hotkey_get_tablet_mode(&state)) {
1598 mutex_lock(&tpacpi_inputdev_send_mutex);
1600 input_report_switch(tpacpi_inputdev,
1601 SW_TABLET_MODE, !!state);
1602 input_sync(tpacpi_inputdev);
1604 mutex_unlock(&tpacpi_inputdev_send_mutex);
1608 static void tpacpi_input_send_key(unsigned int scancode)
1610 unsigned int keycode;
1612 keycode = hotkey_keycode_map[scancode];
1614 if (keycode != KEY_RESERVED) {
1615 mutex_lock(&tpacpi_inputdev_send_mutex);
1617 input_report_key(tpacpi_inputdev, keycode, 1);
1618 if (keycode == KEY_UNKNOWN)
1619 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1620 scancode);
1621 input_sync(tpacpi_inputdev);
1623 input_report_key(tpacpi_inputdev, keycode, 0);
1624 if (keycode == KEY_UNKNOWN)
1625 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1626 scancode);
1627 input_sync(tpacpi_inputdev);
1629 mutex_unlock(&tpacpi_inputdev_send_mutex);
1633 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1634 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1636 static void tpacpi_hotkey_send_key(unsigned int scancode)
1638 tpacpi_input_send_key(scancode);
1639 if (hotkey_report_mode < 2) {
1640 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1641 0x80, 0x1001 + scancode);
1645 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1647 u8 d;
1649 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1650 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1651 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1652 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1653 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1654 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1656 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1657 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1658 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1660 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1661 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1662 n->displayexp_toggle =
1663 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1665 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1666 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1667 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1668 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1669 n->brightness_toggle =
1670 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1672 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1673 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1674 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1675 >> TP_NVRAM_POS_LEVEL_VOLUME;
1676 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1677 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1681 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1682 do { \
1683 if ((mask & (1 << __scancode)) && \
1684 oldn->__member != newn->__member) \
1685 tpacpi_hotkey_send_key(__scancode); \
1686 } while (0)
1688 #define TPACPI_MAY_SEND_KEY(__scancode) \
1689 do { if (mask & (1 << __scancode)) \
1690 tpacpi_hotkey_send_key(__scancode); } while (0)
1692 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1693 struct tp_nvram_state *newn,
1694 u32 mask)
1696 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1698 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1699 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1701 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1703 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1705 /* handle volume */
1706 if (oldn->volume_toggle != newn->volume_toggle) {
1707 if (oldn->mute != newn->mute) {
1708 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1710 if (oldn->volume_level > newn->volume_level) {
1711 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1712 } else if (oldn->volume_level < newn->volume_level) {
1713 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1714 } else if (oldn->mute == newn->mute) {
1715 /* repeated key presses that didn't change state */
1716 if (newn->mute) {
1717 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1718 } else if (newn->volume_level != 0) {
1719 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1720 } else {
1721 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1726 /* handle brightness */
1727 if (oldn->brightness_toggle != newn->brightness_toggle) {
1728 if (oldn->brightness_level < newn->brightness_level) {
1729 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1730 } else if (oldn->brightness_level > newn->brightness_level) {
1731 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1732 } else {
1733 /* repeated key presses that didn't change state */
1734 if (newn->brightness_level != 0) {
1735 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1736 } else {
1737 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1743 #undef TPACPI_COMPARE_KEY
1744 #undef TPACPI_MAY_SEND_KEY
1746 static int hotkey_kthread(void *data)
1748 struct tp_nvram_state s[2];
1749 u32 mask;
1750 unsigned int si, so;
1751 unsigned long t;
1752 unsigned int change_detector, must_reset;
1754 mutex_lock(&hotkey_thread_mutex);
1756 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1757 goto exit;
1759 set_freezable();
1761 so = 0;
1762 si = 1;
1763 t = 0;
1765 /* Initial state for compares */
1766 mutex_lock(&hotkey_thread_data_mutex);
1767 change_detector = hotkey_config_change;
1768 mask = hotkey_source_mask & hotkey_mask;
1769 mutex_unlock(&hotkey_thread_data_mutex);
1770 hotkey_read_nvram(&s[so], mask);
1772 while (!kthread_should_stop() && hotkey_poll_freq) {
1773 if (t == 0)
1774 t = 1000/hotkey_poll_freq;
1775 t = msleep_interruptible(t);
1776 if (unlikely(kthread_should_stop()))
1777 break;
1778 must_reset = try_to_freeze();
1779 if (t > 0 && !must_reset)
1780 continue;
1782 mutex_lock(&hotkey_thread_data_mutex);
1783 if (must_reset || hotkey_config_change != change_detector) {
1784 /* forget old state on thaw or config change */
1785 si = so;
1786 t = 0;
1787 change_detector = hotkey_config_change;
1789 mask = hotkey_source_mask & hotkey_mask;
1790 mutex_unlock(&hotkey_thread_data_mutex);
1792 if (likely(mask)) {
1793 hotkey_read_nvram(&s[si], mask);
1794 if (likely(si != so)) {
1795 hotkey_compare_and_issue_event(&s[so], &s[si],
1796 mask);
1800 so = si;
1801 si ^= 1;
1804 exit:
1805 mutex_unlock(&hotkey_thread_mutex);
1806 return 0;
1809 static void hotkey_poll_stop_sync(void)
1811 if (tpacpi_hotkey_task) {
1812 if (frozen(tpacpi_hotkey_task) ||
1813 freezing(tpacpi_hotkey_task))
1814 thaw_process(tpacpi_hotkey_task);
1816 kthread_stop(tpacpi_hotkey_task);
1817 tpacpi_hotkey_task = NULL;
1818 mutex_lock(&hotkey_thread_mutex);
1819 /* at this point, the thread did exit */
1820 mutex_unlock(&hotkey_thread_mutex);
1824 /* call with hotkey_mutex held */
1825 static void hotkey_poll_setup(int may_warn)
1827 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1828 hotkey_poll_freq > 0 &&
1829 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1830 if (!tpacpi_hotkey_task) {
1831 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1832 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1833 if (IS_ERR(tpacpi_hotkey_task)) {
1834 tpacpi_hotkey_task = NULL;
1835 printk(TPACPI_ERR
1836 "could not create kernel thread "
1837 "for hotkey polling\n");
1840 } else {
1841 hotkey_poll_stop_sync();
1842 if (may_warn &&
1843 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1844 printk(TPACPI_NOTICE
1845 "hot keys 0x%08x require polling, "
1846 "which is currently disabled\n",
1847 hotkey_source_mask);
1852 static void hotkey_poll_setup_safe(int may_warn)
1854 mutex_lock(&hotkey_mutex);
1855 hotkey_poll_setup(may_warn);
1856 mutex_unlock(&hotkey_mutex);
1859 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1861 static void hotkey_poll_setup_safe(int __unused)
1865 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1867 static int hotkey_inputdev_open(struct input_dev *dev)
1869 switch (tpacpi_lifecycle) {
1870 case TPACPI_LIFE_INIT:
1872 * hotkey_init will call hotkey_poll_setup_safe
1873 * at the appropriate moment
1875 return 0;
1876 case TPACPI_LIFE_EXITING:
1877 return -EBUSY;
1878 case TPACPI_LIFE_RUNNING:
1879 hotkey_poll_setup_safe(0);
1880 return 0;
1883 /* Should only happen if tpacpi_lifecycle is corrupt */
1884 BUG();
1885 return -EBUSY;
1888 static void hotkey_inputdev_close(struct input_dev *dev)
1890 /* disable hotkey polling when possible */
1891 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1892 hotkey_poll_setup_safe(0);
1895 /* sysfs hotkey enable ------------------------------------------------- */
1896 static ssize_t hotkey_enable_show(struct device *dev,
1897 struct device_attribute *attr,
1898 char *buf)
1900 int res, status;
1902 printk_deprecated_attribute("hotkey_enable",
1903 "Hotkey reporting is always enabled");
1905 res = hotkey_status_get(&status);
1906 if (res)
1907 return res;
1909 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1912 static ssize_t hotkey_enable_store(struct device *dev,
1913 struct device_attribute *attr,
1914 const char *buf, size_t count)
1916 unsigned long t;
1918 printk_deprecated_attribute("hotkey_enable",
1919 "Hotkeys can be disabled through hotkey_mask");
1921 if (parse_strtoul(buf, 1, &t))
1922 return -EINVAL;
1924 if (t == 0)
1925 return -EPERM;
1927 return count;
1930 static struct device_attribute dev_attr_hotkey_enable =
1931 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1932 hotkey_enable_show, hotkey_enable_store);
1934 /* sysfs hotkey mask --------------------------------------------------- */
1935 static ssize_t hotkey_mask_show(struct device *dev,
1936 struct device_attribute *attr,
1937 char *buf)
1939 int res;
1941 if (mutex_lock_killable(&hotkey_mutex))
1942 return -ERESTARTSYS;
1943 res = hotkey_mask_get();
1944 mutex_unlock(&hotkey_mutex);
1946 return (res)?
1947 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1950 static ssize_t hotkey_mask_store(struct device *dev,
1951 struct device_attribute *attr,
1952 const char *buf, size_t count)
1954 unsigned long t;
1955 int res;
1957 if (parse_strtoul(buf, 0xffffffffUL, &t))
1958 return -EINVAL;
1960 if (mutex_lock_killable(&hotkey_mutex))
1961 return -ERESTARTSYS;
1963 res = hotkey_mask_set(t);
1965 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1966 hotkey_poll_setup(1);
1967 #endif
1969 mutex_unlock(&hotkey_mutex);
1971 tpacpi_disclose_usertask("hotkey_mask", "set to 0x%08lx\n", t);
1973 return (res) ? res : count;
1976 static struct device_attribute dev_attr_hotkey_mask =
1977 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1978 hotkey_mask_show, hotkey_mask_store);
1980 /* sysfs hotkey bios_enabled ------------------------------------------- */
1981 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1982 struct device_attribute *attr,
1983 char *buf)
1985 return sprintf(buf, "0\n");
1988 static struct device_attribute dev_attr_hotkey_bios_enabled =
1989 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1991 /* sysfs hotkey bios_mask ---------------------------------------------- */
1992 static ssize_t hotkey_bios_mask_show(struct device *dev,
1993 struct device_attribute *attr,
1994 char *buf)
1996 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1999 static struct device_attribute dev_attr_hotkey_bios_mask =
2000 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
2002 /* sysfs hotkey all_mask ----------------------------------------------- */
2003 static ssize_t hotkey_all_mask_show(struct device *dev,
2004 struct device_attribute *attr,
2005 char *buf)
2007 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2008 hotkey_all_mask | hotkey_source_mask);
2011 static struct device_attribute dev_attr_hotkey_all_mask =
2012 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2014 /* sysfs hotkey recommended_mask --------------------------------------- */
2015 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2016 struct device_attribute *attr,
2017 char *buf)
2019 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2020 (hotkey_all_mask | hotkey_source_mask)
2021 & ~hotkey_reserved_mask);
2024 static struct device_attribute dev_attr_hotkey_recommended_mask =
2025 __ATTR(hotkey_recommended_mask, S_IRUGO,
2026 hotkey_recommended_mask_show, NULL);
2028 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2030 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2031 static ssize_t hotkey_source_mask_show(struct device *dev,
2032 struct device_attribute *attr,
2033 char *buf)
2035 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2038 static ssize_t hotkey_source_mask_store(struct device *dev,
2039 struct device_attribute *attr,
2040 const char *buf, size_t count)
2042 unsigned long t;
2044 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2045 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2046 return -EINVAL;
2048 if (mutex_lock_killable(&hotkey_mutex))
2049 return -ERESTARTSYS;
2051 HOTKEY_CONFIG_CRITICAL_START
2052 hotkey_source_mask = t;
2053 HOTKEY_CONFIG_CRITICAL_END
2055 hotkey_poll_setup(1);
2057 mutex_unlock(&hotkey_mutex);
2059 tpacpi_disclose_usertask("hotkey_source_mask", "set to 0x%08lx\n", t);
2061 return count;
2064 static struct device_attribute dev_attr_hotkey_source_mask =
2065 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2066 hotkey_source_mask_show, hotkey_source_mask_store);
2068 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2069 static ssize_t hotkey_poll_freq_show(struct device *dev,
2070 struct device_attribute *attr,
2071 char *buf)
2073 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2076 static ssize_t hotkey_poll_freq_store(struct device *dev,
2077 struct device_attribute *attr,
2078 const char *buf, size_t count)
2080 unsigned long t;
2082 if (parse_strtoul(buf, 25, &t))
2083 return -EINVAL;
2085 if (mutex_lock_killable(&hotkey_mutex))
2086 return -ERESTARTSYS;
2088 hotkey_poll_freq = t;
2090 hotkey_poll_setup(1);
2091 mutex_unlock(&hotkey_mutex);
2093 tpacpi_disclose_usertask("hotkey_poll_freq", "set to %lu\n", t);
2095 return count;
2098 static struct device_attribute dev_attr_hotkey_poll_freq =
2099 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2100 hotkey_poll_freq_show, hotkey_poll_freq_store);
2102 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2104 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2105 static ssize_t hotkey_radio_sw_show(struct device *dev,
2106 struct device_attribute *attr,
2107 char *buf)
2109 int res, s;
2110 res = hotkey_get_wlsw(&s);
2111 if (res < 0)
2112 return res;
2114 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2117 static struct device_attribute dev_attr_hotkey_radio_sw =
2118 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2120 static void hotkey_radio_sw_notify_change(void)
2122 if (tp_features.hotkey_wlsw)
2123 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2124 "hotkey_radio_sw");
2127 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2128 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2129 struct device_attribute *attr,
2130 char *buf)
2132 int res, s;
2133 res = hotkey_get_tablet_mode(&s);
2134 if (res < 0)
2135 return res;
2137 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2140 static struct device_attribute dev_attr_hotkey_tablet_mode =
2141 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2143 static void hotkey_tablet_mode_notify_change(void)
2145 if (tp_features.hotkey_tablet)
2146 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2147 "hotkey_tablet_mode");
2150 /* sysfs hotkey report_mode -------------------------------------------- */
2151 static ssize_t hotkey_report_mode_show(struct device *dev,
2152 struct device_attribute *attr,
2153 char *buf)
2155 return snprintf(buf, PAGE_SIZE, "%d\n",
2156 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2159 static struct device_attribute dev_attr_hotkey_report_mode =
2160 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2162 /* sysfs wakeup reason (pollable) -------------------------------------- */
2163 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2164 struct device_attribute *attr,
2165 char *buf)
2167 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2170 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2171 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2173 static void hotkey_wakeup_reason_notify_change(void)
2175 if (tp_features.hotkey_mask)
2176 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2177 "wakeup_reason");
2180 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2181 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2182 struct device_attribute *attr,
2183 char *buf)
2185 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2188 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2189 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2190 hotkey_wakeup_hotunplug_complete_show, NULL);
2192 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2194 if (tp_features.hotkey_mask)
2195 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2196 "wakeup_hotunplug_complete");
2199 /* --------------------------------------------------------------------- */
2201 static struct attribute *hotkey_attributes[] __initdata = {
2202 &dev_attr_hotkey_enable.attr,
2203 &dev_attr_hotkey_bios_enabled.attr,
2204 &dev_attr_hotkey_report_mode.attr,
2205 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2206 &dev_attr_hotkey_mask.attr,
2207 &dev_attr_hotkey_all_mask.attr,
2208 &dev_attr_hotkey_recommended_mask.attr,
2209 &dev_attr_hotkey_source_mask.attr,
2210 &dev_attr_hotkey_poll_freq.attr,
2211 #endif
2214 static struct attribute *hotkey_mask_attributes[] __initdata = {
2215 &dev_attr_hotkey_bios_mask.attr,
2216 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2217 &dev_attr_hotkey_mask.attr,
2218 &dev_attr_hotkey_all_mask.attr,
2219 &dev_attr_hotkey_recommended_mask.attr,
2220 #endif
2221 &dev_attr_hotkey_wakeup_reason.attr,
2222 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2225 static void bluetooth_update_rfk(void);
2226 static void wan_update_rfk(void);
2227 static void uwb_update_rfk(void);
2228 static void tpacpi_send_radiosw_update(void)
2230 int wlsw;
2232 /* Sync these BEFORE sending any rfkill events */
2233 if (tp_features.bluetooth)
2234 bluetooth_update_rfk();
2235 if (tp_features.wan)
2236 wan_update_rfk();
2237 if (tp_features.uwb)
2238 uwb_update_rfk();
2240 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2241 mutex_lock(&tpacpi_inputdev_send_mutex);
2243 input_report_switch(tpacpi_inputdev,
2244 SW_RFKILL_ALL, !!wlsw);
2245 input_sync(tpacpi_inputdev);
2247 mutex_unlock(&tpacpi_inputdev_send_mutex);
2249 hotkey_radio_sw_notify_change();
2252 static void hotkey_exit(void)
2254 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2255 hotkey_poll_stop_sync();
2256 #endif
2258 if (hotkey_dev_attributes)
2259 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2261 kfree(hotkey_keycode_map);
2263 if (tp_features.hotkey) {
2264 dbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_HKEY,
2265 "restoring original hot key mask\n");
2266 /* no short-circuit boolean operator below! */
2267 if ((hotkey_mask_set(hotkey_orig_mask) |
2268 hotkey_status_set(false)) != 0)
2269 printk(TPACPI_ERR
2270 "failed to restore hot key mask "
2271 "to BIOS defaults\n");
2275 static int __init hotkey_init(struct ibm_init_struct *iibm)
2277 /* Requirements for changing the default keymaps:
2279 * 1. Many of the keys are mapped to KEY_RESERVED for very
2280 * good reasons. Do not change them unless you have deep
2281 * knowledge on the IBM and Lenovo ThinkPad firmware for
2282 * the various ThinkPad models. The driver behaves
2283 * differently for KEY_RESERVED: such keys have their
2284 * hot key mask *unset* in mask_recommended, and also
2285 * in the initial hot key mask programmed into the
2286 * firmware at driver load time, which means the firm-
2287 * ware may react very differently if you change them to
2288 * something else;
2290 * 2. You must be subscribed to the linux-thinkpad and
2291 * ibm-acpi-devel mailing lists, and you should read the
2292 * list archives since 2007 if you want to change the
2293 * keymaps. This requirement exists so that you will
2294 * know the past history of problems with the thinkpad-
2295 * acpi driver keymaps, and also that you will be
2296 * listening to any bug reports;
2298 * 3. Do not send thinkpad-acpi specific patches directly to
2299 * for merging, *ever*. Send them to the linux-acpi
2300 * mailinglist for comments. Merging is to be done only
2301 * through acpi-test and the ACPI maintainer.
2303 * If the above is too much to ask, don't change the keymap.
2304 * Ask the thinkpad-acpi maintainer to do it, instead.
2306 static u16 ibm_keycode_map[] __initdata = {
2307 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2308 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2309 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2310 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2312 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2313 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2314 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2315 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2317 /* brightness: firmware always reacts to them, unless
2318 * X.org did some tricks in the radeon BIOS scratch
2319 * registers of *some* models */
2320 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2321 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2323 /* Thinklight: firmware always react to it */
2324 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2326 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2327 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2329 /* Volume: firmware always react to it and reprograms
2330 * the built-in *extra* mixer. Never map it to control
2331 * another mixer by default. */
2332 KEY_RESERVED, /* 0x14: VOLUME UP */
2333 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2334 KEY_RESERVED, /* 0x16: MUTE */
2336 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2338 /* (assignments unknown, please report if found) */
2339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2340 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2342 static u16 lenovo_keycode_map[] __initdata = {
2343 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2344 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2345 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2346 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2348 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2349 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2350 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2351 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2353 /* These either have to go through ACPI video, or
2354 * act like in the IBM ThinkPads, so don't ever
2355 * enable them by default */
2356 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2357 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2359 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2361 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2362 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2364 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2365 * react to it and reprograms the built-in *extra* mixer.
2366 * Never map it to control another mixer by default.
2368 * T60?, T61, R60?, R61: firmware and EC tries to send
2369 * these over the regular keyboard, so these are no-ops,
2370 * but there are still weird bugs re. MUTE, so do not
2371 * change unless you get test reports from all Lenovo
2372 * models. May cause the BIOS to interfere with the
2373 * HDA mixer.
2375 KEY_RESERVED, /* 0x14: VOLUME UP */
2376 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2377 KEY_RESERVED, /* 0x16: MUTE */
2379 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2381 /* (assignments unknown, please report if found) */
2382 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2383 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2386 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2387 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2388 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2390 int res, i;
2391 int status;
2392 int hkeyv;
2394 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2395 "initializing hotkey subdriver\n");
2397 BUG_ON(!tpacpi_inputdev);
2398 BUG_ON(tpacpi_inputdev->open != NULL ||
2399 tpacpi_inputdev->close != NULL);
2401 TPACPI_ACPIHANDLE_INIT(hkey);
2402 mutex_init(&hotkey_mutex);
2404 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2405 mutex_init(&hotkey_thread_mutex);
2406 mutex_init(&hotkey_thread_data_mutex);
2407 #endif
2409 /* hotkey not supported on 570 */
2410 tp_features.hotkey = hkey_handle != NULL;
2412 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2413 "hotkeys are %s\n",
2414 str_supported(tp_features.hotkey));
2416 if (!tp_features.hotkey)
2417 return 1;
2419 tpacpi_disable_brightness_delay();
2421 hotkey_dev_attributes = create_attr_set(13, NULL);
2422 if (!hotkey_dev_attributes)
2423 return -ENOMEM;
2424 res = add_many_to_attr_set(hotkey_dev_attributes,
2425 hotkey_attributes,
2426 ARRAY_SIZE(hotkey_attributes));
2427 if (res)
2428 goto err_exit;
2430 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2431 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2432 for HKEY interface version 0x100 */
2433 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2434 if ((hkeyv >> 8) != 1) {
2435 printk(TPACPI_ERR "unknown version of the "
2436 "HKEY interface: 0x%x\n", hkeyv);
2437 printk(TPACPI_ERR "please report this to %s\n",
2438 TPACPI_MAIL);
2439 } else {
2441 * MHKV 0x100 in A31, R40, R40e,
2442 * T4x, X31, and later
2444 tp_features.hotkey_mask = 1;
2445 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2446 "firmware HKEY interface version: 0x%x\n",
2447 hkeyv);
2451 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2452 "hotkey masks are %s\n",
2453 str_supported(tp_features.hotkey_mask));
2455 if (tp_features.hotkey_mask) {
2456 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2457 "MHKA", "qd")) {
2458 printk(TPACPI_ERR
2459 "missing MHKA handler, "
2460 "please report this to %s\n",
2461 TPACPI_MAIL);
2462 /* FN+F12, FN+F4, FN+F3 */
2463 hotkey_all_mask = 0x080cU;
2467 /* hotkey_source_mask *must* be zero for
2468 * the first hotkey_mask_get */
2469 if (tp_features.hotkey_mask) {
2470 res = hotkey_mask_get();
2471 if (res)
2472 goto err_exit;
2474 hotkey_orig_mask = hotkey_mask;
2475 res = add_many_to_attr_set(
2476 hotkey_dev_attributes,
2477 hotkey_mask_attributes,
2478 ARRAY_SIZE(hotkey_mask_attributes));
2479 if (res)
2480 goto err_exit;
2483 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2484 if (tp_features.hotkey_mask) {
2485 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2486 & ~hotkey_all_mask;
2487 } else {
2488 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2491 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2492 "hotkey source mask 0x%08x, polling freq %d\n",
2493 hotkey_source_mask, hotkey_poll_freq);
2494 #endif
2496 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2497 if (dbg_wlswemul) {
2498 tp_features.hotkey_wlsw = 1;
2499 printk(TPACPI_INFO
2500 "radio switch emulation enabled\n");
2501 } else
2502 #endif
2503 /* Not all thinkpads have a hardware radio switch */
2504 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2505 tp_features.hotkey_wlsw = 1;
2506 printk(TPACPI_INFO
2507 "radio switch found; radios are %s\n",
2508 enabled(status, 0));
2510 if (tp_features.hotkey_wlsw)
2511 res = add_to_attr_set(hotkey_dev_attributes,
2512 &dev_attr_hotkey_radio_sw.attr);
2514 /* For X41t, X60t, X61t Tablets... */
2515 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2516 tp_features.hotkey_tablet = 1;
2517 printk(TPACPI_INFO
2518 "possible tablet mode switch found; "
2519 "ThinkPad in %s mode\n",
2520 (status & TP_HOTKEY_TABLET_MASK)?
2521 "tablet" : "laptop");
2522 res = add_to_attr_set(hotkey_dev_attributes,
2523 &dev_attr_hotkey_tablet_mode.attr);
2526 if (!res)
2527 res = register_attr_set_with_sysfs(
2528 hotkey_dev_attributes,
2529 &tpacpi_pdev->dev.kobj);
2530 if (res)
2531 goto err_exit;
2533 /* Set up key map */
2535 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2536 GFP_KERNEL);
2537 if (!hotkey_keycode_map) {
2538 printk(TPACPI_ERR
2539 "failed to allocate memory for key map\n");
2540 res = -ENOMEM;
2541 goto err_exit;
2544 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2545 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2546 "using Lenovo default hot key map\n");
2547 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2548 TPACPI_HOTKEY_MAP_SIZE);
2549 } else {
2550 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2551 "using IBM default hot key map\n");
2552 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2553 TPACPI_HOTKEY_MAP_SIZE);
2556 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2557 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2558 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2559 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2560 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2561 tpacpi_inputdev->keycode = hotkey_keycode_map;
2562 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2563 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2564 set_bit(hotkey_keycode_map[i],
2565 tpacpi_inputdev->keybit);
2566 } else {
2567 if (i < sizeof(hotkey_reserved_mask)*8)
2568 hotkey_reserved_mask |= 1 << i;
2572 if (tp_features.hotkey_wlsw) {
2573 set_bit(EV_SW, tpacpi_inputdev->evbit);
2574 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2576 if (tp_features.hotkey_tablet) {
2577 set_bit(EV_SW, tpacpi_inputdev->evbit);
2578 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2581 /* Do not issue duplicate brightness change events to
2582 * userspace */
2583 if (!tp_features.bright_acpimode)
2584 /* update bright_acpimode... */
2585 tpacpi_check_std_acpi_brightness_support();
2587 if (tp_features.bright_acpimode) {
2588 printk(TPACPI_INFO
2589 "This ThinkPad has standard ACPI backlight "
2590 "brightness control, supported by the ACPI "
2591 "video driver\n");
2592 printk(TPACPI_NOTICE
2593 "Disabling thinkpad-acpi brightness events "
2594 "by default...\n");
2596 /* The hotkey_reserved_mask change below is not
2597 * necessary while the keys are at KEY_RESERVED in the
2598 * default map, but better safe than sorry, leave it
2599 * here as a marker of what we have to do, especially
2600 * when we finally become able to set this at runtime
2601 * on response to X.org requests */
2602 hotkey_reserved_mask |=
2603 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2604 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2607 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2608 "enabling firmware HKEY event interface...\n");
2609 res = hotkey_status_set(true);
2610 if (res) {
2611 hotkey_exit();
2612 return res;
2614 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2615 & ~hotkey_reserved_mask)
2616 | hotkey_orig_mask);
2617 if (res < 0 && res != -ENXIO) {
2618 hotkey_exit();
2619 return res;
2622 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_HKEY,
2623 "legacy ibm/hotkey event reporting over procfs %s\n",
2624 (hotkey_report_mode < 2) ?
2625 "enabled" : "disabled");
2627 tpacpi_inputdev->open = &hotkey_inputdev_open;
2628 tpacpi_inputdev->close = &hotkey_inputdev_close;
2630 hotkey_poll_setup_safe(1);
2631 tpacpi_send_radiosw_update();
2632 tpacpi_input_send_tabletsw();
2634 return 0;
2636 err_exit:
2637 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2638 hotkey_dev_attributes = NULL;
2640 return (res < 0)? res : 1;
2643 static bool hotkey_notify_hotkey(const u32 hkey,
2644 bool *send_acpi_ev,
2645 bool *ignore_acpi_ev)
2647 /* 0x1000-0x1FFF: key presses */
2648 unsigned int scancode = hkey & 0xfff;
2649 *send_acpi_ev = true;
2650 *ignore_acpi_ev = false;
2652 if (scancode > 0 && scancode < 0x21) {
2653 scancode--;
2654 if (!(hotkey_source_mask & (1 << scancode))) {
2655 tpacpi_input_send_key(scancode);
2656 *send_acpi_ev = false;
2657 } else {
2658 *ignore_acpi_ev = true;
2660 return true;
2662 return false;
2665 static bool hotkey_notify_wakeup(const u32 hkey,
2666 bool *send_acpi_ev,
2667 bool *ignore_acpi_ev)
2669 /* 0x2000-0x2FFF: Wakeup reason */
2670 *send_acpi_ev = true;
2671 *ignore_acpi_ev = false;
2673 switch (hkey) {
2674 case 0x2304: /* suspend, undock */
2675 case 0x2404: /* hibernation, undock */
2676 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2677 *ignore_acpi_ev = true;
2678 break;
2680 case 0x2305: /* suspend, bay eject */
2681 case 0x2405: /* hibernation, bay eject */
2682 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2683 *ignore_acpi_ev = true;
2684 break;
2686 case 0x2313: /* Battery on critical low level (S3) */
2687 case 0x2413: /* Battery on critical low level (S4) */
2688 printk(TPACPI_ALERT
2689 "EMERGENCY WAKEUP: battery almost empty\n");
2690 /* how to auto-heal: */
2691 /* 2313: woke up from S3, go to S4/S5 */
2692 /* 2413: woke up from S4, go to S5 */
2693 break;
2695 default:
2696 return false;
2699 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2700 printk(TPACPI_INFO
2701 "woke up due to a hot-unplug "
2702 "request...\n");
2703 hotkey_wakeup_reason_notify_change();
2705 return true;
2708 static bool hotkey_notify_usrevent(const u32 hkey,
2709 bool *send_acpi_ev,
2710 bool *ignore_acpi_ev)
2712 /* 0x5000-0x5FFF: human interface helpers */
2713 *send_acpi_ev = true;
2714 *ignore_acpi_ev = false;
2716 switch (hkey) {
2717 case 0x5010: /* Lenovo new BIOS: brightness changed */
2718 case 0x500b: /* X61t: tablet pen inserted into bay */
2719 case 0x500c: /* X61t: tablet pen removed from bay */
2720 return true;
2722 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2723 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2724 tpacpi_input_send_tabletsw();
2725 hotkey_tablet_mode_notify_change();
2726 *send_acpi_ev = false;
2727 return true;
2729 case 0x5001:
2730 case 0x5002:
2731 /* LID switch events. Do not propagate */
2732 *ignore_acpi_ev = true;
2733 return true;
2735 default:
2736 return false;
2740 static bool hotkey_notify_thermal(const u32 hkey,
2741 bool *send_acpi_ev,
2742 bool *ignore_acpi_ev)
2744 /* 0x6000-0x6FFF: thermal alarms */
2745 *send_acpi_ev = true;
2746 *ignore_acpi_ev = false;
2748 switch (hkey) {
2749 case 0x6011:
2750 printk(TPACPI_CRIT
2751 "THERMAL ALARM: battery is too hot!\n");
2752 /* recommended action: warn user through gui */
2753 return true;
2754 case 0x6012:
2755 printk(TPACPI_ALERT
2756 "THERMAL EMERGENCY: battery is extremely hot!\n");
2757 /* recommended action: immediate sleep/hibernate */
2758 return true;
2759 case 0x6021:
2760 printk(TPACPI_CRIT
2761 "THERMAL ALARM: "
2762 "a sensor reports something is too hot!\n");
2763 /* recommended action: warn user through gui, that */
2764 /* some internal component is too hot */
2765 return true;
2766 case 0x6022:
2767 printk(TPACPI_ALERT
2768 "THERMAL EMERGENCY: "
2769 "a sensor reports something is extremely hot!\n");
2770 /* recommended action: immediate sleep/hibernate */
2771 return true;
2772 case 0x6030:
2773 printk(TPACPI_INFO
2774 "EC reports that Thermal Table has changed\n");
2775 /* recommended action: do nothing, we don't have
2776 * Lenovo ATM information */
2777 return true;
2778 default:
2779 printk(TPACPI_ALERT
2780 "THERMAL ALERT: unknown thermal alarm received\n");
2781 return false;
2785 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2787 u32 hkey;
2788 bool send_acpi_ev;
2789 bool ignore_acpi_ev;
2790 bool known_ev;
2792 if (event != 0x80) {
2793 printk(TPACPI_ERR
2794 "unknown HKEY notification event %d\n", event);
2795 /* forward it to userspace, maybe it knows how to handle it */
2796 acpi_bus_generate_netlink_event(
2797 ibm->acpi->device->pnp.device_class,
2798 dev_name(&ibm->acpi->device->dev),
2799 event, 0);
2800 return;
2803 while (1) {
2804 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2805 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2806 return;
2809 if (hkey == 0) {
2810 /* queue empty */
2811 return;
2814 send_acpi_ev = true;
2815 ignore_acpi_ev = false;
2817 switch (hkey >> 12) {
2818 case 1:
2819 /* 0x1000-0x1FFF: key presses */
2820 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2821 &ignore_acpi_ev);
2822 break;
2823 case 2:
2824 /* 0x2000-0x2FFF: Wakeup reason */
2825 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2826 &ignore_acpi_ev);
2827 break;
2828 case 3:
2829 /* 0x3000-0x3FFF: bay-related wakeups */
2830 if (hkey == 0x3003) {
2831 hotkey_autosleep_ack = 1;
2832 printk(TPACPI_INFO
2833 "bay ejected\n");
2834 hotkey_wakeup_hotunplug_complete_notify_change();
2835 known_ev = true;
2836 } else {
2837 known_ev = false;
2839 break;
2840 case 4:
2841 /* 0x4000-0x4FFF: dock-related wakeups */
2842 if (hkey == 0x4003) {
2843 hotkey_autosleep_ack = 1;
2844 printk(TPACPI_INFO
2845 "undocked\n");
2846 hotkey_wakeup_hotunplug_complete_notify_change();
2847 known_ev = true;
2848 } else {
2849 known_ev = false;
2851 break;
2852 case 5:
2853 /* 0x5000-0x5FFF: human interface helpers */
2854 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2855 &ignore_acpi_ev);
2856 break;
2857 case 6:
2858 /* 0x6000-0x6FFF: thermal alarms */
2859 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2860 &ignore_acpi_ev);
2861 break;
2862 case 7:
2863 /* 0x7000-0x7FFF: misc */
2864 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2865 tpacpi_send_radiosw_update();
2866 send_acpi_ev = 0;
2867 known_ev = true;
2868 break;
2870 /* fallthrough to default */
2871 default:
2872 known_ev = false;
2874 if (!known_ev) {
2875 printk(TPACPI_NOTICE
2876 "unhandled HKEY event 0x%04x\n", hkey);
2877 printk(TPACPI_NOTICE
2878 "please report the conditions when this "
2879 "event happened to %s\n", TPACPI_MAIL);
2882 /* Legacy events */
2883 if (!ignore_acpi_ev &&
2884 (send_acpi_ev || hotkey_report_mode < 2)) {
2885 acpi_bus_generate_proc_event(ibm->acpi->device,
2886 event, hkey);
2889 /* netlink events */
2890 if (!ignore_acpi_ev && send_acpi_ev) {
2891 acpi_bus_generate_netlink_event(
2892 ibm->acpi->device->pnp.device_class,
2893 dev_name(&ibm->acpi->device->dev),
2894 event, hkey);
2899 static void hotkey_suspend(pm_message_t state)
2901 /* Do these on suspend, we get the events on early resume! */
2902 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2903 hotkey_autosleep_ack = 0;
2906 static void hotkey_resume(void)
2908 tpacpi_disable_brightness_delay();
2910 if (hotkey_mask_get())
2911 printk(TPACPI_ERR
2912 "error while trying to read hot key mask "
2913 "from firmware\n");
2914 tpacpi_send_radiosw_update();
2915 hotkey_tablet_mode_notify_change();
2916 hotkey_wakeup_reason_notify_change();
2917 hotkey_wakeup_hotunplug_complete_notify_change();
2918 hotkey_poll_setup_safe(0);
2921 /* procfs -------------------------------------------------------------- */
2922 static int hotkey_read(char *p)
2924 int res, status;
2925 int len = 0;
2927 if (!tp_features.hotkey) {
2928 len += sprintf(p + len, "status:\t\tnot supported\n");
2929 return len;
2932 if (mutex_lock_killable(&hotkey_mutex))
2933 return -ERESTARTSYS;
2934 res = hotkey_status_get(&status);
2935 if (!res)
2936 res = hotkey_mask_get();
2937 mutex_unlock(&hotkey_mutex);
2938 if (res)
2939 return res;
2941 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2942 if (tp_features.hotkey_mask) {
2943 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2944 len += sprintf(p + len,
2945 "commands:\tenable, disable, reset, <mask>\n");
2946 } else {
2947 len += sprintf(p + len, "mask:\t\tnot supported\n");
2948 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2951 return len;
2954 static void hotkey_enabledisable_warn(void)
2956 tpacpi_log_usertask("procfs hotkey enable/disable");
2957 WARN(1, TPACPI_WARN
2958 "hotkey enable/disable functionality has been "
2959 "removed from the driver. Hotkeys are always enabled.\n");
2962 static int hotkey_write(char *buf)
2964 int res;
2965 u32 mask;
2966 char *cmd;
2968 if (!tp_features.hotkey)
2969 return -ENODEV;
2971 if (mutex_lock_killable(&hotkey_mutex))
2972 return -ERESTARTSYS;
2974 mask = hotkey_mask;
2976 res = 0;
2977 while ((cmd = next_cmd(&buf))) {
2978 if (strlencmp(cmd, "enable") == 0) {
2979 hotkey_enabledisable_warn();
2980 } else if (strlencmp(cmd, "disable") == 0) {
2981 hotkey_enabledisable_warn();
2982 res = -EPERM;
2983 } else if (strlencmp(cmd, "reset") == 0) {
2984 mask = hotkey_orig_mask;
2985 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2986 /* mask set */
2987 } else if (sscanf(cmd, "%x", &mask) == 1) {
2988 /* mask set */
2989 } else {
2990 res = -EINVAL;
2991 goto errexit;
2995 if (!res)
2996 tpacpi_disclose_usertask("procfs hotkey",
2997 "set mask to 0x%08x\n", mask);
2999 if (!res && mask != hotkey_mask)
3000 res = hotkey_mask_set(mask);
3002 errexit:
3003 mutex_unlock(&hotkey_mutex);
3004 return res;
3007 static const struct acpi_device_id ibm_htk_device_ids[] = {
3008 {TPACPI_ACPI_HKEY_HID, 0},
3009 {"", 0},
3012 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
3013 .hid = ibm_htk_device_ids,
3014 .notify = hotkey_notify,
3015 .handle = &hkey_handle,
3016 .type = ACPI_DEVICE_NOTIFY,
3019 static struct ibm_struct hotkey_driver_data = {
3020 .name = "hotkey",
3021 .read = hotkey_read,
3022 .write = hotkey_write,
3023 .exit = hotkey_exit,
3024 .resume = hotkey_resume,
3025 .suspend = hotkey_suspend,
3026 .acpi = &ibm_hotkey_acpidriver,
3029 /*************************************************************************
3030 * Bluetooth subdriver
3033 enum {
3034 /* ACPI GBDC/SBDC bits */
3035 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3036 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3037 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3038 off / last state */
3041 enum {
3042 /* ACPI \BLTH commands */
3043 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3044 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3045 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3046 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3047 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3050 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3052 static struct rfkill *tpacpi_bluetooth_rfkill;
3054 static void bluetooth_suspend(pm_message_t state)
3056 /* Try to make sure radio will resume powered off */
3057 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3058 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3059 vdbg_printk(TPACPI_DBG_RFKILL,
3060 "bluetooth power down on resume request failed\n");
3063 static int bluetooth_get_radiosw(void)
3065 int status;
3067 if (!tp_features.bluetooth)
3068 return -ENODEV;
3070 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3071 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3072 return RFKILL_STATE_HARD_BLOCKED;
3074 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3075 if (dbg_bluetoothemul)
3076 return (tpacpi_bluetooth_emulstate) ?
3077 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3078 #endif
3080 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3081 return -EIO;
3083 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3084 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3087 static void bluetooth_update_rfk(void)
3089 int status;
3091 if (!tpacpi_bluetooth_rfkill)
3092 return;
3094 status = bluetooth_get_radiosw();
3095 if (status < 0)
3096 return;
3097 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3099 vdbg_printk(TPACPI_DBG_RFKILL,
3100 "forced rfkill state to %d\n",
3101 status);
3104 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3106 int status;
3108 if (!tp_features.bluetooth)
3109 return -ENODEV;
3111 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3112 * reason to risk weird behaviour. */
3113 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3114 && radio_on)
3115 return -EPERM;
3117 vdbg_printk(TPACPI_DBG_RFKILL,
3118 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3120 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3121 if (dbg_bluetoothemul) {
3122 tpacpi_bluetooth_emulstate = !!radio_on;
3123 if (update_rfk)
3124 bluetooth_update_rfk();
3125 return 0;
3127 #endif
3129 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3130 if (radio_on)
3131 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3132 else
3133 status = 0;
3134 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3135 return -EIO;
3137 if (update_rfk)
3138 bluetooth_update_rfk();
3140 return 0;
3143 /* sysfs bluetooth enable ---------------------------------------------- */
3144 static ssize_t bluetooth_enable_show(struct device *dev,
3145 struct device_attribute *attr,
3146 char *buf)
3148 int status;
3150 printk_deprecated_rfkill_attribute("bluetooth_enable");
3152 status = bluetooth_get_radiosw();
3153 if (status < 0)
3154 return status;
3156 return snprintf(buf, PAGE_SIZE, "%d\n",
3157 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3160 static ssize_t bluetooth_enable_store(struct device *dev,
3161 struct device_attribute *attr,
3162 const char *buf, size_t count)
3164 unsigned long t;
3165 int res;
3167 printk_deprecated_rfkill_attribute("bluetooth_enable");
3169 if (parse_strtoul(buf, 1, &t))
3170 return -EINVAL;
3172 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3174 res = bluetooth_set_radiosw(t, 1);
3176 return (res) ? res : count;
3179 static struct device_attribute dev_attr_bluetooth_enable =
3180 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3181 bluetooth_enable_show, bluetooth_enable_store);
3183 /* --------------------------------------------------------------------- */
3185 static struct attribute *bluetooth_attributes[] = {
3186 &dev_attr_bluetooth_enable.attr,
3187 NULL
3190 static const struct attribute_group bluetooth_attr_group = {
3191 .attrs = bluetooth_attributes,
3194 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3196 int bts = bluetooth_get_radiosw();
3198 if (bts < 0)
3199 return bts;
3201 *state = bts;
3202 return 0;
3205 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3207 dbg_printk(TPACPI_DBG_RFKILL,
3208 "request to change radio state to %d\n", state);
3209 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3212 static void bluetooth_shutdown(void)
3214 /* Order firmware to save current state to NVRAM */
3215 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3216 TP_ACPI_BLTH_SAVE_STATE))
3217 printk(TPACPI_NOTICE
3218 "failed to save bluetooth state to NVRAM\n");
3219 else
3220 vdbg_printk(TPACPI_DBG_RFKILL,
3221 "bluestooth state saved to NVRAM\n");
3224 static void bluetooth_exit(void)
3226 bluetooth_shutdown();
3228 if (tpacpi_bluetooth_rfkill)
3229 rfkill_unregister(tpacpi_bluetooth_rfkill);
3231 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3232 &bluetooth_attr_group);
3235 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3237 int res;
3238 int status = 0;
3240 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3241 "initializing bluetooth subdriver\n");
3243 TPACPI_ACPIHANDLE_INIT(hkey);
3245 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3246 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3247 tp_features.bluetooth = hkey_handle &&
3248 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3250 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3251 "bluetooth is %s, status 0x%02x\n",
3252 str_supported(tp_features.bluetooth),
3253 status);
3255 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3256 if (dbg_bluetoothemul) {
3257 tp_features.bluetooth = 1;
3258 printk(TPACPI_INFO
3259 "bluetooth switch emulation enabled\n");
3260 } else
3261 #endif
3262 if (tp_features.bluetooth &&
3263 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3264 /* no bluetooth hardware present in system */
3265 tp_features.bluetooth = 0;
3266 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3267 "bluetooth hardware not installed\n");
3270 if (!tp_features.bluetooth)
3271 return 1;
3273 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3274 &bluetooth_attr_group);
3275 if (res)
3276 return res;
3278 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3279 &tpacpi_bluetooth_rfkill,
3280 RFKILL_TYPE_BLUETOOTH,
3281 TPACPI_RFK_BLUETOOTH_SW_NAME,
3282 true,
3283 tpacpi_bluetooth_rfk_set,
3284 tpacpi_bluetooth_rfk_get);
3285 if (res) {
3286 bluetooth_exit();
3287 return res;
3290 return 0;
3293 /* procfs -------------------------------------------------------------- */
3294 static int bluetooth_read(char *p)
3296 int len = 0;
3297 int status = bluetooth_get_radiosw();
3299 if (!tp_features.bluetooth)
3300 len += sprintf(p + len, "status:\t\tnot supported\n");
3301 else {
3302 len += sprintf(p + len, "status:\t\t%s\n",
3303 (status == RFKILL_STATE_UNBLOCKED) ?
3304 "enabled" : "disabled");
3305 len += sprintf(p + len, "commands:\tenable, disable\n");
3308 return len;
3311 static int bluetooth_write(char *buf)
3313 char *cmd;
3314 int state = -1;
3316 if (!tp_features.bluetooth)
3317 return -ENODEV;
3319 while ((cmd = next_cmd(&buf))) {
3320 if (strlencmp(cmd, "enable") == 0) {
3321 state = 1;
3322 } else if (strlencmp(cmd, "disable") == 0) {
3323 state = 0;
3324 } else
3325 return -EINVAL;
3328 if (state != -1) {
3329 tpacpi_disclose_usertask("procfs bluetooth",
3330 "attempt to %s\n",
3331 state ? "enable" : "disable");
3332 bluetooth_set_radiosw(state, 1);
3335 return 0;
3338 static struct ibm_struct bluetooth_driver_data = {
3339 .name = "bluetooth",
3340 .read = bluetooth_read,
3341 .write = bluetooth_write,
3342 .exit = bluetooth_exit,
3343 .suspend = bluetooth_suspend,
3344 .shutdown = bluetooth_shutdown,
3347 /*************************************************************************
3348 * Wan subdriver
3351 enum {
3352 /* ACPI GWAN/SWAN bits */
3353 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3354 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3355 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3356 off / last state */
3359 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3361 static struct rfkill *tpacpi_wan_rfkill;
3363 static void wan_suspend(pm_message_t state)
3365 /* Try to make sure radio will resume powered off */
3366 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3367 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3368 vdbg_printk(TPACPI_DBG_RFKILL,
3369 "WWAN power down on resume request failed\n");
3372 static int wan_get_radiosw(void)
3374 int status;
3376 if (!tp_features.wan)
3377 return -ENODEV;
3379 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3380 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3381 return RFKILL_STATE_HARD_BLOCKED;
3383 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3384 if (dbg_wwanemul)
3385 return (tpacpi_wwan_emulstate) ?
3386 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3387 #endif
3389 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3390 return -EIO;
3392 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3393 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3396 static void wan_update_rfk(void)
3398 int status;
3400 if (!tpacpi_wan_rfkill)
3401 return;
3403 status = wan_get_radiosw();
3404 if (status < 0)
3405 return;
3406 rfkill_force_state(tpacpi_wan_rfkill, status);
3408 vdbg_printk(TPACPI_DBG_RFKILL,
3409 "forced rfkill state to %d\n",
3410 status);
3413 static int wan_set_radiosw(int radio_on, int update_rfk)
3415 int status;
3417 if (!tp_features.wan)
3418 return -ENODEV;
3420 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3421 * reason to risk weird behaviour. */
3422 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3423 && radio_on)
3424 return -EPERM;
3426 vdbg_printk(TPACPI_DBG_RFKILL,
3427 "will %s WWAN\n", radio_on ? "enable" : "disable");
3429 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3430 if (dbg_wwanemul) {
3431 tpacpi_wwan_emulstate = !!radio_on;
3432 if (update_rfk)
3433 wan_update_rfk();
3434 return 0;
3436 #endif
3438 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3439 if (radio_on)
3440 status = TP_ACPI_WANCARD_RADIOSSW;
3441 else
3442 status = 0;
3443 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3444 return -EIO;
3446 if (update_rfk)
3447 wan_update_rfk();
3449 return 0;
3452 /* sysfs wan enable ---------------------------------------------------- */
3453 static ssize_t wan_enable_show(struct device *dev,
3454 struct device_attribute *attr,
3455 char *buf)
3457 int status;
3459 printk_deprecated_rfkill_attribute("wwan_enable");
3461 status = wan_get_radiosw();
3462 if (status < 0)
3463 return status;
3465 return snprintf(buf, PAGE_SIZE, "%d\n",
3466 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3469 static ssize_t wan_enable_store(struct device *dev,
3470 struct device_attribute *attr,
3471 const char *buf, size_t count)
3473 unsigned long t;
3474 int res;
3476 printk_deprecated_rfkill_attribute("wwan_enable");
3478 if (parse_strtoul(buf, 1, &t))
3479 return -EINVAL;
3481 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3483 res = wan_set_radiosw(t, 1);
3485 return (res) ? res : count;
3488 static struct device_attribute dev_attr_wan_enable =
3489 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3490 wan_enable_show, wan_enable_store);
3492 /* --------------------------------------------------------------------- */
3494 static struct attribute *wan_attributes[] = {
3495 &dev_attr_wan_enable.attr,
3496 NULL
3499 static const struct attribute_group wan_attr_group = {
3500 .attrs = wan_attributes,
3503 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3505 int wans = wan_get_radiosw();
3507 if (wans < 0)
3508 return wans;
3510 *state = wans;
3511 return 0;
3514 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3516 dbg_printk(TPACPI_DBG_RFKILL,
3517 "request to change radio state to %d\n", state);
3518 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3521 static void wan_shutdown(void)
3523 /* Order firmware to save current state to NVRAM */
3524 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3525 TP_ACPI_WGSV_SAVE_STATE))
3526 printk(TPACPI_NOTICE
3527 "failed to save WWAN state to NVRAM\n");
3528 else
3529 vdbg_printk(TPACPI_DBG_RFKILL,
3530 "WWAN state saved to NVRAM\n");
3533 static void wan_exit(void)
3535 wan_shutdown();
3537 if (tpacpi_wan_rfkill)
3538 rfkill_unregister(tpacpi_wan_rfkill);
3540 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3541 &wan_attr_group);
3544 static int __init wan_init(struct ibm_init_struct *iibm)
3546 int res;
3547 int status = 0;
3549 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3550 "initializing wan subdriver\n");
3552 TPACPI_ACPIHANDLE_INIT(hkey);
3554 tp_features.wan = hkey_handle &&
3555 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3557 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3558 "wan is %s, status 0x%02x\n",
3559 str_supported(tp_features.wan),
3560 status);
3562 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3563 if (dbg_wwanemul) {
3564 tp_features.wan = 1;
3565 printk(TPACPI_INFO
3566 "wwan switch emulation enabled\n");
3567 } else
3568 #endif
3569 if (tp_features.wan &&
3570 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3571 /* no wan hardware present in system */
3572 tp_features.wan = 0;
3573 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3574 "wan hardware not installed\n");
3577 if (!tp_features.wan)
3578 return 1;
3580 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3581 &wan_attr_group);
3582 if (res)
3583 return res;
3585 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3586 &tpacpi_wan_rfkill,
3587 RFKILL_TYPE_WWAN,
3588 TPACPI_RFK_WWAN_SW_NAME,
3589 true,
3590 tpacpi_wan_rfk_set,
3591 tpacpi_wan_rfk_get);
3592 if (res) {
3593 wan_exit();
3594 return res;
3597 return 0;
3600 /* procfs -------------------------------------------------------------- */
3601 static int wan_read(char *p)
3603 int len = 0;
3604 int status = wan_get_radiosw();
3606 tpacpi_disclose_usertask("procfs wan", "read");
3608 if (!tp_features.wan)
3609 len += sprintf(p + len, "status:\t\tnot supported\n");
3610 else {
3611 len += sprintf(p + len, "status:\t\t%s\n",
3612 (status == RFKILL_STATE_UNBLOCKED) ?
3613 "enabled" : "disabled");
3614 len += sprintf(p + len, "commands:\tenable, disable\n");
3617 return len;
3620 static int wan_write(char *buf)
3622 char *cmd;
3623 int state = -1;
3625 if (!tp_features.wan)
3626 return -ENODEV;
3628 while ((cmd = next_cmd(&buf))) {
3629 if (strlencmp(cmd, "enable") == 0) {
3630 state = 1;
3631 } else if (strlencmp(cmd, "disable") == 0) {
3632 state = 0;
3633 } else
3634 return -EINVAL;
3637 if (state != -1) {
3638 tpacpi_disclose_usertask("procfs wan",
3639 "attempt to %s\n",
3640 state ? "enable" : "disable");
3641 wan_set_radiosw(state, 1);
3644 return 0;
3647 static struct ibm_struct wan_driver_data = {
3648 .name = "wan",
3649 .read = wan_read,
3650 .write = wan_write,
3651 .exit = wan_exit,
3652 .suspend = wan_suspend,
3653 .shutdown = wan_shutdown,
3656 /*************************************************************************
3657 * UWB subdriver
3660 enum {
3661 /* ACPI GUWB/SUWB bits */
3662 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3663 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3666 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3668 static struct rfkill *tpacpi_uwb_rfkill;
3670 static int uwb_get_radiosw(void)
3672 int status;
3674 if (!tp_features.uwb)
3675 return -ENODEV;
3677 /* WLSW overrides UWB in firmware/hardware, reflect that */
3678 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3679 return RFKILL_STATE_HARD_BLOCKED;
3681 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3682 if (dbg_uwbemul)
3683 return (tpacpi_uwb_emulstate) ?
3684 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3685 #endif
3687 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3688 return -EIO;
3690 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3691 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3694 static void uwb_update_rfk(void)
3696 int status;
3698 if (!tpacpi_uwb_rfkill)
3699 return;
3701 status = uwb_get_radiosw();
3702 if (status < 0)
3703 return;
3704 rfkill_force_state(tpacpi_uwb_rfkill, status);
3706 vdbg_printk(TPACPI_DBG_RFKILL,
3707 "forced rfkill state to %d\n",
3708 status);
3711 static int uwb_set_radiosw(int radio_on, int update_rfk)
3713 int status;
3715 if (!tp_features.uwb)
3716 return -ENODEV;
3718 /* WLSW overrides UWB in firmware/hardware, but there is no
3719 * reason to risk weird behaviour. */
3720 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3721 && radio_on)
3722 return -EPERM;
3724 vdbg_printk(TPACPI_DBG_RFKILL,
3725 "will %s UWB\n", radio_on ? "enable" : "disable");
3727 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3728 if (dbg_uwbemul) {
3729 tpacpi_uwb_emulstate = !!radio_on;
3730 if (update_rfk)
3731 uwb_update_rfk();
3732 return 0;
3734 #endif
3736 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3737 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3738 return -EIO;
3740 if (update_rfk)
3741 uwb_update_rfk();
3743 return 0;
3746 /* --------------------------------------------------------------------- */
3748 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3750 int uwbs = uwb_get_radiosw();
3752 if (uwbs < 0)
3753 return uwbs;
3755 *state = uwbs;
3756 return 0;
3759 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3761 dbg_printk(TPACPI_DBG_RFKILL,
3762 "request to change radio state to %d\n", state);
3763 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3766 static void uwb_exit(void)
3768 if (tpacpi_uwb_rfkill)
3769 rfkill_unregister(tpacpi_uwb_rfkill);
3772 static int __init uwb_init(struct ibm_init_struct *iibm)
3774 int res;
3775 int status = 0;
3777 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3778 "initializing uwb subdriver\n");
3780 TPACPI_ACPIHANDLE_INIT(hkey);
3782 tp_features.uwb = hkey_handle &&
3783 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3785 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3786 "uwb is %s, status 0x%02x\n",
3787 str_supported(tp_features.uwb),
3788 status);
3790 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3791 if (dbg_uwbemul) {
3792 tp_features.uwb = 1;
3793 printk(TPACPI_INFO
3794 "uwb switch emulation enabled\n");
3795 } else
3796 #endif
3797 if (tp_features.uwb &&
3798 !(status & TP_ACPI_UWB_HWPRESENT)) {
3799 /* no uwb hardware present in system */
3800 tp_features.uwb = 0;
3801 dbg_printk(TPACPI_DBG_INIT,
3802 "uwb hardware not installed\n");
3805 if (!tp_features.uwb)
3806 return 1;
3808 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3809 &tpacpi_uwb_rfkill,
3810 RFKILL_TYPE_UWB,
3811 TPACPI_RFK_UWB_SW_NAME,
3812 false,
3813 tpacpi_uwb_rfk_set,
3814 tpacpi_uwb_rfk_get);
3816 return res;
3819 static struct ibm_struct uwb_driver_data = {
3820 .name = "uwb",
3821 .exit = uwb_exit,
3822 .flags.experimental = 1,
3825 /*************************************************************************
3826 * Video subdriver
3829 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3831 enum video_access_mode {
3832 TPACPI_VIDEO_NONE = 0,
3833 TPACPI_VIDEO_570, /* 570 */
3834 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3835 TPACPI_VIDEO_NEW, /* all others */
3838 enum { /* video status flags, based on VIDEO_570 */
3839 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3840 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3841 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3844 enum { /* TPACPI_VIDEO_570 constants */
3845 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3846 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3847 * video_status_flags */
3848 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3849 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3852 static enum video_access_mode video_supported;
3853 static int video_orig_autosw;
3855 static int video_autosw_get(void);
3856 static int video_autosw_set(int enable);
3858 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3860 static int __init video_init(struct ibm_init_struct *iibm)
3862 int ivga;
3864 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3866 TPACPI_ACPIHANDLE_INIT(vid);
3867 TPACPI_ACPIHANDLE_INIT(vid2);
3869 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3870 /* G41, assume IVGA doesn't change */
3871 vid_handle = vid2_handle;
3873 if (!vid_handle)
3874 /* video switching not supported on R30, R31 */
3875 video_supported = TPACPI_VIDEO_NONE;
3876 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3877 /* 570 */
3878 video_supported = TPACPI_VIDEO_570;
3879 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3880 /* 600e/x, 770e, 770x */
3881 video_supported = TPACPI_VIDEO_770;
3882 else
3883 /* all others */
3884 video_supported = TPACPI_VIDEO_NEW;
3886 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3887 str_supported(video_supported != TPACPI_VIDEO_NONE),
3888 video_supported);
3890 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3893 static void video_exit(void)
3895 dbg_printk(TPACPI_DBG_EXIT,
3896 "restoring original video autoswitch mode\n");
3897 if (video_autosw_set(video_orig_autosw))
3898 printk(TPACPI_ERR "error while trying to restore original "
3899 "video autoswitch mode\n");
3902 static int video_outputsw_get(void)
3904 int status = 0;
3905 int i;
3907 switch (video_supported) {
3908 case TPACPI_VIDEO_570:
3909 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3910 TP_ACPI_VIDEO_570_PHSCMD))
3911 return -EIO;
3912 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3913 break;
3914 case TPACPI_VIDEO_770:
3915 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3916 return -EIO;
3917 if (i)
3918 status |= TP_ACPI_VIDEO_S_LCD;
3919 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3920 return -EIO;
3921 if (i)
3922 status |= TP_ACPI_VIDEO_S_CRT;
3923 break;
3924 case TPACPI_VIDEO_NEW:
3925 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3926 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3927 return -EIO;
3928 if (i)
3929 status |= TP_ACPI_VIDEO_S_CRT;
3931 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3932 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3933 return -EIO;
3934 if (i)
3935 status |= TP_ACPI_VIDEO_S_LCD;
3936 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3937 return -EIO;
3938 if (i)
3939 status |= TP_ACPI_VIDEO_S_DVI;
3940 break;
3941 default:
3942 return -ENOSYS;
3945 return status;
3948 static int video_outputsw_set(int status)
3950 int autosw;
3951 int res = 0;
3953 switch (video_supported) {
3954 case TPACPI_VIDEO_570:
3955 res = acpi_evalf(NULL, NULL,
3956 "\\_SB.PHS2", "vdd",
3957 TP_ACPI_VIDEO_570_PHS2CMD,
3958 status | TP_ACPI_VIDEO_570_PHS2SET);
3959 break;
3960 case TPACPI_VIDEO_770:
3961 autosw = video_autosw_get();
3962 if (autosw < 0)
3963 return autosw;
3965 res = video_autosw_set(1);
3966 if (res)
3967 return res;
3968 res = acpi_evalf(vid_handle, NULL,
3969 "ASWT", "vdd", status * 0x100, 0);
3970 if (!autosw && video_autosw_set(autosw)) {
3971 printk(TPACPI_ERR
3972 "video auto-switch left enabled due to error\n");
3973 return -EIO;
3975 break;
3976 case TPACPI_VIDEO_NEW:
3977 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3978 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3979 break;
3980 default:
3981 return -ENOSYS;
3984 return (res)? 0 : -EIO;
3987 static int video_autosw_get(void)
3989 int autosw = 0;
3991 switch (video_supported) {
3992 case TPACPI_VIDEO_570:
3993 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3994 return -EIO;
3995 break;
3996 case TPACPI_VIDEO_770:
3997 case TPACPI_VIDEO_NEW:
3998 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3999 return -EIO;
4000 break;
4001 default:
4002 return -ENOSYS;
4005 return autosw & 1;
4008 static int video_autosw_set(int enable)
4010 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
4011 return -EIO;
4012 return 0;
4015 static int video_outputsw_cycle(void)
4017 int autosw = video_autosw_get();
4018 int res;
4020 if (autosw < 0)
4021 return autosw;
4023 switch (video_supported) {
4024 case TPACPI_VIDEO_570:
4025 res = video_autosw_set(1);
4026 if (res)
4027 return res;
4028 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4029 break;
4030 case TPACPI_VIDEO_770:
4031 case TPACPI_VIDEO_NEW:
4032 res = video_autosw_set(1);
4033 if (res)
4034 return res;
4035 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4036 break;
4037 default:
4038 return -ENOSYS;
4040 if (!autosw && video_autosw_set(autosw)) {
4041 printk(TPACPI_ERR
4042 "video auto-switch left enabled due to error\n");
4043 return -EIO;
4046 return (res)? 0 : -EIO;
4049 static int video_expand_toggle(void)
4051 switch (video_supported) {
4052 case TPACPI_VIDEO_570:
4053 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4054 0 : -EIO;
4055 case TPACPI_VIDEO_770:
4056 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4057 0 : -EIO;
4058 case TPACPI_VIDEO_NEW:
4059 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4060 0 : -EIO;
4061 default:
4062 return -ENOSYS;
4064 /* not reached */
4067 static int video_read(char *p)
4069 int status, autosw;
4070 int len = 0;
4072 if (video_supported == TPACPI_VIDEO_NONE) {
4073 len += sprintf(p + len, "status:\t\tnot supported\n");
4074 return len;
4077 status = video_outputsw_get();
4078 if (status < 0)
4079 return status;
4081 autosw = video_autosw_get();
4082 if (autosw < 0)
4083 return autosw;
4085 len += sprintf(p + len, "status:\t\tsupported\n");
4086 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4087 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4088 if (video_supported == TPACPI_VIDEO_NEW)
4089 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4090 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4091 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4092 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4093 if (video_supported == TPACPI_VIDEO_NEW)
4094 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4095 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4096 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4098 return len;
4101 static int video_write(char *buf)
4103 char *cmd;
4104 int enable, disable, status;
4105 int res;
4107 if (video_supported == TPACPI_VIDEO_NONE)
4108 return -ENODEV;
4110 enable = 0;
4111 disable = 0;
4113 while ((cmd = next_cmd(&buf))) {
4114 if (strlencmp(cmd, "lcd_enable") == 0) {
4115 enable |= TP_ACPI_VIDEO_S_LCD;
4116 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4117 disable |= TP_ACPI_VIDEO_S_LCD;
4118 } else if (strlencmp(cmd, "crt_enable") == 0) {
4119 enable |= TP_ACPI_VIDEO_S_CRT;
4120 } else if (strlencmp(cmd, "crt_disable") == 0) {
4121 disable |= TP_ACPI_VIDEO_S_CRT;
4122 } else if (video_supported == TPACPI_VIDEO_NEW &&
4123 strlencmp(cmd, "dvi_enable") == 0) {
4124 enable |= TP_ACPI_VIDEO_S_DVI;
4125 } else if (video_supported == TPACPI_VIDEO_NEW &&
4126 strlencmp(cmd, "dvi_disable") == 0) {
4127 disable |= TP_ACPI_VIDEO_S_DVI;
4128 } else if (strlencmp(cmd, "auto_enable") == 0) {
4129 res = video_autosw_set(1);
4130 if (res)
4131 return res;
4132 } else if (strlencmp(cmd, "auto_disable") == 0) {
4133 res = video_autosw_set(0);
4134 if (res)
4135 return res;
4136 } else if (strlencmp(cmd, "video_switch") == 0) {
4137 res = video_outputsw_cycle();
4138 if (res)
4139 return res;
4140 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4141 res = video_expand_toggle();
4142 if (res)
4143 return res;
4144 } else
4145 return -EINVAL;
4148 if (enable || disable) {
4149 status = video_outputsw_get();
4150 if (status < 0)
4151 return status;
4152 res = video_outputsw_set((status & ~disable) | enable);
4153 if (res)
4154 return res;
4157 return 0;
4160 static struct ibm_struct video_driver_data = {
4161 .name = "video",
4162 .read = video_read,
4163 .write = video_write,
4164 .exit = video_exit,
4167 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4169 /*************************************************************************
4170 * Light (thinklight) subdriver
4173 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4174 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4176 static int light_get_status(void)
4178 int status = 0;
4180 if (tp_features.light_status) {
4181 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4182 return -EIO;
4183 return (!!status);
4186 return -ENXIO;
4189 static int light_set_status(int status)
4191 int rc;
4193 if (tp_features.light) {
4194 if (cmos_handle) {
4195 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4196 (status)?
4197 TP_CMOS_THINKLIGHT_ON :
4198 TP_CMOS_THINKLIGHT_OFF);
4199 } else {
4200 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4201 (status)? 1 : 0);
4203 return (rc)? 0 : -EIO;
4206 return -ENXIO;
4209 static void light_set_status_worker(struct work_struct *work)
4211 struct tpacpi_led_classdev *data =
4212 container_of(work, struct tpacpi_led_classdev, work);
4214 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4215 light_set_status((data->new_state != TPACPI_LED_OFF));
4218 static void light_sysfs_set(struct led_classdev *led_cdev,
4219 enum led_brightness brightness)
4221 struct tpacpi_led_classdev *data =
4222 container_of(led_cdev,
4223 struct tpacpi_led_classdev,
4224 led_classdev);
4225 data->new_state = (brightness != LED_OFF) ?
4226 TPACPI_LED_ON : TPACPI_LED_OFF;
4227 queue_work(tpacpi_wq, &data->work);
4230 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4232 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4235 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4236 .led_classdev = {
4237 .name = "tpacpi::thinklight",
4238 .brightness_set = &light_sysfs_set,
4239 .brightness_get = &light_sysfs_get,
4243 static int __init light_init(struct ibm_init_struct *iibm)
4245 int rc;
4247 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4249 TPACPI_ACPIHANDLE_INIT(ledb);
4250 TPACPI_ACPIHANDLE_INIT(lght);
4251 TPACPI_ACPIHANDLE_INIT(cmos);
4252 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4254 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4255 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4257 if (tp_features.light)
4258 /* light status not supported on
4259 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4260 tp_features.light_status =
4261 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4263 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4264 str_supported(tp_features.light),
4265 str_supported(tp_features.light_status));
4267 if (!tp_features.light)
4268 return 1;
4270 rc = led_classdev_register(&tpacpi_pdev->dev,
4271 &tpacpi_led_thinklight.led_classdev);
4273 if (rc < 0) {
4274 tp_features.light = 0;
4275 tp_features.light_status = 0;
4276 } else {
4277 rc = 0;
4280 return rc;
4283 static void light_exit(void)
4285 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4286 if (work_pending(&tpacpi_led_thinklight.work))
4287 flush_workqueue(tpacpi_wq);
4290 static int light_read(char *p)
4292 int len = 0;
4293 int status;
4295 if (!tp_features.light) {
4296 len += sprintf(p + len, "status:\t\tnot supported\n");
4297 } else if (!tp_features.light_status) {
4298 len += sprintf(p + len, "status:\t\tunknown\n");
4299 len += sprintf(p + len, "commands:\ton, off\n");
4300 } else {
4301 status = light_get_status();
4302 if (status < 0)
4303 return status;
4304 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4305 len += sprintf(p + len, "commands:\ton, off\n");
4308 return len;
4311 static int light_write(char *buf)
4313 char *cmd;
4314 int newstatus = 0;
4316 if (!tp_features.light)
4317 return -ENODEV;
4319 while ((cmd = next_cmd(&buf))) {
4320 if (strlencmp(cmd, "on") == 0) {
4321 newstatus = 1;
4322 } else if (strlencmp(cmd, "off") == 0) {
4323 newstatus = 0;
4324 } else
4325 return -EINVAL;
4328 return light_set_status(newstatus);
4331 static struct ibm_struct light_driver_data = {
4332 .name = "light",
4333 .read = light_read,
4334 .write = light_write,
4335 .exit = light_exit,
4338 /*************************************************************************
4339 * Dock subdriver
4342 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4344 static void dock_notify(struct ibm_struct *ibm, u32 event);
4345 static int dock_read(char *p);
4346 static int dock_write(char *buf);
4348 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4349 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4350 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4351 "\\_SB.PCI.ISA.SLCE", /* 570 */
4352 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4354 /* don't list other alternatives as we install a notify handler on the 570 */
4355 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4357 static const struct acpi_device_id ibm_pci_device_ids[] = {
4358 {PCI_ROOT_HID_STRING, 0},
4359 {"", 0},
4362 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4364 .notify = dock_notify,
4365 .handle = &dock_handle,
4366 .type = ACPI_SYSTEM_NOTIFY,
4369 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4370 * We just use it to get notifications of dock hotplug
4371 * in very old thinkpads */
4372 .hid = ibm_pci_device_ids,
4373 .notify = dock_notify,
4374 .handle = &pci_handle,
4375 .type = ACPI_SYSTEM_NOTIFY,
4379 static struct ibm_struct dock_driver_data[2] = {
4381 .name = "dock",
4382 .read = dock_read,
4383 .write = dock_write,
4384 .acpi = &ibm_dock_acpidriver[0],
4387 .name = "dock",
4388 .acpi = &ibm_dock_acpidriver[1],
4392 #define dock_docked() (_sta(dock_handle) & 1)
4394 static int __init dock_init(struct ibm_init_struct *iibm)
4396 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4398 TPACPI_ACPIHANDLE_INIT(dock);
4400 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4401 str_supported(dock_handle != NULL));
4403 return (dock_handle)? 0 : 1;
4406 static int __init dock_init2(struct ibm_init_struct *iibm)
4408 int dock2_needed;
4410 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4412 if (dock_driver_data[0].flags.acpi_driver_registered &&
4413 dock_driver_data[0].flags.acpi_notify_installed) {
4414 TPACPI_ACPIHANDLE_INIT(pci);
4415 dock2_needed = (pci_handle != NULL);
4416 vdbg_printk(TPACPI_DBG_INIT,
4417 "dock PCI handler for the TP 570 is %s\n",
4418 str_supported(dock2_needed));
4419 } else {
4420 vdbg_printk(TPACPI_DBG_INIT,
4421 "dock subdriver part 2 not required\n");
4422 dock2_needed = 0;
4425 return (dock2_needed)? 0 : 1;
4428 static void dock_notify(struct ibm_struct *ibm, u32 event)
4430 int docked = dock_docked();
4431 int pci = ibm->acpi->hid && ibm->acpi->device &&
4432 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4433 int data;
4435 if (event == 1 && !pci) /* 570 */
4436 data = 1; /* button */
4437 else if (event == 1 && pci) /* 570 */
4438 data = 3; /* dock */
4439 else if (event == 3 && docked)
4440 data = 1; /* button */
4441 else if (event == 3 && !docked)
4442 data = 2; /* undock */
4443 else if (event == 0 && docked)
4444 data = 3; /* dock */
4445 else {
4446 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4447 event, _sta(dock_handle));
4448 data = 0; /* unknown */
4450 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4451 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4452 dev_name(&ibm->acpi->device->dev),
4453 event, data);
4456 static int dock_read(char *p)
4458 int len = 0;
4459 int docked = dock_docked();
4461 if (!dock_handle)
4462 len += sprintf(p + len, "status:\t\tnot supported\n");
4463 else if (!docked)
4464 len += sprintf(p + len, "status:\t\tundocked\n");
4465 else {
4466 len += sprintf(p + len, "status:\t\tdocked\n");
4467 len += sprintf(p + len, "commands:\tdock, undock\n");
4470 return len;
4473 static int dock_write(char *buf)
4475 char *cmd;
4477 if (!dock_docked())
4478 return -ENODEV;
4480 while ((cmd = next_cmd(&buf))) {
4481 if (strlencmp(cmd, "undock") == 0) {
4482 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4483 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4484 return -EIO;
4485 } else if (strlencmp(cmd, "dock") == 0) {
4486 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4487 return -EIO;
4488 } else
4489 return -EINVAL;
4492 return 0;
4495 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4497 /*************************************************************************
4498 * Bay subdriver
4501 #ifdef CONFIG_THINKPAD_ACPI_BAY
4503 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4504 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4505 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4506 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4507 ); /* A21e, R30, R31 */
4508 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4509 "_EJ0", /* all others */
4510 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4511 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4512 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4513 ); /* all others */
4514 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4515 "_EJ0", /* 770x */
4516 ); /* all others */
4518 static int __init bay_init(struct ibm_init_struct *iibm)
4520 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4522 TPACPI_ACPIHANDLE_INIT(bay);
4523 if (bay_handle)
4524 TPACPI_ACPIHANDLE_INIT(bay_ej);
4525 TPACPI_ACPIHANDLE_INIT(bay2);
4526 if (bay2_handle)
4527 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4529 tp_features.bay_status = bay_handle &&
4530 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4531 tp_features.bay_status2 = bay2_handle &&
4532 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4534 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4535 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4536 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4537 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4539 vdbg_printk(TPACPI_DBG_INIT,
4540 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4541 str_supported(tp_features.bay_status),
4542 str_supported(tp_features.bay_eject),
4543 str_supported(tp_features.bay_status2),
4544 str_supported(tp_features.bay_eject2));
4546 return (tp_features.bay_status || tp_features.bay_eject ||
4547 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4550 static void bay_notify(struct ibm_struct *ibm, u32 event)
4552 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4553 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4554 dev_name(&ibm->acpi->device->dev),
4555 event, 0);
4558 #define bay_occupied(b) (_sta(b##_handle) & 1)
4560 static int bay_read(char *p)
4562 int len = 0;
4563 int occupied = bay_occupied(bay);
4564 int occupied2 = bay_occupied(bay2);
4565 int eject, eject2;
4567 len += sprintf(p + len, "status:\t\t%s\n",
4568 tp_features.bay_status ?
4569 (occupied ? "occupied" : "unoccupied") :
4570 "not supported");
4571 if (tp_features.bay_status2)
4572 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4573 "occupied" : "unoccupied");
4575 eject = tp_features.bay_eject && occupied;
4576 eject2 = tp_features.bay_eject2 && occupied2;
4578 if (eject && eject2)
4579 len += sprintf(p + len, "commands:\teject, eject2\n");
4580 else if (eject)
4581 len += sprintf(p + len, "commands:\teject\n");
4582 else if (eject2)
4583 len += sprintf(p + len, "commands:\teject2\n");
4585 return len;
4588 static int bay_write(char *buf)
4590 char *cmd;
4592 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4593 return -ENODEV;
4595 while ((cmd = next_cmd(&buf))) {
4596 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4597 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4598 return -EIO;
4599 } else if (tp_features.bay_eject2 &&
4600 strlencmp(cmd, "eject2") == 0) {
4601 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4602 return -EIO;
4603 } else
4604 return -EINVAL;
4607 return 0;
4610 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4611 .notify = bay_notify,
4612 .handle = &bay_handle,
4613 .type = ACPI_SYSTEM_NOTIFY,
4616 static struct ibm_struct bay_driver_data = {
4617 .name = "bay",
4618 .read = bay_read,
4619 .write = bay_write,
4620 .acpi = &ibm_bay_acpidriver,
4623 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4625 /*************************************************************************
4626 * CMOS subdriver
4629 /* sysfs cmos_command -------------------------------------------------- */
4630 static ssize_t cmos_command_store(struct device *dev,
4631 struct device_attribute *attr,
4632 const char *buf, size_t count)
4634 unsigned long cmos_cmd;
4635 int res;
4637 if (parse_strtoul(buf, 21, &cmos_cmd))
4638 return -EINVAL;
4640 res = issue_thinkpad_cmos_command(cmos_cmd);
4641 return (res)? res : count;
4644 static struct device_attribute dev_attr_cmos_command =
4645 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4647 /* --------------------------------------------------------------------- */
4649 static int __init cmos_init(struct ibm_init_struct *iibm)
4651 int res;
4653 vdbg_printk(TPACPI_DBG_INIT,
4654 "initializing cmos commands subdriver\n");
4656 TPACPI_ACPIHANDLE_INIT(cmos);
4658 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4659 str_supported(cmos_handle != NULL));
4661 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4662 if (res)
4663 return res;
4665 return (cmos_handle)? 0 : 1;
4668 static void cmos_exit(void)
4670 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4673 static int cmos_read(char *p)
4675 int len = 0;
4677 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4678 R30, R31, T20-22, X20-21 */
4679 if (!cmos_handle)
4680 len += sprintf(p + len, "status:\t\tnot supported\n");
4681 else {
4682 len += sprintf(p + len, "status:\t\tsupported\n");
4683 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4686 return len;
4689 static int cmos_write(char *buf)
4691 char *cmd;
4692 int cmos_cmd, res;
4694 while ((cmd = next_cmd(&buf))) {
4695 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4696 cmos_cmd >= 0 && cmos_cmd <= 21) {
4697 /* cmos_cmd set */
4698 } else
4699 return -EINVAL;
4701 res = issue_thinkpad_cmos_command(cmos_cmd);
4702 if (res)
4703 return res;
4706 return 0;
4709 static struct ibm_struct cmos_driver_data = {
4710 .name = "cmos",
4711 .read = cmos_read,
4712 .write = cmos_write,
4713 .exit = cmos_exit,
4716 /*************************************************************************
4717 * LED subdriver
4720 enum led_access_mode {
4721 TPACPI_LED_NONE = 0,
4722 TPACPI_LED_570, /* 570 */
4723 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4724 TPACPI_LED_NEW, /* all others */
4727 enum { /* For TPACPI_LED_OLD */
4728 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4729 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4730 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4733 static enum led_access_mode led_supported;
4735 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4736 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4737 /* T20-22, X20-21 */
4738 "LED", /* all others */
4739 ); /* R30, R31 */
4741 #define TPACPI_LED_NUMLEDS 8
4742 static struct tpacpi_led_classdev *tpacpi_leds;
4743 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4744 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4745 /* there's a limit of 19 chars + NULL before 2.6.26 */
4746 "tpacpi::power",
4747 "tpacpi:orange:batt",
4748 "tpacpi:green:batt",
4749 "tpacpi::dock_active",
4750 "tpacpi::bay_active",
4751 "tpacpi::dock_batt",
4752 "tpacpi::unknown_led",
4753 "tpacpi::standby",
4755 #define TPACPI_SAFE_LEDS 0x0081U
4757 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4759 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4760 return false;
4761 #else
4762 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4763 #endif
4766 static int led_get_status(const unsigned int led)
4768 int status;
4769 enum led_status_t led_s;
4771 switch (led_supported) {
4772 case TPACPI_LED_570:
4773 if (!acpi_evalf(ec_handle,
4774 &status, "GLED", "dd", 1 << led))
4775 return -EIO;
4776 led_s = (status == 0)?
4777 TPACPI_LED_OFF :
4778 ((status == 1)?
4779 TPACPI_LED_ON :
4780 TPACPI_LED_BLINK);
4781 tpacpi_led_state_cache[led] = led_s;
4782 return led_s;
4783 default:
4784 return -ENXIO;
4787 /* not reached */
4790 static int led_set_status(const unsigned int led,
4791 const enum led_status_t ledstatus)
4793 /* off, on, blink. Index is led_status_t */
4794 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4795 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4797 int rc = 0;
4799 switch (led_supported) {
4800 case TPACPI_LED_570:
4801 /* 570 */
4802 if (unlikely(led > 7))
4803 return -EINVAL;
4804 if (unlikely(tpacpi_is_led_restricted(led)))
4805 return -EPERM;
4806 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4807 (1 << led), led_sled_arg1[ledstatus]))
4808 rc = -EIO;
4809 break;
4810 case TPACPI_LED_OLD:
4811 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4812 if (unlikely(led > 7))
4813 return -EINVAL;
4814 if (unlikely(tpacpi_is_led_restricted(led)))
4815 return -EPERM;
4816 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4817 if (rc >= 0)
4818 rc = ec_write(TPACPI_LED_EC_HLBL,
4819 (ledstatus == TPACPI_LED_BLINK) << led);
4820 if (rc >= 0)
4821 rc = ec_write(TPACPI_LED_EC_HLCL,
4822 (ledstatus != TPACPI_LED_OFF) << led);
4823 break;
4824 case TPACPI_LED_NEW:
4825 /* all others */
4826 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4827 return -EINVAL;
4828 if (unlikely(tpacpi_is_led_restricted(led)))
4829 return -EPERM;
4830 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4831 led, led_led_arg1[ledstatus]))
4832 rc = -EIO;
4833 break;
4834 default:
4835 rc = -ENXIO;
4838 if (!rc)
4839 tpacpi_led_state_cache[led] = ledstatus;
4841 return rc;
4844 static void led_set_status_worker(struct work_struct *work)
4846 struct tpacpi_led_classdev *data =
4847 container_of(work, struct tpacpi_led_classdev, work);
4849 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4850 led_set_status(data->led, data->new_state);
4853 static void led_sysfs_set(struct led_classdev *led_cdev,
4854 enum led_brightness brightness)
4856 struct tpacpi_led_classdev *data = container_of(led_cdev,
4857 struct tpacpi_led_classdev, led_classdev);
4859 if (brightness == LED_OFF)
4860 data->new_state = TPACPI_LED_OFF;
4861 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4862 data->new_state = TPACPI_LED_ON;
4863 else
4864 data->new_state = TPACPI_LED_BLINK;
4866 queue_work(tpacpi_wq, &data->work);
4869 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4870 unsigned long *delay_on, unsigned long *delay_off)
4872 struct tpacpi_led_classdev *data = container_of(led_cdev,
4873 struct tpacpi_led_classdev, led_classdev);
4875 /* Can we choose the flash rate? */
4876 if (*delay_on == 0 && *delay_off == 0) {
4877 /* yes. set them to the hardware blink rate (1 Hz) */
4878 *delay_on = 500; /* ms */
4879 *delay_off = 500; /* ms */
4880 } else if ((*delay_on != 500) || (*delay_off != 500))
4881 return -EINVAL;
4883 data->new_state = TPACPI_LED_BLINK;
4884 queue_work(tpacpi_wq, &data->work);
4886 return 0;
4889 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4891 int rc;
4893 struct tpacpi_led_classdev *data = container_of(led_cdev,
4894 struct tpacpi_led_classdev, led_classdev);
4896 rc = led_get_status(data->led);
4898 if (rc == TPACPI_LED_OFF || rc < 0)
4899 rc = LED_OFF; /* no error handling in led class :( */
4900 else
4901 rc = LED_FULL;
4903 return rc;
4906 static void led_exit(void)
4908 unsigned int i;
4910 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4911 if (tpacpi_leds[i].led_classdev.name)
4912 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4915 kfree(tpacpi_leds);
4918 static int __init tpacpi_init_led(unsigned int led)
4920 int rc;
4922 tpacpi_leds[led].led = led;
4924 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4925 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4926 if (led_supported == TPACPI_LED_570)
4927 tpacpi_leds[led].led_classdev.brightness_get =
4928 &led_sysfs_get;
4930 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4932 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4934 rc = led_classdev_register(&tpacpi_pdev->dev,
4935 &tpacpi_leds[led].led_classdev);
4936 if (rc < 0)
4937 tpacpi_leds[led].led_classdev.name = NULL;
4939 return rc;
4942 static int __init led_init(struct ibm_init_struct *iibm)
4944 unsigned int i;
4945 int rc;
4947 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4949 TPACPI_ACPIHANDLE_INIT(led);
4951 if (!led_handle)
4952 /* led not supported on R30, R31 */
4953 led_supported = TPACPI_LED_NONE;
4954 else if (strlencmp(led_path, "SLED") == 0)
4955 /* 570 */
4956 led_supported = TPACPI_LED_570;
4957 else if (strlencmp(led_path, "SYSL") == 0)
4958 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4959 led_supported = TPACPI_LED_OLD;
4960 else
4961 /* all others */
4962 led_supported = TPACPI_LED_NEW;
4964 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4965 str_supported(led_supported), led_supported);
4967 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4968 GFP_KERNEL);
4969 if (!tpacpi_leds) {
4970 printk(TPACPI_ERR "Out of memory for LED data\n");
4971 return -ENOMEM;
4974 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4975 if (!tpacpi_is_led_restricted(i)) {
4976 rc = tpacpi_init_led(i);
4977 if (rc < 0) {
4978 led_exit();
4979 return rc;
4984 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4985 if (led_supported != TPACPI_LED_NONE)
4986 printk(TPACPI_NOTICE
4987 "warning: userspace override of important "
4988 "firmware LEDs is enabled\n");
4989 #endif
4990 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4993 #define str_led_status(s) \
4994 ((s) == TPACPI_LED_OFF ? "off" : \
4995 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4997 static int led_read(char *p)
4999 int len = 0;
5001 if (!led_supported) {
5002 len += sprintf(p + len, "status:\t\tnot supported\n");
5003 return len;
5005 len += sprintf(p + len, "status:\t\tsupported\n");
5007 if (led_supported == TPACPI_LED_570) {
5008 /* 570 */
5009 int i, status;
5010 for (i = 0; i < 8; i++) {
5011 status = led_get_status(i);
5012 if (status < 0)
5013 return -EIO;
5014 len += sprintf(p + len, "%d:\t\t%s\n",
5015 i, str_led_status(status));
5019 len += sprintf(p + len, "commands:\t"
5020 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5022 return len;
5025 static int led_write(char *buf)
5027 char *cmd;
5028 int led, rc;
5029 enum led_status_t s;
5031 if (!led_supported)
5032 return -ENODEV;
5034 while ((cmd = next_cmd(&buf))) {
5035 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5036 return -EINVAL;
5038 if (strstr(cmd, "off")) {
5039 s = TPACPI_LED_OFF;
5040 } else if (strstr(cmd, "on")) {
5041 s = TPACPI_LED_ON;
5042 } else if (strstr(cmd, "blink")) {
5043 s = TPACPI_LED_BLINK;
5044 } else {
5045 return -EINVAL;
5048 rc = led_set_status(led, s);
5049 if (rc < 0)
5050 return rc;
5053 return 0;
5056 static struct ibm_struct led_driver_data = {
5057 .name = "led",
5058 .read = led_read,
5059 .write = led_write,
5060 .exit = led_exit,
5063 /*************************************************************************
5064 * Beep subdriver
5067 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5069 static int __init beep_init(struct ibm_init_struct *iibm)
5071 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5073 TPACPI_ACPIHANDLE_INIT(beep);
5075 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5076 str_supported(beep_handle != NULL));
5078 return (beep_handle)? 0 : 1;
5081 static int beep_read(char *p)
5083 int len = 0;
5085 if (!beep_handle)
5086 len += sprintf(p + len, "status:\t\tnot supported\n");
5087 else {
5088 len += sprintf(p + len, "status:\t\tsupported\n");
5089 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5092 return len;
5095 static int beep_write(char *buf)
5097 char *cmd;
5098 int beep_cmd;
5100 if (!beep_handle)
5101 return -ENODEV;
5103 while ((cmd = next_cmd(&buf))) {
5104 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5105 beep_cmd >= 0 && beep_cmd <= 17) {
5106 /* beep_cmd set */
5107 } else
5108 return -EINVAL;
5109 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5110 return -EIO;
5113 return 0;
5116 static struct ibm_struct beep_driver_data = {
5117 .name = "beep",
5118 .read = beep_read,
5119 .write = beep_write,
5122 /*************************************************************************
5123 * Thermal subdriver
5126 enum thermal_access_mode {
5127 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5128 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5129 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5130 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5131 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5134 enum { /* TPACPI_THERMAL_TPEC_* */
5135 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5136 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5137 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5140 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5141 struct ibm_thermal_sensors_struct {
5142 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5145 static enum thermal_access_mode thermal_read_mode;
5147 /* idx is zero-based */
5148 static int thermal_get_sensor(int idx, s32 *value)
5150 int t;
5151 s8 tmp;
5152 char tmpi[5];
5154 t = TP_EC_THERMAL_TMP0;
5156 switch (thermal_read_mode) {
5157 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5158 case TPACPI_THERMAL_TPEC_16:
5159 if (idx >= 8 && idx <= 15) {
5160 t = TP_EC_THERMAL_TMP8;
5161 idx -= 8;
5163 /* fallthrough */
5164 #endif
5165 case TPACPI_THERMAL_TPEC_8:
5166 if (idx <= 7) {
5167 if (!acpi_ec_read(t + idx, &tmp))
5168 return -EIO;
5169 *value = tmp * 1000;
5170 return 0;
5172 break;
5174 case TPACPI_THERMAL_ACPI_UPDT:
5175 if (idx <= 7) {
5176 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5177 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5178 return -EIO;
5179 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5180 return -EIO;
5181 *value = (t - 2732) * 100;
5182 return 0;
5184 break;
5186 case TPACPI_THERMAL_ACPI_TMP07:
5187 if (idx <= 7) {
5188 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5189 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5190 return -EIO;
5191 if (t > 127 || t < -127)
5192 t = TP_EC_THERMAL_TMP_NA;
5193 *value = t * 1000;
5194 return 0;
5196 break;
5198 case TPACPI_THERMAL_NONE:
5199 default:
5200 return -ENOSYS;
5203 return -EINVAL;
5206 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5208 int res, i;
5209 int n;
5211 n = 8;
5212 i = 0;
5214 if (!s)
5215 return -EINVAL;
5217 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5218 n = 16;
5220 for (i = 0 ; i < n; i++) {
5221 res = thermal_get_sensor(i, &s->temp[i]);
5222 if (res)
5223 return res;
5226 return n;
5229 /* sysfs temp##_input -------------------------------------------------- */
5231 static ssize_t thermal_temp_input_show(struct device *dev,
5232 struct device_attribute *attr,
5233 char *buf)
5235 struct sensor_device_attribute *sensor_attr =
5236 to_sensor_dev_attr(attr);
5237 int idx = sensor_attr->index;
5238 s32 value;
5239 int res;
5241 res = thermal_get_sensor(idx, &value);
5242 if (res)
5243 return res;
5244 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5245 return -ENXIO;
5247 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5250 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5251 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5252 thermal_temp_input_show, NULL, _idxB)
5254 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5255 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5256 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5257 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5258 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5259 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5260 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5261 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5262 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5263 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5264 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5265 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5266 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5267 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5268 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5269 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5270 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5273 #define THERMAL_ATTRS(X) \
5274 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5276 static struct attribute *thermal_temp_input_attr[] = {
5277 THERMAL_ATTRS(8),
5278 THERMAL_ATTRS(9),
5279 THERMAL_ATTRS(10),
5280 THERMAL_ATTRS(11),
5281 THERMAL_ATTRS(12),
5282 THERMAL_ATTRS(13),
5283 THERMAL_ATTRS(14),
5284 THERMAL_ATTRS(15),
5285 THERMAL_ATTRS(0),
5286 THERMAL_ATTRS(1),
5287 THERMAL_ATTRS(2),
5288 THERMAL_ATTRS(3),
5289 THERMAL_ATTRS(4),
5290 THERMAL_ATTRS(5),
5291 THERMAL_ATTRS(6),
5292 THERMAL_ATTRS(7),
5293 NULL
5296 static const struct attribute_group thermal_temp_input16_group = {
5297 .attrs = thermal_temp_input_attr
5300 static const struct attribute_group thermal_temp_input8_group = {
5301 .attrs = &thermal_temp_input_attr[8]
5304 #undef THERMAL_SENSOR_ATTR_TEMP
5305 #undef THERMAL_ATTRS
5307 /* --------------------------------------------------------------------- */
5309 static int __init thermal_init(struct ibm_init_struct *iibm)
5311 u8 t, ta1, ta2;
5312 int i;
5313 int acpi_tmp7;
5314 int res;
5316 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5318 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5320 if (thinkpad_id.ec_model) {
5322 * Direct EC access mode: sensors at registers
5323 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5324 * non-implemented, thermal sensors return 0x80 when
5325 * not available
5328 ta1 = ta2 = 0;
5329 for (i = 0; i < 8; i++) {
5330 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5331 ta1 |= t;
5332 } else {
5333 ta1 = 0;
5334 break;
5336 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5337 ta2 |= t;
5338 } else {
5339 ta1 = 0;
5340 break;
5343 if (ta1 == 0) {
5344 /* This is sheer paranoia, but we handle it anyway */
5345 if (acpi_tmp7) {
5346 printk(TPACPI_ERR
5347 "ThinkPad ACPI EC access misbehaving, "
5348 "falling back to ACPI TMPx access "
5349 "mode\n");
5350 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5351 } else {
5352 printk(TPACPI_ERR
5353 "ThinkPad ACPI EC access misbehaving, "
5354 "disabling thermal sensors access\n");
5355 thermal_read_mode = TPACPI_THERMAL_NONE;
5357 } else {
5358 thermal_read_mode =
5359 (ta2 != 0) ?
5360 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5362 } else if (acpi_tmp7) {
5363 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5364 /* 600e/x, 770e, 770x */
5365 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5366 } else {
5367 /* Standard ACPI TMPx access, max 8 sensors */
5368 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5370 } else {
5371 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5372 thermal_read_mode = TPACPI_THERMAL_NONE;
5375 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5376 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5377 thermal_read_mode);
5379 switch (thermal_read_mode) {
5380 case TPACPI_THERMAL_TPEC_16:
5381 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5382 &thermal_temp_input16_group);
5383 if (res)
5384 return res;
5385 break;
5386 case TPACPI_THERMAL_TPEC_8:
5387 case TPACPI_THERMAL_ACPI_TMP07:
5388 case TPACPI_THERMAL_ACPI_UPDT:
5389 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5390 &thermal_temp_input8_group);
5391 if (res)
5392 return res;
5393 break;
5394 case TPACPI_THERMAL_NONE:
5395 default:
5396 return 1;
5399 return 0;
5402 static void thermal_exit(void)
5404 switch (thermal_read_mode) {
5405 case TPACPI_THERMAL_TPEC_16:
5406 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5407 &thermal_temp_input16_group);
5408 break;
5409 case TPACPI_THERMAL_TPEC_8:
5410 case TPACPI_THERMAL_ACPI_TMP07:
5411 case TPACPI_THERMAL_ACPI_UPDT:
5412 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5413 &thermal_temp_input16_group);
5414 break;
5415 case TPACPI_THERMAL_NONE:
5416 default:
5417 break;
5421 static int thermal_read(char *p)
5423 int len = 0;
5424 int n, i;
5425 struct ibm_thermal_sensors_struct t;
5427 n = thermal_get_sensors(&t);
5428 if (unlikely(n < 0))
5429 return n;
5431 len += sprintf(p + len, "temperatures:\t");
5433 if (n > 0) {
5434 for (i = 0; i < (n - 1); i++)
5435 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5436 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5437 } else
5438 len += sprintf(p + len, "not supported\n");
5440 return len;
5443 static struct ibm_struct thermal_driver_data = {
5444 .name = "thermal",
5445 .read = thermal_read,
5446 .exit = thermal_exit,
5449 /*************************************************************************
5450 * EC Dump subdriver
5453 static u8 ecdump_regs[256];
5455 static int ecdump_read(char *p)
5457 int len = 0;
5458 int i, j;
5459 u8 v;
5461 len += sprintf(p + len, "EC "
5462 " +00 +01 +02 +03 +04 +05 +06 +07"
5463 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5464 for (i = 0; i < 256; i += 16) {
5465 len += sprintf(p + len, "EC 0x%02x:", i);
5466 for (j = 0; j < 16; j++) {
5467 if (!acpi_ec_read(i + j, &v))
5468 break;
5469 if (v != ecdump_regs[i + j])
5470 len += sprintf(p + len, " *%02x", v);
5471 else
5472 len += sprintf(p + len, " %02x", v);
5473 ecdump_regs[i + j] = v;
5475 len += sprintf(p + len, "\n");
5476 if (j != 16)
5477 break;
5480 /* These are way too dangerous to advertise openly... */
5481 #if 0
5482 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5483 " (<offset> is 00-ff, <value> is 00-ff)\n");
5484 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5485 " (<offset> is 00-ff, <value> is 0-255)\n");
5486 #endif
5487 return len;
5490 static int ecdump_write(char *buf)
5492 char *cmd;
5493 int i, v;
5495 while ((cmd = next_cmd(&buf))) {
5496 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5497 /* i and v set */
5498 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5499 /* i and v set */
5500 } else
5501 return -EINVAL;
5502 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5503 if (!acpi_ec_write(i, v))
5504 return -EIO;
5505 } else
5506 return -EINVAL;
5509 return 0;
5512 static struct ibm_struct ecdump_driver_data = {
5513 .name = "ecdump",
5514 .read = ecdump_read,
5515 .write = ecdump_write,
5516 .flags.experimental = 1,
5519 /*************************************************************************
5520 * Backlight/brightness subdriver
5523 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5525 enum {
5526 TP_EC_BACKLIGHT = 0x31,
5528 /* TP_EC_BACKLIGHT bitmasks */
5529 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5530 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5531 TP_EC_BACKLIGHT_MAPSW = 0x20,
5534 static struct backlight_device *ibm_backlight_device;
5535 static int brightness_mode;
5536 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5538 static struct mutex brightness_mutex;
5541 * ThinkPads can read brightness from two places: EC 0x31, or
5542 * CMOS NVRAM byte 0x5E, bits 0-3.
5544 * EC 0x31 has the following layout
5545 * Bit 7: unknown function
5546 * Bit 6: unknown function
5547 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5548 * Bit 4: must be set to zero to avoid problems
5549 * Bit 3-0: backlight brightness level
5551 * brightness_get_raw returns status data in the EC 0x31 layout
5553 static int brightness_get_raw(int *status)
5555 u8 lec = 0, lcmos = 0, level = 0;
5557 if (brightness_mode & 1) {
5558 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5559 return -EIO;
5560 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5562 if (brightness_mode & 2) {
5563 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5564 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5565 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5566 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5567 level = lcmos;
5570 if (brightness_mode == 3) {
5571 *status = lec; /* Prefer EC, CMOS is just a backing store */
5572 lec &= TP_EC_BACKLIGHT_LVLMSK;
5573 if (lec == lcmos)
5574 tp_warned.bright_cmos_ec_unsync = 0;
5575 else {
5576 if (!tp_warned.bright_cmos_ec_unsync) {
5577 printk(TPACPI_ERR
5578 "CMOS NVRAM (%u) and EC (%u) do not "
5579 "agree on display brightness level\n",
5580 (unsigned int) lcmos,
5581 (unsigned int) lec);
5582 tp_warned.bright_cmos_ec_unsync = 1;
5584 return -EIO;
5586 } else {
5587 *status = level;
5590 return 0;
5593 /* May return EINTR which can always be mapped to ERESTARTSYS */
5594 static int brightness_set(int value)
5596 int cmos_cmd, inc, i, res;
5597 int current_value;
5598 int command_bits;
5600 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5601 value < 0)
5602 return -EINVAL;
5604 res = mutex_lock_killable(&brightness_mutex);
5605 if (res < 0)
5606 return res;
5608 res = brightness_get_raw(&current_value);
5609 if (res < 0)
5610 goto errout;
5612 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5613 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5615 cmos_cmd = value > current_value ?
5616 TP_CMOS_BRIGHTNESS_UP :
5617 TP_CMOS_BRIGHTNESS_DOWN;
5618 inc = (value > current_value)? 1 : -1;
5620 res = 0;
5621 for (i = current_value; i != value; i += inc) {
5622 if ((brightness_mode & 2) &&
5623 issue_thinkpad_cmos_command(cmos_cmd)) {
5624 res = -EIO;
5625 goto errout;
5627 if ((brightness_mode & 1) &&
5628 !acpi_ec_write(TP_EC_BACKLIGHT,
5629 (i + inc) | command_bits)) {
5630 res = -EIO;
5631 goto errout;;
5635 errout:
5636 mutex_unlock(&brightness_mutex);
5637 return res;
5640 /* sysfs backlight class ----------------------------------------------- */
5642 static int brightness_update_status(struct backlight_device *bd)
5644 /* it is the backlight class's job (caller) to handle
5645 * EINTR and other errors properly */
5646 return brightness_set(
5647 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5648 bd->props.power == FB_BLANK_UNBLANK) ?
5649 bd->props.brightness : 0);
5652 static int brightness_get(struct backlight_device *bd)
5654 int status, res;
5656 res = brightness_get_raw(&status);
5657 if (res < 0)
5658 return 0; /* FIXME: teach backlight about error handling */
5660 return status & TP_EC_BACKLIGHT_LVLMSK;
5663 static struct backlight_ops ibm_backlight_data = {
5664 .get_brightness = brightness_get,
5665 .update_status = brightness_update_status,
5668 /* --------------------------------------------------------------------- */
5670 static int __init brightness_init(struct ibm_init_struct *iibm)
5672 int b;
5674 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5676 mutex_init(&brightness_mutex);
5679 * We always attempt to detect acpi support, so as to switch
5680 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5681 * going to publish a backlight interface
5683 b = tpacpi_check_std_acpi_brightness_support();
5684 if (b > 0) {
5685 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5686 printk(TPACPI_NOTICE
5687 "Lenovo BIOS switched to ACPI backlight "
5688 "control mode\n");
5690 if (brightness_enable > 1) {
5691 printk(TPACPI_NOTICE
5692 "standard ACPI backlight interface "
5693 "available, not loading native one...\n");
5694 return 1;
5698 if (!brightness_enable) {
5699 dbg_printk(TPACPI_DBG_INIT,
5700 "brightness support disabled by "
5701 "module parameter\n");
5702 return 1;
5705 if (b > 16) {
5706 printk(TPACPI_ERR
5707 "Unsupported brightness interface, "
5708 "please contact %s\n", TPACPI_MAIL);
5709 return 1;
5711 if (b == 16)
5712 tp_features.bright_16levels = 1;
5714 if (!brightness_mode) {
5715 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5716 brightness_mode = 2;
5717 else
5718 brightness_mode = 3;
5720 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5721 brightness_mode);
5724 if (brightness_mode > 3)
5725 return -EINVAL;
5727 if (brightness_get_raw(&b) < 0)
5728 return 1;
5730 if (tp_features.bright_16levels)
5731 printk(TPACPI_INFO
5732 "detected a 16-level brightness capable ThinkPad\n");
5734 ibm_backlight_device = backlight_device_register(
5735 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5736 &ibm_backlight_data);
5737 if (IS_ERR(ibm_backlight_device)) {
5738 printk(TPACPI_ERR "Could not register backlight device\n");
5739 return PTR_ERR(ibm_backlight_device);
5741 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5743 ibm_backlight_device->props.max_brightness =
5744 (tp_features.bright_16levels)? 15 : 7;
5745 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5746 backlight_update_status(ibm_backlight_device);
5748 return 0;
5751 static void brightness_exit(void)
5753 if (ibm_backlight_device) {
5754 vdbg_printk(TPACPI_DBG_EXIT,
5755 "calling backlight_device_unregister()\n");
5756 backlight_device_unregister(ibm_backlight_device);
5760 static int brightness_read(char *p)
5762 int len = 0;
5763 int level;
5765 level = brightness_get(NULL);
5766 if (level < 0) {
5767 len += sprintf(p + len, "level:\t\tunreadable\n");
5768 } else {
5769 len += sprintf(p + len, "level:\t\t%d\n", level);
5770 len += sprintf(p + len, "commands:\tup, down\n");
5771 len += sprintf(p + len, "commands:\tlevel <level>"
5772 " (<level> is 0-%d)\n",
5773 (tp_features.bright_16levels) ? 15 : 7);
5776 return len;
5779 static int brightness_write(char *buf)
5781 int level;
5782 int rc;
5783 char *cmd;
5784 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5786 level = brightness_get(NULL);
5787 if (level < 0)
5788 return level;
5790 while ((cmd = next_cmd(&buf))) {
5791 if (strlencmp(cmd, "up") == 0) {
5792 if (level < max_level)
5793 level++;
5794 } else if (strlencmp(cmd, "down") == 0) {
5795 if (level > 0)
5796 level--;
5797 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5798 level >= 0 && level <= max_level) {
5799 /* new level set */
5800 } else
5801 return -EINVAL;
5805 * Now we know what the final level should be, so we try to set it.
5806 * Doing it this way makes the syscall restartable in case of EINTR
5808 rc = brightness_set(level);
5809 return (rc == -EINTR)? ERESTARTSYS : rc;
5812 static struct ibm_struct brightness_driver_data = {
5813 .name = "brightness",
5814 .read = brightness_read,
5815 .write = brightness_write,
5816 .exit = brightness_exit,
5819 /*************************************************************************
5820 * Volume subdriver
5823 static int volume_offset = 0x30;
5825 static int volume_read(char *p)
5827 int len = 0;
5828 u8 level;
5830 if (!acpi_ec_read(volume_offset, &level)) {
5831 len += sprintf(p + len, "level:\t\tunreadable\n");
5832 } else {
5833 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5834 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5835 len += sprintf(p + len, "commands:\tup, down, mute\n");
5836 len += sprintf(p + len, "commands:\tlevel <level>"
5837 " (<level> is 0-15)\n");
5840 return len;
5843 static int volume_write(char *buf)
5845 int cmos_cmd, inc, i;
5846 u8 level, mute;
5847 int new_level, new_mute;
5848 char *cmd;
5850 while ((cmd = next_cmd(&buf))) {
5851 if (!acpi_ec_read(volume_offset, &level))
5852 return -EIO;
5853 new_mute = mute = level & 0x40;
5854 new_level = level = level & 0xf;
5856 if (strlencmp(cmd, "up") == 0) {
5857 if (mute)
5858 new_mute = 0;
5859 else
5860 new_level = level == 15 ? 15 : level + 1;
5861 } else if (strlencmp(cmd, "down") == 0) {
5862 if (mute)
5863 new_mute = 0;
5864 else
5865 new_level = level == 0 ? 0 : level - 1;
5866 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5867 new_level >= 0 && new_level <= 15) {
5868 /* new_level set */
5869 } else if (strlencmp(cmd, "mute") == 0) {
5870 new_mute = 0x40;
5871 } else
5872 return -EINVAL;
5874 if (new_level != level) {
5875 /* mute doesn't change */
5877 cmos_cmd = (new_level > level) ?
5878 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5879 inc = new_level > level ? 1 : -1;
5881 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5882 !acpi_ec_write(volume_offset, level)))
5883 return -EIO;
5885 for (i = level; i != new_level; i += inc)
5886 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5887 !acpi_ec_write(volume_offset, i + inc))
5888 return -EIO;
5890 if (mute &&
5891 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5892 !acpi_ec_write(volume_offset, new_level + mute))) {
5893 return -EIO;
5897 if (new_mute != mute) {
5898 /* level doesn't change */
5900 cmos_cmd = (new_mute) ?
5901 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5903 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5904 !acpi_ec_write(volume_offset, level + new_mute))
5905 return -EIO;
5909 return 0;
5912 static struct ibm_struct volume_driver_data = {
5913 .name = "volume",
5914 .read = volume_read,
5915 .write = volume_write,
5918 /*************************************************************************
5919 * Fan subdriver
5923 * FAN ACCESS MODES
5925 * TPACPI_FAN_RD_ACPI_GFAN:
5926 * ACPI GFAN method: returns fan level
5928 * see TPACPI_FAN_WR_ACPI_SFAN
5929 * EC 0x2f (HFSP) not available if GFAN exists
5931 * TPACPI_FAN_WR_ACPI_SFAN:
5932 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5934 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5935 * it for writing.
5937 * TPACPI_FAN_WR_TPEC:
5938 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5939 * Supported on almost all ThinkPads
5941 * Fan speed changes of any sort (including those caused by the
5942 * disengaged mode) are usually done slowly by the firmware as the
5943 * maximum ammount of fan duty cycle change per second seems to be
5944 * limited.
5946 * Reading is not available if GFAN exists.
5947 * Writing is not available if SFAN exists.
5949 * Bits
5950 * 7 automatic mode engaged;
5951 * (default operation mode of the ThinkPad)
5952 * fan level is ignored in this mode.
5953 * 6 full speed mode (takes precedence over bit 7);
5954 * not available on all thinkpads. May disable
5955 * the tachometer while the fan controller ramps up
5956 * the speed (which can take up to a few *minutes*).
5957 * Speeds up fan to 100% duty-cycle, which is far above
5958 * the standard RPM levels. It is not impossible that
5959 * it could cause hardware damage.
5960 * 5-3 unused in some models. Extra bits for fan level
5961 * in others, but still useless as all values above
5962 * 7 map to the same speed as level 7 in these models.
5963 * 2-0 fan level (0..7 usually)
5964 * 0x00 = stop
5965 * 0x07 = max (set when temperatures critical)
5966 * Some ThinkPads may have other levels, see
5967 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5969 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5970 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5971 * does so, its initial value is meaningless (0x07).
5973 * For firmware bugs, refer to:
5974 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5976 * ----
5978 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5979 * Main fan tachometer reading (in RPM)
5981 * This register is present on all ThinkPads with a new-style EC, and
5982 * it is known not to be present on the A21m/e, and T22, as there is
5983 * something else in offset 0x84 according to the ACPI DSDT. Other
5984 * ThinkPads from this same time period (and earlier) probably lack the
5985 * tachometer as well.
5987 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5988 * was never fixed by IBM to report the EC firmware version string
5989 * probably support the tachometer (like the early X models), so
5990 * detecting it is quite hard. We need more data to know for sure.
5992 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5993 * might result.
5995 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5996 * mode.
5998 * For firmware bugs, refer to:
5999 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6001 * TPACPI_FAN_WR_ACPI_FANS:
6002 * ThinkPad X31, X40, X41. Not available in the X60.
6004 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6005 * high speed. ACPI DSDT seems to map these three speeds to levels
6006 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6007 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6009 * The speeds are stored on handles
6010 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6012 * There are three default speed sets, acessible as handles:
6013 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6015 * ACPI DSDT switches which set is in use depending on various
6016 * factors.
6018 * TPACPI_FAN_WR_TPEC is also available and should be used to
6019 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6020 * but the ACPI tables just mention level 7.
6023 enum { /* Fan control constants */
6024 fan_status_offset = 0x2f, /* EC register 0x2f */
6025 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6026 * 0x84 must be read before 0x85 */
6028 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6029 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6031 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6034 enum fan_status_access_mode {
6035 TPACPI_FAN_NONE = 0, /* No fan status or control */
6036 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6037 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6040 enum fan_control_access_mode {
6041 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6042 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6043 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6044 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6047 enum fan_control_commands {
6048 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6049 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6050 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6051 * and also watchdog cmd */
6054 static int fan_control_allowed;
6056 static enum fan_status_access_mode fan_status_access_mode;
6057 static enum fan_control_access_mode fan_control_access_mode;
6058 static enum fan_control_commands fan_control_commands;
6060 static u8 fan_control_initial_status;
6061 static u8 fan_control_desired_level;
6062 static u8 fan_control_resume_level;
6063 static int fan_watchdog_maxinterval;
6065 static struct mutex fan_mutex;
6067 static void fan_watchdog_fire(struct work_struct *ignored);
6068 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6070 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6071 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6072 "\\FSPD", /* 600e/x, 770e, 770x */
6073 ); /* all others */
6074 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6075 "JFNS", /* 770x-JL */
6076 ); /* all others */
6079 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6080 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6081 * be in auto mode (0x80).
6083 * This is corrected by any write to HFSP either by the driver, or
6084 * by the firmware.
6086 * We assume 0x07 really means auto mode while this quirk is active,
6087 * as this is far more likely than the ThinkPad being in level 7,
6088 * which is only used by the firmware during thermal emergencies.
6091 static void fan_quirk1_detect(void)
6093 /* In some ThinkPads, neither the EC nor the ACPI
6094 * DSDT initialize the HFSP register, and it ends up
6095 * being initially set to 0x07 when it *could* be
6096 * either 0x07 or 0x80.
6098 * Enable for TP-1Y (T43), TP-78 (R51e),
6099 * TP-76 (R52), TP-70 (T43, R52), which are known
6100 * to be buggy. */
6101 if (fan_control_initial_status == 0x07) {
6102 switch (thinkpad_id.ec_model) {
6103 case 0x5931: /* TP-1Y */
6104 case 0x3837: /* TP-78 */
6105 case 0x3637: /* TP-76 */
6106 case 0x3037: /* TP-70 */
6107 printk(TPACPI_NOTICE
6108 "fan_init: initial fan status is unknown, "
6109 "assuming it is in auto mode\n");
6110 tp_features.fan_ctrl_status_undef = 1;
6116 static void fan_quirk1_handle(u8 *fan_status)
6118 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6119 if (*fan_status != fan_control_initial_status) {
6120 /* something changed the HFSP regisnter since
6121 * driver init time, so it is not undefined
6122 * anymore */
6123 tp_features.fan_ctrl_status_undef = 0;
6124 } else {
6125 /* Return most likely status. In fact, it
6126 * might be the only possible status */
6127 *fan_status = TP_EC_FAN_AUTO;
6133 * Call with fan_mutex held
6135 static void fan_update_desired_level(u8 status)
6137 if ((status &
6138 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6139 if (status > 7)
6140 fan_control_desired_level = 7;
6141 else
6142 fan_control_desired_level = status;
6146 static int fan_get_status(u8 *status)
6148 u8 s;
6150 /* TODO:
6151 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6153 switch (fan_status_access_mode) {
6154 case TPACPI_FAN_RD_ACPI_GFAN:
6155 /* 570, 600e/x, 770e, 770x */
6157 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6158 return -EIO;
6160 if (likely(status))
6161 *status = s & 0x07;
6163 break;
6165 case TPACPI_FAN_RD_TPEC:
6166 /* all except 570, 600e/x, 770e, 770x */
6167 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6168 return -EIO;
6170 if (likely(status)) {
6171 *status = s;
6172 fan_quirk1_handle(status);
6175 break;
6177 default:
6178 return -ENXIO;
6181 return 0;
6184 static int fan_get_status_safe(u8 *status)
6186 int rc;
6187 u8 s;
6189 if (mutex_lock_killable(&fan_mutex))
6190 return -ERESTARTSYS;
6191 rc = fan_get_status(&s);
6192 if (!rc)
6193 fan_update_desired_level(s);
6194 mutex_unlock(&fan_mutex);
6196 if (status)
6197 *status = s;
6199 return rc;
6202 static int fan_get_speed(unsigned int *speed)
6204 u8 hi, lo;
6206 switch (fan_status_access_mode) {
6207 case TPACPI_FAN_RD_TPEC:
6208 /* all except 570, 600e/x, 770e, 770x */
6209 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6210 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6211 return -EIO;
6213 if (likely(speed))
6214 *speed = (hi << 8) | lo;
6216 break;
6218 default:
6219 return -ENXIO;
6222 return 0;
6225 static int fan_set_level(int level)
6227 if (!fan_control_allowed)
6228 return -EPERM;
6230 switch (fan_control_access_mode) {
6231 case TPACPI_FAN_WR_ACPI_SFAN:
6232 if (level >= 0 && level <= 7) {
6233 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6234 return -EIO;
6235 } else
6236 return -EINVAL;
6237 break;
6239 case TPACPI_FAN_WR_ACPI_FANS:
6240 case TPACPI_FAN_WR_TPEC:
6241 if (!(level & TP_EC_FAN_AUTO) &&
6242 !(level & TP_EC_FAN_FULLSPEED) &&
6243 ((level < 0) || (level > 7)))
6244 return -EINVAL;
6246 /* safety net should the EC not support AUTO
6247 * or FULLSPEED mode bits and just ignore them */
6248 if (level & TP_EC_FAN_FULLSPEED)
6249 level |= 7; /* safety min speed 7 */
6250 else if (level & TP_EC_FAN_AUTO)
6251 level |= 4; /* safety min speed 4 */
6253 if (!acpi_ec_write(fan_status_offset, level))
6254 return -EIO;
6255 else
6256 tp_features.fan_ctrl_status_undef = 0;
6257 break;
6259 default:
6260 return -ENXIO;
6262 return 0;
6265 static int fan_set_level_safe(int level)
6267 int rc;
6269 if (!fan_control_allowed)
6270 return -EPERM;
6272 if (mutex_lock_killable(&fan_mutex))
6273 return -ERESTARTSYS;
6275 if (level == TPACPI_FAN_LAST_LEVEL)
6276 level = fan_control_desired_level;
6278 rc = fan_set_level(level);
6279 if (!rc)
6280 fan_update_desired_level(level);
6282 mutex_unlock(&fan_mutex);
6283 return rc;
6286 static int fan_set_enable(void)
6288 u8 s;
6289 int rc;
6291 if (!fan_control_allowed)
6292 return -EPERM;
6294 if (mutex_lock_killable(&fan_mutex))
6295 return -ERESTARTSYS;
6297 switch (fan_control_access_mode) {
6298 case TPACPI_FAN_WR_ACPI_FANS:
6299 case TPACPI_FAN_WR_TPEC:
6300 rc = fan_get_status(&s);
6301 if (rc < 0)
6302 break;
6304 /* Don't go out of emergency fan mode */
6305 if (s != 7) {
6306 s &= 0x07;
6307 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6310 if (!acpi_ec_write(fan_status_offset, s))
6311 rc = -EIO;
6312 else {
6313 tp_features.fan_ctrl_status_undef = 0;
6314 rc = 0;
6316 break;
6318 case TPACPI_FAN_WR_ACPI_SFAN:
6319 rc = fan_get_status(&s);
6320 if (rc < 0)
6321 break;
6323 s &= 0x07;
6325 /* Set fan to at least level 4 */
6326 s |= 4;
6328 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6329 rc = -EIO;
6330 else
6331 rc = 0;
6332 break;
6334 default:
6335 rc = -ENXIO;
6338 mutex_unlock(&fan_mutex);
6339 return rc;
6342 static int fan_set_disable(void)
6344 int rc;
6346 if (!fan_control_allowed)
6347 return -EPERM;
6349 if (mutex_lock_killable(&fan_mutex))
6350 return -ERESTARTSYS;
6352 rc = 0;
6353 switch (fan_control_access_mode) {
6354 case TPACPI_FAN_WR_ACPI_FANS:
6355 case TPACPI_FAN_WR_TPEC:
6356 if (!acpi_ec_write(fan_status_offset, 0x00))
6357 rc = -EIO;
6358 else {
6359 fan_control_desired_level = 0;
6360 tp_features.fan_ctrl_status_undef = 0;
6362 break;
6364 case TPACPI_FAN_WR_ACPI_SFAN:
6365 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6366 rc = -EIO;
6367 else
6368 fan_control_desired_level = 0;
6369 break;
6371 default:
6372 rc = -ENXIO;
6376 mutex_unlock(&fan_mutex);
6377 return rc;
6380 static int fan_set_speed(int speed)
6382 int rc;
6384 if (!fan_control_allowed)
6385 return -EPERM;
6387 if (mutex_lock_killable(&fan_mutex))
6388 return -ERESTARTSYS;
6390 rc = 0;
6391 switch (fan_control_access_mode) {
6392 case TPACPI_FAN_WR_ACPI_FANS:
6393 if (speed >= 0 && speed <= 65535) {
6394 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6395 speed, speed, speed))
6396 rc = -EIO;
6397 } else
6398 rc = -EINVAL;
6399 break;
6401 default:
6402 rc = -ENXIO;
6405 mutex_unlock(&fan_mutex);
6406 return rc;
6409 static void fan_watchdog_reset(void)
6411 static int fan_watchdog_active;
6413 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6414 return;
6416 if (fan_watchdog_active)
6417 cancel_delayed_work(&fan_watchdog_task);
6419 if (fan_watchdog_maxinterval > 0 &&
6420 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6421 fan_watchdog_active = 1;
6422 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6423 msecs_to_jiffies(fan_watchdog_maxinterval
6424 * 1000))) {
6425 printk(TPACPI_ERR
6426 "failed to queue the fan watchdog, "
6427 "watchdog will not trigger\n");
6429 } else
6430 fan_watchdog_active = 0;
6433 static void fan_watchdog_fire(struct work_struct *ignored)
6435 int rc;
6437 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6438 return;
6440 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6441 rc = fan_set_enable();
6442 if (rc < 0) {
6443 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6444 "will try again later...\n", -rc);
6445 /* reschedule for later */
6446 fan_watchdog_reset();
6451 * SYSFS fan layout: hwmon compatible (device)
6453 * pwm*_enable:
6454 * 0: "disengaged" mode
6455 * 1: manual mode
6456 * 2: native EC "auto" mode (recommended, hardware default)
6458 * pwm*: set speed in manual mode, ignored otherwise.
6459 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6460 * interpolation.
6462 * fan*_input: tachometer reading, RPM
6465 * SYSFS fan layout: extensions
6467 * fan_watchdog (driver):
6468 * fan watchdog interval in seconds, 0 disables (default), max 120
6471 /* sysfs fan pwm1_enable ----------------------------------------------- */
6472 static ssize_t fan_pwm1_enable_show(struct device *dev,
6473 struct device_attribute *attr,
6474 char *buf)
6476 int res, mode;
6477 u8 status;
6479 res = fan_get_status_safe(&status);
6480 if (res)
6481 return res;
6483 if (status & TP_EC_FAN_FULLSPEED) {
6484 mode = 0;
6485 } else if (status & TP_EC_FAN_AUTO) {
6486 mode = 2;
6487 } else
6488 mode = 1;
6490 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6493 static ssize_t fan_pwm1_enable_store(struct device *dev,
6494 struct device_attribute *attr,
6495 const char *buf, size_t count)
6497 unsigned long t;
6498 int res, level;
6500 if (parse_strtoul(buf, 2, &t))
6501 return -EINVAL;
6503 switch (t) {
6504 case 0:
6505 level = TP_EC_FAN_FULLSPEED;
6506 break;
6507 case 1:
6508 level = TPACPI_FAN_LAST_LEVEL;
6509 break;
6510 case 2:
6511 level = TP_EC_FAN_AUTO;
6512 break;
6513 case 3:
6514 /* reserved for software-controlled auto mode */
6515 return -ENOSYS;
6516 default:
6517 return -EINVAL;
6520 res = fan_set_level_safe(level);
6521 if (res == -ENXIO)
6522 return -EINVAL;
6523 else if (res < 0)
6524 return res;
6526 fan_watchdog_reset();
6528 return count;
6531 static struct device_attribute dev_attr_fan_pwm1_enable =
6532 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6533 fan_pwm1_enable_show, fan_pwm1_enable_store);
6535 /* sysfs fan pwm1 ------------------------------------------------------ */
6536 static ssize_t fan_pwm1_show(struct device *dev,
6537 struct device_attribute *attr,
6538 char *buf)
6540 int res;
6541 u8 status;
6543 res = fan_get_status_safe(&status);
6544 if (res)
6545 return res;
6547 if ((status &
6548 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6549 status = fan_control_desired_level;
6551 if (status > 7)
6552 status = 7;
6554 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6557 static ssize_t fan_pwm1_store(struct device *dev,
6558 struct device_attribute *attr,
6559 const char *buf, size_t count)
6561 unsigned long s;
6562 int rc;
6563 u8 status, newlevel;
6565 if (parse_strtoul(buf, 255, &s))
6566 return -EINVAL;
6568 /* scale down from 0-255 to 0-7 */
6569 newlevel = (s >> 5) & 0x07;
6571 if (mutex_lock_killable(&fan_mutex))
6572 return -ERESTARTSYS;
6574 rc = fan_get_status(&status);
6575 if (!rc && (status &
6576 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6577 rc = fan_set_level(newlevel);
6578 if (rc == -ENXIO)
6579 rc = -EINVAL;
6580 else if (!rc) {
6581 fan_update_desired_level(newlevel);
6582 fan_watchdog_reset();
6586 mutex_unlock(&fan_mutex);
6587 return (rc)? rc : count;
6590 static struct device_attribute dev_attr_fan_pwm1 =
6591 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6592 fan_pwm1_show, fan_pwm1_store);
6594 /* sysfs fan fan1_input ------------------------------------------------ */
6595 static ssize_t fan_fan1_input_show(struct device *dev,
6596 struct device_attribute *attr,
6597 char *buf)
6599 int res;
6600 unsigned int speed;
6602 res = fan_get_speed(&speed);
6603 if (res < 0)
6604 return res;
6606 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6609 static struct device_attribute dev_attr_fan_fan1_input =
6610 __ATTR(fan1_input, S_IRUGO,
6611 fan_fan1_input_show, NULL);
6613 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6614 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6615 char *buf)
6617 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6620 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6621 const char *buf, size_t count)
6623 unsigned long t;
6625 if (parse_strtoul(buf, 120, &t))
6626 return -EINVAL;
6628 if (!fan_control_allowed)
6629 return -EPERM;
6631 fan_watchdog_maxinterval = t;
6632 fan_watchdog_reset();
6634 return count;
6637 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6638 fan_fan_watchdog_show, fan_fan_watchdog_store);
6640 /* --------------------------------------------------------------------- */
6641 static struct attribute *fan_attributes[] = {
6642 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6643 &dev_attr_fan_fan1_input.attr,
6644 NULL
6647 static const struct attribute_group fan_attr_group = {
6648 .attrs = fan_attributes,
6651 static int __init fan_init(struct ibm_init_struct *iibm)
6653 int rc;
6655 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6657 mutex_init(&fan_mutex);
6658 fan_status_access_mode = TPACPI_FAN_NONE;
6659 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6660 fan_control_commands = 0;
6661 fan_watchdog_maxinterval = 0;
6662 tp_features.fan_ctrl_status_undef = 0;
6663 fan_control_desired_level = 7;
6665 TPACPI_ACPIHANDLE_INIT(fans);
6666 TPACPI_ACPIHANDLE_INIT(gfan);
6667 TPACPI_ACPIHANDLE_INIT(sfan);
6669 if (gfan_handle) {
6670 /* 570, 600e/x, 770e, 770x */
6671 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6672 } else {
6673 /* all other ThinkPads: note that even old-style
6674 * ThinkPad ECs supports the fan control register */
6675 if (likely(acpi_ec_read(fan_status_offset,
6676 &fan_control_initial_status))) {
6677 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6678 fan_quirk1_detect();
6679 } else {
6680 printk(TPACPI_ERR
6681 "ThinkPad ACPI EC access misbehaving, "
6682 "fan status and control unavailable\n");
6683 return 1;
6687 if (sfan_handle) {
6688 /* 570, 770x-JL */
6689 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6690 fan_control_commands |=
6691 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6692 } else {
6693 if (!gfan_handle) {
6694 /* gfan without sfan means no fan control */
6695 /* all other models implement TP EC 0x2f control */
6697 if (fans_handle) {
6698 /* X31, X40, X41 */
6699 fan_control_access_mode =
6700 TPACPI_FAN_WR_ACPI_FANS;
6701 fan_control_commands |=
6702 TPACPI_FAN_CMD_SPEED |
6703 TPACPI_FAN_CMD_LEVEL |
6704 TPACPI_FAN_CMD_ENABLE;
6705 } else {
6706 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6707 fan_control_commands |=
6708 TPACPI_FAN_CMD_LEVEL |
6709 TPACPI_FAN_CMD_ENABLE;
6714 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6715 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6716 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6717 fan_status_access_mode, fan_control_access_mode);
6719 /* fan control master switch */
6720 if (!fan_control_allowed) {
6721 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6722 fan_control_commands = 0;
6723 dbg_printk(TPACPI_DBG_INIT,
6724 "fan control features disabled by parameter\n");
6727 /* update fan_control_desired_level */
6728 if (fan_status_access_mode != TPACPI_FAN_NONE)
6729 fan_get_status_safe(NULL);
6731 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6732 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6733 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6734 &fan_attr_group);
6735 if (rc < 0)
6736 return rc;
6738 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6739 &driver_attr_fan_watchdog);
6740 if (rc < 0) {
6741 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6742 &fan_attr_group);
6743 return rc;
6745 return 0;
6746 } else
6747 return 1;
6750 static void fan_exit(void)
6752 vdbg_printk(TPACPI_DBG_EXIT,
6753 "cancelling any pending fan watchdog tasks\n");
6755 /* FIXME: can we really do this unconditionally? */
6756 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6757 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6758 &driver_attr_fan_watchdog);
6760 cancel_delayed_work(&fan_watchdog_task);
6761 flush_workqueue(tpacpi_wq);
6764 static void fan_suspend(pm_message_t state)
6766 int rc;
6768 if (!fan_control_allowed)
6769 return;
6771 /* Store fan status in cache */
6772 fan_control_resume_level = 0;
6773 rc = fan_get_status_safe(&fan_control_resume_level);
6774 if (rc < 0)
6775 printk(TPACPI_NOTICE
6776 "failed to read fan level for later "
6777 "restore during resume: %d\n", rc);
6779 /* if it is undefined, don't attempt to restore it.
6780 * KEEP THIS LAST */
6781 if (tp_features.fan_ctrl_status_undef)
6782 fan_control_resume_level = 0;
6785 static void fan_resume(void)
6787 u8 current_level = 7;
6788 bool do_set = false;
6789 int rc;
6791 /* DSDT *always* updates status on resume */
6792 tp_features.fan_ctrl_status_undef = 0;
6794 if (!fan_control_allowed ||
6795 !fan_control_resume_level ||
6796 (fan_get_status_safe(&current_level) < 0))
6797 return;
6799 switch (fan_control_access_mode) {
6800 case TPACPI_FAN_WR_ACPI_SFAN:
6801 /* never decrease fan level */
6802 do_set = (fan_control_resume_level > current_level);
6803 break;
6804 case TPACPI_FAN_WR_ACPI_FANS:
6805 case TPACPI_FAN_WR_TPEC:
6806 /* never decrease fan level, scale is:
6807 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6809 * We expect the firmware to set either 7 or AUTO, but we
6810 * handle FULLSPEED out of paranoia.
6812 * So, we can safely only restore FULLSPEED or 7, anything
6813 * else could slow the fan. Restoring AUTO is useless, at
6814 * best that's exactly what the DSDT already set (it is the
6815 * slower it uses).
6817 * Always keep in mind that the DSDT *will* have set the
6818 * fans to what the vendor supposes is the best level. We
6819 * muck with it only to speed the fan up.
6821 if (fan_control_resume_level != 7 &&
6822 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6823 return;
6824 else
6825 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6826 (current_level != fan_control_resume_level);
6827 break;
6828 default:
6829 return;
6831 if (do_set) {
6832 printk(TPACPI_NOTICE
6833 "restoring fan level to 0x%02x\n",
6834 fan_control_resume_level);
6835 rc = fan_set_level_safe(fan_control_resume_level);
6836 if (rc < 0)
6837 printk(TPACPI_NOTICE
6838 "failed to restore fan level: %d\n", rc);
6842 static int fan_read(char *p)
6844 int len = 0;
6845 int rc;
6846 u8 status;
6847 unsigned int speed = 0;
6849 switch (fan_status_access_mode) {
6850 case TPACPI_FAN_RD_ACPI_GFAN:
6851 /* 570, 600e/x, 770e, 770x */
6852 rc = fan_get_status_safe(&status);
6853 if (rc < 0)
6854 return rc;
6856 len += sprintf(p + len, "status:\t\t%s\n"
6857 "level:\t\t%d\n",
6858 (status != 0) ? "enabled" : "disabled", status);
6859 break;
6861 case TPACPI_FAN_RD_TPEC:
6862 /* all except 570, 600e/x, 770e, 770x */
6863 rc = fan_get_status_safe(&status);
6864 if (rc < 0)
6865 return rc;
6867 len += sprintf(p + len, "status:\t\t%s\n",
6868 (status != 0) ? "enabled" : "disabled");
6870 rc = fan_get_speed(&speed);
6871 if (rc < 0)
6872 return rc;
6874 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6876 if (status & TP_EC_FAN_FULLSPEED)
6877 /* Disengaged mode takes precedence */
6878 len += sprintf(p + len, "level:\t\tdisengaged\n");
6879 else if (status & TP_EC_FAN_AUTO)
6880 len += sprintf(p + len, "level:\t\tauto\n");
6881 else
6882 len += sprintf(p + len, "level:\t\t%d\n", status);
6883 break;
6885 case TPACPI_FAN_NONE:
6886 default:
6887 len += sprintf(p + len, "status:\t\tnot supported\n");
6890 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6891 len += sprintf(p + len, "commands:\tlevel <level>");
6893 switch (fan_control_access_mode) {
6894 case TPACPI_FAN_WR_ACPI_SFAN:
6895 len += sprintf(p + len, " (<level> is 0-7)\n");
6896 break;
6898 default:
6899 len += sprintf(p + len, " (<level> is 0-7, "
6900 "auto, disengaged, full-speed)\n");
6901 break;
6905 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6906 len += sprintf(p + len, "commands:\tenable, disable\n"
6907 "commands:\twatchdog <timeout> (<timeout> "
6908 "is 0 (off), 1-120 (seconds))\n");
6910 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6911 len += sprintf(p + len, "commands:\tspeed <speed>"
6912 " (<speed> is 0-65535)\n");
6914 return len;
6917 static int fan_write_cmd_level(const char *cmd, int *rc)
6919 int level;
6921 if (strlencmp(cmd, "level auto") == 0)
6922 level = TP_EC_FAN_AUTO;
6923 else if ((strlencmp(cmd, "level disengaged") == 0) |
6924 (strlencmp(cmd, "level full-speed") == 0))
6925 level = TP_EC_FAN_FULLSPEED;
6926 else if (sscanf(cmd, "level %d", &level) != 1)
6927 return 0;
6929 *rc = fan_set_level_safe(level);
6930 if (*rc == -ENXIO)
6931 printk(TPACPI_ERR "level command accepted for unsupported "
6932 "access mode %d", fan_control_access_mode);
6934 return 1;
6937 static int fan_write_cmd_enable(const char *cmd, int *rc)
6939 if (strlencmp(cmd, "enable") != 0)
6940 return 0;
6942 *rc = fan_set_enable();
6943 if (*rc == -ENXIO)
6944 printk(TPACPI_ERR "enable command accepted for unsupported "
6945 "access mode %d", fan_control_access_mode);
6947 return 1;
6950 static int fan_write_cmd_disable(const char *cmd, int *rc)
6952 if (strlencmp(cmd, "disable") != 0)
6953 return 0;
6955 *rc = fan_set_disable();
6956 if (*rc == -ENXIO)
6957 printk(TPACPI_ERR "disable command accepted for unsupported "
6958 "access mode %d", fan_control_access_mode);
6960 return 1;
6963 static int fan_write_cmd_speed(const char *cmd, int *rc)
6965 int speed;
6967 /* TODO:
6968 * Support speed <low> <medium> <high> ? */
6970 if (sscanf(cmd, "speed %d", &speed) != 1)
6971 return 0;
6973 *rc = fan_set_speed(speed);
6974 if (*rc == -ENXIO)
6975 printk(TPACPI_ERR "speed command accepted for unsupported "
6976 "access mode %d", fan_control_access_mode);
6978 return 1;
6981 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6983 int interval;
6985 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6986 return 0;
6988 if (interval < 0 || interval > 120)
6989 *rc = -EINVAL;
6990 else
6991 fan_watchdog_maxinterval = interval;
6993 return 1;
6996 static int fan_write(char *buf)
6998 char *cmd;
6999 int rc = 0;
7001 while (!rc && (cmd = next_cmd(&buf))) {
7002 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7003 fan_write_cmd_level(cmd, &rc)) &&
7004 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7005 (fan_write_cmd_enable(cmd, &rc) ||
7006 fan_write_cmd_disable(cmd, &rc) ||
7007 fan_write_cmd_watchdog(cmd, &rc))) &&
7008 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7009 fan_write_cmd_speed(cmd, &rc))
7011 rc = -EINVAL;
7012 else if (!rc)
7013 fan_watchdog_reset();
7016 return rc;
7019 static struct ibm_struct fan_driver_data = {
7020 .name = "fan",
7021 .read = fan_read,
7022 .write = fan_write,
7023 .exit = fan_exit,
7024 .suspend = fan_suspend,
7025 .resume = fan_resume,
7028 /****************************************************************************
7029 ****************************************************************************
7031 * Infrastructure
7033 ****************************************************************************
7034 ****************************************************************************/
7036 /* sysfs name ---------------------------------------------------------- */
7037 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7038 struct device_attribute *attr,
7039 char *buf)
7041 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7044 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7045 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7047 /* --------------------------------------------------------------------- */
7049 /* /proc support */
7050 static struct proc_dir_entry *proc_dir;
7053 * Module and infrastructure proble, init and exit handling
7056 static int force_load;
7058 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7059 static const char * __init str_supported(int is_supported)
7061 static char text_unsupported[] __initdata = "not supported";
7063 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7065 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7067 static void ibm_exit(struct ibm_struct *ibm)
7069 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7071 list_del_init(&ibm->all_drivers);
7073 if (ibm->flags.acpi_notify_installed) {
7074 dbg_printk(TPACPI_DBG_EXIT,
7075 "%s: acpi_remove_notify_handler\n", ibm->name);
7076 BUG_ON(!ibm->acpi);
7077 acpi_remove_notify_handler(*ibm->acpi->handle,
7078 ibm->acpi->type,
7079 dispatch_acpi_notify);
7080 ibm->flags.acpi_notify_installed = 0;
7081 ibm->flags.acpi_notify_installed = 0;
7084 if (ibm->flags.proc_created) {
7085 dbg_printk(TPACPI_DBG_EXIT,
7086 "%s: remove_proc_entry\n", ibm->name);
7087 remove_proc_entry(ibm->name, proc_dir);
7088 ibm->flags.proc_created = 0;
7091 if (ibm->flags.acpi_driver_registered) {
7092 dbg_printk(TPACPI_DBG_EXIT,
7093 "%s: acpi_bus_unregister_driver\n", ibm->name);
7094 BUG_ON(!ibm->acpi);
7095 acpi_bus_unregister_driver(ibm->acpi->driver);
7096 kfree(ibm->acpi->driver);
7097 ibm->acpi->driver = NULL;
7098 ibm->flags.acpi_driver_registered = 0;
7101 if (ibm->flags.init_called && ibm->exit) {
7102 ibm->exit();
7103 ibm->flags.init_called = 0;
7106 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7109 static int __init ibm_init(struct ibm_init_struct *iibm)
7111 int ret;
7112 struct ibm_struct *ibm = iibm->data;
7113 struct proc_dir_entry *entry;
7115 BUG_ON(ibm == NULL);
7117 INIT_LIST_HEAD(&ibm->all_drivers);
7119 if (ibm->flags.experimental && !experimental)
7120 return 0;
7122 dbg_printk(TPACPI_DBG_INIT,
7123 "probing for %s\n", ibm->name);
7125 if (iibm->init) {
7126 ret = iibm->init(iibm);
7127 if (ret > 0)
7128 return 0; /* probe failed */
7129 if (ret)
7130 return ret;
7132 ibm->flags.init_called = 1;
7135 if (ibm->acpi) {
7136 if (ibm->acpi->hid) {
7137 ret = register_tpacpi_subdriver(ibm);
7138 if (ret)
7139 goto err_out;
7142 if (ibm->acpi->notify) {
7143 ret = setup_acpi_notify(ibm);
7144 if (ret == -ENODEV) {
7145 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7146 ibm->name);
7147 ret = 0;
7148 goto err_out;
7150 if (ret < 0)
7151 goto err_out;
7155 dbg_printk(TPACPI_DBG_INIT,
7156 "%s installed\n", ibm->name);
7158 if (ibm->read) {
7159 entry = create_proc_entry(ibm->name,
7160 S_IFREG | S_IRUGO | S_IWUSR,
7161 proc_dir);
7162 if (!entry) {
7163 printk(TPACPI_ERR "unable to create proc entry %s\n",
7164 ibm->name);
7165 ret = -ENODEV;
7166 goto err_out;
7168 entry->owner = THIS_MODULE;
7169 entry->data = ibm;
7170 entry->read_proc = &dispatch_procfs_read;
7171 if (ibm->write)
7172 entry->write_proc = &dispatch_procfs_write;
7173 ibm->flags.proc_created = 1;
7176 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7178 return 0;
7180 err_out:
7181 dbg_printk(TPACPI_DBG_INIT,
7182 "%s: at error exit path with result %d\n",
7183 ibm->name, ret);
7185 ibm_exit(ibm);
7186 return (ret < 0)? ret : 0;
7189 /* Probing */
7191 /* returns 0 - probe ok, or < 0 - probe error.
7192 * Probe ok doesn't mean thinkpad found.
7193 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7194 static int __must_check __init get_thinkpad_model_data(
7195 struct thinkpad_id_data *tp)
7197 const struct dmi_device *dev = NULL;
7198 char ec_fw_string[18];
7199 char const *s;
7201 if (!tp)
7202 return -EINVAL;
7204 memset(tp, 0, sizeof(*tp));
7206 if (dmi_name_in_vendors("IBM"))
7207 tp->vendor = PCI_VENDOR_ID_IBM;
7208 else if (dmi_name_in_vendors("LENOVO"))
7209 tp->vendor = PCI_VENDOR_ID_LENOVO;
7210 else
7211 return 0;
7213 s = dmi_get_system_info(DMI_BIOS_VERSION);
7214 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7215 if (s && !tp->bios_version_str)
7216 return -ENOMEM;
7217 if (!tp->bios_version_str)
7218 return 0;
7219 tp->bios_model = tp->bios_version_str[0]
7220 | (tp->bios_version_str[1] << 8);
7223 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7224 * X32 or newer, all Z series; Some models must have an
7225 * up-to-date BIOS or they will not be detected.
7227 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7229 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7230 if (sscanf(dev->name,
7231 "IBM ThinkPad Embedded Controller -[%17c",
7232 ec_fw_string) == 1) {
7233 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7234 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7236 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7237 if (!tp->ec_version_str)
7238 return -ENOMEM;
7239 tp->ec_model = ec_fw_string[0]
7240 | (ec_fw_string[1] << 8);
7241 break;
7245 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7246 if (s && !strnicmp(s, "ThinkPad", 8)) {
7247 tp->model_str = kstrdup(s, GFP_KERNEL);
7248 if (!tp->model_str)
7249 return -ENOMEM;
7252 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7253 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7254 if (s && !tp->nummodel_str)
7255 return -ENOMEM;
7257 return 0;
7260 static int __init probe_for_thinkpad(void)
7262 int is_thinkpad;
7264 if (acpi_disabled)
7265 return -ENODEV;
7268 * Non-ancient models have better DMI tagging, but very old models
7269 * don't.
7271 is_thinkpad = (thinkpad_id.model_str != NULL);
7273 /* ec is required because many other handles are relative to it */
7274 TPACPI_ACPIHANDLE_INIT(ec);
7275 if (!ec_handle) {
7276 if (is_thinkpad)
7277 printk(TPACPI_ERR
7278 "Not yet supported ThinkPad detected!\n");
7279 return -ENODEV;
7283 * Risks a regression on very old machines, but reduces potential
7284 * false positives a damn great deal
7286 if (!is_thinkpad)
7287 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7289 if (!is_thinkpad && !force_load)
7290 return -ENODEV;
7292 return 0;
7296 /* Module init, exit, parameters */
7298 static struct ibm_init_struct ibms_init[] __initdata = {
7300 .init = thinkpad_acpi_driver_init,
7301 .data = &thinkpad_acpi_driver_data,
7304 .init = hotkey_init,
7305 .data = &hotkey_driver_data,
7308 .init = bluetooth_init,
7309 .data = &bluetooth_driver_data,
7312 .init = wan_init,
7313 .data = &wan_driver_data,
7316 .init = uwb_init,
7317 .data = &uwb_driver_data,
7319 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7321 .init = video_init,
7322 .data = &video_driver_data,
7324 #endif
7326 .init = light_init,
7327 .data = &light_driver_data,
7329 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7331 .init = dock_init,
7332 .data = &dock_driver_data[0],
7335 .init = dock_init2,
7336 .data = &dock_driver_data[1],
7338 #endif
7339 #ifdef CONFIG_THINKPAD_ACPI_BAY
7341 .init = bay_init,
7342 .data = &bay_driver_data,
7344 #endif
7346 .init = cmos_init,
7347 .data = &cmos_driver_data,
7350 .init = led_init,
7351 .data = &led_driver_data,
7354 .init = beep_init,
7355 .data = &beep_driver_data,
7358 .init = thermal_init,
7359 .data = &thermal_driver_data,
7362 .data = &ecdump_driver_data,
7365 .init = brightness_init,
7366 .data = &brightness_driver_data,
7369 .data = &volume_driver_data,
7372 .init = fan_init,
7373 .data = &fan_driver_data,
7377 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7379 unsigned int i;
7380 struct ibm_struct *ibm;
7382 if (!kp || !kp->name || !val)
7383 return -EINVAL;
7385 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7386 ibm = ibms_init[i].data;
7387 WARN_ON(ibm == NULL);
7389 if (!ibm || !ibm->name)
7390 continue;
7392 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7393 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7394 return -ENOSPC;
7395 strcpy(ibms_init[i].param, val);
7396 strcat(ibms_init[i].param, ",");
7397 return 0;
7401 return -EINVAL;
7404 module_param(experimental, int, 0);
7405 MODULE_PARM_DESC(experimental,
7406 "Enables experimental features when non-zero");
7408 module_param_named(debug, dbg_level, uint, 0);
7409 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7411 module_param(force_load, bool, 0);
7412 MODULE_PARM_DESC(force_load,
7413 "Attempts to load the driver even on a "
7414 "mis-identified ThinkPad when true");
7416 module_param_named(fan_control, fan_control_allowed, bool, 0);
7417 MODULE_PARM_DESC(fan_control,
7418 "Enables setting fan parameters features when true");
7420 module_param_named(brightness_mode, brightness_mode, int, 0);
7421 MODULE_PARM_DESC(brightness_mode,
7422 "Selects brightness control strategy: "
7423 "0=auto, 1=EC, 2=CMOS, 3=both");
7425 module_param(brightness_enable, uint, 0);
7426 MODULE_PARM_DESC(brightness_enable,
7427 "Enables backlight control when 1, disables when 0");
7429 module_param(hotkey_report_mode, uint, 0);
7430 MODULE_PARM_DESC(hotkey_report_mode,
7431 "used for backwards compatibility with userspace, "
7432 "see documentation");
7434 #define TPACPI_PARAM(feature) \
7435 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7436 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7437 "at module load, see documentation")
7439 TPACPI_PARAM(hotkey);
7440 TPACPI_PARAM(bluetooth);
7441 TPACPI_PARAM(video);
7442 TPACPI_PARAM(light);
7443 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7444 TPACPI_PARAM(dock);
7445 #endif
7446 #ifdef CONFIG_THINKPAD_ACPI_BAY
7447 TPACPI_PARAM(bay);
7448 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7449 TPACPI_PARAM(cmos);
7450 TPACPI_PARAM(led);
7451 TPACPI_PARAM(beep);
7452 TPACPI_PARAM(ecdump);
7453 TPACPI_PARAM(brightness);
7454 TPACPI_PARAM(volume);
7455 TPACPI_PARAM(fan);
7457 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7458 module_param(dbg_wlswemul, uint, 0);
7459 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7460 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7461 MODULE_PARM_DESC(wlsw_state,
7462 "Initial state of the emulated WLSW switch");
7464 module_param(dbg_bluetoothemul, uint, 0);
7465 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7466 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7467 MODULE_PARM_DESC(bluetooth_state,
7468 "Initial state of the emulated bluetooth switch");
7470 module_param(dbg_wwanemul, uint, 0);
7471 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7472 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7473 MODULE_PARM_DESC(wwan_state,
7474 "Initial state of the emulated WWAN switch");
7476 module_param(dbg_uwbemul, uint, 0);
7477 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7478 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7479 MODULE_PARM_DESC(uwb_state,
7480 "Initial state of the emulated UWB switch");
7481 #endif
7483 static void thinkpad_acpi_module_exit(void)
7485 struct ibm_struct *ibm, *itmp;
7487 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7489 list_for_each_entry_safe_reverse(ibm, itmp,
7490 &tpacpi_all_drivers,
7491 all_drivers) {
7492 ibm_exit(ibm);
7495 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7497 if (tpacpi_inputdev) {
7498 if (tp_features.input_device_registered)
7499 input_unregister_device(tpacpi_inputdev);
7500 else
7501 input_free_device(tpacpi_inputdev);
7504 if (tpacpi_hwmon)
7505 hwmon_device_unregister(tpacpi_hwmon);
7507 if (tp_features.sensors_pdev_attrs_registered)
7508 device_remove_file(&tpacpi_sensors_pdev->dev,
7509 &dev_attr_thinkpad_acpi_pdev_name);
7510 if (tpacpi_sensors_pdev)
7511 platform_device_unregister(tpacpi_sensors_pdev);
7512 if (tpacpi_pdev)
7513 platform_device_unregister(tpacpi_pdev);
7515 if (tp_features.sensors_pdrv_attrs_registered)
7516 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7517 if (tp_features.platform_drv_attrs_registered)
7518 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7520 if (tp_features.sensors_pdrv_registered)
7521 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7523 if (tp_features.platform_drv_registered)
7524 platform_driver_unregister(&tpacpi_pdriver);
7526 if (proc_dir)
7527 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7529 if (tpacpi_wq)
7530 destroy_workqueue(tpacpi_wq);
7532 kfree(thinkpad_id.bios_version_str);
7533 kfree(thinkpad_id.ec_version_str);
7534 kfree(thinkpad_id.model_str);
7538 static int __init thinkpad_acpi_module_init(void)
7540 int ret, i;
7542 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7544 /* Parameter checking */
7545 if (hotkey_report_mode > 2)
7546 return -EINVAL;
7548 /* Driver-level probe */
7550 ret = get_thinkpad_model_data(&thinkpad_id);
7551 if (ret) {
7552 printk(TPACPI_ERR
7553 "unable to get DMI data: %d\n", ret);
7554 thinkpad_acpi_module_exit();
7555 return ret;
7557 ret = probe_for_thinkpad();
7558 if (ret) {
7559 thinkpad_acpi_module_exit();
7560 return ret;
7563 /* Driver initialization */
7565 TPACPI_ACPIHANDLE_INIT(ecrd);
7566 TPACPI_ACPIHANDLE_INIT(ecwr);
7568 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7569 if (!tpacpi_wq) {
7570 thinkpad_acpi_module_exit();
7571 return -ENOMEM;
7574 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7575 if (!proc_dir) {
7576 printk(TPACPI_ERR
7577 "unable to create proc dir " TPACPI_PROC_DIR);
7578 thinkpad_acpi_module_exit();
7579 return -ENODEV;
7581 proc_dir->owner = THIS_MODULE;
7583 ret = platform_driver_register(&tpacpi_pdriver);
7584 if (ret) {
7585 printk(TPACPI_ERR
7586 "unable to register main platform driver\n");
7587 thinkpad_acpi_module_exit();
7588 return ret;
7590 tp_features.platform_drv_registered = 1;
7592 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7593 if (ret) {
7594 printk(TPACPI_ERR
7595 "unable to register hwmon platform driver\n");
7596 thinkpad_acpi_module_exit();
7597 return ret;
7599 tp_features.sensors_pdrv_registered = 1;
7601 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7602 if (!ret) {
7603 tp_features.platform_drv_attrs_registered = 1;
7604 ret = tpacpi_create_driver_attributes(
7605 &tpacpi_hwmon_pdriver.driver);
7607 if (ret) {
7608 printk(TPACPI_ERR
7609 "unable to create sysfs driver attributes\n");
7610 thinkpad_acpi_module_exit();
7611 return ret;
7613 tp_features.sensors_pdrv_attrs_registered = 1;
7616 /* Device initialization */
7617 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7618 NULL, 0);
7619 if (IS_ERR(tpacpi_pdev)) {
7620 ret = PTR_ERR(tpacpi_pdev);
7621 tpacpi_pdev = NULL;
7622 printk(TPACPI_ERR "unable to register platform device\n");
7623 thinkpad_acpi_module_exit();
7624 return ret;
7626 tpacpi_sensors_pdev = platform_device_register_simple(
7627 TPACPI_HWMON_DRVR_NAME,
7628 -1, NULL, 0);
7629 if (IS_ERR(tpacpi_sensors_pdev)) {
7630 ret = PTR_ERR(tpacpi_sensors_pdev);
7631 tpacpi_sensors_pdev = NULL;
7632 printk(TPACPI_ERR
7633 "unable to register hwmon platform device\n");
7634 thinkpad_acpi_module_exit();
7635 return ret;
7637 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7638 &dev_attr_thinkpad_acpi_pdev_name);
7639 if (ret) {
7640 printk(TPACPI_ERR
7641 "unable to create sysfs hwmon device attributes\n");
7642 thinkpad_acpi_module_exit();
7643 return ret;
7645 tp_features.sensors_pdev_attrs_registered = 1;
7646 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7647 if (IS_ERR(tpacpi_hwmon)) {
7648 ret = PTR_ERR(tpacpi_hwmon);
7649 tpacpi_hwmon = NULL;
7650 printk(TPACPI_ERR "unable to register hwmon device\n");
7651 thinkpad_acpi_module_exit();
7652 return ret;
7654 mutex_init(&tpacpi_inputdev_send_mutex);
7655 tpacpi_inputdev = input_allocate_device();
7656 if (!tpacpi_inputdev) {
7657 printk(TPACPI_ERR "unable to allocate input device\n");
7658 thinkpad_acpi_module_exit();
7659 return -ENOMEM;
7660 } else {
7661 /* Prepare input device, but don't register */
7662 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7663 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7664 tpacpi_inputdev->id.bustype = BUS_HOST;
7665 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7666 thinkpad_id.vendor :
7667 PCI_VENDOR_ID_IBM;
7668 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7669 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7671 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7672 ret = ibm_init(&ibms_init[i]);
7673 if (ret >= 0 && *ibms_init[i].param)
7674 ret = ibms_init[i].data->write(ibms_init[i].param);
7675 if (ret < 0) {
7676 thinkpad_acpi_module_exit();
7677 return ret;
7680 ret = input_register_device(tpacpi_inputdev);
7681 if (ret < 0) {
7682 printk(TPACPI_ERR "unable to register input device\n");
7683 thinkpad_acpi_module_exit();
7684 return ret;
7685 } else {
7686 tp_features.input_device_registered = 1;
7689 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7690 return 0;
7693 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7696 * DMI matching for module autoloading
7698 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7699 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7701 * Only models listed in thinkwiki will be supported, so add yours
7702 * if it is not there yet.
7704 #define IBM_BIOS_MODULE_ALIAS(__type) \
7705 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7707 /* Non-ancient thinkpads */
7708 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7709 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7711 /* Ancient thinkpad BIOSes have to be identified by
7712 * BIOS type or model number, and there are far less
7713 * BIOS types than model numbers... */
7714 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7715 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7716 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7718 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7719 MODULE_DESCRIPTION(TPACPI_DESC);
7720 MODULE_VERSION(TPACPI_VERSION);
7721 MODULE_LICENSE("GPL");
7723 module_init(thinkpad_acpi_module_init);
7724 module_exit(thinkpad_acpi_module_exit);