thinkpad-acpi: enhanced debugging messages for rfkill subdrivers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobc49f9c2d0e33d01587e1f66f668e5e6948290062
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
194 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
195 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
196 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
199 /****************************************************************************
200 * Driver-wide structs and misc. variables
203 struct ibm_struct;
205 struct tp_acpi_drv_struct {
206 const struct acpi_device_id *hid;
207 struct acpi_driver *driver;
209 void (*notify) (struct ibm_struct *, u32);
210 acpi_handle *handle;
211 u32 type;
212 struct acpi_device *device;
215 struct ibm_struct {
216 char *name;
218 int (*read) (char *);
219 int (*write) (char *);
220 void (*exit) (void);
221 void (*resume) (void);
222 void (*suspend) (pm_message_t state);
223 void (*shutdown) (void);
225 struct list_head all_drivers;
227 struct tp_acpi_drv_struct *acpi;
229 struct {
230 u8 acpi_driver_registered:1;
231 u8 acpi_notify_installed:1;
232 u8 proc_created:1;
233 u8 init_called:1;
234 u8 experimental:1;
235 } flags;
238 struct ibm_init_struct {
239 char param[32];
241 int (*init) (struct ibm_init_struct *);
242 struct ibm_struct *data;
245 static struct {
246 #ifdef CONFIG_THINKPAD_ACPI_BAY
247 u32 bay_status:1;
248 u32 bay_eject:1;
249 u32 bay_status2:1;
250 u32 bay_eject2:1;
251 #endif
252 u32 bluetooth:1;
253 u32 hotkey:1;
254 u32 hotkey_mask:1;
255 u32 hotkey_wlsw:1;
256 u32 hotkey_tablet:1;
257 u32 light:1;
258 u32 light_status:1;
259 u32 bright_16levels:1;
260 u32 bright_acpimode:1;
261 u32 wan:1;
262 u32 uwb:1;
263 u32 fan_ctrl_status_undef:1;
264 u32 input_device_registered:1;
265 u32 platform_drv_registered:1;
266 u32 platform_drv_attrs_registered:1;
267 u32 sensors_pdrv_registered:1;
268 u32 sensors_pdrv_attrs_registered:1;
269 u32 sensors_pdev_attrs_registered:1;
270 u32 hotkey_poll_active:1;
271 } tp_features;
273 static struct {
274 u16 hotkey_mask_ff:1;
275 u16 bright_cmos_ec_unsync:1;
276 } tp_warned;
278 struct thinkpad_id_data {
279 unsigned int vendor; /* ThinkPad vendor:
280 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
282 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
283 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
285 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
286 u16 ec_model;
288 char *model_str; /* ThinkPad T43 */
289 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
291 static struct thinkpad_id_data thinkpad_id;
293 static enum {
294 TPACPI_LIFE_INIT = 0,
295 TPACPI_LIFE_RUNNING,
296 TPACPI_LIFE_EXITING,
297 } tpacpi_lifecycle;
299 static int experimental;
300 static u32 dbg_level;
302 static struct workqueue_struct *tpacpi_wq;
304 enum led_status_t {
305 TPACPI_LED_OFF = 0,
306 TPACPI_LED_ON,
307 TPACPI_LED_BLINK,
310 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev {
312 struct led_classdev led_classdev;
313 struct work_struct work;
314 enum led_status_t new_state;
315 unsigned int led;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul;
320 static int tpacpi_wlsw_emulstate;
321 static int dbg_bluetoothemul;
322 static int tpacpi_bluetooth_emulstate;
323 static int dbg_wwanemul;
324 static int tpacpi_wwan_emulstate;
325 static int dbg_uwbemul;
326 static int tpacpi_uwb_emulstate;
327 #endif
330 /*************************************************************************
331 * Debugging helpers
334 #define dbg_printk(a_dbg_level, format, arg...) \
335 do { if (dbg_level & (a_dbg_level)) \
336 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
337 } while (0)
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 static const char *str_supported(int is_supported);
342 #else
343 #define vdbg_printk(a_dbg_level, format, arg...) \
344 do { } while (0)
345 #endif
347 static void tpacpi_log_usertask(const char * const what)
349 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
350 what, task_tgid_vnr(current));
353 #define tpacpi_disclose_usertask(what, format, arg...) \
354 do { \
355 if (unlikely( \
356 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
357 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
358 printk(TPACPI_DEBUG "%s: PID %d: " format, \
359 what, task_tgid_vnr(current), ## arg); \
361 } while (0)
363 /****************************************************************************
364 ****************************************************************************
366 * ACPI Helpers and device model
368 ****************************************************************************
369 ****************************************************************************/
371 /*************************************************************************
372 * ACPI basic handles
375 static acpi_handle root_handle;
377 #define TPACPI_HANDLE(object, parent, paths...) \
378 static acpi_handle object##_handle; \
379 static acpi_handle *object##_parent = &parent##_handle; \
380 static char *object##_path; \
381 static char *object##_paths[] = { paths }
383 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
384 "\\_SB.PCI.ISA.EC", /* 570 */
385 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
386 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
387 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
388 "\\_SB.PCI0.ICH3.EC0", /* R31 */
389 "\\_SB.PCI0.LPC.EC", /* all others */
392 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
393 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
395 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
396 /* T4x, X31, X40 */
397 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
398 "\\CMS", /* R40, R40e */
399 ); /* all others */
401 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
402 "^HKEY", /* R30, R31 */
403 "HKEY", /* all others */
404 ); /* 570 */
406 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
407 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
408 "\\_SB.PCI0.VID0", /* 770e */
409 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
410 "\\_SB.PCI0.AGP.VID", /* all others */
411 ); /* R30, R31 */
414 /*************************************************************************
415 * ACPI helpers
418 static int acpi_evalf(acpi_handle handle,
419 void *res, char *method, char *fmt, ...)
421 char *fmt0 = fmt;
422 struct acpi_object_list params;
423 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
424 struct acpi_buffer result, *resultp;
425 union acpi_object out_obj;
426 acpi_status status;
427 va_list ap;
428 char res_type;
429 int success;
430 int quiet;
432 if (!*fmt) {
433 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
434 return 0;
437 if (*fmt == 'q') {
438 quiet = 1;
439 fmt++;
440 } else
441 quiet = 0;
443 res_type = *(fmt++);
445 params.count = 0;
446 params.pointer = &in_objs[0];
448 va_start(ap, fmt);
449 while (*fmt) {
450 char c = *(fmt++);
451 switch (c) {
452 case 'd': /* int */
453 in_objs[params.count].integer.value = va_arg(ap, int);
454 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
455 break;
456 /* add more types as needed */
457 default:
458 printk(TPACPI_ERR "acpi_evalf() called "
459 "with invalid format character '%c'\n", c);
460 return 0;
463 va_end(ap);
465 if (res_type != 'v') {
466 result.length = sizeof(out_obj);
467 result.pointer = &out_obj;
468 resultp = &result;
469 } else
470 resultp = NULL;
472 status = acpi_evaluate_object(handle, method, &params, resultp);
474 switch (res_type) {
475 case 'd': /* int */
476 if (res)
477 *(int *)res = out_obj.integer.value;
478 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
479 break;
480 case 'v': /* void */
481 success = status == AE_OK;
482 break;
483 /* add more types as needed */
484 default:
485 printk(TPACPI_ERR "acpi_evalf() called "
486 "with invalid format character '%c'\n", res_type);
487 return 0;
490 if (!success && !quiet)
491 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
492 method, fmt0, status);
494 return success;
497 static int acpi_ec_read(int i, u8 *p)
499 int v;
501 if (ecrd_handle) {
502 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
503 return 0;
504 *p = v;
505 } else {
506 if (ec_read(i, p) < 0)
507 return 0;
510 return 1;
513 static int acpi_ec_write(int i, u8 v)
515 if (ecwr_handle) {
516 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
517 return 0;
518 } else {
519 if (ec_write(i, v) < 0)
520 return 0;
523 return 1;
526 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
527 static int _sta(acpi_handle handle)
529 int status;
531 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
532 status = 0;
534 return status;
536 #endif
538 static int issue_thinkpad_cmos_command(int cmos_cmd)
540 if (!cmos_handle)
541 return -ENXIO;
543 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
544 return -EIO;
546 return 0;
549 /*************************************************************************
550 * ACPI device model
553 #define TPACPI_ACPIHANDLE_INIT(object) \
554 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
555 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
557 static void drv_acpi_handle_init(char *name,
558 acpi_handle *handle, acpi_handle parent,
559 char **paths, int num_paths, char **path)
561 int i;
562 acpi_status status;
564 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
565 name);
567 for (i = 0; i < num_paths; i++) {
568 status = acpi_get_handle(parent, paths[i], handle);
569 if (ACPI_SUCCESS(status)) {
570 *path = paths[i];
571 dbg_printk(TPACPI_DBG_INIT,
572 "Found ACPI handle %s for %s\n",
573 *path, name);
574 return;
578 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
579 name);
580 *handle = NULL;
583 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
585 struct ibm_struct *ibm = data;
587 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
588 return;
590 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
591 return;
593 ibm->acpi->notify(ibm, event);
596 static int __init setup_acpi_notify(struct ibm_struct *ibm)
598 acpi_status status;
599 int rc;
601 BUG_ON(!ibm->acpi);
603 if (!*ibm->acpi->handle)
604 return 0;
606 vdbg_printk(TPACPI_DBG_INIT,
607 "setting up ACPI notify for %s\n", ibm->name);
609 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
610 if (rc < 0) {
611 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
612 ibm->name, rc);
613 return -ENODEV;
616 ibm->acpi->device->driver_data = ibm;
617 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
618 TPACPI_ACPI_EVENT_PREFIX,
619 ibm->name);
621 status = acpi_install_notify_handler(*ibm->acpi->handle,
622 ibm->acpi->type, dispatch_acpi_notify, ibm);
623 if (ACPI_FAILURE(status)) {
624 if (status == AE_ALREADY_EXISTS) {
625 printk(TPACPI_NOTICE
626 "another device driver is already "
627 "handling %s events\n", ibm->name);
628 } else {
629 printk(TPACPI_ERR
630 "acpi_install_notify_handler(%s) failed: %d\n",
631 ibm->name, status);
633 return -ENODEV;
635 ibm->flags.acpi_notify_installed = 1;
636 return 0;
639 static int __init tpacpi_device_add(struct acpi_device *device)
641 return 0;
644 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
646 int rc;
648 dbg_printk(TPACPI_DBG_INIT,
649 "registering %s as an ACPI driver\n", ibm->name);
651 BUG_ON(!ibm->acpi);
653 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
654 if (!ibm->acpi->driver) {
655 printk(TPACPI_ERR
656 "failed to allocate memory for ibm->acpi->driver\n");
657 return -ENOMEM;
660 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
661 ibm->acpi->driver->ids = ibm->acpi->hid;
663 ibm->acpi->driver->ops.add = &tpacpi_device_add;
665 rc = acpi_bus_register_driver(ibm->acpi->driver);
666 if (rc < 0) {
667 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
668 ibm->name, rc);
669 kfree(ibm->acpi->driver);
670 ibm->acpi->driver = NULL;
671 } else if (!rc)
672 ibm->flags.acpi_driver_registered = 1;
674 return rc;
678 /****************************************************************************
679 ****************************************************************************
681 * Procfs Helpers
683 ****************************************************************************
684 ****************************************************************************/
686 static int dispatch_procfs_read(char *page, char **start, off_t off,
687 int count, int *eof, void *data)
689 struct ibm_struct *ibm = data;
690 int len;
692 if (!ibm || !ibm->read)
693 return -EINVAL;
695 len = ibm->read(page);
696 if (len < 0)
697 return len;
699 if (len <= off + count)
700 *eof = 1;
701 *start = page + off;
702 len -= off;
703 if (len > count)
704 len = count;
705 if (len < 0)
706 len = 0;
708 return len;
711 static int dispatch_procfs_write(struct file *file,
712 const char __user *userbuf,
713 unsigned long count, void *data)
715 struct ibm_struct *ibm = data;
716 char *kernbuf;
717 int ret;
719 if (!ibm || !ibm->write)
720 return -EINVAL;
722 kernbuf = kmalloc(count + 2, GFP_KERNEL);
723 if (!kernbuf)
724 return -ENOMEM;
726 if (copy_from_user(kernbuf, userbuf, count)) {
727 kfree(kernbuf);
728 return -EFAULT;
731 kernbuf[count] = 0;
732 strcat(kernbuf, ",");
733 ret = ibm->write(kernbuf);
734 if (ret == 0)
735 ret = count;
737 kfree(kernbuf);
739 return ret;
742 static char *next_cmd(char **cmds)
744 char *start = *cmds;
745 char *end;
747 while ((end = strchr(start, ',')) && end == start)
748 start = end + 1;
750 if (!end)
751 return NULL;
753 *end = 0;
754 *cmds = end + 1;
755 return start;
759 /****************************************************************************
760 ****************************************************************************
762 * Device model: input, hwmon and platform
764 ****************************************************************************
765 ****************************************************************************/
767 static struct platform_device *tpacpi_pdev;
768 static struct platform_device *tpacpi_sensors_pdev;
769 static struct device *tpacpi_hwmon;
770 static struct input_dev *tpacpi_inputdev;
771 static struct mutex tpacpi_inputdev_send_mutex;
772 static LIST_HEAD(tpacpi_all_drivers);
774 static int tpacpi_suspend_handler(struct platform_device *pdev,
775 pm_message_t state)
777 struct ibm_struct *ibm, *itmp;
779 list_for_each_entry_safe(ibm, itmp,
780 &tpacpi_all_drivers,
781 all_drivers) {
782 if (ibm->suspend)
783 (ibm->suspend)(state);
786 return 0;
789 static int tpacpi_resume_handler(struct platform_device *pdev)
791 struct ibm_struct *ibm, *itmp;
793 list_for_each_entry_safe(ibm, itmp,
794 &tpacpi_all_drivers,
795 all_drivers) {
796 if (ibm->resume)
797 (ibm->resume)();
800 return 0;
803 static void tpacpi_shutdown_handler(struct platform_device *pdev)
805 struct ibm_struct *ibm, *itmp;
807 list_for_each_entry_safe(ibm, itmp,
808 &tpacpi_all_drivers,
809 all_drivers) {
810 if (ibm->shutdown)
811 (ibm->shutdown)();
815 static struct platform_driver tpacpi_pdriver = {
816 .driver = {
817 .name = TPACPI_DRVR_NAME,
818 .owner = THIS_MODULE,
820 .suspend = tpacpi_suspend_handler,
821 .resume = tpacpi_resume_handler,
822 .shutdown = tpacpi_shutdown_handler,
825 static struct platform_driver tpacpi_hwmon_pdriver = {
826 .driver = {
827 .name = TPACPI_HWMON_DRVR_NAME,
828 .owner = THIS_MODULE,
832 /*************************************************************************
833 * sysfs support helpers
836 struct attribute_set {
837 unsigned int members, max_members;
838 struct attribute_group group;
841 struct attribute_set_obj {
842 struct attribute_set s;
843 struct attribute *a;
844 } __attribute__((packed));
846 static struct attribute_set *create_attr_set(unsigned int max_members,
847 const char *name)
849 struct attribute_set_obj *sobj;
851 if (max_members == 0)
852 return NULL;
854 /* Allocates space for implicit NULL at the end too */
855 sobj = kzalloc(sizeof(struct attribute_set_obj) +
856 max_members * sizeof(struct attribute *),
857 GFP_KERNEL);
858 if (!sobj)
859 return NULL;
860 sobj->s.max_members = max_members;
861 sobj->s.group.attrs = &sobj->a;
862 sobj->s.group.name = name;
864 return &sobj->s;
867 #define destroy_attr_set(_set) \
868 kfree(_set);
870 /* not multi-threaded safe, use it in a single thread per set */
871 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
873 if (!s || !attr)
874 return -EINVAL;
876 if (s->members >= s->max_members)
877 return -ENOMEM;
879 s->group.attrs[s->members] = attr;
880 s->members++;
882 return 0;
885 static int add_many_to_attr_set(struct attribute_set *s,
886 struct attribute **attr,
887 unsigned int count)
889 int i, res;
891 for (i = 0; i < count; i++) {
892 res = add_to_attr_set(s, attr[i]);
893 if (res)
894 return res;
897 return 0;
900 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
902 sysfs_remove_group(kobj, &s->group);
903 destroy_attr_set(s);
906 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
907 sysfs_create_group(_kobj, &_attr_set->group)
909 static int parse_strtoul(const char *buf,
910 unsigned long max, unsigned long *value)
912 char *endp;
914 while (*buf && isspace(*buf))
915 buf++;
916 *value = simple_strtoul(buf, &endp, 0);
917 while (*endp && isspace(*endp))
918 endp++;
919 if (*endp || *value > max)
920 return -EINVAL;
922 return 0;
925 static void tpacpi_disable_brightness_delay(void)
927 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
928 printk(TPACPI_NOTICE
929 "ACPI backlight control delay disabled\n");
932 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
934 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
935 union acpi_object *obj;
936 int rc;
938 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
939 obj = (union acpi_object *)buffer.pointer;
940 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
941 printk(TPACPI_ERR "Unknown _BCL data, "
942 "please report this to %s\n", TPACPI_MAIL);
943 rc = 0;
944 } else {
945 rc = obj->package.count;
947 } else {
948 return 0;
951 kfree(buffer.pointer);
952 return rc;
955 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
956 u32 lvl, void *context, void **rv)
958 char name[ACPI_PATH_SEGMENT_LENGTH];
959 struct acpi_buffer buffer = { sizeof(name), &name };
961 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
962 !strncmp("_BCL", name, sizeof(name) - 1)) {
963 BUG_ON(!rv || !*rv);
964 **(int **)rv = tpacpi_query_bcl_levels(handle);
965 return AE_CTRL_TERMINATE;
966 } else {
967 return AE_OK;
972 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
974 static int __init tpacpi_check_std_acpi_brightness_support(void)
976 int status;
977 int bcl_levels = 0;
978 void *bcl_ptr = &bcl_levels;
980 if (!vid_handle) {
981 TPACPI_ACPIHANDLE_INIT(vid);
983 if (!vid_handle)
984 return 0;
987 * Search for a _BCL method, and execute it. This is safe on all
988 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
989 * BIOS in ACPI backlight control mode. We do NOT have to care
990 * about calling the _BCL method in an enabled video device, any
991 * will do for our purposes.
994 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
995 tpacpi_acpi_walk_find_bcl, NULL,
996 &bcl_ptr);
998 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
999 tp_features.bright_acpimode = 1;
1000 return (bcl_levels - 2);
1003 return 0;
1006 static int __init tpacpi_new_rfkill(const unsigned int id,
1007 struct rfkill **rfk,
1008 const enum rfkill_type rfktype,
1009 const char *name,
1010 const bool set_default,
1011 int (*toggle_radio)(void *, enum rfkill_state),
1012 int (*get_state)(void *, enum rfkill_state *))
1014 int res;
1015 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1017 res = get_state(NULL, &initial_state);
1018 if (res < 0) {
1019 printk(TPACPI_ERR
1020 "failed to read initial state for %s, error %d; "
1021 "will turn radio off\n", name, res);
1022 } else if (set_default) {
1023 /* try to set the initial state as the default for the rfkill
1024 * type, since we ask the firmware to preserve it across S5 in
1025 * NVRAM */
1026 if (rfkill_set_default(rfktype,
1027 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1028 RFKILL_STATE_UNBLOCKED :
1029 RFKILL_STATE_SOFT_BLOCKED) == -EPERM)
1030 vdbg_printk(TPACPI_DBG_RFKILL,
1031 "Default state for %s cannot be changed\n",
1032 name);
1035 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1036 if (!*rfk) {
1037 printk(TPACPI_ERR
1038 "failed to allocate memory for rfkill class\n");
1039 return -ENOMEM;
1042 (*rfk)->name = name;
1043 (*rfk)->get_state = get_state;
1044 (*rfk)->toggle_radio = toggle_radio;
1045 (*rfk)->state = initial_state;
1047 res = rfkill_register(*rfk);
1048 if (res < 0) {
1049 printk(TPACPI_ERR
1050 "failed to register %s rfkill switch: %d\n",
1051 name, res);
1052 rfkill_free(*rfk);
1053 *rfk = NULL;
1054 return res;
1057 return 0;
1060 static void printk_deprecated_attribute(const char * const what,
1061 const char * const details)
1063 tpacpi_log_usertask("deprecated sysfs attribute");
1064 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1065 "will be removed. %s\n",
1066 what, details);
1069 static void printk_deprecated_rfkill_attribute(const char * const what)
1071 printk_deprecated_attribute(what,
1072 "Please switch to generic rfkill before year 2010");
1075 /*************************************************************************
1076 * thinkpad-acpi driver attributes
1079 /* interface_version --------------------------------------------------- */
1080 static ssize_t tpacpi_driver_interface_version_show(
1081 struct device_driver *drv,
1082 char *buf)
1084 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1087 static DRIVER_ATTR(interface_version, S_IRUGO,
1088 tpacpi_driver_interface_version_show, NULL);
1090 /* debug_level --------------------------------------------------------- */
1091 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1092 char *buf)
1094 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1097 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1098 const char *buf, size_t count)
1100 unsigned long t;
1102 if (parse_strtoul(buf, 0xffff, &t))
1103 return -EINVAL;
1105 dbg_level = t;
1107 return count;
1110 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1111 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1113 /* version ------------------------------------------------------------- */
1114 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1115 char *buf)
1117 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1118 TPACPI_DESC, TPACPI_VERSION);
1121 static DRIVER_ATTR(version, S_IRUGO,
1122 tpacpi_driver_version_show, NULL);
1124 /* --------------------------------------------------------------------- */
1126 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1128 static void tpacpi_send_radiosw_update(void);
1130 /* wlsw_emulstate ------------------------------------------------------ */
1131 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1132 char *buf)
1134 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1137 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1138 const char *buf, size_t count)
1140 unsigned long t;
1142 if (parse_strtoul(buf, 1, &t))
1143 return -EINVAL;
1145 if (tpacpi_wlsw_emulstate != t) {
1146 tpacpi_wlsw_emulstate = !!t;
1147 tpacpi_send_radiosw_update();
1148 } else
1149 tpacpi_wlsw_emulstate = !!t;
1151 return count;
1154 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1155 tpacpi_driver_wlsw_emulstate_show,
1156 tpacpi_driver_wlsw_emulstate_store);
1158 /* bluetooth_emulstate ------------------------------------------------- */
1159 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1160 struct device_driver *drv,
1161 char *buf)
1163 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1166 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1167 struct device_driver *drv,
1168 const char *buf, size_t count)
1170 unsigned long t;
1172 if (parse_strtoul(buf, 1, &t))
1173 return -EINVAL;
1175 tpacpi_bluetooth_emulstate = !!t;
1177 return count;
1180 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1181 tpacpi_driver_bluetooth_emulstate_show,
1182 tpacpi_driver_bluetooth_emulstate_store);
1184 /* wwan_emulstate ------------------------------------------------- */
1185 static ssize_t tpacpi_driver_wwan_emulstate_show(
1186 struct device_driver *drv,
1187 char *buf)
1189 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1192 static ssize_t tpacpi_driver_wwan_emulstate_store(
1193 struct device_driver *drv,
1194 const char *buf, size_t count)
1196 unsigned long t;
1198 if (parse_strtoul(buf, 1, &t))
1199 return -EINVAL;
1201 tpacpi_wwan_emulstate = !!t;
1203 return count;
1206 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1207 tpacpi_driver_wwan_emulstate_show,
1208 tpacpi_driver_wwan_emulstate_store);
1210 /* uwb_emulstate ------------------------------------------------- */
1211 static ssize_t tpacpi_driver_uwb_emulstate_show(
1212 struct device_driver *drv,
1213 char *buf)
1215 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1218 static ssize_t tpacpi_driver_uwb_emulstate_store(
1219 struct device_driver *drv,
1220 const char *buf, size_t count)
1222 unsigned long t;
1224 if (parse_strtoul(buf, 1, &t))
1225 return -EINVAL;
1227 tpacpi_uwb_emulstate = !!t;
1229 return count;
1232 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1233 tpacpi_driver_uwb_emulstate_show,
1234 tpacpi_driver_uwb_emulstate_store);
1235 #endif
1237 /* --------------------------------------------------------------------- */
1239 static struct driver_attribute *tpacpi_driver_attributes[] = {
1240 &driver_attr_debug_level, &driver_attr_version,
1241 &driver_attr_interface_version,
1244 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1246 int i, res;
1248 i = 0;
1249 res = 0;
1250 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1251 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1252 i++;
1255 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1256 if (!res && dbg_wlswemul)
1257 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1258 if (!res && dbg_bluetoothemul)
1259 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1260 if (!res && dbg_wwanemul)
1261 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1262 if (!res && dbg_uwbemul)
1263 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1264 #endif
1266 return res;
1269 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1271 int i;
1273 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1274 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1276 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1277 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1278 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1279 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1280 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1281 #endif
1284 /****************************************************************************
1285 ****************************************************************************
1287 * Subdrivers
1289 ****************************************************************************
1290 ****************************************************************************/
1292 /*************************************************************************
1293 * thinkpad-acpi init subdriver
1296 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1298 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1299 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1301 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1302 (thinkpad_id.bios_version_str) ?
1303 thinkpad_id.bios_version_str : "unknown",
1304 (thinkpad_id.ec_version_str) ?
1305 thinkpad_id.ec_version_str : "unknown");
1307 if (thinkpad_id.vendor && thinkpad_id.model_str)
1308 printk(TPACPI_INFO "%s %s, model %s\n",
1309 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1310 "IBM" : ((thinkpad_id.vendor ==
1311 PCI_VENDOR_ID_LENOVO) ?
1312 "Lenovo" : "Unknown vendor"),
1313 thinkpad_id.model_str,
1314 (thinkpad_id.nummodel_str) ?
1315 thinkpad_id.nummodel_str : "unknown");
1317 return 0;
1320 static int thinkpad_acpi_driver_read(char *p)
1322 int len = 0;
1324 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1325 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1327 return len;
1330 static struct ibm_struct thinkpad_acpi_driver_data = {
1331 .name = "driver",
1332 .read = thinkpad_acpi_driver_read,
1335 /*************************************************************************
1336 * Hotkey subdriver
1339 enum { /* hot key scan codes (derived from ACPI DSDT) */
1340 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1341 TP_ACPI_HOTKEYSCAN_FNF2,
1342 TP_ACPI_HOTKEYSCAN_FNF3,
1343 TP_ACPI_HOTKEYSCAN_FNF4,
1344 TP_ACPI_HOTKEYSCAN_FNF5,
1345 TP_ACPI_HOTKEYSCAN_FNF6,
1346 TP_ACPI_HOTKEYSCAN_FNF7,
1347 TP_ACPI_HOTKEYSCAN_FNF8,
1348 TP_ACPI_HOTKEYSCAN_FNF9,
1349 TP_ACPI_HOTKEYSCAN_FNF10,
1350 TP_ACPI_HOTKEYSCAN_FNF11,
1351 TP_ACPI_HOTKEYSCAN_FNF12,
1352 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1353 TP_ACPI_HOTKEYSCAN_FNINSERT,
1354 TP_ACPI_HOTKEYSCAN_FNDELETE,
1355 TP_ACPI_HOTKEYSCAN_FNHOME,
1356 TP_ACPI_HOTKEYSCAN_FNEND,
1357 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1358 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1359 TP_ACPI_HOTKEYSCAN_FNSPACE,
1360 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1361 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1362 TP_ACPI_HOTKEYSCAN_MUTE,
1363 TP_ACPI_HOTKEYSCAN_THINKPAD,
1366 enum { /* Keys available through NVRAM polling */
1367 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1368 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1371 enum { /* Positions of some of the keys in hotkey masks */
1372 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1373 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1374 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1375 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1376 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1377 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1378 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1379 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1380 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1381 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1382 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1385 enum { /* NVRAM to ACPI HKEY group map */
1386 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1387 TP_ACPI_HKEY_ZOOM_MASK |
1388 TP_ACPI_HKEY_DISPSWTCH_MASK |
1389 TP_ACPI_HKEY_HIBERNATE_MASK,
1390 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1391 TP_ACPI_HKEY_BRGHTDWN_MASK,
1392 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1393 TP_ACPI_HKEY_VOLDWN_MASK |
1394 TP_ACPI_HKEY_MUTE_MASK,
1397 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1398 struct tp_nvram_state {
1399 u16 thinkpad_toggle:1;
1400 u16 zoom_toggle:1;
1401 u16 display_toggle:1;
1402 u16 thinklight_toggle:1;
1403 u16 hibernate_toggle:1;
1404 u16 displayexp_toggle:1;
1405 u16 display_state:1;
1406 u16 brightness_toggle:1;
1407 u16 volume_toggle:1;
1408 u16 mute:1;
1410 u8 brightness_level;
1411 u8 volume_level;
1414 static struct task_struct *tpacpi_hotkey_task;
1415 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1416 static int hotkey_poll_freq = 10; /* Hz */
1417 static struct mutex hotkey_thread_mutex;
1418 static struct mutex hotkey_thread_data_mutex;
1419 static unsigned int hotkey_config_change;
1421 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1423 #define hotkey_source_mask 0U
1425 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1427 static struct mutex hotkey_mutex;
1429 static enum { /* Reasons for waking up */
1430 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1431 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1432 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1433 } hotkey_wakeup_reason;
1435 static int hotkey_autosleep_ack;
1437 static u32 hotkey_orig_mask;
1438 static u32 hotkey_all_mask;
1439 static u32 hotkey_reserved_mask;
1440 static u32 hotkey_mask;
1442 static unsigned int hotkey_report_mode;
1444 static u16 *hotkey_keycode_map;
1446 static struct attribute_set *hotkey_dev_attributes;
1448 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1449 #define HOTKEY_CONFIG_CRITICAL_START \
1450 do { \
1451 mutex_lock(&hotkey_thread_data_mutex); \
1452 hotkey_config_change++; \
1453 } while (0);
1454 #define HOTKEY_CONFIG_CRITICAL_END \
1455 mutex_unlock(&hotkey_thread_data_mutex);
1456 #else
1457 #define HOTKEY_CONFIG_CRITICAL_START
1458 #define HOTKEY_CONFIG_CRITICAL_END
1459 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1461 /* HKEY.MHKG() return bits */
1462 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1464 static int hotkey_get_wlsw(int *status)
1466 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1467 if (dbg_wlswemul) {
1468 *status = !!tpacpi_wlsw_emulstate;
1469 return 0;
1471 #endif
1472 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1473 return -EIO;
1474 return 0;
1477 static int hotkey_get_tablet_mode(int *status)
1479 int s;
1481 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1482 return -EIO;
1484 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1485 return 0;
1489 * Call with hotkey_mutex held
1491 static int hotkey_mask_get(void)
1493 u32 m = 0;
1495 if (tp_features.hotkey_mask) {
1496 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1497 return -EIO;
1499 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1501 return 0;
1505 * Call with hotkey_mutex held
1507 static int hotkey_mask_set(u32 mask)
1509 int i;
1510 int rc = 0;
1512 if (tp_features.hotkey_mask) {
1513 if (!tp_warned.hotkey_mask_ff &&
1514 (mask == 0xffff || mask == 0xffffff ||
1515 mask == 0xffffffff)) {
1516 tp_warned.hotkey_mask_ff = 1;
1517 printk(TPACPI_NOTICE
1518 "setting the hotkey mask to 0x%08x is likely "
1519 "not the best way to go about it\n", mask);
1520 printk(TPACPI_NOTICE
1521 "please consider using the driver defaults, "
1522 "and refer to up-to-date thinkpad-acpi "
1523 "documentation\n");
1526 HOTKEY_CONFIG_CRITICAL_START
1527 for (i = 0; i < 32; i++) {
1528 u32 m = 1 << i;
1529 /* enable in firmware mask only keys not in NVRAM
1530 * mode, but enable the key in the cached hotkey_mask
1531 * regardless of mode, or the key will end up
1532 * disabled by hotkey_mask_get() */
1533 if (!acpi_evalf(hkey_handle,
1534 NULL, "MHKM", "vdd", i + 1,
1535 !!((mask & ~hotkey_source_mask) & m))) {
1536 rc = -EIO;
1537 break;
1538 } else {
1539 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1542 HOTKEY_CONFIG_CRITICAL_END
1544 /* hotkey_mask_get must be called unconditionally below */
1545 if (!hotkey_mask_get() && !rc &&
1546 (hotkey_mask & ~hotkey_source_mask) !=
1547 (mask & ~hotkey_source_mask)) {
1548 printk(TPACPI_NOTICE
1549 "requested hot key mask 0x%08x, but "
1550 "firmware forced it to 0x%08x\n",
1551 mask, hotkey_mask);
1553 } else {
1554 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1555 HOTKEY_CONFIG_CRITICAL_START
1556 hotkey_mask = mask & hotkey_source_mask;
1557 HOTKEY_CONFIG_CRITICAL_END
1558 hotkey_mask_get();
1559 if (hotkey_mask != mask) {
1560 printk(TPACPI_NOTICE
1561 "requested hot key mask 0x%08x, "
1562 "forced to 0x%08x (NVRAM poll mask is "
1563 "0x%08x): no firmware mask support\n",
1564 mask, hotkey_mask, hotkey_source_mask);
1566 #else
1567 hotkey_mask_get();
1568 rc = -ENXIO;
1569 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1572 return rc;
1575 static int hotkey_status_get(int *status)
1577 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1578 return -EIO;
1580 return 0;
1583 static int hotkey_status_set(bool enable)
1585 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1586 return -EIO;
1588 return 0;
1591 static void tpacpi_input_send_tabletsw(void)
1593 int state;
1595 if (tp_features.hotkey_tablet &&
1596 !hotkey_get_tablet_mode(&state)) {
1597 mutex_lock(&tpacpi_inputdev_send_mutex);
1599 input_report_switch(tpacpi_inputdev,
1600 SW_TABLET_MODE, !!state);
1601 input_sync(tpacpi_inputdev);
1603 mutex_unlock(&tpacpi_inputdev_send_mutex);
1607 static void tpacpi_input_send_key(unsigned int scancode)
1609 unsigned int keycode;
1611 keycode = hotkey_keycode_map[scancode];
1613 if (keycode != KEY_RESERVED) {
1614 mutex_lock(&tpacpi_inputdev_send_mutex);
1616 input_report_key(tpacpi_inputdev, keycode, 1);
1617 if (keycode == KEY_UNKNOWN)
1618 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1619 scancode);
1620 input_sync(tpacpi_inputdev);
1622 input_report_key(tpacpi_inputdev, keycode, 0);
1623 if (keycode == KEY_UNKNOWN)
1624 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1625 scancode);
1626 input_sync(tpacpi_inputdev);
1628 mutex_unlock(&tpacpi_inputdev_send_mutex);
1632 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1633 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1635 static void tpacpi_hotkey_send_key(unsigned int scancode)
1637 tpacpi_input_send_key(scancode);
1638 if (hotkey_report_mode < 2) {
1639 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1640 0x80, 0x1001 + scancode);
1644 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1646 u8 d;
1648 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1649 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1650 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1651 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1652 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1653 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1655 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1656 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1657 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1659 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1660 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1661 n->displayexp_toggle =
1662 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1664 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1665 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1666 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1667 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1668 n->brightness_toggle =
1669 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1671 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1672 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1673 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1674 >> TP_NVRAM_POS_LEVEL_VOLUME;
1675 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1676 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1680 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1681 do { \
1682 if ((mask & (1 << __scancode)) && \
1683 oldn->__member != newn->__member) \
1684 tpacpi_hotkey_send_key(__scancode); \
1685 } while (0)
1687 #define TPACPI_MAY_SEND_KEY(__scancode) \
1688 do { if (mask & (1 << __scancode)) \
1689 tpacpi_hotkey_send_key(__scancode); } while (0)
1691 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1692 struct tp_nvram_state *newn,
1693 u32 mask)
1695 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1696 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1698 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1700 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1702 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1704 /* handle volume */
1705 if (oldn->volume_toggle != newn->volume_toggle) {
1706 if (oldn->mute != newn->mute) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1709 if (oldn->volume_level > newn->volume_level) {
1710 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1711 } else if (oldn->volume_level < newn->volume_level) {
1712 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1713 } else if (oldn->mute == newn->mute) {
1714 /* repeated key presses that didn't change state */
1715 if (newn->mute) {
1716 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1717 } else if (newn->volume_level != 0) {
1718 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1719 } else {
1720 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1725 /* handle brightness */
1726 if (oldn->brightness_toggle != newn->brightness_toggle) {
1727 if (oldn->brightness_level < newn->brightness_level) {
1728 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1729 } else if (oldn->brightness_level > newn->brightness_level) {
1730 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1731 } else {
1732 /* repeated key presses that didn't change state */
1733 if (newn->brightness_level != 0) {
1734 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1735 } else {
1736 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1742 #undef TPACPI_COMPARE_KEY
1743 #undef TPACPI_MAY_SEND_KEY
1745 static int hotkey_kthread(void *data)
1747 struct tp_nvram_state s[2];
1748 u32 mask;
1749 unsigned int si, so;
1750 unsigned long t;
1751 unsigned int change_detector, must_reset;
1753 mutex_lock(&hotkey_thread_mutex);
1755 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1756 goto exit;
1758 set_freezable();
1760 so = 0;
1761 si = 1;
1762 t = 0;
1764 /* Initial state for compares */
1765 mutex_lock(&hotkey_thread_data_mutex);
1766 change_detector = hotkey_config_change;
1767 mask = hotkey_source_mask & hotkey_mask;
1768 mutex_unlock(&hotkey_thread_data_mutex);
1769 hotkey_read_nvram(&s[so], mask);
1771 while (!kthread_should_stop() && hotkey_poll_freq) {
1772 if (t == 0)
1773 t = 1000/hotkey_poll_freq;
1774 t = msleep_interruptible(t);
1775 if (unlikely(kthread_should_stop()))
1776 break;
1777 must_reset = try_to_freeze();
1778 if (t > 0 && !must_reset)
1779 continue;
1781 mutex_lock(&hotkey_thread_data_mutex);
1782 if (must_reset || hotkey_config_change != change_detector) {
1783 /* forget old state on thaw or config change */
1784 si = so;
1785 t = 0;
1786 change_detector = hotkey_config_change;
1788 mask = hotkey_source_mask & hotkey_mask;
1789 mutex_unlock(&hotkey_thread_data_mutex);
1791 if (likely(mask)) {
1792 hotkey_read_nvram(&s[si], mask);
1793 if (likely(si != so)) {
1794 hotkey_compare_and_issue_event(&s[so], &s[si],
1795 mask);
1799 so = si;
1800 si ^= 1;
1803 exit:
1804 mutex_unlock(&hotkey_thread_mutex);
1805 return 0;
1808 static void hotkey_poll_stop_sync(void)
1810 if (tpacpi_hotkey_task) {
1811 if (frozen(tpacpi_hotkey_task) ||
1812 freezing(tpacpi_hotkey_task))
1813 thaw_process(tpacpi_hotkey_task);
1815 kthread_stop(tpacpi_hotkey_task);
1816 tpacpi_hotkey_task = NULL;
1817 mutex_lock(&hotkey_thread_mutex);
1818 /* at this point, the thread did exit */
1819 mutex_unlock(&hotkey_thread_mutex);
1823 /* call with hotkey_mutex held */
1824 static void hotkey_poll_setup(int may_warn)
1826 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1827 hotkey_poll_freq > 0 &&
1828 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1829 if (!tpacpi_hotkey_task) {
1830 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1831 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1832 if (IS_ERR(tpacpi_hotkey_task)) {
1833 tpacpi_hotkey_task = NULL;
1834 printk(TPACPI_ERR
1835 "could not create kernel thread "
1836 "for hotkey polling\n");
1839 } else {
1840 hotkey_poll_stop_sync();
1841 if (may_warn &&
1842 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1843 printk(TPACPI_NOTICE
1844 "hot keys 0x%08x require polling, "
1845 "which is currently disabled\n",
1846 hotkey_source_mask);
1851 static void hotkey_poll_setup_safe(int may_warn)
1853 mutex_lock(&hotkey_mutex);
1854 hotkey_poll_setup(may_warn);
1855 mutex_unlock(&hotkey_mutex);
1858 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1860 static void hotkey_poll_setup_safe(int __unused)
1864 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1866 static int hotkey_inputdev_open(struct input_dev *dev)
1868 switch (tpacpi_lifecycle) {
1869 case TPACPI_LIFE_INIT:
1871 * hotkey_init will call hotkey_poll_setup_safe
1872 * at the appropriate moment
1874 return 0;
1875 case TPACPI_LIFE_EXITING:
1876 return -EBUSY;
1877 case TPACPI_LIFE_RUNNING:
1878 hotkey_poll_setup_safe(0);
1879 return 0;
1882 /* Should only happen if tpacpi_lifecycle is corrupt */
1883 BUG();
1884 return -EBUSY;
1887 static void hotkey_inputdev_close(struct input_dev *dev)
1889 /* disable hotkey polling when possible */
1890 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1891 hotkey_poll_setup_safe(0);
1894 /* sysfs hotkey enable ------------------------------------------------- */
1895 static ssize_t hotkey_enable_show(struct device *dev,
1896 struct device_attribute *attr,
1897 char *buf)
1899 int res, status;
1901 printk_deprecated_attribute("hotkey_enable",
1902 "Hotkey reporting is always enabled");
1904 res = hotkey_status_get(&status);
1905 if (res)
1906 return res;
1908 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1911 static ssize_t hotkey_enable_store(struct device *dev,
1912 struct device_attribute *attr,
1913 const char *buf, size_t count)
1915 unsigned long t;
1917 printk_deprecated_attribute("hotkey_enable",
1918 "Hotkeys can be disabled through hotkey_mask");
1920 if (parse_strtoul(buf, 1, &t))
1921 return -EINVAL;
1923 if (t == 0)
1924 return -EPERM;
1926 return count;
1929 static struct device_attribute dev_attr_hotkey_enable =
1930 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1931 hotkey_enable_show, hotkey_enable_store);
1933 /* sysfs hotkey mask --------------------------------------------------- */
1934 static ssize_t hotkey_mask_show(struct device *dev,
1935 struct device_attribute *attr,
1936 char *buf)
1938 int res;
1940 if (mutex_lock_killable(&hotkey_mutex))
1941 return -ERESTARTSYS;
1942 res = hotkey_mask_get();
1943 mutex_unlock(&hotkey_mutex);
1945 return (res)?
1946 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1949 static ssize_t hotkey_mask_store(struct device *dev,
1950 struct device_attribute *attr,
1951 const char *buf, size_t count)
1953 unsigned long t;
1954 int res;
1956 if (parse_strtoul(buf, 0xffffffffUL, &t))
1957 return -EINVAL;
1959 if (mutex_lock_killable(&hotkey_mutex))
1960 return -ERESTARTSYS;
1962 res = hotkey_mask_set(t);
1964 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1965 hotkey_poll_setup(1);
1966 #endif
1968 mutex_unlock(&hotkey_mutex);
1970 return (res) ? res : count;
1973 static struct device_attribute dev_attr_hotkey_mask =
1974 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1975 hotkey_mask_show, hotkey_mask_store);
1977 /* sysfs hotkey bios_enabled ------------------------------------------- */
1978 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1979 struct device_attribute *attr,
1980 char *buf)
1982 return sprintf(buf, "0\n");
1985 static struct device_attribute dev_attr_hotkey_bios_enabled =
1986 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1988 /* sysfs hotkey bios_mask ---------------------------------------------- */
1989 static ssize_t hotkey_bios_mask_show(struct device *dev,
1990 struct device_attribute *attr,
1991 char *buf)
1993 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1996 static struct device_attribute dev_attr_hotkey_bios_mask =
1997 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1999 /* sysfs hotkey all_mask ----------------------------------------------- */
2000 static ssize_t hotkey_all_mask_show(struct device *dev,
2001 struct device_attribute *attr,
2002 char *buf)
2004 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2005 hotkey_all_mask | hotkey_source_mask);
2008 static struct device_attribute dev_attr_hotkey_all_mask =
2009 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2011 /* sysfs hotkey recommended_mask --------------------------------------- */
2012 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2013 struct device_attribute *attr,
2014 char *buf)
2016 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2017 (hotkey_all_mask | hotkey_source_mask)
2018 & ~hotkey_reserved_mask);
2021 static struct device_attribute dev_attr_hotkey_recommended_mask =
2022 __ATTR(hotkey_recommended_mask, S_IRUGO,
2023 hotkey_recommended_mask_show, NULL);
2025 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2027 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2028 static ssize_t hotkey_source_mask_show(struct device *dev,
2029 struct device_attribute *attr,
2030 char *buf)
2032 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2035 static ssize_t hotkey_source_mask_store(struct device *dev,
2036 struct device_attribute *attr,
2037 const char *buf, size_t count)
2039 unsigned long t;
2041 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2042 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2043 return -EINVAL;
2045 if (mutex_lock_killable(&hotkey_mutex))
2046 return -ERESTARTSYS;
2048 HOTKEY_CONFIG_CRITICAL_START
2049 hotkey_source_mask = t;
2050 HOTKEY_CONFIG_CRITICAL_END
2052 hotkey_poll_setup(1);
2054 mutex_unlock(&hotkey_mutex);
2056 return count;
2059 static struct device_attribute dev_attr_hotkey_source_mask =
2060 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2061 hotkey_source_mask_show, hotkey_source_mask_store);
2063 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2064 static ssize_t hotkey_poll_freq_show(struct device *dev,
2065 struct device_attribute *attr,
2066 char *buf)
2068 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2071 static ssize_t hotkey_poll_freq_store(struct device *dev,
2072 struct device_attribute *attr,
2073 const char *buf, size_t count)
2075 unsigned long t;
2077 if (parse_strtoul(buf, 25, &t))
2078 return -EINVAL;
2080 if (mutex_lock_killable(&hotkey_mutex))
2081 return -ERESTARTSYS;
2083 hotkey_poll_freq = t;
2085 hotkey_poll_setup(1);
2086 mutex_unlock(&hotkey_mutex);
2088 return count;
2091 static struct device_attribute dev_attr_hotkey_poll_freq =
2092 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2093 hotkey_poll_freq_show, hotkey_poll_freq_store);
2095 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2097 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2098 static ssize_t hotkey_radio_sw_show(struct device *dev,
2099 struct device_attribute *attr,
2100 char *buf)
2102 int res, s;
2103 res = hotkey_get_wlsw(&s);
2104 if (res < 0)
2105 return res;
2107 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2110 static struct device_attribute dev_attr_hotkey_radio_sw =
2111 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2113 static void hotkey_radio_sw_notify_change(void)
2115 if (tp_features.hotkey_wlsw)
2116 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2117 "hotkey_radio_sw");
2120 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2121 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2122 struct device_attribute *attr,
2123 char *buf)
2125 int res, s;
2126 res = hotkey_get_tablet_mode(&s);
2127 if (res < 0)
2128 return res;
2130 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2133 static struct device_attribute dev_attr_hotkey_tablet_mode =
2134 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2136 static void hotkey_tablet_mode_notify_change(void)
2138 if (tp_features.hotkey_tablet)
2139 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2140 "hotkey_tablet_mode");
2143 /* sysfs hotkey report_mode -------------------------------------------- */
2144 static ssize_t hotkey_report_mode_show(struct device *dev,
2145 struct device_attribute *attr,
2146 char *buf)
2148 return snprintf(buf, PAGE_SIZE, "%d\n",
2149 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2152 static struct device_attribute dev_attr_hotkey_report_mode =
2153 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2155 /* sysfs wakeup reason (pollable) -------------------------------------- */
2156 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2157 struct device_attribute *attr,
2158 char *buf)
2160 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2163 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2164 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2166 static void hotkey_wakeup_reason_notify_change(void)
2168 if (tp_features.hotkey_mask)
2169 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2170 "wakeup_reason");
2173 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2174 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2175 struct device_attribute *attr,
2176 char *buf)
2178 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2181 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2182 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2183 hotkey_wakeup_hotunplug_complete_show, NULL);
2185 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2187 if (tp_features.hotkey_mask)
2188 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2189 "wakeup_hotunplug_complete");
2192 /* --------------------------------------------------------------------- */
2194 static struct attribute *hotkey_attributes[] __initdata = {
2195 &dev_attr_hotkey_enable.attr,
2196 &dev_attr_hotkey_bios_enabled.attr,
2197 &dev_attr_hotkey_report_mode.attr,
2198 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2199 &dev_attr_hotkey_mask.attr,
2200 &dev_attr_hotkey_all_mask.attr,
2201 &dev_attr_hotkey_recommended_mask.attr,
2202 &dev_attr_hotkey_source_mask.attr,
2203 &dev_attr_hotkey_poll_freq.attr,
2204 #endif
2207 static struct attribute *hotkey_mask_attributes[] __initdata = {
2208 &dev_attr_hotkey_bios_mask.attr,
2209 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2210 &dev_attr_hotkey_mask.attr,
2211 &dev_attr_hotkey_all_mask.attr,
2212 &dev_attr_hotkey_recommended_mask.attr,
2213 #endif
2214 &dev_attr_hotkey_wakeup_reason.attr,
2215 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2218 static void bluetooth_update_rfk(void);
2219 static void wan_update_rfk(void);
2220 static void uwb_update_rfk(void);
2221 static void tpacpi_send_radiosw_update(void)
2223 int wlsw;
2225 /* Sync these BEFORE sending any rfkill events */
2226 if (tp_features.bluetooth)
2227 bluetooth_update_rfk();
2228 if (tp_features.wan)
2229 wan_update_rfk();
2230 if (tp_features.uwb)
2231 uwb_update_rfk();
2233 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2234 mutex_lock(&tpacpi_inputdev_send_mutex);
2236 input_report_switch(tpacpi_inputdev,
2237 SW_RFKILL_ALL, !!wlsw);
2238 input_sync(tpacpi_inputdev);
2240 mutex_unlock(&tpacpi_inputdev_send_mutex);
2242 hotkey_radio_sw_notify_change();
2245 static void hotkey_exit(void)
2247 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2248 hotkey_poll_stop_sync();
2249 #endif
2251 if (hotkey_dev_attributes)
2252 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2254 kfree(hotkey_keycode_map);
2256 if (tp_features.hotkey) {
2257 dbg_printk(TPACPI_DBG_EXIT,
2258 "restoring original hot key mask\n");
2259 /* no short-circuit boolean operator below! */
2260 if ((hotkey_mask_set(hotkey_orig_mask) |
2261 hotkey_status_set(false)) != 0)
2262 printk(TPACPI_ERR
2263 "failed to restore hot key mask "
2264 "to BIOS defaults\n");
2268 static int __init hotkey_init(struct ibm_init_struct *iibm)
2270 /* Requirements for changing the default keymaps:
2272 * 1. Many of the keys are mapped to KEY_RESERVED for very
2273 * good reasons. Do not change them unless you have deep
2274 * knowledge on the IBM and Lenovo ThinkPad firmware for
2275 * the various ThinkPad models. The driver behaves
2276 * differently for KEY_RESERVED: such keys have their
2277 * hot key mask *unset* in mask_recommended, and also
2278 * in the initial hot key mask programmed into the
2279 * firmware at driver load time, which means the firm-
2280 * ware may react very differently if you change them to
2281 * something else;
2283 * 2. You must be subscribed to the linux-thinkpad and
2284 * ibm-acpi-devel mailing lists, and you should read the
2285 * list archives since 2007 if you want to change the
2286 * keymaps. This requirement exists so that you will
2287 * know the past history of problems with the thinkpad-
2288 * acpi driver keymaps, and also that you will be
2289 * listening to any bug reports;
2291 * 3. Do not send thinkpad-acpi specific patches directly to
2292 * for merging, *ever*. Send them to the linux-acpi
2293 * mailinglist for comments. Merging is to be done only
2294 * through acpi-test and the ACPI maintainer.
2296 * If the above is too much to ask, don't change the keymap.
2297 * Ask the thinkpad-acpi maintainer to do it, instead.
2299 static u16 ibm_keycode_map[] __initdata = {
2300 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2301 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2302 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2303 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2305 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2306 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2307 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2308 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2310 /* brightness: firmware always reacts to them, unless
2311 * X.org did some tricks in the radeon BIOS scratch
2312 * registers of *some* models */
2313 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2314 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2316 /* Thinklight: firmware always react to it */
2317 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2319 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2320 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2322 /* Volume: firmware always react to it and reprograms
2323 * the built-in *extra* mixer. Never map it to control
2324 * another mixer by default. */
2325 KEY_RESERVED, /* 0x14: VOLUME UP */
2326 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2327 KEY_RESERVED, /* 0x16: MUTE */
2329 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2331 /* (assignments unknown, please report if found) */
2332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2333 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2335 static u16 lenovo_keycode_map[] __initdata = {
2336 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2337 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2338 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2339 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2341 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2342 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2343 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2344 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2346 /* These either have to go through ACPI video, or
2347 * act like in the IBM ThinkPads, so don't ever
2348 * enable them by default */
2349 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2350 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2352 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2354 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2355 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2357 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2358 * react to it and reprograms the built-in *extra* mixer.
2359 * Never map it to control another mixer by default.
2361 * T60?, T61, R60?, R61: firmware and EC tries to send
2362 * these over the regular keyboard, so these are no-ops,
2363 * but there are still weird bugs re. MUTE, so do not
2364 * change unless you get test reports from all Lenovo
2365 * models. May cause the BIOS to interfere with the
2366 * HDA mixer.
2368 KEY_RESERVED, /* 0x14: VOLUME UP */
2369 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2370 KEY_RESERVED, /* 0x16: MUTE */
2372 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2374 /* (assignments unknown, please report if found) */
2375 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2376 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2379 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2380 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2381 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2383 int res, i;
2384 int status;
2385 int hkeyv;
2387 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2389 BUG_ON(!tpacpi_inputdev);
2390 BUG_ON(tpacpi_inputdev->open != NULL ||
2391 tpacpi_inputdev->close != NULL);
2393 TPACPI_ACPIHANDLE_INIT(hkey);
2394 mutex_init(&hotkey_mutex);
2396 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2397 mutex_init(&hotkey_thread_mutex);
2398 mutex_init(&hotkey_thread_data_mutex);
2399 #endif
2401 /* hotkey not supported on 570 */
2402 tp_features.hotkey = hkey_handle != NULL;
2404 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2405 str_supported(tp_features.hotkey));
2407 if (!tp_features.hotkey)
2408 return 1;
2410 tpacpi_disable_brightness_delay();
2412 hotkey_dev_attributes = create_attr_set(13, NULL);
2413 if (!hotkey_dev_attributes)
2414 return -ENOMEM;
2415 res = add_many_to_attr_set(hotkey_dev_attributes,
2416 hotkey_attributes,
2417 ARRAY_SIZE(hotkey_attributes));
2418 if (res)
2419 goto err_exit;
2421 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2422 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2423 for HKEY interface version 0x100 */
2424 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2425 if ((hkeyv >> 8) != 1) {
2426 printk(TPACPI_ERR "unknown version of the "
2427 "HKEY interface: 0x%x\n", hkeyv);
2428 printk(TPACPI_ERR "please report this to %s\n",
2429 TPACPI_MAIL);
2430 } else {
2432 * MHKV 0x100 in A31, R40, R40e,
2433 * T4x, X31, and later
2435 tp_features.hotkey_mask = 1;
2439 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2440 str_supported(tp_features.hotkey_mask));
2442 if (tp_features.hotkey_mask) {
2443 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2444 "MHKA", "qd")) {
2445 printk(TPACPI_ERR
2446 "missing MHKA handler, "
2447 "please report this to %s\n",
2448 TPACPI_MAIL);
2449 /* FN+F12, FN+F4, FN+F3 */
2450 hotkey_all_mask = 0x080cU;
2454 /* hotkey_source_mask *must* be zero for
2455 * the first hotkey_mask_get */
2456 if (tp_features.hotkey_mask) {
2457 res = hotkey_mask_get();
2458 if (res)
2459 goto err_exit;
2461 hotkey_orig_mask = hotkey_mask;
2462 res = add_many_to_attr_set(
2463 hotkey_dev_attributes,
2464 hotkey_mask_attributes,
2465 ARRAY_SIZE(hotkey_mask_attributes));
2466 if (res)
2467 goto err_exit;
2470 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2471 if (tp_features.hotkey_mask) {
2472 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2473 & ~hotkey_all_mask;
2474 } else {
2475 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2478 vdbg_printk(TPACPI_DBG_INIT,
2479 "hotkey source mask 0x%08x, polling freq %d\n",
2480 hotkey_source_mask, hotkey_poll_freq);
2481 #endif
2483 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2484 if (dbg_wlswemul) {
2485 tp_features.hotkey_wlsw = 1;
2486 printk(TPACPI_INFO
2487 "radio switch emulation enabled\n");
2488 } else
2489 #endif
2490 /* Not all thinkpads have a hardware radio switch */
2491 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2492 tp_features.hotkey_wlsw = 1;
2493 printk(TPACPI_INFO
2494 "radio switch found; radios are %s\n",
2495 enabled(status, 0));
2497 if (tp_features.hotkey_wlsw)
2498 res = add_to_attr_set(hotkey_dev_attributes,
2499 &dev_attr_hotkey_radio_sw.attr);
2501 /* For X41t, X60t, X61t Tablets... */
2502 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2503 tp_features.hotkey_tablet = 1;
2504 printk(TPACPI_INFO
2505 "possible tablet mode switch found; "
2506 "ThinkPad in %s mode\n",
2507 (status & TP_HOTKEY_TABLET_MASK)?
2508 "tablet" : "laptop");
2509 res = add_to_attr_set(hotkey_dev_attributes,
2510 &dev_attr_hotkey_tablet_mode.attr);
2513 if (!res)
2514 res = register_attr_set_with_sysfs(
2515 hotkey_dev_attributes,
2516 &tpacpi_pdev->dev.kobj);
2517 if (res)
2518 goto err_exit;
2520 /* Set up key map */
2522 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2523 GFP_KERNEL);
2524 if (!hotkey_keycode_map) {
2525 printk(TPACPI_ERR
2526 "failed to allocate memory for key map\n");
2527 res = -ENOMEM;
2528 goto err_exit;
2531 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2532 dbg_printk(TPACPI_DBG_INIT,
2533 "using Lenovo default hot key map\n");
2534 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2535 TPACPI_HOTKEY_MAP_SIZE);
2536 } else {
2537 dbg_printk(TPACPI_DBG_INIT,
2538 "using IBM default hot key map\n");
2539 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2540 TPACPI_HOTKEY_MAP_SIZE);
2543 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2544 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2545 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2546 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2547 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2548 tpacpi_inputdev->keycode = hotkey_keycode_map;
2549 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2550 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2551 set_bit(hotkey_keycode_map[i],
2552 tpacpi_inputdev->keybit);
2553 } else {
2554 if (i < sizeof(hotkey_reserved_mask)*8)
2555 hotkey_reserved_mask |= 1 << i;
2559 if (tp_features.hotkey_wlsw) {
2560 set_bit(EV_SW, tpacpi_inputdev->evbit);
2561 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2563 if (tp_features.hotkey_tablet) {
2564 set_bit(EV_SW, tpacpi_inputdev->evbit);
2565 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2568 /* Do not issue duplicate brightness change events to
2569 * userspace */
2570 if (!tp_features.bright_acpimode)
2571 /* update bright_acpimode... */
2572 tpacpi_check_std_acpi_brightness_support();
2574 if (tp_features.bright_acpimode) {
2575 printk(TPACPI_INFO
2576 "This ThinkPad has standard ACPI backlight "
2577 "brightness control, supported by the ACPI "
2578 "video driver\n");
2579 printk(TPACPI_NOTICE
2580 "Disabling thinkpad-acpi brightness events "
2581 "by default...\n");
2583 /* The hotkey_reserved_mask change below is not
2584 * necessary while the keys are at KEY_RESERVED in the
2585 * default map, but better safe than sorry, leave it
2586 * here as a marker of what we have to do, especially
2587 * when we finally become able to set this at runtime
2588 * on response to X.org requests */
2589 hotkey_reserved_mask |=
2590 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2591 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2594 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2595 res = hotkey_status_set(true);
2596 if (res) {
2597 hotkey_exit();
2598 return res;
2600 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2601 & ~hotkey_reserved_mask)
2602 | hotkey_orig_mask);
2603 if (res < 0 && res != -ENXIO) {
2604 hotkey_exit();
2605 return res;
2608 dbg_printk(TPACPI_DBG_INIT,
2609 "legacy hot key reporting over procfs %s\n",
2610 (hotkey_report_mode < 2) ?
2611 "enabled" : "disabled");
2613 tpacpi_inputdev->open = &hotkey_inputdev_open;
2614 tpacpi_inputdev->close = &hotkey_inputdev_close;
2616 hotkey_poll_setup_safe(1);
2617 tpacpi_send_radiosw_update();
2618 tpacpi_input_send_tabletsw();
2620 return 0;
2622 err_exit:
2623 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2624 hotkey_dev_attributes = NULL;
2626 return (res < 0)? res : 1;
2629 static bool hotkey_notify_hotkey(const u32 hkey,
2630 bool *send_acpi_ev,
2631 bool *ignore_acpi_ev)
2633 /* 0x1000-0x1FFF: key presses */
2634 unsigned int scancode = hkey & 0xfff;
2635 *send_acpi_ev = true;
2636 *ignore_acpi_ev = false;
2638 if (scancode > 0 && scancode < 0x21) {
2639 scancode--;
2640 if (!(hotkey_source_mask & (1 << scancode))) {
2641 tpacpi_input_send_key(scancode);
2642 *send_acpi_ev = false;
2643 } else {
2644 *ignore_acpi_ev = true;
2646 return true;
2648 return false;
2651 static bool hotkey_notify_wakeup(const u32 hkey,
2652 bool *send_acpi_ev,
2653 bool *ignore_acpi_ev)
2655 /* 0x2000-0x2FFF: Wakeup reason */
2656 *send_acpi_ev = true;
2657 *ignore_acpi_ev = false;
2659 switch (hkey) {
2660 case 0x2304: /* suspend, undock */
2661 case 0x2404: /* hibernation, undock */
2662 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2663 *ignore_acpi_ev = true;
2664 break;
2666 case 0x2305: /* suspend, bay eject */
2667 case 0x2405: /* hibernation, bay eject */
2668 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2669 *ignore_acpi_ev = true;
2670 break;
2672 case 0x2313: /* Battery on critical low level (S3) */
2673 case 0x2413: /* Battery on critical low level (S4) */
2674 printk(TPACPI_ALERT
2675 "EMERGENCY WAKEUP: battery almost empty\n");
2676 /* how to auto-heal: */
2677 /* 2313: woke up from S3, go to S4/S5 */
2678 /* 2413: woke up from S4, go to S5 */
2679 break;
2681 default:
2682 return false;
2685 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2686 printk(TPACPI_INFO
2687 "woke up due to a hot-unplug "
2688 "request...\n");
2689 hotkey_wakeup_reason_notify_change();
2691 return true;
2694 static bool hotkey_notify_usrevent(const u32 hkey,
2695 bool *send_acpi_ev,
2696 bool *ignore_acpi_ev)
2698 /* 0x5000-0x5FFF: human interface helpers */
2699 *send_acpi_ev = true;
2700 *ignore_acpi_ev = false;
2702 switch (hkey) {
2703 case 0x5010: /* Lenovo new BIOS: brightness changed */
2704 case 0x500b: /* X61t: tablet pen inserted into bay */
2705 case 0x500c: /* X61t: tablet pen removed from bay */
2706 return true;
2708 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2709 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2710 tpacpi_input_send_tabletsw();
2711 hotkey_tablet_mode_notify_change();
2712 *send_acpi_ev = false;
2713 return true;
2715 case 0x5001:
2716 case 0x5002:
2717 /* LID switch events. Do not propagate */
2718 *ignore_acpi_ev = true;
2719 return true;
2721 default:
2722 return false;
2726 static bool hotkey_notify_thermal(const u32 hkey,
2727 bool *send_acpi_ev,
2728 bool *ignore_acpi_ev)
2730 /* 0x6000-0x6FFF: thermal alarms */
2731 *send_acpi_ev = true;
2732 *ignore_acpi_ev = false;
2734 switch (hkey) {
2735 case 0x6011:
2736 printk(TPACPI_CRIT
2737 "THERMAL ALARM: battery is too hot!\n");
2738 /* recommended action: warn user through gui */
2739 return true;
2740 case 0x6012:
2741 printk(TPACPI_ALERT
2742 "THERMAL EMERGENCY: battery is extremely hot!\n");
2743 /* recommended action: immediate sleep/hibernate */
2744 return true;
2745 case 0x6021:
2746 printk(TPACPI_CRIT
2747 "THERMAL ALARM: "
2748 "a sensor reports something is too hot!\n");
2749 /* recommended action: warn user through gui, that */
2750 /* some internal component is too hot */
2751 return true;
2752 case 0x6022:
2753 printk(TPACPI_ALERT
2754 "THERMAL EMERGENCY: "
2755 "a sensor reports something is extremely hot!\n");
2756 /* recommended action: immediate sleep/hibernate */
2757 return true;
2758 case 0x6030:
2759 printk(TPACPI_INFO
2760 "EC reports that Thermal Table has changed\n");
2761 /* recommended action: do nothing, we don't have
2762 * Lenovo ATM information */
2763 return true;
2764 default:
2765 printk(TPACPI_ALERT
2766 "THERMAL ALERT: unknown thermal alarm received\n");
2767 return false;
2771 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2773 u32 hkey;
2774 bool send_acpi_ev;
2775 bool ignore_acpi_ev;
2776 bool known_ev;
2778 if (event != 0x80) {
2779 printk(TPACPI_ERR
2780 "unknown HKEY notification event %d\n", event);
2781 /* forward it to userspace, maybe it knows how to handle it */
2782 acpi_bus_generate_netlink_event(
2783 ibm->acpi->device->pnp.device_class,
2784 dev_name(&ibm->acpi->device->dev),
2785 event, 0);
2786 return;
2789 while (1) {
2790 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2791 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2792 return;
2795 if (hkey == 0) {
2796 /* queue empty */
2797 return;
2800 send_acpi_ev = true;
2801 ignore_acpi_ev = false;
2803 switch (hkey >> 12) {
2804 case 1:
2805 /* 0x1000-0x1FFF: key presses */
2806 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2807 &ignore_acpi_ev);
2808 break;
2809 case 2:
2810 /* 0x2000-0x2FFF: Wakeup reason */
2811 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2812 &ignore_acpi_ev);
2813 break;
2814 case 3:
2815 /* 0x3000-0x3FFF: bay-related wakeups */
2816 if (hkey == 0x3003) {
2817 hotkey_autosleep_ack = 1;
2818 printk(TPACPI_INFO
2819 "bay ejected\n");
2820 hotkey_wakeup_hotunplug_complete_notify_change();
2821 known_ev = true;
2822 } else {
2823 known_ev = false;
2825 break;
2826 case 4:
2827 /* 0x4000-0x4FFF: dock-related wakeups */
2828 if (hkey == 0x4003) {
2829 hotkey_autosleep_ack = 1;
2830 printk(TPACPI_INFO
2831 "undocked\n");
2832 hotkey_wakeup_hotunplug_complete_notify_change();
2833 known_ev = true;
2834 } else {
2835 known_ev = false;
2837 break;
2838 case 5:
2839 /* 0x5000-0x5FFF: human interface helpers */
2840 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2841 &ignore_acpi_ev);
2842 break;
2843 case 6:
2844 /* 0x6000-0x6FFF: thermal alarms */
2845 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2846 &ignore_acpi_ev);
2847 break;
2848 case 7:
2849 /* 0x7000-0x7FFF: misc */
2850 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2851 tpacpi_send_radiosw_update();
2852 send_acpi_ev = 0;
2853 known_ev = true;
2854 break;
2856 /* fallthrough to default */
2857 default:
2858 known_ev = false;
2860 if (!known_ev) {
2861 printk(TPACPI_NOTICE
2862 "unhandled HKEY event 0x%04x\n", hkey);
2863 printk(TPACPI_NOTICE
2864 "please report the conditions when this "
2865 "event happened to %s\n", TPACPI_MAIL);
2868 /* Legacy events */
2869 if (!ignore_acpi_ev &&
2870 (send_acpi_ev || hotkey_report_mode < 2)) {
2871 acpi_bus_generate_proc_event(ibm->acpi->device,
2872 event, hkey);
2875 /* netlink events */
2876 if (!ignore_acpi_ev && send_acpi_ev) {
2877 acpi_bus_generate_netlink_event(
2878 ibm->acpi->device->pnp.device_class,
2879 dev_name(&ibm->acpi->device->dev),
2880 event, hkey);
2885 static void hotkey_suspend(pm_message_t state)
2887 /* Do these on suspend, we get the events on early resume! */
2888 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2889 hotkey_autosleep_ack = 0;
2892 static void hotkey_resume(void)
2894 tpacpi_disable_brightness_delay();
2896 if (hotkey_mask_get())
2897 printk(TPACPI_ERR
2898 "error while trying to read hot key mask "
2899 "from firmware\n");
2900 tpacpi_send_radiosw_update();
2901 hotkey_tablet_mode_notify_change();
2902 hotkey_wakeup_reason_notify_change();
2903 hotkey_wakeup_hotunplug_complete_notify_change();
2904 hotkey_poll_setup_safe(0);
2907 /* procfs -------------------------------------------------------------- */
2908 static int hotkey_read(char *p)
2910 int res, status;
2911 int len = 0;
2913 if (!tp_features.hotkey) {
2914 len += sprintf(p + len, "status:\t\tnot supported\n");
2915 return len;
2918 if (mutex_lock_killable(&hotkey_mutex))
2919 return -ERESTARTSYS;
2920 res = hotkey_status_get(&status);
2921 if (!res)
2922 res = hotkey_mask_get();
2923 mutex_unlock(&hotkey_mutex);
2924 if (res)
2925 return res;
2927 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2928 if (tp_features.hotkey_mask) {
2929 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2930 len += sprintf(p + len,
2931 "commands:\tenable, disable, reset, <mask>\n");
2932 } else {
2933 len += sprintf(p + len, "mask:\t\tnot supported\n");
2934 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2937 return len;
2940 static void hotkey_enabledisable_warn(void)
2942 tpacpi_log_usertask("procfs hotkey enable/disable");
2943 WARN(1, TPACPI_WARN
2944 "hotkey enable/disable functionality has been "
2945 "removed from the driver. Hotkeys are always enabled.\n");
2948 static int hotkey_write(char *buf)
2950 int res;
2951 u32 mask;
2952 char *cmd;
2954 if (!tp_features.hotkey)
2955 return -ENODEV;
2957 if (mutex_lock_killable(&hotkey_mutex))
2958 return -ERESTARTSYS;
2960 mask = hotkey_mask;
2962 res = 0;
2963 while ((cmd = next_cmd(&buf))) {
2964 if (strlencmp(cmd, "enable") == 0) {
2965 hotkey_enabledisable_warn();
2966 } else if (strlencmp(cmd, "disable") == 0) {
2967 hotkey_enabledisable_warn();
2968 res = -EPERM;
2969 } else if (strlencmp(cmd, "reset") == 0) {
2970 mask = hotkey_orig_mask;
2971 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2972 /* mask set */
2973 } else if (sscanf(cmd, "%x", &mask) == 1) {
2974 /* mask set */
2975 } else {
2976 res = -EINVAL;
2977 goto errexit;
2980 if (!res && mask != hotkey_mask)
2981 res = hotkey_mask_set(mask);
2983 errexit:
2984 mutex_unlock(&hotkey_mutex);
2985 return res;
2988 static const struct acpi_device_id ibm_htk_device_ids[] = {
2989 {TPACPI_ACPI_HKEY_HID, 0},
2990 {"", 0},
2993 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2994 .hid = ibm_htk_device_ids,
2995 .notify = hotkey_notify,
2996 .handle = &hkey_handle,
2997 .type = ACPI_DEVICE_NOTIFY,
3000 static struct ibm_struct hotkey_driver_data = {
3001 .name = "hotkey",
3002 .read = hotkey_read,
3003 .write = hotkey_write,
3004 .exit = hotkey_exit,
3005 .resume = hotkey_resume,
3006 .suspend = hotkey_suspend,
3007 .acpi = &ibm_hotkey_acpidriver,
3010 /*************************************************************************
3011 * Bluetooth subdriver
3014 enum {
3015 /* ACPI GBDC/SBDC bits */
3016 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3017 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3018 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3019 off / last state */
3022 enum {
3023 /* ACPI \BLTH commands */
3024 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3025 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3026 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3027 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3028 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3031 #define TPACPI_RFK_BLUETOOTH_SW_NAME "tpacpi_bluetooth_sw"
3033 static struct rfkill *tpacpi_bluetooth_rfkill;
3035 static void bluetooth_suspend(pm_message_t state)
3037 /* Try to make sure radio will resume powered off */
3038 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3039 TP_ACPI_BLTH_PWR_OFF_ON_RESUME))
3040 vdbg_printk(TPACPI_DBG_RFKILL,
3041 "bluetooth power down on resume request failed\n");
3044 static int bluetooth_get_radiosw(void)
3046 int status;
3048 if (!tp_features.bluetooth)
3049 return -ENODEV;
3051 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3052 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3053 return RFKILL_STATE_HARD_BLOCKED;
3055 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3056 if (dbg_bluetoothemul)
3057 return (tpacpi_bluetooth_emulstate) ?
3058 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3059 #endif
3061 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3062 return -EIO;
3064 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3065 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3068 static void bluetooth_update_rfk(void)
3070 int status;
3072 if (!tpacpi_bluetooth_rfkill)
3073 return;
3075 status = bluetooth_get_radiosw();
3076 if (status < 0)
3077 return;
3078 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3080 vdbg_printk(TPACPI_DBG_RFKILL,
3081 "forced rfkill state to %d\n",
3082 status);
3085 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3087 int status;
3089 if (!tp_features.bluetooth)
3090 return -ENODEV;
3092 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3093 * reason to risk weird behaviour. */
3094 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3095 && radio_on)
3096 return -EPERM;
3098 vdbg_printk(TPACPI_DBG_RFKILL,
3099 "will %s bluetooth\n", radio_on ? "enable" : "disable");
3101 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3102 if (dbg_bluetoothemul) {
3103 tpacpi_bluetooth_emulstate = !!radio_on;
3104 if (update_rfk)
3105 bluetooth_update_rfk();
3106 return 0;
3108 #endif
3110 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3111 if (radio_on)
3112 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3113 else
3114 status = 0;
3115 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3116 return -EIO;
3118 if (update_rfk)
3119 bluetooth_update_rfk();
3121 return 0;
3124 /* sysfs bluetooth enable ---------------------------------------------- */
3125 static ssize_t bluetooth_enable_show(struct device *dev,
3126 struct device_attribute *attr,
3127 char *buf)
3129 int status;
3131 printk_deprecated_rfkill_attribute("bluetooth_enable");
3133 status = bluetooth_get_radiosw();
3134 if (status < 0)
3135 return status;
3137 return snprintf(buf, PAGE_SIZE, "%d\n",
3138 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3141 static ssize_t bluetooth_enable_store(struct device *dev,
3142 struct device_attribute *attr,
3143 const char *buf, size_t count)
3145 unsigned long t;
3146 int res;
3148 printk_deprecated_rfkill_attribute("bluetooth_enable");
3150 if (parse_strtoul(buf, 1, &t))
3151 return -EINVAL;
3153 tpacpi_disclose_usertask("bluetooth_enable", "set to %ld\n", t);
3155 res = bluetooth_set_radiosw(t, 1);
3157 return (res) ? res : count;
3160 static struct device_attribute dev_attr_bluetooth_enable =
3161 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3162 bluetooth_enable_show, bluetooth_enable_store);
3164 /* --------------------------------------------------------------------- */
3166 static struct attribute *bluetooth_attributes[] = {
3167 &dev_attr_bluetooth_enable.attr,
3168 NULL
3171 static const struct attribute_group bluetooth_attr_group = {
3172 .attrs = bluetooth_attributes,
3175 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3177 int bts = bluetooth_get_radiosw();
3179 if (bts < 0)
3180 return bts;
3182 *state = bts;
3183 return 0;
3186 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3188 dbg_printk(TPACPI_DBG_RFKILL,
3189 "request to change radio state to %d\n", state);
3190 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3193 static void bluetooth_shutdown(void)
3195 /* Order firmware to save current state to NVRAM */
3196 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3197 TP_ACPI_BLTH_SAVE_STATE))
3198 printk(TPACPI_NOTICE
3199 "failed to save bluetooth state to NVRAM\n");
3200 else
3201 vdbg_printk(TPACPI_DBG_RFKILL,
3202 "bluestooth state saved to NVRAM\n");
3205 static void bluetooth_exit(void)
3207 bluetooth_shutdown();
3209 if (tpacpi_bluetooth_rfkill)
3210 rfkill_unregister(tpacpi_bluetooth_rfkill);
3212 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3213 &bluetooth_attr_group);
3216 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3218 int res;
3219 int status = 0;
3221 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3222 "initializing bluetooth subdriver\n");
3224 TPACPI_ACPIHANDLE_INIT(hkey);
3226 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3227 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3228 tp_features.bluetooth = hkey_handle &&
3229 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3231 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3232 "bluetooth is %s, status 0x%02x\n",
3233 str_supported(tp_features.bluetooth),
3234 status);
3236 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3237 if (dbg_bluetoothemul) {
3238 tp_features.bluetooth = 1;
3239 printk(TPACPI_INFO
3240 "bluetooth switch emulation enabled\n");
3241 } else
3242 #endif
3243 if (tp_features.bluetooth &&
3244 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3245 /* no bluetooth hardware present in system */
3246 tp_features.bluetooth = 0;
3247 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3248 "bluetooth hardware not installed\n");
3251 if (!tp_features.bluetooth)
3252 return 1;
3254 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3255 &bluetooth_attr_group);
3256 if (res)
3257 return res;
3259 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3260 &tpacpi_bluetooth_rfkill,
3261 RFKILL_TYPE_BLUETOOTH,
3262 TPACPI_RFK_BLUETOOTH_SW_NAME,
3263 true,
3264 tpacpi_bluetooth_rfk_set,
3265 tpacpi_bluetooth_rfk_get);
3266 if (res) {
3267 bluetooth_exit();
3268 return res;
3271 return 0;
3274 /* procfs -------------------------------------------------------------- */
3275 static int bluetooth_read(char *p)
3277 int len = 0;
3278 int status = bluetooth_get_radiosw();
3280 if (!tp_features.bluetooth)
3281 len += sprintf(p + len, "status:\t\tnot supported\n");
3282 else {
3283 len += sprintf(p + len, "status:\t\t%s\n",
3284 (status == RFKILL_STATE_UNBLOCKED) ?
3285 "enabled" : "disabled");
3286 len += sprintf(p + len, "commands:\tenable, disable\n");
3289 return len;
3292 static int bluetooth_write(char *buf)
3294 char *cmd;
3295 int state = -1;
3297 if (!tp_features.bluetooth)
3298 return -ENODEV;
3300 while ((cmd = next_cmd(&buf))) {
3301 if (strlencmp(cmd, "enable") == 0) {
3302 state = 1;
3303 } else if (strlencmp(cmd, "disable") == 0) {
3304 state = 0;
3305 } else
3306 return -EINVAL;
3309 if (state != -1) {
3310 tpacpi_disclose_usertask("procfs bluetooth",
3311 "attempt to %s\n",
3312 state ? "enable" : "disable");
3313 bluetooth_set_radiosw(state, 1);
3316 return 0;
3319 static struct ibm_struct bluetooth_driver_data = {
3320 .name = "bluetooth",
3321 .read = bluetooth_read,
3322 .write = bluetooth_write,
3323 .exit = bluetooth_exit,
3324 .suspend = bluetooth_suspend,
3325 .shutdown = bluetooth_shutdown,
3328 /*************************************************************************
3329 * Wan subdriver
3332 enum {
3333 /* ACPI GWAN/SWAN bits */
3334 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3335 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3336 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3337 off / last state */
3340 #define TPACPI_RFK_WWAN_SW_NAME "tpacpi_wwan_sw"
3342 static struct rfkill *tpacpi_wan_rfkill;
3344 static void wan_suspend(pm_message_t state)
3346 /* Try to make sure radio will resume powered off */
3347 if (!acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3348 TP_ACPI_WGSV_PWR_OFF_ON_RESUME))
3349 vdbg_printk(TPACPI_DBG_RFKILL,
3350 "WWAN power down on resume request failed\n");
3353 static int wan_get_radiosw(void)
3355 int status;
3357 if (!tp_features.wan)
3358 return -ENODEV;
3360 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3361 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3362 return RFKILL_STATE_HARD_BLOCKED;
3364 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3365 if (dbg_wwanemul)
3366 return (tpacpi_wwan_emulstate) ?
3367 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3368 #endif
3370 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3371 return -EIO;
3373 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3374 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3377 static void wan_update_rfk(void)
3379 int status;
3381 if (!tpacpi_wan_rfkill)
3382 return;
3384 status = wan_get_radiosw();
3385 if (status < 0)
3386 return;
3387 rfkill_force_state(tpacpi_wan_rfkill, status);
3389 vdbg_printk(TPACPI_DBG_RFKILL,
3390 "forced rfkill state to %d\n",
3391 status);
3394 static int wan_set_radiosw(int radio_on, int update_rfk)
3396 int status;
3398 if (!tp_features.wan)
3399 return -ENODEV;
3401 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3402 * reason to risk weird behaviour. */
3403 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3404 && radio_on)
3405 return -EPERM;
3407 vdbg_printk(TPACPI_DBG_RFKILL,
3408 "will %s WWAN\n", radio_on ? "enable" : "disable");
3410 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3411 if (dbg_wwanemul) {
3412 tpacpi_wwan_emulstate = !!radio_on;
3413 if (update_rfk)
3414 wan_update_rfk();
3415 return 0;
3417 #endif
3419 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3420 if (radio_on)
3421 status = TP_ACPI_WANCARD_RADIOSSW;
3422 else
3423 status = 0;
3424 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3425 return -EIO;
3427 if (update_rfk)
3428 wan_update_rfk();
3430 return 0;
3433 /* sysfs wan enable ---------------------------------------------------- */
3434 static ssize_t wan_enable_show(struct device *dev,
3435 struct device_attribute *attr,
3436 char *buf)
3438 int status;
3440 printk_deprecated_rfkill_attribute("wwan_enable");
3442 status = wan_get_radiosw();
3443 if (status < 0)
3444 return status;
3446 return snprintf(buf, PAGE_SIZE, "%d\n",
3447 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3450 static ssize_t wan_enable_store(struct device *dev,
3451 struct device_attribute *attr,
3452 const char *buf, size_t count)
3454 unsigned long t;
3455 int res;
3457 printk_deprecated_rfkill_attribute("wwan_enable");
3459 if (parse_strtoul(buf, 1, &t))
3460 return -EINVAL;
3462 tpacpi_disclose_usertask("wwan_enable", "set to %ld\n", t);
3464 res = wan_set_radiosw(t, 1);
3466 return (res) ? res : count;
3469 static struct device_attribute dev_attr_wan_enable =
3470 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3471 wan_enable_show, wan_enable_store);
3473 /* --------------------------------------------------------------------- */
3475 static struct attribute *wan_attributes[] = {
3476 &dev_attr_wan_enable.attr,
3477 NULL
3480 static const struct attribute_group wan_attr_group = {
3481 .attrs = wan_attributes,
3484 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3486 int wans = wan_get_radiosw();
3488 if (wans < 0)
3489 return wans;
3491 *state = wans;
3492 return 0;
3495 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3497 dbg_printk(TPACPI_DBG_RFKILL,
3498 "request to change radio state to %d\n", state);
3499 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3502 static void wan_shutdown(void)
3504 /* Order firmware to save current state to NVRAM */
3505 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3506 TP_ACPI_WGSV_SAVE_STATE))
3507 printk(TPACPI_NOTICE
3508 "failed to save WWAN state to NVRAM\n");
3509 else
3510 vdbg_printk(TPACPI_DBG_RFKILL,
3511 "WWAN state saved to NVRAM\n");
3514 static void wan_exit(void)
3516 wan_shutdown();
3518 if (tpacpi_wan_rfkill)
3519 rfkill_unregister(tpacpi_wan_rfkill);
3521 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3522 &wan_attr_group);
3525 static int __init wan_init(struct ibm_init_struct *iibm)
3527 int res;
3528 int status = 0;
3530 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3531 "initializing wan subdriver\n");
3533 TPACPI_ACPIHANDLE_INIT(hkey);
3535 tp_features.wan = hkey_handle &&
3536 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3538 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3539 "wan is %s, status 0x%02x\n",
3540 str_supported(tp_features.wan),
3541 status);
3543 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3544 if (dbg_wwanemul) {
3545 tp_features.wan = 1;
3546 printk(TPACPI_INFO
3547 "wwan switch emulation enabled\n");
3548 } else
3549 #endif
3550 if (tp_features.wan &&
3551 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3552 /* no wan hardware present in system */
3553 tp_features.wan = 0;
3554 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3555 "wan hardware not installed\n");
3558 if (!tp_features.wan)
3559 return 1;
3561 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3562 &wan_attr_group);
3563 if (res)
3564 return res;
3566 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3567 &tpacpi_wan_rfkill,
3568 RFKILL_TYPE_WWAN,
3569 TPACPI_RFK_WWAN_SW_NAME,
3570 true,
3571 tpacpi_wan_rfk_set,
3572 tpacpi_wan_rfk_get);
3573 if (res) {
3574 wan_exit();
3575 return res;
3578 return 0;
3581 /* procfs -------------------------------------------------------------- */
3582 static int wan_read(char *p)
3584 int len = 0;
3585 int status = wan_get_radiosw();
3587 tpacpi_disclose_usertask("procfs wan", "read");
3589 if (!tp_features.wan)
3590 len += sprintf(p + len, "status:\t\tnot supported\n");
3591 else {
3592 len += sprintf(p + len, "status:\t\t%s\n",
3593 (status == RFKILL_STATE_UNBLOCKED) ?
3594 "enabled" : "disabled");
3595 len += sprintf(p + len, "commands:\tenable, disable\n");
3598 return len;
3601 static int wan_write(char *buf)
3603 char *cmd;
3604 int state = -1;
3606 if (!tp_features.wan)
3607 return -ENODEV;
3609 while ((cmd = next_cmd(&buf))) {
3610 if (strlencmp(cmd, "enable") == 0) {
3611 state = 1;
3612 } else if (strlencmp(cmd, "disable") == 0) {
3613 state = 0;
3614 } else
3615 return -EINVAL;
3618 if (state != -1) {
3619 tpacpi_disclose_usertask("procfs wan",
3620 "attempt to %s\n",
3621 state ? "enable" : "disable");
3622 wan_set_radiosw(state, 1);
3625 return 0;
3628 static struct ibm_struct wan_driver_data = {
3629 .name = "wan",
3630 .read = wan_read,
3631 .write = wan_write,
3632 .exit = wan_exit,
3633 .suspend = wan_suspend,
3634 .shutdown = wan_shutdown,
3637 /*************************************************************************
3638 * UWB subdriver
3641 enum {
3642 /* ACPI GUWB/SUWB bits */
3643 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3644 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3647 #define TPACPI_RFK_UWB_SW_NAME "tpacpi_uwb_sw"
3649 static struct rfkill *tpacpi_uwb_rfkill;
3651 static int uwb_get_radiosw(void)
3653 int status;
3655 if (!tp_features.uwb)
3656 return -ENODEV;
3658 /* WLSW overrides UWB in firmware/hardware, reflect that */
3659 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3660 return RFKILL_STATE_HARD_BLOCKED;
3662 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3663 if (dbg_uwbemul)
3664 return (tpacpi_uwb_emulstate) ?
3665 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3666 #endif
3668 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3669 return -EIO;
3671 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3672 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3675 static void uwb_update_rfk(void)
3677 int status;
3679 if (!tpacpi_uwb_rfkill)
3680 return;
3682 status = uwb_get_radiosw();
3683 if (status < 0)
3684 return;
3685 rfkill_force_state(tpacpi_uwb_rfkill, status);
3687 vdbg_printk(TPACPI_DBG_RFKILL,
3688 "forced rfkill state to %d\n",
3689 status);
3692 static int uwb_set_radiosw(int radio_on, int update_rfk)
3694 int status;
3696 if (!tp_features.uwb)
3697 return -ENODEV;
3699 /* WLSW overrides UWB in firmware/hardware, but there is no
3700 * reason to risk weird behaviour. */
3701 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3702 && radio_on)
3703 return -EPERM;
3705 vdbg_printk(TPACPI_DBG_RFKILL,
3706 "will %s UWB\n", radio_on ? "enable" : "disable");
3708 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3709 if (dbg_uwbemul) {
3710 tpacpi_uwb_emulstate = !!radio_on;
3711 if (update_rfk)
3712 uwb_update_rfk();
3713 return 0;
3715 #endif
3717 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3718 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3719 return -EIO;
3721 if (update_rfk)
3722 uwb_update_rfk();
3724 return 0;
3727 /* --------------------------------------------------------------------- */
3729 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3731 int uwbs = uwb_get_radiosw();
3733 if (uwbs < 0)
3734 return uwbs;
3736 *state = uwbs;
3737 return 0;
3740 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3742 dbg_printk(TPACPI_DBG_RFKILL,
3743 "request to change radio state to %d\n", state);
3744 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3747 static void uwb_exit(void)
3749 if (tpacpi_uwb_rfkill)
3750 rfkill_unregister(tpacpi_uwb_rfkill);
3753 static int __init uwb_init(struct ibm_init_struct *iibm)
3755 int res;
3756 int status = 0;
3758 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3759 "initializing uwb subdriver\n");
3761 TPACPI_ACPIHANDLE_INIT(hkey);
3763 tp_features.uwb = hkey_handle &&
3764 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3766 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_RFKILL,
3767 "uwb is %s, status 0x%02x\n",
3768 str_supported(tp_features.uwb),
3769 status);
3771 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3772 if (dbg_uwbemul) {
3773 tp_features.uwb = 1;
3774 printk(TPACPI_INFO
3775 "uwb switch emulation enabled\n");
3776 } else
3777 #endif
3778 if (tp_features.uwb &&
3779 !(status & TP_ACPI_UWB_HWPRESENT)) {
3780 /* no uwb hardware present in system */
3781 tp_features.uwb = 0;
3782 dbg_printk(TPACPI_DBG_INIT,
3783 "uwb hardware not installed\n");
3786 if (!tp_features.uwb)
3787 return 1;
3789 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3790 &tpacpi_uwb_rfkill,
3791 RFKILL_TYPE_UWB,
3792 TPACPI_RFK_UWB_SW_NAME,
3793 false,
3794 tpacpi_uwb_rfk_set,
3795 tpacpi_uwb_rfk_get);
3797 return res;
3800 static struct ibm_struct uwb_driver_data = {
3801 .name = "uwb",
3802 .exit = uwb_exit,
3803 .flags.experimental = 1,
3806 /*************************************************************************
3807 * Video subdriver
3810 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3812 enum video_access_mode {
3813 TPACPI_VIDEO_NONE = 0,
3814 TPACPI_VIDEO_570, /* 570 */
3815 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3816 TPACPI_VIDEO_NEW, /* all others */
3819 enum { /* video status flags, based on VIDEO_570 */
3820 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3821 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3822 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3825 enum { /* TPACPI_VIDEO_570 constants */
3826 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3827 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3828 * video_status_flags */
3829 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3830 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3833 static enum video_access_mode video_supported;
3834 static int video_orig_autosw;
3836 static int video_autosw_get(void);
3837 static int video_autosw_set(int enable);
3839 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3841 static int __init video_init(struct ibm_init_struct *iibm)
3843 int ivga;
3845 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3847 TPACPI_ACPIHANDLE_INIT(vid);
3848 TPACPI_ACPIHANDLE_INIT(vid2);
3850 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3851 /* G41, assume IVGA doesn't change */
3852 vid_handle = vid2_handle;
3854 if (!vid_handle)
3855 /* video switching not supported on R30, R31 */
3856 video_supported = TPACPI_VIDEO_NONE;
3857 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3858 /* 570 */
3859 video_supported = TPACPI_VIDEO_570;
3860 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3861 /* 600e/x, 770e, 770x */
3862 video_supported = TPACPI_VIDEO_770;
3863 else
3864 /* all others */
3865 video_supported = TPACPI_VIDEO_NEW;
3867 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3868 str_supported(video_supported != TPACPI_VIDEO_NONE),
3869 video_supported);
3871 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3874 static void video_exit(void)
3876 dbg_printk(TPACPI_DBG_EXIT,
3877 "restoring original video autoswitch mode\n");
3878 if (video_autosw_set(video_orig_autosw))
3879 printk(TPACPI_ERR "error while trying to restore original "
3880 "video autoswitch mode\n");
3883 static int video_outputsw_get(void)
3885 int status = 0;
3886 int i;
3888 switch (video_supported) {
3889 case TPACPI_VIDEO_570:
3890 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3891 TP_ACPI_VIDEO_570_PHSCMD))
3892 return -EIO;
3893 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3894 break;
3895 case TPACPI_VIDEO_770:
3896 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3897 return -EIO;
3898 if (i)
3899 status |= TP_ACPI_VIDEO_S_LCD;
3900 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3901 return -EIO;
3902 if (i)
3903 status |= TP_ACPI_VIDEO_S_CRT;
3904 break;
3905 case TPACPI_VIDEO_NEW:
3906 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3907 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3908 return -EIO;
3909 if (i)
3910 status |= TP_ACPI_VIDEO_S_CRT;
3912 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3913 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3914 return -EIO;
3915 if (i)
3916 status |= TP_ACPI_VIDEO_S_LCD;
3917 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3918 return -EIO;
3919 if (i)
3920 status |= TP_ACPI_VIDEO_S_DVI;
3921 break;
3922 default:
3923 return -ENOSYS;
3926 return status;
3929 static int video_outputsw_set(int status)
3931 int autosw;
3932 int res = 0;
3934 switch (video_supported) {
3935 case TPACPI_VIDEO_570:
3936 res = acpi_evalf(NULL, NULL,
3937 "\\_SB.PHS2", "vdd",
3938 TP_ACPI_VIDEO_570_PHS2CMD,
3939 status | TP_ACPI_VIDEO_570_PHS2SET);
3940 break;
3941 case TPACPI_VIDEO_770:
3942 autosw = video_autosw_get();
3943 if (autosw < 0)
3944 return autosw;
3946 res = video_autosw_set(1);
3947 if (res)
3948 return res;
3949 res = acpi_evalf(vid_handle, NULL,
3950 "ASWT", "vdd", status * 0x100, 0);
3951 if (!autosw && video_autosw_set(autosw)) {
3952 printk(TPACPI_ERR
3953 "video auto-switch left enabled due to error\n");
3954 return -EIO;
3956 break;
3957 case TPACPI_VIDEO_NEW:
3958 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3959 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3960 break;
3961 default:
3962 return -ENOSYS;
3965 return (res)? 0 : -EIO;
3968 static int video_autosw_get(void)
3970 int autosw = 0;
3972 switch (video_supported) {
3973 case TPACPI_VIDEO_570:
3974 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3975 return -EIO;
3976 break;
3977 case TPACPI_VIDEO_770:
3978 case TPACPI_VIDEO_NEW:
3979 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3980 return -EIO;
3981 break;
3982 default:
3983 return -ENOSYS;
3986 return autosw & 1;
3989 static int video_autosw_set(int enable)
3991 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3992 return -EIO;
3993 return 0;
3996 static int video_outputsw_cycle(void)
3998 int autosw = video_autosw_get();
3999 int res;
4001 if (autosw < 0)
4002 return autosw;
4004 switch (video_supported) {
4005 case TPACPI_VIDEO_570:
4006 res = video_autosw_set(1);
4007 if (res)
4008 return res;
4009 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
4010 break;
4011 case TPACPI_VIDEO_770:
4012 case TPACPI_VIDEO_NEW:
4013 res = video_autosw_set(1);
4014 if (res)
4015 return res;
4016 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
4017 break;
4018 default:
4019 return -ENOSYS;
4021 if (!autosw && video_autosw_set(autosw)) {
4022 printk(TPACPI_ERR
4023 "video auto-switch left enabled due to error\n");
4024 return -EIO;
4027 return (res)? 0 : -EIO;
4030 static int video_expand_toggle(void)
4032 switch (video_supported) {
4033 case TPACPI_VIDEO_570:
4034 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
4035 0 : -EIO;
4036 case TPACPI_VIDEO_770:
4037 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
4038 0 : -EIO;
4039 case TPACPI_VIDEO_NEW:
4040 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
4041 0 : -EIO;
4042 default:
4043 return -ENOSYS;
4045 /* not reached */
4048 static int video_read(char *p)
4050 int status, autosw;
4051 int len = 0;
4053 if (video_supported == TPACPI_VIDEO_NONE) {
4054 len += sprintf(p + len, "status:\t\tnot supported\n");
4055 return len;
4058 status = video_outputsw_get();
4059 if (status < 0)
4060 return status;
4062 autosw = video_autosw_get();
4063 if (autosw < 0)
4064 return autosw;
4066 len += sprintf(p + len, "status:\t\tsupported\n");
4067 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
4068 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
4069 if (video_supported == TPACPI_VIDEO_NEW)
4070 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
4071 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
4072 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
4073 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
4074 if (video_supported == TPACPI_VIDEO_NEW)
4075 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4076 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4077 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4079 return len;
4082 static int video_write(char *buf)
4084 char *cmd;
4085 int enable, disable, status;
4086 int res;
4088 if (video_supported == TPACPI_VIDEO_NONE)
4089 return -ENODEV;
4091 enable = 0;
4092 disable = 0;
4094 while ((cmd = next_cmd(&buf))) {
4095 if (strlencmp(cmd, "lcd_enable") == 0) {
4096 enable |= TP_ACPI_VIDEO_S_LCD;
4097 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4098 disable |= TP_ACPI_VIDEO_S_LCD;
4099 } else if (strlencmp(cmd, "crt_enable") == 0) {
4100 enable |= TP_ACPI_VIDEO_S_CRT;
4101 } else if (strlencmp(cmd, "crt_disable") == 0) {
4102 disable |= TP_ACPI_VIDEO_S_CRT;
4103 } else if (video_supported == TPACPI_VIDEO_NEW &&
4104 strlencmp(cmd, "dvi_enable") == 0) {
4105 enable |= TP_ACPI_VIDEO_S_DVI;
4106 } else if (video_supported == TPACPI_VIDEO_NEW &&
4107 strlencmp(cmd, "dvi_disable") == 0) {
4108 disable |= TP_ACPI_VIDEO_S_DVI;
4109 } else if (strlencmp(cmd, "auto_enable") == 0) {
4110 res = video_autosw_set(1);
4111 if (res)
4112 return res;
4113 } else if (strlencmp(cmd, "auto_disable") == 0) {
4114 res = video_autosw_set(0);
4115 if (res)
4116 return res;
4117 } else if (strlencmp(cmd, "video_switch") == 0) {
4118 res = video_outputsw_cycle();
4119 if (res)
4120 return res;
4121 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4122 res = video_expand_toggle();
4123 if (res)
4124 return res;
4125 } else
4126 return -EINVAL;
4129 if (enable || disable) {
4130 status = video_outputsw_get();
4131 if (status < 0)
4132 return status;
4133 res = video_outputsw_set((status & ~disable) | enable);
4134 if (res)
4135 return res;
4138 return 0;
4141 static struct ibm_struct video_driver_data = {
4142 .name = "video",
4143 .read = video_read,
4144 .write = video_write,
4145 .exit = video_exit,
4148 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4150 /*************************************************************************
4151 * Light (thinklight) subdriver
4154 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4155 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4157 static int light_get_status(void)
4159 int status = 0;
4161 if (tp_features.light_status) {
4162 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4163 return -EIO;
4164 return (!!status);
4167 return -ENXIO;
4170 static int light_set_status(int status)
4172 int rc;
4174 if (tp_features.light) {
4175 if (cmos_handle) {
4176 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4177 (status)?
4178 TP_CMOS_THINKLIGHT_ON :
4179 TP_CMOS_THINKLIGHT_OFF);
4180 } else {
4181 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4182 (status)? 1 : 0);
4184 return (rc)? 0 : -EIO;
4187 return -ENXIO;
4190 static void light_set_status_worker(struct work_struct *work)
4192 struct tpacpi_led_classdev *data =
4193 container_of(work, struct tpacpi_led_classdev, work);
4195 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4196 light_set_status((data->new_state != TPACPI_LED_OFF));
4199 static void light_sysfs_set(struct led_classdev *led_cdev,
4200 enum led_brightness brightness)
4202 struct tpacpi_led_classdev *data =
4203 container_of(led_cdev,
4204 struct tpacpi_led_classdev,
4205 led_classdev);
4206 data->new_state = (brightness != LED_OFF) ?
4207 TPACPI_LED_ON : TPACPI_LED_OFF;
4208 queue_work(tpacpi_wq, &data->work);
4211 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4213 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4216 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4217 .led_classdev = {
4218 .name = "tpacpi::thinklight",
4219 .brightness_set = &light_sysfs_set,
4220 .brightness_get = &light_sysfs_get,
4224 static int __init light_init(struct ibm_init_struct *iibm)
4226 int rc;
4228 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4230 TPACPI_ACPIHANDLE_INIT(ledb);
4231 TPACPI_ACPIHANDLE_INIT(lght);
4232 TPACPI_ACPIHANDLE_INIT(cmos);
4233 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4235 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4236 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4238 if (tp_features.light)
4239 /* light status not supported on
4240 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4241 tp_features.light_status =
4242 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4244 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4245 str_supported(tp_features.light),
4246 str_supported(tp_features.light_status));
4248 if (!tp_features.light)
4249 return 1;
4251 rc = led_classdev_register(&tpacpi_pdev->dev,
4252 &tpacpi_led_thinklight.led_classdev);
4254 if (rc < 0) {
4255 tp_features.light = 0;
4256 tp_features.light_status = 0;
4257 } else {
4258 rc = 0;
4261 return rc;
4264 static void light_exit(void)
4266 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4267 if (work_pending(&tpacpi_led_thinklight.work))
4268 flush_workqueue(tpacpi_wq);
4271 static int light_read(char *p)
4273 int len = 0;
4274 int status;
4276 if (!tp_features.light) {
4277 len += sprintf(p + len, "status:\t\tnot supported\n");
4278 } else if (!tp_features.light_status) {
4279 len += sprintf(p + len, "status:\t\tunknown\n");
4280 len += sprintf(p + len, "commands:\ton, off\n");
4281 } else {
4282 status = light_get_status();
4283 if (status < 0)
4284 return status;
4285 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4286 len += sprintf(p + len, "commands:\ton, off\n");
4289 return len;
4292 static int light_write(char *buf)
4294 char *cmd;
4295 int newstatus = 0;
4297 if (!tp_features.light)
4298 return -ENODEV;
4300 while ((cmd = next_cmd(&buf))) {
4301 if (strlencmp(cmd, "on") == 0) {
4302 newstatus = 1;
4303 } else if (strlencmp(cmd, "off") == 0) {
4304 newstatus = 0;
4305 } else
4306 return -EINVAL;
4309 return light_set_status(newstatus);
4312 static struct ibm_struct light_driver_data = {
4313 .name = "light",
4314 .read = light_read,
4315 .write = light_write,
4316 .exit = light_exit,
4319 /*************************************************************************
4320 * Dock subdriver
4323 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4325 static void dock_notify(struct ibm_struct *ibm, u32 event);
4326 static int dock_read(char *p);
4327 static int dock_write(char *buf);
4329 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4330 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4331 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4332 "\\_SB.PCI.ISA.SLCE", /* 570 */
4333 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4335 /* don't list other alternatives as we install a notify handler on the 570 */
4336 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4338 static const struct acpi_device_id ibm_pci_device_ids[] = {
4339 {PCI_ROOT_HID_STRING, 0},
4340 {"", 0},
4343 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4345 .notify = dock_notify,
4346 .handle = &dock_handle,
4347 .type = ACPI_SYSTEM_NOTIFY,
4350 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4351 * We just use it to get notifications of dock hotplug
4352 * in very old thinkpads */
4353 .hid = ibm_pci_device_ids,
4354 .notify = dock_notify,
4355 .handle = &pci_handle,
4356 .type = ACPI_SYSTEM_NOTIFY,
4360 static struct ibm_struct dock_driver_data[2] = {
4362 .name = "dock",
4363 .read = dock_read,
4364 .write = dock_write,
4365 .acpi = &ibm_dock_acpidriver[0],
4368 .name = "dock",
4369 .acpi = &ibm_dock_acpidriver[1],
4373 #define dock_docked() (_sta(dock_handle) & 1)
4375 static int __init dock_init(struct ibm_init_struct *iibm)
4377 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4379 TPACPI_ACPIHANDLE_INIT(dock);
4381 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4382 str_supported(dock_handle != NULL));
4384 return (dock_handle)? 0 : 1;
4387 static int __init dock_init2(struct ibm_init_struct *iibm)
4389 int dock2_needed;
4391 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4393 if (dock_driver_data[0].flags.acpi_driver_registered &&
4394 dock_driver_data[0].flags.acpi_notify_installed) {
4395 TPACPI_ACPIHANDLE_INIT(pci);
4396 dock2_needed = (pci_handle != NULL);
4397 vdbg_printk(TPACPI_DBG_INIT,
4398 "dock PCI handler for the TP 570 is %s\n",
4399 str_supported(dock2_needed));
4400 } else {
4401 vdbg_printk(TPACPI_DBG_INIT,
4402 "dock subdriver part 2 not required\n");
4403 dock2_needed = 0;
4406 return (dock2_needed)? 0 : 1;
4409 static void dock_notify(struct ibm_struct *ibm, u32 event)
4411 int docked = dock_docked();
4412 int pci = ibm->acpi->hid && ibm->acpi->device &&
4413 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4414 int data;
4416 if (event == 1 && !pci) /* 570 */
4417 data = 1; /* button */
4418 else if (event == 1 && pci) /* 570 */
4419 data = 3; /* dock */
4420 else if (event == 3 && docked)
4421 data = 1; /* button */
4422 else if (event == 3 && !docked)
4423 data = 2; /* undock */
4424 else if (event == 0 && docked)
4425 data = 3; /* dock */
4426 else {
4427 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4428 event, _sta(dock_handle));
4429 data = 0; /* unknown */
4431 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4432 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4433 dev_name(&ibm->acpi->device->dev),
4434 event, data);
4437 static int dock_read(char *p)
4439 int len = 0;
4440 int docked = dock_docked();
4442 if (!dock_handle)
4443 len += sprintf(p + len, "status:\t\tnot supported\n");
4444 else if (!docked)
4445 len += sprintf(p + len, "status:\t\tundocked\n");
4446 else {
4447 len += sprintf(p + len, "status:\t\tdocked\n");
4448 len += sprintf(p + len, "commands:\tdock, undock\n");
4451 return len;
4454 static int dock_write(char *buf)
4456 char *cmd;
4458 if (!dock_docked())
4459 return -ENODEV;
4461 while ((cmd = next_cmd(&buf))) {
4462 if (strlencmp(cmd, "undock") == 0) {
4463 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4464 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4465 return -EIO;
4466 } else if (strlencmp(cmd, "dock") == 0) {
4467 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4468 return -EIO;
4469 } else
4470 return -EINVAL;
4473 return 0;
4476 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4478 /*************************************************************************
4479 * Bay subdriver
4482 #ifdef CONFIG_THINKPAD_ACPI_BAY
4484 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4485 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4486 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4487 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4488 ); /* A21e, R30, R31 */
4489 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4490 "_EJ0", /* all others */
4491 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4492 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4493 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4494 ); /* all others */
4495 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4496 "_EJ0", /* 770x */
4497 ); /* all others */
4499 static int __init bay_init(struct ibm_init_struct *iibm)
4501 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4503 TPACPI_ACPIHANDLE_INIT(bay);
4504 if (bay_handle)
4505 TPACPI_ACPIHANDLE_INIT(bay_ej);
4506 TPACPI_ACPIHANDLE_INIT(bay2);
4507 if (bay2_handle)
4508 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4510 tp_features.bay_status = bay_handle &&
4511 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4512 tp_features.bay_status2 = bay2_handle &&
4513 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4515 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4516 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4517 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4518 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4520 vdbg_printk(TPACPI_DBG_INIT,
4521 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4522 str_supported(tp_features.bay_status),
4523 str_supported(tp_features.bay_eject),
4524 str_supported(tp_features.bay_status2),
4525 str_supported(tp_features.bay_eject2));
4527 return (tp_features.bay_status || tp_features.bay_eject ||
4528 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4531 static void bay_notify(struct ibm_struct *ibm, u32 event)
4533 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4534 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4535 dev_name(&ibm->acpi->device->dev),
4536 event, 0);
4539 #define bay_occupied(b) (_sta(b##_handle) & 1)
4541 static int bay_read(char *p)
4543 int len = 0;
4544 int occupied = bay_occupied(bay);
4545 int occupied2 = bay_occupied(bay2);
4546 int eject, eject2;
4548 len += sprintf(p + len, "status:\t\t%s\n",
4549 tp_features.bay_status ?
4550 (occupied ? "occupied" : "unoccupied") :
4551 "not supported");
4552 if (tp_features.bay_status2)
4553 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4554 "occupied" : "unoccupied");
4556 eject = tp_features.bay_eject && occupied;
4557 eject2 = tp_features.bay_eject2 && occupied2;
4559 if (eject && eject2)
4560 len += sprintf(p + len, "commands:\teject, eject2\n");
4561 else if (eject)
4562 len += sprintf(p + len, "commands:\teject\n");
4563 else if (eject2)
4564 len += sprintf(p + len, "commands:\teject2\n");
4566 return len;
4569 static int bay_write(char *buf)
4571 char *cmd;
4573 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4574 return -ENODEV;
4576 while ((cmd = next_cmd(&buf))) {
4577 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4578 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4579 return -EIO;
4580 } else if (tp_features.bay_eject2 &&
4581 strlencmp(cmd, "eject2") == 0) {
4582 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4583 return -EIO;
4584 } else
4585 return -EINVAL;
4588 return 0;
4591 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4592 .notify = bay_notify,
4593 .handle = &bay_handle,
4594 .type = ACPI_SYSTEM_NOTIFY,
4597 static struct ibm_struct bay_driver_data = {
4598 .name = "bay",
4599 .read = bay_read,
4600 .write = bay_write,
4601 .acpi = &ibm_bay_acpidriver,
4604 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4606 /*************************************************************************
4607 * CMOS subdriver
4610 /* sysfs cmos_command -------------------------------------------------- */
4611 static ssize_t cmos_command_store(struct device *dev,
4612 struct device_attribute *attr,
4613 const char *buf, size_t count)
4615 unsigned long cmos_cmd;
4616 int res;
4618 if (parse_strtoul(buf, 21, &cmos_cmd))
4619 return -EINVAL;
4621 res = issue_thinkpad_cmos_command(cmos_cmd);
4622 return (res)? res : count;
4625 static struct device_attribute dev_attr_cmos_command =
4626 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4628 /* --------------------------------------------------------------------- */
4630 static int __init cmos_init(struct ibm_init_struct *iibm)
4632 int res;
4634 vdbg_printk(TPACPI_DBG_INIT,
4635 "initializing cmos commands subdriver\n");
4637 TPACPI_ACPIHANDLE_INIT(cmos);
4639 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4640 str_supported(cmos_handle != NULL));
4642 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4643 if (res)
4644 return res;
4646 return (cmos_handle)? 0 : 1;
4649 static void cmos_exit(void)
4651 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4654 static int cmos_read(char *p)
4656 int len = 0;
4658 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4659 R30, R31, T20-22, X20-21 */
4660 if (!cmos_handle)
4661 len += sprintf(p + len, "status:\t\tnot supported\n");
4662 else {
4663 len += sprintf(p + len, "status:\t\tsupported\n");
4664 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4667 return len;
4670 static int cmos_write(char *buf)
4672 char *cmd;
4673 int cmos_cmd, res;
4675 while ((cmd = next_cmd(&buf))) {
4676 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4677 cmos_cmd >= 0 && cmos_cmd <= 21) {
4678 /* cmos_cmd set */
4679 } else
4680 return -EINVAL;
4682 res = issue_thinkpad_cmos_command(cmos_cmd);
4683 if (res)
4684 return res;
4687 return 0;
4690 static struct ibm_struct cmos_driver_data = {
4691 .name = "cmos",
4692 .read = cmos_read,
4693 .write = cmos_write,
4694 .exit = cmos_exit,
4697 /*************************************************************************
4698 * LED subdriver
4701 enum led_access_mode {
4702 TPACPI_LED_NONE = 0,
4703 TPACPI_LED_570, /* 570 */
4704 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4705 TPACPI_LED_NEW, /* all others */
4708 enum { /* For TPACPI_LED_OLD */
4709 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4710 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4711 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4714 static enum led_access_mode led_supported;
4716 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4717 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4718 /* T20-22, X20-21 */
4719 "LED", /* all others */
4720 ); /* R30, R31 */
4722 #define TPACPI_LED_NUMLEDS 8
4723 static struct tpacpi_led_classdev *tpacpi_leds;
4724 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4725 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4726 /* there's a limit of 19 chars + NULL before 2.6.26 */
4727 "tpacpi::power",
4728 "tpacpi:orange:batt",
4729 "tpacpi:green:batt",
4730 "tpacpi::dock_active",
4731 "tpacpi::bay_active",
4732 "tpacpi::dock_batt",
4733 "tpacpi::unknown_led",
4734 "tpacpi::standby",
4736 #define TPACPI_SAFE_LEDS 0x0081U
4738 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4740 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4741 return false;
4742 #else
4743 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4744 #endif
4747 static int led_get_status(const unsigned int led)
4749 int status;
4750 enum led_status_t led_s;
4752 switch (led_supported) {
4753 case TPACPI_LED_570:
4754 if (!acpi_evalf(ec_handle,
4755 &status, "GLED", "dd", 1 << led))
4756 return -EIO;
4757 led_s = (status == 0)?
4758 TPACPI_LED_OFF :
4759 ((status == 1)?
4760 TPACPI_LED_ON :
4761 TPACPI_LED_BLINK);
4762 tpacpi_led_state_cache[led] = led_s;
4763 return led_s;
4764 default:
4765 return -ENXIO;
4768 /* not reached */
4771 static int led_set_status(const unsigned int led,
4772 const enum led_status_t ledstatus)
4774 /* off, on, blink. Index is led_status_t */
4775 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4776 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4778 int rc = 0;
4780 switch (led_supported) {
4781 case TPACPI_LED_570:
4782 /* 570 */
4783 if (unlikely(led > 7))
4784 return -EINVAL;
4785 if (unlikely(tpacpi_is_led_restricted(led)))
4786 return -EPERM;
4787 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4788 (1 << led), led_sled_arg1[ledstatus]))
4789 rc = -EIO;
4790 break;
4791 case TPACPI_LED_OLD:
4792 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4793 if (unlikely(led > 7))
4794 return -EINVAL;
4795 if (unlikely(tpacpi_is_led_restricted(led)))
4796 return -EPERM;
4797 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4798 if (rc >= 0)
4799 rc = ec_write(TPACPI_LED_EC_HLBL,
4800 (ledstatus == TPACPI_LED_BLINK) << led);
4801 if (rc >= 0)
4802 rc = ec_write(TPACPI_LED_EC_HLCL,
4803 (ledstatus != TPACPI_LED_OFF) << led);
4804 break;
4805 case TPACPI_LED_NEW:
4806 /* all others */
4807 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4808 return -EINVAL;
4809 if (unlikely(tpacpi_is_led_restricted(led)))
4810 return -EPERM;
4811 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4812 led, led_led_arg1[ledstatus]))
4813 rc = -EIO;
4814 break;
4815 default:
4816 rc = -ENXIO;
4819 if (!rc)
4820 tpacpi_led_state_cache[led] = ledstatus;
4822 return rc;
4825 static void led_set_status_worker(struct work_struct *work)
4827 struct tpacpi_led_classdev *data =
4828 container_of(work, struct tpacpi_led_classdev, work);
4830 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4831 led_set_status(data->led, data->new_state);
4834 static void led_sysfs_set(struct led_classdev *led_cdev,
4835 enum led_brightness brightness)
4837 struct tpacpi_led_classdev *data = container_of(led_cdev,
4838 struct tpacpi_led_classdev, led_classdev);
4840 if (brightness == LED_OFF)
4841 data->new_state = TPACPI_LED_OFF;
4842 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4843 data->new_state = TPACPI_LED_ON;
4844 else
4845 data->new_state = TPACPI_LED_BLINK;
4847 queue_work(tpacpi_wq, &data->work);
4850 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4851 unsigned long *delay_on, unsigned long *delay_off)
4853 struct tpacpi_led_classdev *data = container_of(led_cdev,
4854 struct tpacpi_led_classdev, led_classdev);
4856 /* Can we choose the flash rate? */
4857 if (*delay_on == 0 && *delay_off == 0) {
4858 /* yes. set them to the hardware blink rate (1 Hz) */
4859 *delay_on = 500; /* ms */
4860 *delay_off = 500; /* ms */
4861 } else if ((*delay_on != 500) || (*delay_off != 500))
4862 return -EINVAL;
4864 data->new_state = TPACPI_LED_BLINK;
4865 queue_work(tpacpi_wq, &data->work);
4867 return 0;
4870 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4872 int rc;
4874 struct tpacpi_led_classdev *data = container_of(led_cdev,
4875 struct tpacpi_led_classdev, led_classdev);
4877 rc = led_get_status(data->led);
4879 if (rc == TPACPI_LED_OFF || rc < 0)
4880 rc = LED_OFF; /* no error handling in led class :( */
4881 else
4882 rc = LED_FULL;
4884 return rc;
4887 static void led_exit(void)
4889 unsigned int i;
4891 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4892 if (tpacpi_leds[i].led_classdev.name)
4893 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4896 kfree(tpacpi_leds);
4899 static int __init tpacpi_init_led(unsigned int led)
4901 int rc;
4903 tpacpi_leds[led].led = led;
4905 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4906 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4907 if (led_supported == TPACPI_LED_570)
4908 tpacpi_leds[led].led_classdev.brightness_get =
4909 &led_sysfs_get;
4911 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4913 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4915 rc = led_classdev_register(&tpacpi_pdev->dev,
4916 &tpacpi_leds[led].led_classdev);
4917 if (rc < 0)
4918 tpacpi_leds[led].led_classdev.name = NULL;
4920 return rc;
4923 static int __init led_init(struct ibm_init_struct *iibm)
4925 unsigned int i;
4926 int rc;
4928 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4930 TPACPI_ACPIHANDLE_INIT(led);
4932 if (!led_handle)
4933 /* led not supported on R30, R31 */
4934 led_supported = TPACPI_LED_NONE;
4935 else if (strlencmp(led_path, "SLED") == 0)
4936 /* 570 */
4937 led_supported = TPACPI_LED_570;
4938 else if (strlencmp(led_path, "SYSL") == 0)
4939 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4940 led_supported = TPACPI_LED_OLD;
4941 else
4942 /* all others */
4943 led_supported = TPACPI_LED_NEW;
4945 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4946 str_supported(led_supported), led_supported);
4948 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4949 GFP_KERNEL);
4950 if (!tpacpi_leds) {
4951 printk(TPACPI_ERR "Out of memory for LED data\n");
4952 return -ENOMEM;
4955 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4956 if (!tpacpi_is_led_restricted(i)) {
4957 rc = tpacpi_init_led(i);
4958 if (rc < 0) {
4959 led_exit();
4960 return rc;
4965 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4966 if (led_supported != TPACPI_LED_NONE)
4967 printk(TPACPI_NOTICE
4968 "warning: userspace override of important "
4969 "firmware LEDs is enabled\n");
4970 #endif
4971 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4974 #define str_led_status(s) \
4975 ((s) == TPACPI_LED_OFF ? "off" : \
4976 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4978 static int led_read(char *p)
4980 int len = 0;
4982 if (!led_supported) {
4983 len += sprintf(p + len, "status:\t\tnot supported\n");
4984 return len;
4986 len += sprintf(p + len, "status:\t\tsupported\n");
4988 if (led_supported == TPACPI_LED_570) {
4989 /* 570 */
4990 int i, status;
4991 for (i = 0; i < 8; i++) {
4992 status = led_get_status(i);
4993 if (status < 0)
4994 return -EIO;
4995 len += sprintf(p + len, "%d:\t\t%s\n",
4996 i, str_led_status(status));
5000 len += sprintf(p + len, "commands:\t"
5001 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
5003 return len;
5006 static int led_write(char *buf)
5008 char *cmd;
5009 int led, rc;
5010 enum led_status_t s;
5012 if (!led_supported)
5013 return -ENODEV;
5015 while ((cmd = next_cmd(&buf))) {
5016 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
5017 return -EINVAL;
5019 if (strstr(cmd, "off")) {
5020 s = TPACPI_LED_OFF;
5021 } else if (strstr(cmd, "on")) {
5022 s = TPACPI_LED_ON;
5023 } else if (strstr(cmd, "blink")) {
5024 s = TPACPI_LED_BLINK;
5025 } else {
5026 return -EINVAL;
5029 rc = led_set_status(led, s);
5030 if (rc < 0)
5031 return rc;
5034 return 0;
5037 static struct ibm_struct led_driver_data = {
5038 .name = "led",
5039 .read = led_read,
5040 .write = led_write,
5041 .exit = led_exit,
5044 /*************************************************************************
5045 * Beep subdriver
5048 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
5050 static int __init beep_init(struct ibm_init_struct *iibm)
5052 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
5054 TPACPI_ACPIHANDLE_INIT(beep);
5056 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
5057 str_supported(beep_handle != NULL));
5059 return (beep_handle)? 0 : 1;
5062 static int beep_read(char *p)
5064 int len = 0;
5066 if (!beep_handle)
5067 len += sprintf(p + len, "status:\t\tnot supported\n");
5068 else {
5069 len += sprintf(p + len, "status:\t\tsupported\n");
5070 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
5073 return len;
5076 static int beep_write(char *buf)
5078 char *cmd;
5079 int beep_cmd;
5081 if (!beep_handle)
5082 return -ENODEV;
5084 while ((cmd = next_cmd(&buf))) {
5085 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5086 beep_cmd >= 0 && beep_cmd <= 17) {
5087 /* beep_cmd set */
5088 } else
5089 return -EINVAL;
5090 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5091 return -EIO;
5094 return 0;
5097 static struct ibm_struct beep_driver_data = {
5098 .name = "beep",
5099 .read = beep_read,
5100 .write = beep_write,
5103 /*************************************************************************
5104 * Thermal subdriver
5107 enum thermal_access_mode {
5108 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5109 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5110 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5111 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5112 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5115 enum { /* TPACPI_THERMAL_TPEC_* */
5116 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5117 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5118 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5121 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5122 struct ibm_thermal_sensors_struct {
5123 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5126 static enum thermal_access_mode thermal_read_mode;
5128 /* idx is zero-based */
5129 static int thermal_get_sensor(int idx, s32 *value)
5131 int t;
5132 s8 tmp;
5133 char tmpi[5];
5135 t = TP_EC_THERMAL_TMP0;
5137 switch (thermal_read_mode) {
5138 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5139 case TPACPI_THERMAL_TPEC_16:
5140 if (idx >= 8 && idx <= 15) {
5141 t = TP_EC_THERMAL_TMP8;
5142 idx -= 8;
5144 /* fallthrough */
5145 #endif
5146 case TPACPI_THERMAL_TPEC_8:
5147 if (idx <= 7) {
5148 if (!acpi_ec_read(t + idx, &tmp))
5149 return -EIO;
5150 *value = tmp * 1000;
5151 return 0;
5153 break;
5155 case TPACPI_THERMAL_ACPI_UPDT:
5156 if (idx <= 7) {
5157 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5158 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5159 return -EIO;
5160 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5161 return -EIO;
5162 *value = (t - 2732) * 100;
5163 return 0;
5165 break;
5167 case TPACPI_THERMAL_ACPI_TMP07:
5168 if (idx <= 7) {
5169 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5170 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5171 return -EIO;
5172 if (t > 127 || t < -127)
5173 t = TP_EC_THERMAL_TMP_NA;
5174 *value = t * 1000;
5175 return 0;
5177 break;
5179 case TPACPI_THERMAL_NONE:
5180 default:
5181 return -ENOSYS;
5184 return -EINVAL;
5187 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5189 int res, i;
5190 int n;
5192 n = 8;
5193 i = 0;
5195 if (!s)
5196 return -EINVAL;
5198 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5199 n = 16;
5201 for (i = 0 ; i < n; i++) {
5202 res = thermal_get_sensor(i, &s->temp[i]);
5203 if (res)
5204 return res;
5207 return n;
5210 /* sysfs temp##_input -------------------------------------------------- */
5212 static ssize_t thermal_temp_input_show(struct device *dev,
5213 struct device_attribute *attr,
5214 char *buf)
5216 struct sensor_device_attribute *sensor_attr =
5217 to_sensor_dev_attr(attr);
5218 int idx = sensor_attr->index;
5219 s32 value;
5220 int res;
5222 res = thermal_get_sensor(idx, &value);
5223 if (res)
5224 return res;
5225 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5226 return -ENXIO;
5228 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5231 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5232 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5233 thermal_temp_input_show, NULL, _idxB)
5235 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5236 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5237 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5238 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5239 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5240 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5241 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5242 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5243 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5244 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5245 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5246 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5247 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5248 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5249 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5250 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5251 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5254 #define THERMAL_ATTRS(X) \
5255 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5257 static struct attribute *thermal_temp_input_attr[] = {
5258 THERMAL_ATTRS(8),
5259 THERMAL_ATTRS(9),
5260 THERMAL_ATTRS(10),
5261 THERMAL_ATTRS(11),
5262 THERMAL_ATTRS(12),
5263 THERMAL_ATTRS(13),
5264 THERMAL_ATTRS(14),
5265 THERMAL_ATTRS(15),
5266 THERMAL_ATTRS(0),
5267 THERMAL_ATTRS(1),
5268 THERMAL_ATTRS(2),
5269 THERMAL_ATTRS(3),
5270 THERMAL_ATTRS(4),
5271 THERMAL_ATTRS(5),
5272 THERMAL_ATTRS(6),
5273 THERMAL_ATTRS(7),
5274 NULL
5277 static const struct attribute_group thermal_temp_input16_group = {
5278 .attrs = thermal_temp_input_attr
5281 static const struct attribute_group thermal_temp_input8_group = {
5282 .attrs = &thermal_temp_input_attr[8]
5285 #undef THERMAL_SENSOR_ATTR_TEMP
5286 #undef THERMAL_ATTRS
5288 /* --------------------------------------------------------------------- */
5290 static int __init thermal_init(struct ibm_init_struct *iibm)
5292 u8 t, ta1, ta2;
5293 int i;
5294 int acpi_tmp7;
5295 int res;
5297 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5299 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5301 if (thinkpad_id.ec_model) {
5303 * Direct EC access mode: sensors at registers
5304 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5305 * non-implemented, thermal sensors return 0x80 when
5306 * not available
5309 ta1 = ta2 = 0;
5310 for (i = 0; i < 8; i++) {
5311 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5312 ta1 |= t;
5313 } else {
5314 ta1 = 0;
5315 break;
5317 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5318 ta2 |= t;
5319 } else {
5320 ta1 = 0;
5321 break;
5324 if (ta1 == 0) {
5325 /* This is sheer paranoia, but we handle it anyway */
5326 if (acpi_tmp7) {
5327 printk(TPACPI_ERR
5328 "ThinkPad ACPI EC access misbehaving, "
5329 "falling back to ACPI TMPx access "
5330 "mode\n");
5331 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5332 } else {
5333 printk(TPACPI_ERR
5334 "ThinkPad ACPI EC access misbehaving, "
5335 "disabling thermal sensors access\n");
5336 thermal_read_mode = TPACPI_THERMAL_NONE;
5338 } else {
5339 thermal_read_mode =
5340 (ta2 != 0) ?
5341 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5343 } else if (acpi_tmp7) {
5344 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5345 /* 600e/x, 770e, 770x */
5346 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5347 } else {
5348 /* Standard ACPI TMPx access, max 8 sensors */
5349 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5351 } else {
5352 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5353 thermal_read_mode = TPACPI_THERMAL_NONE;
5356 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5357 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5358 thermal_read_mode);
5360 switch (thermal_read_mode) {
5361 case TPACPI_THERMAL_TPEC_16:
5362 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5363 &thermal_temp_input16_group);
5364 if (res)
5365 return res;
5366 break;
5367 case TPACPI_THERMAL_TPEC_8:
5368 case TPACPI_THERMAL_ACPI_TMP07:
5369 case TPACPI_THERMAL_ACPI_UPDT:
5370 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5371 &thermal_temp_input8_group);
5372 if (res)
5373 return res;
5374 break;
5375 case TPACPI_THERMAL_NONE:
5376 default:
5377 return 1;
5380 return 0;
5383 static void thermal_exit(void)
5385 switch (thermal_read_mode) {
5386 case TPACPI_THERMAL_TPEC_16:
5387 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5388 &thermal_temp_input16_group);
5389 break;
5390 case TPACPI_THERMAL_TPEC_8:
5391 case TPACPI_THERMAL_ACPI_TMP07:
5392 case TPACPI_THERMAL_ACPI_UPDT:
5393 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5394 &thermal_temp_input16_group);
5395 break;
5396 case TPACPI_THERMAL_NONE:
5397 default:
5398 break;
5402 static int thermal_read(char *p)
5404 int len = 0;
5405 int n, i;
5406 struct ibm_thermal_sensors_struct t;
5408 n = thermal_get_sensors(&t);
5409 if (unlikely(n < 0))
5410 return n;
5412 len += sprintf(p + len, "temperatures:\t");
5414 if (n > 0) {
5415 for (i = 0; i < (n - 1); i++)
5416 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5417 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5418 } else
5419 len += sprintf(p + len, "not supported\n");
5421 return len;
5424 static struct ibm_struct thermal_driver_data = {
5425 .name = "thermal",
5426 .read = thermal_read,
5427 .exit = thermal_exit,
5430 /*************************************************************************
5431 * EC Dump subdriver
5434 static u8 ecdump_regs[256];
5436 static int ecdump_read(char *p)
5438 int len = 0;
5439 int i, j;
5440 u8 v;
5442 len += sprintf(p + len, "EC "
5443 " +00 +01 +02 +03 +04 +05 +06 +07"
5444 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5445 for (i = 0; i < 256; i += 16) {
5446 len += sprintf(p + len, "EC 0x%02x:", i);
5447 for (j = 0; j < 16; j++) {
5448 if (!acpi_ec_read(i + j, &v))
5449 break;
5450 if (v != ecdump_regs[i + j])
5451 len += sprintf(p + len, " *%02x", v);
5452 else
5453 len += sprintf(p + len, " %02x", v);
5454 ecdump_regs[i + j] = v;
5456 len += sprintf(p + len, "\n");
5457 if (j != 16)
5458 break;
5461 /* These are way too dangerous to advertise openly... */
5462 #if 0
5463 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5464 " (<offset> is 00-ff, <value> is 00-ff)\n");
5465 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5466 " (<offset> is 00-ff, <value> is 0-255)\n");
5467 #endif
5468 return len;
5471 static int ecdump_write(char *buf)
5473 char *cmd;
5474 int i, v;
5476 while ((cmd = next_cmd(&buf))) {
5477 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5478 /* i and v set */
5479 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5480 /* i and v set */
5481 } else
5482 return -EINVAL;
5483 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5484 if (!acpi_ec_write(i, v))
5485 return -EIO;
5486 } else
5487 return -EINVAL;
5490 return 0;
5493 static struct ibm_struct ecdump_driver_data = {
5494 .name = "ecdump",
5495 .read = ecdump_read,
5496 .write = ecdump_write,
5497 .flags.experimental = 1,
5500 /*************************************************************************
5501 * Backlight/brightness subdriver
5504 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5506 enum {
5507 TP_EC_BACKLIGHT = 0x31,
5509 /* TP_EC_BACKLIGHT bitmasks */
5510 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5511 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5512 TP_EC_BACKLIGHT_MAPSW = 0x20,
5515 static struct backlight_device *ibm_backlight_device;
5516 static int brightness_mode;
5517 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5519 static struct mutex brightness_mutex;
5522 * ThinkPads can read brightness from two places: EC 0x31, or
5523 * CMOS NVRAM byte 0x5E, bits 0-3.
5525 * EC 0x31 has the following layout
5526 * Bit 7: unknown function
5527 * Bit 6: unknown function
5528 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5529 * Bit 4: must be set to zero to avoid problems
5530 * Bit 3-0: backlight brightness level
5532 * brightness_get_raw returns status data in the EC 0x31 layout
5534 static int brightness_get_raw(int *status)
5536 u8 lec = 0, lcmos = 0, level = 0;
5538 if (brightness_mode & 1) {
5539 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5540 return -EIO;
5541 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5543 if (brightness_mode & 2) {
5544 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5545 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5546 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5547 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5548 level = lcmos;
5551 if (brightness_mode == 3) {
5552 *status = lec; /* Prefer EC, CMOS is just a backing store */
5553 lec &= TP_EC_BACKLIGHT_LVLMSK;
5554 if (lec == lcmos)
5555 tp_warned.bright_cmos_ec_unsync = 0;
5556 else {
5557 if (!tp_warned.bright_cmos_ec_unsync) {
5558 printk(TPACPI_ERR
5559 "CMOS NVRAM (%u) and EC (%u) do not "
5560 "agree on display brightness level\n",
5561 (unsigned int) lcmos,
5562 (unsigned int) lec);
5563 tp_warned.bright_cmos_ec_unsync = 1;
5565 return -EIO;
5567 } else {
5568 *status = level;
5571 return 0;
5574 /* May return EINTR which can always be mapped to ERESTARTSYS */
5575 static int brightness_set(int value)
5577 int cmos_cmd, inc, i, res;
5578 int current_value;
5579 int command_bits;
5581 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5582 value < 0)
5583 return -EINVAL;
5585 res = mutex_lock_killable(&brightness_mutex);
5586 if (res < 0)
5587 return res;
5589 res = brightness_get_raw(&current_value);
5590 if (res < 0)
5591 goto errout;
5593 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5594 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5596 cmos_cmd = value > current_value ?
5597 TP_CMOS_BRIGHTNESS_UP :
5598 TP_CMOS_BRIGHTNESS_DOWN;
5599 inc = (value > current_value)? 1 : -1;
5601 res = 0;
5602 for (i = current_value; i != value; i += inc) {
5603 if ((brightness_mode & 2) &&
5604 issue_thinkpad_cmos_command(cmos_cmd)) {
5605 res = -EIO;
5606 goto errout;
5608 if ((brightness_mode & 1) &&
5609 !acpi_ec_write(TP_EC_BACKLIGHT,
5610 (i + inc) | command_bits)) {
5611 res = -EIO;
5612 goto errout;;
5616 errout:
5617 mutex_unlock(&brightness_mutex);
5618 return res;
5621 /* sysfs backlight class ----------------------------------------------- */
5623 static int brightness_update_status(struct backlight_device *bd)
5625 /* it is the backlight class's job (caller) to handle
5626 * EINTR and other errors properly */
5627 return brightness_set(
5628 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5629 bd->props.power == FB_BLANK_UNBLANK) ?
5630 bd->props.brightness : 0);
5633 static int brightness_get(struct backlight_device *bd)
5635 int status, res;
5637 res = brightness_get_raw(&status);
5638 if (res < 0)
5639 return 0; /* FIXME: teach backlight about error handling */
5641 return status & TP_EC_BACKLIGHT_LVLMSK;
5644 static struct backlight_ops ibm_backlight_data = {
5645 .get_brightness = brightness_get,
5646 .update_status = brightness_update_status,
5649 /* --------------------------------------------------------------------- */
5651 static int __init brightness_init(struct ibm_init_struct *iibm)
5653 int b;
5655 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5657 mutex_init(&brightness_mutex);
5660 * We always attempt to detect acpi support, so as to switch
5661 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5662 * going to publish a backlight interface
5664 b = tpacpi_check_std_acpi_brightness_support();
5665 if (b > 0) {
5667 if (acpi_video_backlight_support()) {
5668 if (brightness_enable > 1) {
5669 printk(TPACPI_NOTICE
5670 "Standard ACPI backlight interface "
5671 "available, not loading native one.\n");
5672 return 1;
5673 } else if (brightness_enable == 1) {
5674 printk(TPACPI_NOTICE
5675 "Backlight control force enabled, even if standard "
5676 "ACPI backlight interface is available\n");
5678 } else {
5679 if (brightness_enable > 1) {
5680 printk(TPACPI_NOTICE
5681 "Standard ACPI backlight interface not "
5682 "available, thinkpad_acpi native "
5683 "brightness control enabled\n");
5688 if (!brightness_enable) {
5689 dbg_printk(TPACPI_DBG_INIT,
5690 "brightness support disabled by "
5691 "module parameter\n");
5692 return 1;
5695 if (b > 16) {
5696 printk(TPACPI_ERR
5697 "Unsupported brightness interface, "
5698 "please contact %s\n", TPACPI_MAIL);
5699 return 1;
5701 if (b == 16)
5702 tp_features.bright_16levels = 1;
5704 if (!brightness_mode) {
5705 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5706 brightness_mode = 2;
5707 else
5708 brightness_mode = 3;
5710 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5711 brightness_mode);
5714 if (brightness_mode > 3)
5715 return -EINVAL;
5717 if (brightness_get_raw(&b) < 0)
5718 return 1;
5720 if (tp_features.bright_16levels)
5721 printk(TPACPI_INFO
5722 "detected a 16-level brightness capable ThinkPad\n");
5724 ibm_backlight_device = backlight_device_register(
5725 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5726 &ibm_backlight_data);
5727 if (IS_ERR(ibm_backlight_device)) {
5728 printk(TPACPI_ERR "Could not register backlight device\n");
5729 return PTR_ERR(ibm_backlight_device);
5731 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5733 ibm_backlight_device->props.max_brightness =
5734 (tp_features.bright_16levels)? 15 : 7;
5735 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5736 backlight_update_status(ibm_backlight_device);
5738 return 0;
5741 static void brightness_exit(void)
5743 if (ibm_backlight_device) {
5744 vdbg_printk(TPACPI_DBG_EXIT,
5745 "calling backlight_device_unregister()\n");
5746 backlight_device_unregister(ibm_backlight_device);
5750 static int brightness_read(char *p)
5752 int len = 0;
5753 int level;
5755 level = brightness_get(NULL);
5756 if (level < 0) {
5757 len += sprintf(p + len, "level:\t\tunreadable\n");
5758 } else {
5759 len += sprintf(p + len, "level:\t\t%d\n", level);
5760 len += sprintf(p + len, "commands:\tup, down\n");
5761 len += sprintf(p + len, "commands:\tlevel <level>"
5762 " (<level> is 0-%d)\n",
5763 (tp_features.bright_16levels) ? 15 : 7);
5766 return len;
5769 static int brightness_write(char *buf)
5771 int level;
5772 int rc;
5773 char *cmd;
5774 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5776 level = brightness_get(NULL);
5777 if (level < 0)
5778 return level;
5780 while ((cmd = next_cmd(&buf))) {
5781 if (strlencmp(cmd, "up") == 0) {
5782 if (level < max_level)
5783 level++;
5784 } else if (strlencmp(cmd, "down") == 0) {
5785 if (level > 0)
5786 level--;
5787 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5788 level >= 0 && level <= max_level) {
5789 /* new level set */
5790 } else
5791 return -EINVAL;
5795 * Now we know what the final level should be, so we try to set it.
5796 * Doing it this way makes the syscall restartable in case of EINTR
5798 rc = brightness_set(level);
5799 return (rc == -EINTR)? ERESTARTSYS : rc;
5802 static struct ibm_struct brightness_driver_data = {
5803 .name = "brightness",
5804 .read = brightness_read,
5805 .write = brightness_write,
5806 .exit = brightness_exit,
5809 /*************************************************************************
5810 * Volume subdriver
5813 static int volume_offset = 0x30;
5815 static int volume_read(char *p)
5817 int len = 0;
5818 u8 level;
5820 if (!acpi_ec_read(volume_offset, &level)) {
5821 len += sprintf(p + len, "level:\t\tunreadable\n");
5822 } else {
5823 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5824 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5825 len += sprintf(p + len, "commands:\tup, down, mute\n");
5826 len += sprintf(p + len, "commands:\tlevel <level>"
5827 " (<level> is 0-15)\n");
5830 return len;
5833 static int volume_write(char *buf)
5835 int cmos_cmd, inc, i;
5836 u8 level, mute;
5837 int new_level, new_mute;
5838 char *cmd;
5840 while ((cmd = next_cmd(&buf))) {
5841 if (!acpi_ec_read(volume_offset, &level))
5842 return -EIO;
5843 new_mute = mute = level & 0x40;
5844 new_level = level = level & 0xf;
5846 if (strlencmp(cmd, "up") == 0) {
5847 if (mute)
5848 new_mute = 0;
5849 else
5850 new_level = level == 15 ? 15 : level + 1;
5851 } else if (strlencmp(cmd, "down") == 0) {
5852 if (mute)
5853 new_mute = 0;
5854 else
5855 new_level = level == 0 ? 0 : level - 1;
5856 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5857 new_level >= 0 && new_level <= 15) {
5858 /* new_level set */
5859 } else if (strlencmp(cmd, "mute") == 0) {
5860 new_mute = 0x40;
5861 } else
5862 return -EINVAL;
5864 if (new_level != level) {
5865 /* mute doesn't change */
5867 cmos_cmd = (new_level > level) ?
5868 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5869 inc = new_level > level ? 1 : -1;
5871 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5872 !acpi_ec_write(volume_offset, level)))
5873 return -EIO;
5875 for (i = level; i != new_level; i += inc)
5876 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5877 !acpi_ec_write(volume_offset, i + inc))
5878 return -EIO;
5880 if (mute &&
5881 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5882 !acpi_ec_write(volume_offset, new_level + mute))) {
5883 return -EIO;
5887 if (new_mute != mute) {
5888 /* level doesn't change */
5890 cmos_cmd = (new_mute) ?
5891 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5893 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5894 !acpi_ec_write(volume_offset, level + new_mute))
5895 return -EIO;
5899 return 0;
5902 static struct ibm_struct volume_driver_data = {
5903 .name = "volume",
5904 .read = volume_read,
5905 .write = volume_write,
5908 /*************************************************************************
5909 * Fan subdriver
5913 * FAN ACCESS MODES
5915 * TPACPI_FAN_RD_ACPI_GFAN:
5916 * ACPI GFAN method: returns fan level
5918 * see TPACPI_FAN_WR_ACPI_SFAN
5919 * EC 0x2f (HFSP) not available if GFAN exists
5921 * TPACPI_FAN_WR_ACPI_SFAN:
5922 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5924 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5925 * it for writing.
5927 * TPACPI_FAN_WR_TPEC:
5928 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5929 * Supported on almost all ThinkPads
5931 * Fan speed changes of any sort (including those caused by the
5932 * disengaged mode) are usually done slowly by the firmware as the
5933 * maximum ammount of fan duty cycle change per second seems to be
5934 * limited.
5936 * Reading is not available if GFAN exists.
5937 * Writing is not available if SFAN exists.
5939 * Bits
5940 * 7 automatic mode engaged;
5941 * (default operation mode of the ThinkPad)
5942 * fan level is ignored in this mode.
5943 * 6 full speed mode (takes precedence over bit 7);
5944 * not available on all thinkpads. May disable
5945 * the tachometer while the fan controller ramps up
5946 * the speed (which can take up to a few *minutes*).
5947 * Speeds up fan to 100% duty-cycle, which is far above
5948 * the standard RPM levels. It is not impossible that
5949 * it could cause hardware damage.
5950 * 5-3 unused in some models. Extra bits for fan level
5951 * in others, but still useless as all values above
5952 * 7 map to the same speed as level 7 in these models.
5953 * 2-0 fan level (0..7 usually)
5954 * 0x00 = stop
5955 * 0x07 = max (set when temperatures critical)
5956 * Some ThinkPads may have other levels, see
5957 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5959 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5960 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5961 * does so, its initial value is meaningless (0x07).
5963 * For firmware bugs, refer to:
5964 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5966 * ----
5968 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5969 * Main fan tachometer reading (in RPM)
5971 * This register is present on all ThinkPads with a new-style EC, and
5972 * it is known not to be present on the A21m/e, and T22, as there is
5973 * something else in offset 0x84 according to the ACPI DSDT. Other
5974 * ThinkPads from this same time period (and earlier) probably lack the
5975 * tachometer as well.
5977 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5978 * was never fixed by IBM to report the EC firmware version string
5979 * probably support the tachometer (like the early X models), so
5980 * detecting it is quite hard. We need more data to know for sure.
5982 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5983 * might result.
5985 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5986 * mode.
5988 * For firmware bugs, refer to:
5989 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5991 * TPACPI_FAN_WR_ACPI_FANS:
5992 * ThinkPad X31, X40, X41. Not available in the X60.
5994 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5995 * high speed. ACPI DSDT seems to map these three speeds to levels
5996 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5997 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5999 * The speeds are stored on handles
6000 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6002 * There are three default speed sets, acessible as handles:
6003 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6005 * ACPI DSDT switches which set is in use depending on various
6006 * factors.
6008 * TPACPI_FAN_WR_TPEC is also available and should be used to
6009 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6010 * but the ACPI tables just mention level 7.
6013 enum { /* Fan control constants */
6014 fan_status_offset = 0x2f, /* EC register 0x2f */
6015 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6016 * 0x84 must be read before 0x85 */
6018 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6019 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6021 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6024 enum fan_status_access_mode {
6025 TPACPI_FAN_NONE = 0, /* No fan status or control */
6026 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6027 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6030 enum fan_control_access_mode {
6031 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6032 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6033 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6034 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6037 enum fan_control_commands {
6038 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6039 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6040 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6041 * and also watchdog cmd */
6044 static int fan_control_allowed;
6046 static enum fan_status_access_mode fan_status_access_mode;
6047 static enum fan_control_access_mode fan_control_access_mode;
6048 static enum fan_control_commands fan_control_commands;
6050 static u8 fan_control_initial_status;
6051 static u8 fan_control_desired_level;
6052 static u8 fan_control_resume_level;
6053 static int fan_watchdog_maxinterval;
6055 static struct mutex fan_mutex;
6057 static void fan_watchdog_fire(struct work_struct *ignored);
6058 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6060 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6061 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6062 "\\FSPD", /* 600e/x, 770e, 770x */
6063 ); /* all others */
6064 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6065 "JFNS", /* 770x-JL */
6066 ); /* all others */
6069 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6070 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6071 * be in auto mode (0x80).
6073 * This is corrected by any write to HFSP either by the driver, or
6074 * by the firmware.
6076 * We assume 0x07 really means auto mode while this quirk is active,
6077 * as this is far more likely than the ThinkPad being in level 7,
6078 * which is only used by the firmware during thermal emergencies.
6081 static void fan_quirk1_detect(void)
6083 /* In some ThinkPads, neither the EC nor the ACPI
6084 * DSDT initialize the HFSP register, and it ends up
6085 * being initially set to 0x07 when it *could* be
6086 * either 0x07 or 0x80.
6088 * Enable for TP-1Y (T43), TP-78 (R51e),
6089 * TP-76 (R52), TP-70 (T43, R52), which are known
6090 * to be buggy. */
6091 if (fan_control_initial_status == 0x07) {
6092 switch (thinkpad_id.ec_model) {
6093 case 0x5931: /* TP-1Y */
6094 case 0x3837: /* TP-78 */
6095 case 0x3637: /* TP-76 */
6096 case 0x3037: /* TP-70 */
6097 printk(TPACPI_NOTICE
6098 "fan_init: initial fan status is unknown, "
6099 "assuming it is in auto mode\n");
6100 tp_features.fan_ctrl_status_undef = 1;
6106 static void fan_quirk1_handle(u8 *fan_status)
6108 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6109 if (*fan_status != fan_control_initial_status) {
6110 /* something changed the HFSP regisnter since
6111 * driver init time, so it is not undefined
6112 * anymore */
6113 tp_features.fan_ctrl_status_undef = 0;
6114 } else {
6115 /* Return most likely status. In fact, it
6116 * might be the only possible status */
6117 *fan_status = TP_EC_FAN_AUTO;
6123 * Call with fan_mutex held
6125 static void fan_update_desired_level(u8 status)
6127 if ((status &
6128 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6129 if (status > 7)
6130 fan_control_desired_level = 7;
6131 else
6132 fan_control_desired_level = status;
6136 static int fan_get_status(u8 *status)
6138 u8 s;
6140 /* TODO:
6141 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6143 switch (fan_status_access_mode) {
6144 case TPACPI_FAN_RD_ACPI_GFAN:
6145 /* 570, 600e/x, 770e, 770x */
6147 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6148 return -EIO;
6150 if (likely(status))
6151 *status = s & 0x07;
6153 break;
6155 case TPACPI_FAN_RD_TPEC:
6156 /* all except 570, 600e/x, 770e, 770x */
6157 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6158 return -EIO;
6160 if (likely(status)) {
6161 *status = s;
6162 fan_quirk1_handle(status);
6165 break;
6167 default:
6168 return -ENXIO;
6171 return 0;
6174 static int fan_get_status_safe(u8 *status)
6176 int rc;
6177 u8 s;
6179 if (mutex_lock_killable(&fan_mutex))
6180 return -ERESTARTSYS;
6181 rc = fan_get_status(&s);
6182 if (!rc)
6183 fan_update_desired_level(s);
6184 mutex_unlock(&fan_mutex);
6186 if (status)
6187 *status = s;
6189 return rc;
6192 static int fan_get_speed(unsigned int *speed)
6194 u8 hi, lo;
6196 switch (fan_status_access_mode) {
6197 case TPACPI_FAN_RD_TPEC:
6198 /* all except 570, 600e/x, 770e, 770x */
6199 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6200 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6201 return -EIO;
6203 if (likely(speed))
6204 *speed = (hi << 8) | lo;
6206 break;
6208 default:
6209 return -ENXIO;
6212 return 0;
6215 static int fan_set_level(int level)
6217 if (!fan_control_allowed)
6218 return -EPERM;
6220 switch (fan_control_access_mode) {
6221 case TPACPI_FAN_WR_ACPI_SFAN:
6222 if (level >= 0 && level <= 7) {
6223 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6224 return -EIO;
6225 } else
6226 return -EINVAL;
6227 break;
6229 case TPACPI_FAN_WR_ACPI_FANS:
6230 case TPACPI_FAN_WR_TPEC:
6231 if (!(level & TP_EC_FAN_AUTO) &&
6232 !(level & TP_EC_FAN_FULLSPEED) &&
6233 ((level < 0) || (level > 7)))
6234 return -EINVAL;
6236 /* safety net should the EC not support AUTO
6237 * or FULLSPEED mode bits and just ignore them */
6238 if (level & TP_EC_FAN_FULLSPEED)
6239 level |= 7; /* safety min speed 7 */
6240 else if (level & TP_EC_FAN_AUTO)
6241 level |= 4; /* safety min speed 4 */
6243 if (!acpi_ec_write(fan_status_offset, level))
6244 return -EIO;
6245 else
6246 tp_features.fan_ctrl_status_undef = 0;
6247 break;
6249 default:
6250 return -ENXIO;
6252 return 0;
6255 static int fan_set_level_safe(int level)
6257 int rc;
6259 if (!fan_control_allowed)
6260 return -EPERM;
6262 if (mutex_lock_killable(&fan_mutex))
6263 return -ERESTARTSYS;
6265 if (level == TPACPI_FAN_LAST_LEVEL)
6266 level = fan_control_desired_level;
6268 rc = fan_set_level(level);
6269 if (!rc)
6270 fan_update_desired_level(level);
6272 mutex_unlock(&fan_mutex);
6273 return rc;
6276 static int fan_set_enable(void)
6278 u8 s;
6279 int rc;
6281 if (!fan_control_allowed)
6282 return -EPERM;
6284 if (mutex_lock_killable(&fan_mutex))
6285 return -ERESTARTSYS;
6287 switch (fan_control_access_mode) {
6288 case TPACPI_FAN_WR_ACPI_FANS:
6289 case TPACPI_FAN_WR_TPEC:
6290 rc = fan_get_status(&s);
6291 if (rc < 0)
6292 break;
6294 /* Don't go out of emergency fan mode */
6295 if (s != 7) {
6296 s &= 0x07;
6297 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6300 if (!acpi_ec_write(fan_status_offset, s))
6301 rc = -EIO;
6302 else {
6303 tp_features.fan_ctrl_status_undef = 0;
6304 rc = 0;
6306 break;
6308 case TPACPI_FAN_WR_ACPI_SFAN:
6309 rc = fan_get_status(&s);
6310 if (rc < 0)
6311 break;
6313 s &= 0x07;
6315 /* Set fan to at least level 4 */
6316 s |= 4;
6318 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6319 rc = -EIO;
6320 else
6321 rc = 0;
6322 break;
6324 default:
6325 rc = -ENXIO;
6328 mutex_unlock(&fan_mutex);
6329 return rc;
6332 static int fan_set_disable(void)
6334 int rc;
6336 if (!fan_control_allowed)
6337 return -EPERM;
6339 if (mutex_lock_killable(&fan_mutex))
6340 return -ERESTARTSYS;
6342 rc = 0;
6343 switch (fan_control_access_mode) {
6344 case TPACPI_FAN_WR_ACPI_FANS:
6345 case TPACPI_FAN_WR_TPEC:
6346 if (!acpi_ec_write(fan_status_offset, 0x00))
6347 rc = -EIO;
6348 else {
6349 fan_control_desired_level = 0;
6350 tp_features.fan_ctrl_status_undef = 0;
6352 break;
6354 case TPACPI_FAN_WR_ACPI_SFAN:
6355 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6356 rc = -EIO;
6357 else
6358 fan_control_desired_level = 0;
6359 break;
6361 default:
6362 rc = -ENXIO;
6366 mutex_unlock(&fan_mutex);
6367 return rc;
6370 static int fan_set_speed(int speed)
6372 int rc;
6374 if (!fan_control_allowed)
6375 return -EPERM;
6377 if (mutex_lock_killable(&fan_mutex))
6378 return -ERESTARTSYS;
6380 rc = 0;
6381 switch (fan_control_access_mode) {
6382 case TPACPI_FAN_WR_ACPI_FANS:
6383 if (speed >= 0 && speed <= 65535) {
6384 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6385 speed, speed, speed))
6386 rc = -EIO;
6387 } else
6388 rc = -EINVAL;
6389 break;
6391 default:
6392 rc = -ENXIO;
6395 mutex_unlock(&fan_mutex);
6396 return rc;
6399 static void fan_watchdog_reset(void)
6401 static int fan_watchdog_active;
6403 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6404 return;
6406 if (fan_watchdog_active)
6407 cancel_delayed_work(&fan_watchdog_task);
6409 if (fan_watchdog_maxinterval > 0 &&
6410 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6411 fan_watchdog_active = 1;
6412 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6413 msecs_to_jiffies(fan_watchdog_maxinterval
6414 * 1000))) {
6415 printk(TPACPI_ERR
6416 "failed to queue the fan watchdog, "
6417 "watchdog will not trigger\n");
6419 } else
6420 fan_watchdog_active = 0;
6423 static void fan_watchdog_fire(struct work_struct *ignored)
6425 int rc;
6427 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6428 return;
6430 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6431 rc = fan_set_enable();
6432 if (rc < 0) {
6433 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6434 "will try again later...\n", -rc);
6435 /* reschedule for later */
6436 fan_watchdog_reset();
6441 * SYSFS fan layout: hwmon compatible (device)
6443 * pwm*_enable:
6444 * 0: "disengaged" mode
6445 * 1: manual mode
6446 * 2: native EC "auto" mode (recommended, hardware default)
6448 * pwm*: set speed in manual mode, ignored otherwise.
6449 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6450 * interpolation.
6452 * fan*_input: tachometer reading, RPM
6455 * SYSFS fan layout: extensions
6457 * fan_watchdog (driver):
6458 * fan watchdog interval in seconds, 0 disables (default), max 120
6461 /* sysfs fan pwm1_enable ----------------------------------------------- */
6462 static ssize_t fan_pwm1_enable_show(struct device *dev,
6463 struct device_attribute *attr,
6464 char *buf)
6466 int res, mode;
6467 u8 status;
6469 res = fan_get_status_safe(&status);
6470 if (res)
6471 return res;
6473 if (status & TP_EC_FAN_FULLSPEED) {
6474 mode = 0;
6475 } else if (status & TP_EC_FAN_AUTO) {
6476 mode = 2;
6477 } else
6478 mode = 1;
6480 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6483 static ssize_t fan_pwm1_enable_store(struct device *dev,
6484 struct device_attribute *attr,
6485 const char *buf, size_t count)
6487 unsigned long t;
6488 int res, level;
6490 if (parse_strtoul(buf, 2, &t))
6491 return -EINVAL;
6493 switch (t) {
6494 case 0:
6495 level = TP_EC_FAN_FULLSPEED;
6496 break;
6497 case 1:
6498 level = TPACPI_FAN_LAST_LEVEL;
6499 break;
6500 case 2:
6501 level = TP_EC_FAN_AUTO;
6502 break;
6503 case 3:
6504 /* reserved for software-controlled auto mode */
6505 return -ENOSYS;
6506 default:
6507 return -EINVAL;
6510 res = fan_set_level_safe(level);
6511 if (res == -ENXIO)
6512 return -EINVAL;
6513 else if (res < 0)
6514 return res;
6516 fan_watchdog_reset();
6518 return count;
6521 static struct device_attribute dev_attr_fan_pwm1_enable =
6522 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6523 fan_pwm1_enable_show, fan_pwm1_enable_store);
6525 /* sysfs fan pwm1 ------------------------------------------------------ */
6526 static ssize_t fan_pwm1_show(struct device *dev,
6527 struct device_attribute *attr,
6528 char *buf)
6530 int res;
6531 u8 status;
6533 res = fan_get_status_safe(&status);
6534 if (res)
6535 return res;
6537 if ((status &
6538 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6539 status = fan_control_desired_level;
6541 if (status > 7)
6542 status = 7;
6544 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6547 static ssize_t fan_pwm1_store(struct device *dev,
6548 struct device_attribute *attr,
6549 const char *buf, size_t count)
6551 unsigned long s;
6552 int rc;
6553 u8 status, newlevel;
6555 if (parse_strtoul(buf, 255, &s))
6556 return -EINVAL;
6558 /* scale down from 0-255 to 0-7 */
6559 newlevel = (s >> 5) & 0x07;
6561 if (mutex_lock_killable(&fan_mutex))
6562 return -ERESTARTSYS;
6564 rc = fan_get_status(&status);
6565 if (!rc && (status &
6566 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6567 rc = fan_set_level(newlevel);
6568 if (rc == -ENXIO)
6569 rc = -EINVAL;
6570 else if (!rc) {
6571 fan_update_desired_level(newlevel);
6572 fan_watchdog_reset();
6576 mutex_unlock(&fan_mutex);
6577 return (rc)? rc : count;
6580 static struct device_attribute dev_attr_fan_pwm1 =
6581 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6582 fan_pwm1_show, fan_pwm1_store);
6584 /* sysfs fan fan1_input ------------------------------------------------ */
6585 static ssize_t fan_fan1_input_show(struct device *dev,
6586 struct device_attribute *attr,
6587 char *buf)
6589 int res;
6590 unsigned int speed;
6592 res = fan_get_speed(&speed);
6593 if (res < 0)
6594 return res;
6596 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6599 static struct device_attribute dev_attr_fan_fan1_input =
6600 __ATTR(fan1_input, S_IRUGO,
6601 fan_fan1_input_show, NULL);
6603 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6604 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6605 char *buf)
6607 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6610 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6611 const char *buf, size_t count)
6613 unsigned long t;
6615 if (parse_strtoul(buf, 120, &t))
6616 return -EINVAL;
6618 if (!fan_control_allowed)
6619 return -EPERM;
6621 fan_watchdog_maxinterval = t;
6622 fan_watchdog_reset();
6624 return count;
6627 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6628 fan_fan_watchdog_show, fan_fan_watchdog_store);
6630 /* --------------------------------------------------------------------- */
6631 static struct attribute *fan_attributes[] = {
6632 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6633 &dev_attr_fan_fan1_input.attr,
6634 NULL
6637 static const struct attribute_group fan_attr_group = {
6638 .attrs = fan_attributes,
6641 static int __init fan_init(struct ibm_init_struct *iibm)
6643 int rc;
6645 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6647 mutex_init(&fan_mutex);
6648 fan_status_access_mode = TPACPI_FAN_NONE;
6649 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6650 fan_control_commands = 0;
6651 fan_watchdog_maxinterval = 0;
6652 tp_features.fan_ctrl_status_undef = 0;
6653 fan_control_desired_level = 7;
6655 TPACPI_ACPIHANDLE_INIT(fans);
6656 TPACPI_ACPIHANDLE_INIT(gfan);
6657 TPACPI_ACPIHANDLE_INIT(sfan);
6659 if (gfan_handle) {
6660 /* 570, 600e/x, 770e, 770x */
6661 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6662 } else {
6663 /* all other ThinkPads: note that even old-style
6664 * ThinkPad ECs supports the fan control register */
6665 if (likely(acpi_ec_read(fan_status_offset,
6666 &fan_control_initial_status))) {
6667 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6668 fan_quirk1_detect();
6669 } else {
6670 printk(TPACPI_ERR
6671 "ThinkPad ACPI EC access misbehaving, "
6672 "fan status and control unavailable\n");
6673 return 1;
6677 if (sfan_handle) {
6678 /* 570, 770x-JL */
6679 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6680 fan_control_commands |=
6681 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6682 } else {
6683 if (!gfan_handle) {
6684 /* gfan without sfan means no fan control */
6685 /* all other models implement TP EC 0x2f control */
6687 if (fans_handle) {
6688 /* X31, X40, X41 */
6689 fan_control_access_mode =
6690 TPACPI_FAN_WR_ACPI_FANS;
6691 fan_control_commands |=
6692 TPACPI_FAN_CMD_SPEED |
6693 TPACPI_FAN_CMD_LEVEL |
6694 TPACPI_FAN_CMD_ENABLE;
6695 } else {
6696 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6697 fan_control_commands |=
6698 TPACPI_FAN_CMD_LEVEL |
6699 TPACPI_FAN_CMD_ENABLE;
6704 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6705 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6706 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6707 fan_status_access_mode, fan_control_access_mode);
6709 /* fan control master switch */
6710 if (!fan_control_allowed) {
6711 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6712 fan_control_commands = 0;
6713 dbg_printk(TPACPI_DBG_INIT,
6714 "fan control features disabled by parameter\n");
6717 /* update fan_control_desired_level */
6718 if (fan_status_access_mode != TPACPI_FAN_NONE)
6719 fan_get_status_safe(NULL);
6721 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6722 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6723 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6724 &fan_attr_group);
6725 if (rc < 0)
6726 return rc;
6728 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6729 &driver_attr_fan_watchdog);
6730 if (rc < 0) {
6731 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6732 &fan_attr_group);
6733 return rc;
6735 return 0;
6736 } else
6737 return 1;
6740 static void fan_exit(void)
6742 vdbg_printk(TPACPI_DBG_EXIT,
6743 "cancelling any pending fan watchdog tasks\n");
6745 /* FIXME: can we really do this unconditionally? */
6746 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6747 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6748 &driver_attr_fan_watchdog);
6750 cancel_delayed_work(&fan_watchdog_task);
6751 flush_workqueue(tpacpi_wq);
6754 static void fan_suspend(pm_message_t state)
6756 int rc;
6758 if (!fan_control_allowed)
6759 return;
6761 /* Store fan status in cache */
6762 fan_control_resume_level = 0;
6763 rc = fan_get_status_safe(&fan_control_resume_level);
6764 if (rc < 0)
6765 printk(TPACPI_NOTICE
6766 "failed to read fan level for later "
6767 "restore during resume: %d\n", rc);
6769 /* if it is undefined, don't attempt to restore it.
6770 * KEEP THIS LAST */
6771 if (tp_features.fan_ctrl_status_undef)
6772 fan_control_resume_level = 0;
6775 static void fan_resume(void)
6777 u8 current_level = 7;
6778 bool do_set = false;
6779 int rc;
6781 /* DSDT *always* updates status on resume */
6782 tp_features.fan_ctrl_status_undef = 0;
6784 if (!fan_control_allowed ||
6785 !fan_control_resume_level ||
6786 (fan_get_status_safe(&current_level) < 0))
6787 return;
6789 switch (fan_control_access_mode) {
6790 case TPACPI_FAN_WR_ACPI_SFAN:
6791 /* never decrease fan level */
6792 do_set = (fan_control_resume_level > current_level);
6793 break;
6794 case TPACPI_FAN_WR_ACPI_FANS:
6795 case TPACPI_FAN_WR_TPEC:
6796 /* never decrease fan level, scale is:
6797 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6799 * We expect the firmware to set either 7 or AUTO, but we
6800 * handle FULLSPEED out of paranoia.
6802 * So, we can safely only restore FULLSPEED or 7, anything
6803 * else could slow the fan. Restoring AUTO is useless, at
6804 * best that's exactly what the DSDT already set (it is the
6805 * slower it uses).
6807 * Always keep in mind that the DSDT *will* have set the
6808 * fans to what the vendor supposes is the best level. We
6809 * muck with it only to speed the fan up.
6811 if (fan_control_resume_level != 7 &&
6812 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6813 return;
6814 else
6815 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6816 (current_level != fan_control_resume_level);
6817 break;
6818 default:
6819 return;
6821 if (do_set) {
6822 printk(TPACPI_NOTICE
6823 "restoring fan level to 0x%02x\n",
6824 fan_control_resume_level);
6825 rc = fan_set_level_safe(fan_control_resume_level);
6826 if (rc < 0)
6827 printk(TPACPI_NOTICE
6828 "failed to restore fan level: %d\n", rc);
6832 static int fan_read(char *p)
6834 int len = 0;
6835 int rc;
6836 u8 status;
6837 unsigned int speed = 0;
6839 switch (fan_status_access_mode) {
6840 case TPACPI_FAN_RD_ACPI_GFAN:
6841 /* 570, 600e/x, 770e, 770x */
6842 rc = fan_get_status_safe(&status);
6843 if (rc < 0)
6844 return rc;
6846 len += sprintf(p + len, "status:\t\t%s\n"
6847 "level:\t\t%d\n",
6848 (status != 0) ? "enabled" : "disabled", status);
6849 break;
6851 case TPACPI_FAN_RD_TPEC:
6852 /* all except 570, 600e/x, 770e, 770x */
6853 rc = fan_get_status_safe(&status);
6854 if (rc < 0)
6855 return rc;
6857 len += sprintf(p + len, "status:\t\t%s\n",
6858 (status != 0) ? "enabled" : "disabled");
6860 rc = fan_get_speed(&speed);
6861 if (rc < 0)
6862 return rc;
6864 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6866 if (status & TP_EC_FAN_FULLSPEED)
6867 /* Disengaged mode takes precedence */
6868 len += sprintf(p + len, "level:\t\tdisengaged\n");
6869 else if (status & TP_EC_FAN_AUTO)
6870 len += sprintf(p + len, "level:\t\tauto\n");
6871 else
6872 len += sprintf(p + len, "level:\t\t%d\n", status);
6873 break;
6875 case TPACPI_FAN_NONE:
6876 default:
6877 len += sprintf(p + len, "status:\t\tnot supported\n");
6880 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6881 len += sprintf(p + len, "commands:\tlevel <level>");
6883 switch (fan_control_access_mode) {
6884 case TPACPI_FAN_WR_ACPI_SFAN:
6885 len += sprintf(p + len, " (<level> is 0-7)\n");
6886 break;
6888 default:
6889 len += sprintf(p + len, " (<level> is 0-7, "
6890 "auto, disengaged, full-speed)\n");
6891 break;
6895 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6896 len += sprintf(p + len, "commands:\tenable, disable\n"
6897 "commands:\twatchdog <timeout> (<timeout> "
6898 "is 0 (off), 1-120 (seconds))\n");
6900 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6901 len += sprintf(p + len, "commands:\tspeed <speed>"
6902 " (<speed> is 0-65535)\n");
6904 return len;
6907 static int fan_write_cmd_level(const char *cmd, int *rc)
6909 int level;
6911 if (strlencmp(cmd, "level auto") == 0)
6912 level = TP_EC_FAN_AUTO;
6913 else if ((strlencmp(cmd, "level disengaged") == 0) |
6914 (strlencmp(cmd, "level full-speed") == 0))
6915 level = TP_EC_FAN_FULLSPEED;
6916 else if (sscanf(cmd, "level %d", &level) != 1)
6917 return 0;
6919 *rc = fan_set_level_safe(level);
6920 if (*rc == -ENXIO)
6921 printk(TPACPI_ERR "level command accepted for unsupported "
6922 "access mode %d", fan_control_access_mode);
6924 return 1;
6927 static int fan_write_cmd_enable(const char *cmd, int *rc)
6929 if (strlencmp(cmd, "enable") != 0)
6930 return 0;
6932 *rc = fan_set_enable();
6933 if (*rc == -ENXIO)
6934 printk(TPACPI_ERR "enable command accepted for unsupported "
6935 "access mode %d", fan_control_access_mode);
6937 return 1;
6940 static int fan_write_cmd_disable(const char *cmd, int *rc)
6942 if (strlencmp(cmd, "disable") != 0)
6943 return 0;
6945 *rc = fan_set_disable();
6946 if (*rc == -ENXIO)
6947 printk(TPACPI_ERR "disable command accepted for unsupported "
6948 "access mode %d", fan_control_access_mode);
6950 return 1;
6953 static int fan_write_cmd_speed(const char *cmd, int *rc)
6955 int speed;
6957 /* TODO:
6958 * Support speed <low> <medium> <high> ? */
6960 if (sscanf(cmd, "speed %d", &speed) != 1)
6961 return 0;
6963 *rc = fan_set_speed(speed);
6964 if (*rc == -ENXIO)
6965 printk(TPACPI_ERR "speed command accepted for unsupported "
6966 "access mode %d", fan_control_access_mode);
6968 return 1;
6971 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6973 int interval;
6975 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6976 return 0;
6978 if (interval < 0 || interval > 120)
6979 *rc = -EINVAL;
6980 else
6981 fan_watchdog_maxinterval = interval;
6983 return 1;
6986 static int fan_write(char *buf)
6988 char *cmd;
6989 int rc = 0;
6991 while (!rc && (cmd = next_cmd(&buf))) {
6992 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6993 fan_write_cmd_level(cmd, &rc)) &&
6994 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6995 (fan_write_cmd_enable(cmd, &rc) ||
6996 fan_write_cmd_disable(cmd, &rc) ||
6997 fan_write_cmd_watchdog(cmd, &rc))) &&
6998 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6999 fan_write_cmd_speed(cmd, &rc))
7001 rc = -EINVAL;
7002 else if (!rc)
7003 fan_watchdog_reset();
7006 return rc;
7009 static struct ibm_struct fan_driver_data = {
7010 .name = "fan",
7011 .read = fan_read,
7012 .write = fan_write,
7013 .exit = fan_exit,
7014 .suspend = fan_suspend,
7015 .resume = fan_resume,
7018 /****************************************************************************
7019 ****************************************************************************
7021 * Infrastructure
7023 ****************************************************************************
7024 ****************************************************************************/
7026 /* sysfs name ---------------------------------------------------------- */
7027 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7028 struct device_attribute *attr,
7029 char *buf)
7031 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7034 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7035 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7037 /* --------------------------------------------------------------------- */
7039 /* /proc support */
7040 static struct proc_dir_entry *proc_dir;
7043 * Module and infrastructure proble, init and exit handling
7046 static int force_load;
7048 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7049 static const char * __init str_supported(int is_supported)
7051 static char text_unsupported[] __initdata = "not supported";
7053 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7055 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7057 static void ibm_exit(struct ibm_struct *ibm)
7059 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7061 list_del_init(&ibm->all_drivers);
7063 if (ibm->flags.acpi_notify_installed) {
7064 dbg_printk(TPACPI_DBG_EXIT,
7065 "%s: acpi_remove_notify_handler\n", ibm->name);
7066 BUG_ON(!ibm->acpi);
7067 acpi_remove_notify_handler(*ibm->acpi->handle,
7068 ibm->acpi->type,
7069 dispatch_acpi_notify);
7070 ibm->flags.acpi_notify_installed = 0;
7071 ibm->flags.acpi_notify_installed = 0;
7074 if (ibm->flags.proc_created) {
7075 dbg_printk(TPACPI_DBG_EXIT,
7076 "%s: remove_proc_entry\n", ibm->name);
7077 remove_proc_entry(ibm->name, proc_dir);
7078 ibm->flags.proc_created = 0;
7081 if (ibm->flags.acpi_driver_registered) {
7082 dbg_printk(TPACPI_DBG_EXIT,
7083 "%s: acpi_bus_unregister_driver\n", ibm->name);
7084 BUG_ON(!ibm->acpi);
7085 acpi_bus_unregister_driver(ibm->acpi->driver);
7086 kfree(ibm->acpi->driver);
7087 ibm->acpi->driver = NULL;
7088 ibm->flags.acpi_driver_registered = 0;
7091 if (ibm->flags.init_called && ibm->exit) {
7092 ibm->exit();
7093 ibm->flags.init_called = 0;
7096 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7099 static int __init ibm_init(struct ibm_init_struct *iibm)
7101 int ret;
7102 struct ibm_struct *ibm = iibm->data;
7103 struct proc_dir_entry *entry;
7105 BUG_ON(ibm == NULL);
7107 INIT_LIST_HEAD(&ibm->all_drivers);
7109 if (ibm->flags.experimental && !experimental)
7110 return 0;
7112 dbg_printk(TPACPI_DBG_INIT,
7113 "probing for %s\n", ibm->name);
7115 if (iibm->init) {
7116 ret = iibm->init(iibm);
7117 if (ret > 0)
7118 return 0; /* probe failed */
7119 if (ret)
7120 return ret;
7122 ibm->flags.init_called = 1;
7125 if (ibm->acpi) {
7126 if (ibm->acpi->hid) {
7127 ret = register_tpacpi_subdriver(ibm);
7128 if (ret)
7129 goto err_out;
7132 if (ibm->acpi->notify) {
7133 ret = setup_acpi_notify(ibm);
7134 if (ret == -ENODEV) {
7135 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7136 ibm->name);
7137 ret = 0;
7138 goto err_out;
7140 if (ret < 0)
7141 goto err_out;
7145 dbg_printk(TPACPI_DBG_INIT,
7146 "%s installed\n", ibm->name);
7148 if (ibm->read) {
7149 entry = create_proc_entry(ibm->name,
7150 S_IFREG | S_IRUGO | S_IWUSR,
7151 proc_dir);
7152 if (!entry) {
7153 printk(TPACPI_ERR "unable to create proc entry %s\n",
7154 ibm->name);
7155 ret = -ENODEV;
7156 goto err_out;
7158 entry->owner = THIS_MODULE;
7159 entry->data = ibm;
7160 entry->read_proc = &dispatch_procfs_read;
7161 if (ibm->write)
7162 entry->write_proc = &dispatch_procfs_write;
7163 ibm->flags.proc_created = 1;
7166 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7168 return 0;
7170 err_out:
7171 dbg_printk(TPACPI_DBG_INIT,
7172 "%s: at error exit path with result %d\n",
7173 ibm->name, ret);
7175 ibm_exit(ibm);
7176 return (ret < 0)? ret : 0;
7179 /* Probing */
7181 /* returns 0 - probe ok, or < 0 - probe error.
7182 * Probe ok doesn't mean thinkpad found.
7183 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7184 static int __must_check __init get_thinkpad_model_data(
7185 struct thinkpad_id_data *tp)
7187 const struct dmi_device *dev = NULL;
7188 char ec_fw_string[18];
7189 char const *s;
7191 if (!tp)
7192 return -EINVAL;
7194 memset(tp, 0, sizeof(*tp));
7196 if (dmi_name_in_vendors("IBM"))
7197 tp->vendor = PCI_VENDOR_ID_IBM;
7198 else if (dmi_name_in_vendors("LENOVO"))
7199 tp->vendor = PCI_VENDOR_ID_LENOVO;
7200 else
7201 return 0;
7203 s = dmi_get_system_info(DMI_BIOS_VERSION);
7204 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7205 if (s && !tp->bios_version_str)
7206 return -ENOMEM;
7207 if (!tp->bios_version_str)
7208 return 0;
7209 tp->bios_model = tp->bios_version_str[0]
7210 | (tp->bios_version_str[1] << 8);
7213 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7214 * X32 or newer, all Z series; Some models must have an
7215 * up-to-date BIOS or they will not be detected.
7217 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7219 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7220 if (sscanf(dev->name,
7221 "IBM ThinkPad Embedded Controller -[%17c",
7222 ec_fw_string) == 1) {
7223 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7224 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7226 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7227 if (!tp->ec_version_str)
7228 return -ENOMEM;
7229 tp->ec_model = ec_fw_string[0]
7230 | (ec_fw_string[1] << 8);
7231 break;
7235 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7236 if (s && !strnicmp(s, "ThinkPad", 8)) {
7237 tp->model_str = kstrdup(s, GFP_KERNEL);
7238 if (!tp->model_str)
7239 return -ENOMEM;
7242 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7243 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7244 if (s && !tp->nummodel_str)
7245 return -ENOMEM;
7247 return 0;
7250 static int __init probe_for_thinkpad(void)
7252 int is_thinkpad;
7254 if (acpi_disabled)
7255 return -ENODEV;
7258 * Non-ancient models have better DMI tagging, but very old models
7259 * don't.
7261 is_thinkpad = (thinkpad_id.model_str != NULL);
7263 /* ec is required because many other handles are relative to it */
7264 TPACPI_ACPIHANDLE_INIT(ec);
7265 if (!ec_handle) {
7266 if (is_thinkpad)
7267 printk(TPACPI_ERR
7268 "Not yet supported ThinkPad detected!\n");
7269 return -ENODEV;
7273 * Risks a regression on very old machines, but reduces potential
7274 * false positives a damn great deal
7276 if (!is_thinkpad)
7277 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7279 if (!is_thinkpad && !force_load)
7280 return -ENODEV;
7282 return 0;
7286 /* Module init, exit, parameters */
7288 static struct ibm_init_struct ibms_init[] __initdata = {
7290 .init = thinkpad_acpi_driver_init,
7291 .data = &thinkpad_acpi_driver_data,
7294 .init = hotkey_init,
7295 .data = &hotkey_driver_data,
7298 .init = bluetooth_init,
7299 .data = &bluetooth_driver_data,
7302 .init = wan_init,
7303 .data = &wan_driver_data,
7306 .init = uwb_init,
7307 .data = &uwb_driver_data,
7309 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7311 .init = video_init,
7312 .data = &video_driver_data,
7314 #endif
7316 .init = light_init,
7317 .data = &light_driver_data,
7319 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7321 .init = dock_init,
7322 .data = &dock_driver_data[0],
7325 .init = dock_init2,
7326 .data = &dock_driver_data[1],
7328 #endif
7329 #ifdef CONFIG_THINKPAD_ACPI_BAY
7331 .init = bay_init,
7332 .data = &bay_driver_data,
7334 #endif
7336 .init = cmos_init,
7337 .data = &cmos_driver_data,
7340 .init = led_init,
7341 .data = &led_driver_data,
7344 .init = beep_init,
7345 .data = &beep_driver_data,
7348 .init = thermal_init,
7349 .data = &thermal_driver_data,
7352 .data = &ecdump_driver_data,
7355 .init = brightness_init,
7356 .data = &brightness_driver_data,
7359 .data = &volume_driver_data,
7362 .init = fan_init,
7363 .data = &fan_driver_data,
7367 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7369 unsigned int i;
7370 struct ibm_struct *ibm;
7372 if (!kp || !kp->name || !val)
7373 return -EINVAL;
7375 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7376 ibm = ibms_init[i].data;
7377 WARN_ON(ibm == NULL);
7379 if (!ibm || !ibm->name)
7380 continue;
7382 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7383 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7384 return -ENOSPC;
7385 strcpy(ibms_init[i].param, val);
7386 strcat(ibms_init[i].param, ",");
7387 return 0;
7391 return -EINVAL;
7394 module_param(experimental, int, 0);
7395 MODULE_PARM_DESC(experimental,
7396 "Enables experimental features when non-zero");
7398 module_param_named(debug, dbg_level, uint, 0);
7399 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7401 module_param(force_load, bool, 0);
7402 MODULE_PARM_DESC(force_load,
7403 "Attempts to load the driver even on a "
7404 "mis-identified ThinkPad when true");
7406 module_param_named(fan_control, fan_control_allowed, bool, 0);
7407 MODULE_PARM_DESC(fan_control,
7408 "Enables setting fan parameters features when true");
7410 module_param_named(brightness_mode, brightness_mode, int, 0);
7411 MODULE_PARM_DESC(brightness_mode,
7412 "Selects brightness control strategy: "
7413 "0=auto, 1=EC, 2=CMOS, 3=both");
7415 module_param(brightness_enable, uint, 0);
7416 MODULE_PARM_DESC(brightness_enable,
7417 "Enables backlight control when 1, disables when 0");
7419 module_param(hotkey_report_mode, uint, 0);
7420 MODULE_PARM_DESC(hotkey_report_mode,
7421 "used for backwards compatibility with userspace, "
7422 "see documentation");
7424 #define TPACPI_PARAM(feature) \
7425 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7426 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7427 "at module load, see documentation")
7429 TPACPI_PARAM(hotkey);
7430 TPACPI_PARAM(bluetooth);
7431 TPACPI_PARAM(video);
7432 TPACPI_PARAM(light);
7433 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7434 TPACPI_PARAM(dock);
7435 #endif
7436 #ifdef CONFIG_THINKPAD_ACPI_BAY
7437 TPACPI_PARAM(bay);
7438 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7439 TPACPI_PARAM(cmos);
7440 TPACPI_PARAM(led);
7441 TPACPI_PARAM(beep);
7442 TPACPI_PARAM(ecdump);
7443 TPACPI_PARAM(brightness);
7444 TPACPI_PARAM(volume);
7445 TPACPI_PARAM(fan);
7447 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7448 module_param(dbg_wlswemul, uint, 0);
7449 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7450 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7451 MODULE_PARM_DESC(wlsw_state,
7452 "Initial state of the emulated WLSW switch");
7454 module_param(dbg_bluetoothemul, uint, 0);
7455 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7456 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7457 MODULE_PARM_DESC(bluetooth_state,
7458 "Initial state of the emulated bluetooth switch");
7460 module_param(dbg_wwanemul, uint, 0);
7461 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7462 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7463 MODULE_PARM_DESC(wwan_state,
7464 "Initial state of the emulated WWAN switch");
7466 module_param(dbg_uwbemul, uint, 0);
7467 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7468 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7469 MODULE_PARM_DESC(uwb_state,
7470 "Initial state of the emulated UWB switch");
7471 #endif
7473 static void thinkpad_acpi_module_exit(void)
7475 struct ibm_struct *ibm, *itmp;
7477 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7479 list_for_each_entry_safe_reverse(ibm, itmp,
7480 &tpacpi_all_drivers,
7481 all_drivers) {
7482 ibm_exit(ibm);
7485 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7487 if (tpacpi_inputdev) {
7488 if (tp_features.input_device_registered)
7489 input_unregister_device(tpacpi_inputdev);
7490 else
7491 input_free_device(tpacpi_inputdev);
7494 if (tpacpi_hwmon)
7495 hwmon_device_unregister(tpacpi_hwmon);
7497 if (tp_features.sensors_pdev_attrs_registered)
7498 device_remove_file(&tpacpi_sensors_pdev->dev,
7499 &dev_attr_thinkpad_acpi_pdev_name);
7500 if (tpacpi_sensors_pdev)
7501 platform_device_unregister(tpacpi_sensors_pdev);
7502 if (tpacpi_pdev)
7503 platform_device_unregister(tpacpi_pdev);
7505 if (tp_features.sensors_pdrv_attrs_registered)
7506 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7507 if (tp_features.platform_drv_attrs_registered)
7508 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7510 if (tp_features.sensors_pdrv_registered)
7511 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7513 if (tp_features.platform_drv_registered)
7514 platform_driver_unregister(&tpacpi_pdriver);
7516 if (proc_dir)
7517 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7519 if (tpacpi_wq)
7520 destroy_workqueue(tpacpi_wq);
7522 kfree(thinkpad_id.bios_version_str);
7523 kfree(thinkpad_id.ec_version_str);
7524 kfree(thinkpad_id.model_str);
7528 static int __init thinkpad_acpi_module_init(void)
7530 int ret, i;
7532 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7534 /* Parameter checking */
7535 if (hotkey_report_mode > 2)
7536 return -EINVAL;
7538 /* Driver-level probe */
7540 ret = get_thinkpad_model_data(&thinkpad_id);
7541 if (ret) {
7542 printk(TPACPI_ERR
7543 "unable to get DMI data: %d\n", ret);
7544 thinkpad_acpi_module_exit();
7545 return ret;
7547 ret = probe_for_thinkpad();
7548 if (ret) {
7549 thinkpad_acpi_module_exit();
7550 return ret;
7553 /* Driver initialization */
7555 TPACPI_ACPIHANDLE_INIT(ecrd);
7556 TPACPI_ACPIHANDLE_INIT(ecwr);
7558 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7559 if (!tpacpi_wq) {
7560 thinkpad_acpi_module_exit();
7561 return -ENOMEM;
7564 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7565 if (!proc_dir) {
7566 printk(TPACPI_ERR
7567 "unable to create proc dir " TPACPI_PROC_DIR);
7568 thinkpad_acpi_module_exit();
7569 return -ENODEV;
7571 proc_dir->owner = THIS_MODULE;
7573 ret = platform_driver_register(&tpacpi_pdriver);
7574 if (ret) {
7575 printk(TPACPI_ERR
7576 "unable to register main platform driver\n");
7577 thinkpad_acpi_module_exit();
7578 return ret;
7580 tp_features.platform_drv_registered = 1;
7582 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7583 if (ret) {
7584 printk(TPACPI_ERR
7585 "unable to register hwmon platform driver\n");
7586 thinkpad_acpi_module_exit();
7587 return ret;
7589 tp_features.sensors_pdrv_registered = 1;
7591 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7592 if (!ret) {
7593 tp_features.platform_drv_attrs_registered = 1;
7594 ret = tpacpi_create_driver_attributes(
7595 &tpacpi_hwmon_pdriver.driver);
7597 if (ret) {
7598 printk(TPACPI_ERR
7599 "unable to create sysfs driver attributes\n");
7600 thinkpad_acpi_module_exit();
7601 return ret;
7603 tp_features.sensors_pdrv_attrs_registered = 1;
7606 /* Device initialization */
7607 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7608 NULL, 0);
7609 if (IS_ERR(tpacpi_pdev)) {
7610 ret = PTR_ERR(tpacpi_pdev);
7611 tpacpi_pdev = NULL;
7612 printk(TPACPI_ERR "unable to register platform device\n");
7613 thinkpad_acpi_module_exit();
7614 return ret;
7616 tpacpi_sensors_pdev = platform_device_register_simple(
7617 TPACPI_HWMON_DRVR_NAME,
7618 -1, NULL, 0);
7619 if (IS_ERR(tpacpi_sensors_pdev)) {
7620 ret = PTR_ERR(tpacpi_sensors_pdev);
7621 tpacpi_sensors_pdev = NULL;
7622 printk(TPACPI_ERR
7623 "unable to register hwmon platform device\n");
7624 thinkpad_acpi_module_exit();
7625 return ret;
7627 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7628 &dev_attr_thinkpad_acpi_pdev_name);
7629 if (ret) {
7630 printk(TPACPI_ERR
7631 "unable to create sysfs hwmon device attributes\n");
7632 thinkpad_acpi_module_exit();
7633 return ret;
7635 tp_features.sensors_pdev_attrs_registered = 1;
7636 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7637 if (IS_ERR(tpacpi_hwmon)) {
7638 ret = PTR_ERR(tpacpi_hwmon);
7639 tpacpi_hwmon = NULL;
7640 printk(TPACPI_ERR "unable to register hwmon device\n");
7641 thinkpad_acpi_module_exit();
7642 return ret;
7644 mutex_init(&tpacpi_inputdev_send_mutex);
7645 tpacpi_inputdev = input_allocate_device();
7646 if (!tpacpi_inputdev) {
7647 printk(TPACPI_ERR "unable to allocate input device\n");
7648 thinkpad_acpi_module_exit();
7649 return -ENOMEM;
7650 } else {
7651 /* Prepare input device, but don't register */
7652 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7653 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7654 tpacpi_inputdev->id.bustype = BUS_HOST;
7655 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7656 thinkpad_id.vendor :
7657 PCI_VENDOR_ID_IBM;
7658 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7659 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7661 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7662 ret = ibm_init(&ibms_init[i]);
7663 if (ret >= 0 && *ibms_init[i].param)
7664 ret = ibms_init[i].data->write(ibms_init[i].param);
7665 if (ret < 0) {
7666 thinkpad_acpi_module_exit();
7667 return ret;
7670 ret = input_register_device(tpacpi_inputdev);
7671 if (ret < 0) {
7672 printk(TPACPI_ERR "unable to register input device\n");
7673 thinkpad_acpi_module_exit();
7674 return ret;
7675 } else {
7676 tp_features.input_device_registered = 1;
7679 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7680 return 0;
7683 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7686 * DMI matching for module autoloading
7688 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7689 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7691 * Only models listed in thinkwiki will be supported, so add yours
7692 * if it is not there yet.
7694 #define IBM_BIOS_MODULE_ALIAS(__type) \
7695 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7697 /* Non-ancient thinkpads */
7698 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7699 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7701 /* Ancient thinkpad BIOSes have to be identified by
7702 * BIOS type or model number, and there are far less
7703 * BIOS types than model numbers... */
7704 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7705 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7706 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7708 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7709 MODULE_DESCRIPTION(TPACPI_DESC);
7710 MODULE_VERSION(TPACPI_VERSION);
7711 MODULE_LICENSE("GPL");
7713 module_init(thinkpad_acpi_module_init);
7714 module_exit(thinkpad_acpi_module_exit);