thinkpad-acpi: update copyright notices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob487181372d82865c1537af6f92e760109f1adf0b
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 0x020200
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/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
80 #include <linux/pci_ids.h>
83 /* ThinkPad CMOS commands */
84 #define TP_CMOS_VOLUME_DOWN 0
85 #define TP_CMOS_VOLUME_UP 1
86 #define TP_CMOS_VOLUME_MUTE 2
87 #define TP_CMOS_BRIGHTNESS_UP 4
88 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 #define TP_CMOS_THINKLIGHT_ON 12
90 #define TP_CMOS_THINKLIGHT_OFF 13
92 /* NVRAM Addresses */
93 enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
101 /* NVRAM bit masks */
102 enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
118 /* ACPI HIDs */
119 #define TPACPI_ACPI_HKEY_HID "IBM0068"
121 /* Input IDs */
122 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123 #define TPACPI_HKEY_INPUT_VERSION 0x4101
125 /* ACPI \WGSV commands */
126 enum {
127 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
128 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
129 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
130 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
133 /* TP_ACPI_WGSV_GET_STATE bits */
134 enum {
135 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
136 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
137 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
138 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
139 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
140 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
141 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
142 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
143 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
144 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
147 /****************************************************************************
148 * Main driver
151 #define TPACPI_NAME "thinkpad"
152 #define TPACPI_DESC "ThinkPad ACPI Extras"
153 #define TPACPI_FILE TPACPI_NAME "_acpi"
154 #define TPACPI_URL "http://ibm-acpi.sf.net/"
155 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
157 #define TPACPI_PROC_DIR "ibm"
158 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
159 #define TPACPI_DRVR_NAME TPACPI_FILE
160 #define TPACPI_DRVR_SHORTNAME "tpacpi"
161 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
163 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
164 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
166 #define TPACPI_MAX_ACPI_ARGS 3
168 /* rfkill switches */
169 enum {
170 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
171 TPACPI_RFK_WWAN_SW_ID,
172 TPACPI_RFK_UWB_SW_ID,
175 /* Debugging */
176 #define TPACPI_LOG TPACPI_FILE ": "
177 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
178 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
179 #define TPACPI_ERR KERN_ERR TPACPI_LOG
180 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
181 #define TPACPI_INFO KERN_INFO TPACPI_LOG
182 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
184 #define TPACPI_DBG_ALL 0xffff
185 #define TPACPI_DBG_INIT 0x0001
186 #define TPACPI_DBG_EXIT 0x0002
187 #define dbg_printk(a_dbg_level, format, arg...) \
188 do { if (dbg_level & a_dbg_level) \
189 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
190 } while (0)
191 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
192 #define vdbg_printk(a_dbg_level, format, arg...) \
193 dbg_printk(a_dbg_level, format, ## arg)
194 static const char *str_supported(int is_supported);
195 #else
196 #define vdbg_printk(a_dbg_level, format, arg...)
197 #endif
199 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
200 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
201 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
204 /****************************************************************************
205 * Driver-wide structs and misc. variables
208 struct ibm_struct;
210 struct tp_acpi_drv_struct {
211 const struct acpi_device_id *hid;
212 struct acpi_driver *driver;
214 void (*notify) (struct ibm_struct *, u32);
215 acpi_handle *handle;
216 u32 type;
217 struct acpi_device *device;
220 struct ibm_struct {
221 char *name;
223 int (*read) (char *);
224 int (*write) (char *);
225 void (*exit) (void);
226 void (*resume) (void);
227 void (*suspend) (pm_message_t state);
228 void (*shutdown) (void);
230 struct list_head all_drivers;
232 struct tp_acpi_drv_struct *acpi;
234 struct {
235 u8 acpi_driver_registered:1;
236 u8 acpi_notify_installed:1;
237 u8 proc_created:1;
238 u8 init_called:1;
239 u8 experimental:1;
240 } flags;
243 struct ibm_init_struct {
244 char param[32];
246 int (*init) (struct ibm_init_struct *);
247 struct ibm_struct *data;
250 static struct {
251 #ifdef CONFIG_THINKPAD_ACPI_BAY
252 u32 bay_status:1;
253 u32 bay_eject:1;
254 u32 bay_status2:1;
255 u32 bay_eject2:1;
256 #endif
257 u32 bluetooth:1;
258 u32 hotkey:1;
259 u32 hotkey_mask:1;
260 u32 hotkey_wlsw:1;
261 u32 hotkey_tablet:1;
262 u32 light:1;
263 u32 light_status:1;
264 u32 bright_16levels:1;
265 u32 bright_acpimode:1;
266 u32 wan:1;
267 u32 uwb:1;
268 u32 fan_ctrl_status_undef:1;
269 u32 input_device_registered:1;
270 u32 platform_drv_registered:1;
271 u32 platform_drv_attrs_registered:1;
272 u32 sensors_pdrv_registered:1;
273 u32 sensors_pdrv_attrs_registered:1;
274 u32 sensors_pdev_attrs_registered:1;
275 u32 hotkey_poll_active:1;
276 } tp_features;
278 static struct {
279 u16 hotkey_mask_ff:1;
280 u16 bright_cmos_ec_unsync:1;
281 } tp_warned;
283 struct thinkpad_id_data {
284 unsigned int vendor; /* ThinkPad vendor:
285 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
287 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
288 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
290 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
291 u16 ec_model;
293 char *model_str; /* ThinkPad T43 */
294 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
296 static struct thinkpad_id_data thinkpad_id;
298 static enum {
299 TPACPI_LIFE_INIT = 0,
300 TPACPI_LIFE_RUNNING,
301 TPACPI_LIFE_EXITING,
302 } tpacpi_lifecycle;
304 static int experimental;
305 static u32 dbg_level;
307 static struct workqueue_struct *tpacpi_wq;
309 enum led_status_t {
310 TPACPI_LED_OFF = 0,
311 TPACPI_LED_ON,
312 TPACPI_LED_BLINK,
315 /* Special LED class that can defer work */
316 struct tpacpi_led_classdev {
317 struct led_classdev led_classdev;
318 struct work_struct work;
319 enum led_status_t new_state;
320 unsigned int led;
323 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
324 static int dbg_wlswemul;
325 static int tpacpi_wlsw_emulstate;
326 static int dbg_bluetoothemul;
327 static int tpacpi_bluetooth_emulstate;
328 static int dbg_wwanemul;
329 static int tpacpi_wwan_emulstate;
330 static int dbg_uwbemul;
331 static int tpacpi_uwb_emulstate;
332 #endif
335 /****************************************************************************
336 ****************************************************************************
338 * ACPI Helpers and device model
340 ****************************************************************************
341 ****************************************************************************/
343 /*************************************************************************
344 * ACPI basic handles
347 static acpi_handle root_handle;
349 #define TPACPI_HANDLE(object, parent, paths...) \
350 static acpi_handle object##_handle; \
351 static acpi_handle *object##_parent = &parent##_handle; \
352 static char *object##_path; \
353 static char *object##_paths[] = { paths }
355 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
356 "\\_SB.PCI.ISA.EC", /* 570 */
357 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
358 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
359 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
360 "\\_SB.PCI0.ICH3.EC0", /* R31 */
361 "\\_SB.PCI0.LPC.EC", /* all others */
364 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
365 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
367 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
368 /* T4x, X31, X40 */
369 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
370 "\\CMS", /* R40, R40e */
371 ); /* all others */
373 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
374 "^HKEY", /* R30, R31 */
375 "HKEY", /* all others */
376 ); /* 570 */
378 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
379 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
380 "\\_SB.PCI0.VID0", /* 770e */
381 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
382 "\\_SB.PCI0.AGP.VID", /* all others */
383 ); /* R30, R31 */
386 /*************************************************************************
387 * ACPI helpers
390 static int acpi_evalf(acpi_handle handle,
391 void *res, char *method, char *fmt, ...)
393 char *fmt0 = fmt;
394 struct acpi_object_list params;
395 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
396 struct acpi_buffer result, *resultp;
397 union acpi_object out_obj;
398 acpi_status status;
399 va_list ap;
400 char res_type;
401 int success;
402 int quiet;
404 if (!*fmt) {
405 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
406 return 0;
409 if (*fmt == 'q') {
410 quiet = 1;
411 fmt++;
412 } else
413 quiet = 0;
415 res_type = *(fmt++);
417 params.count = 0;
418 params.pointer = &in_objs[0];
420 va_start(ap, fmt);
421 while (*fmt) {
422 char c = *(fmt++);
423 switch (c) {
424 case 'd': /* int */
425 in_objs[params.count].integer.value = va_arg(ap, int);
426 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
427 break;
428 /* add more types as needed */
429 default:
430 printk(TPACPI_ERR "acpi_evalf() called "
431 "with invalid format character '%c'\n", c);
432 return 0;
435 va_end(ap);
437 if (res_type != 'v') {
438 result.length = sizeof(out_obj);
439 result.pointer = &out_obj;
440 resultp = &result;
441 } else
442 resultp = NULL;
444 status = acpi_evaluate_object(handle, method, &params, resultp);
446 switch (res_type) {
447 case 'd': /* int */
448 if (res)
449 *(int *)res = out_obj.integer.value;
450 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
451 break;
452 case 'v': /* void */
453 success = status == AE_OK;
454 break;
455 /* add more types as needed */
456 default:
457 printk(TPACPI_ERR "acpi_evalf() called "
458 "with invalid format character '%c'\n", res_type);
459 return 0;
462 if (!success && !quiet)
463 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
464 method, fmt0, status);
466 return success;
469 static int acpi_ec_read(int i, u8 *p)
471 int v;
473 if (ecrd_handle) {
474 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
475 return 0;
476 *p = v;
477 } else {
478 if (ec_read(i, p) < 0)
479 return 0;
482 return 1;
485 static int acpi_ec_write(int i, u8 v)
487 if (ecwr_handle) {
488 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
489 return 0;
490 } else {
491 if (ec_write(i, v) < 0)
492 return 0;
495 return 1;
498 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
499 static int _sta(acpi_handle handle)
501 int status;
503 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
504 status = 0;
506 return status;
508 #endif
510 static int issue_thinkpad_cmos_command(int cmos_cmd)
512 if (!cmos_handle)
513 return -ENXIO;
515 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
516 return -EIO;
518 return 0;
521 /*************************************************************************
522 * ACPI device model
525 #define TPACPI_ACPIHANDLE_INIT(object) \
526 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
527 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
529 static void drv_acpi_handle_init(char *name,
530 acpi_handle *handle, acpi_handle parent,
531 char **paths, int num_paths, char **path)
533 int i;
534 acpi_status status;
536 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
537 name);
539 for (i = 0; i < num_paths; i++) {
540 status = acpi_get_handle(parent, paths[i], handle);
541 if (ACPI_SUCCESS(status)) {
542 *path = paths[i];
543 dbg_printk(TPACPI_DBG_INIT,
544 "Found ACPI handle %s for %s\n",
545 *path, name);
546 return;
550 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
551 name);
552 *handle = NULL;
555 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
557 struct ibm_struct *ibm = data;
559 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
560 return;
562 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
563 return;
565 ibm->acpi->notify(ibm, event);
568 static int __init setup_acpi_notify(struct ibm_struct *ibm)
570 acpi_status status;
571 int rc;
573 BUG_ON(!ibm->acpi);
575 if (!*ibm->acpi->handle)
576 return 0;
578 vdbg_printk(TPACPI_DBG_INIT,
579 "setting up ACPI notify for %s\n", ibm->name);
581 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
582 if (rc < 0) {
583 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
584 ibm->name, rc);
585 return -ENODEV;
588 ibm->acpi->device->driver_data = ibm;
589 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
590 TPACPI_ACPI_EVENT_PREFIX,
591 ibm->name);
593 status = acpi_install_notify_handler(*ibm->acpi->handle,
594 ibm->acpi->type, dispatch_acpi_notify, ibm);
595 if (ACPI_FAILURE(status)) {
596 if (status == AE_ALREADY_EXISTS) {
597 printk(TPACPI_NOTICE
598 "another device driver is already "
599 "handling %s events\n", ibm->name);
600 } else {
601 printk(TPACPI_ERR
602 "acpi_install_notify_handler(%s) failed: %d\n",
603 ibm->name, status);
605 return -ENODEV;
607 ibm->flags.acpi_notify_installed = 1;
608 return 0;
611 static int __init tpacpi_device_add(struct acpi_device *device)
613 return 0;
616 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
618 int rc;
620 dbg_printk(TPACPI_DBG_INIT,
621 "registering %s as an ACPI driver\n", ibm->name);
623 BUG_ON(!ibm->acpi);
625 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
626 if (!ibm->acpi->driver) {
627 printk(TPACPI_ERR
628 "failed to allocate memory for ibm->acpi->driver\n");
629 return -ENOMEM;
632 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
633 ibm->acpi->driver->ids = ibm->acpi->hid;
635 ibm->acpi->driver->ops.add = &tpacpi_device_add;
637 rc = acpi_bus_register_driver(ibm->acpi->driver);
638 if (rc < 0) {
639 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
640 ibm->name, rc);
641 kfree(ibm->acpi->driver);
642 ibm->acpi->driver = NULL;
643 } else if (!rc)
644 ibm->flags.acpi_driver_registered = 1;
646 return rc;
650 /****************************************************************************
651 ****************************************************************************
653 * Procfs Helpers
655 ****************************************************************************
656 ****************************************************************************/
658 static int dispatch_procfs_read(char *page, char **start, off_t off,
659 int count, int *eof, void *data)
661 struct ibm_struct *ibm = data;
662 int len;
664 if (!ibm || !ibm->read)
665 return -EINVAL;
667 len = ibm->read(page);
668 if (len < 0)
669 return len;
671 if (len <= off + count)
672 *eof = 1;
673 *start = page + off;
674 len -= off;
675 if (len > count)
676 len = count;
677 if (len < 0)
678 len = 0;
680 return len;
683 static int dispatch_procfs_write(struct file *file,
684 const char __user *userbuf,
685 unsigned long count, void *data)
687 struct ibm_struct *ibm = data;
688 char *kernbuf;
689 int ret;
691 if (!ibm || !ibm->write)
692 return -EINVAL;
694 kernbuf = kmalloc(count + 2, GFP_KERNEL);
695 if (!kernbuf)
696 return -ENOMEM;
698 if (copy_from_user(kernbuf, userbuf, count)) {
699 kfree(kernbuf);
700 return -EFAULT;
703 kernbuf[count] = 0;
704 strcat(kernbuf, ",");
705 ret = ibm->write(kernbuf);
706 if (ret == 0)
707 ret = count;
709 kfree(kernbuf);
711 return ret;
714 static char *next_cmd(char **cmds)
716 char *start = *cmds;
717 char *end;
719 while ((end = strchr(start, ',')) && end == start)
720 start = end + 1;
722 if (!end)
723 return NULL;
725 *end = 0;
726 *cmds = end + 1;
727 return start;
731 /****************************************************************************
732 ****************************************************************************
734 * Device model: input, hwmon and platform
736 ****************************************************************************
737 ****************************************************************************/
739 static struct platform_device *tpacpi_pdev;
740 static struct platform_device *tpacpi_sensors_pdev;
741 static struct device *tpacpi_hwmon;
742 static struct input_dev *tpacpi_inputdev;
743 static struct mutex tpacpi_inputdev_send_mutex;
744 static LIST_HEAD(tpacpi_all_drivers);
746 static int tpacpi_suspend_handler(struct platform_device *pdev,
747 pm_message_t state)
749 struct ibm_struct *ibm, *itmp;
751 list_for_each_entry_safe(ibm, itmp,
752 &tpacpi_all_drivers,
753 all_drivers) {
754 if (ibm->suspend)
755 (ibm->suspend)(state);
758 return 0;
761 static int tpacpi_resume_handler(struct platform_device *pdev)
763 struct ibm_struct *ibm, *itmp;
765 list_for_each_entry_safe(ibm, itmp,
766 &tpacpi_all_drivers,
767 all_drivers) {
768 if (ibm->resume)
769 (ibm->resume)();
772 return 0;
775 static void tpacpi_shutdown_handler(struct platform_device *pdev)
777 struct ibm_struct *ibm, *itmp;
779 list_for_each_entry_safe(ibm, itmp,
780 &tpacpi_all_drivers,
781 all_drivers) {
782 if (ibm->shutdown)
783 (ibm->shutdown)();
787 static struct platform_driver tpacpi_pdriver = {
788 .driver = {
789 .name = TPACPI_DRVR_NAME,
790 .owner = THIS_MODULE,
792 .suspend = tpacpi_suspend_handler,
793 .resume = tpacpi_resume_handler,
794 .shutdown = tpacpi_shutdown_handler,
797 static struct platform_driver tpacpi_hwmon_pdriver = {
798 .driver = {
799 .name = TPACPI_HWMON_DRVR_NAME,
800 .owner = THIS_MODULE,
804 /*************************************************************************
805 * sysfs support helpers
808 struct attribute_set {
809 unsigned int members, max_members;
810 struct attribute_group group;
813 struct attribute_set_obj {
814 struct attribute_set s;
815 struct attribute *a;
816 } __attribute__((packed));
818 static struct attribute_set *create_attr_set(unsigned int max_members,
819 const char *name)
821 struct attribute_set_obj *sobj;
823 if (max_members == 0)
824 return NULL;
826 /* Allocates space for implicit NULL at the end too */
827 sobj = kzalloc(sizeof(struct attribute_set_obj) +
828 max_members * sizeof(struct attribute *),
829 GFP_KERNEL);
830 if (!sobj)
831 return NULL;
832 sobj->s.max_members = max_members;
833 sobj->s.group.attrs = &sobj->a;
834 sobj->s.group.name = name;
836 return &sobj->s;
839 #define destroy_attr_set(_set) \
840 kfree(_set);
842 /* not multi-threaded safe, use it in a single thread per set */
843 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
845 if (!s || !attr)
846 return -EINVAL;
848 if (s->members >= s->max_members)
849 return -ENOMEM;
851 s->group.attrs[s->members] = attr;
852 s->members++;
854 return 0;
857 static int add_many_to_attr_set(struct attribute_set *s,
858 struct attribute **attr,
859 unsigned int count)
861 int i, res;
863 for (i = 0; i < count; i++) {
864 res = add_to_attr_set(s, attr[i]);
865 if (res)
866 return res;
869 return 0;
872 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
874 sysfs_remove_group(kobj, &s->group);
875 destroy_attr_set(s);
878 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
879 sysfs_create_group(_kobj, &_attr_set->group)
881 static int parse_strtoul(const char *buf,
882 unsigned long max, unsigned long *value)
884 char *endp;
886 while (*buf && isspace(*buf))
887 buf++;
888 *value = simple_strtoul(buf, &endp, 0);
889 while (*endp && isspace(*endp))
890 endp++;
891 if (*endp || *value > max)
892 return -EINVAL;
894 return 0;
897 static void tpacpi_disable_brightness_delay(void)
899 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
900 printk(TPACPI_NOTICE
901 "ACPI backlight control delay disabled\n");
904 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
906 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
907 union acpi_object *obj;
908 int rc;
910 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
911 obj = (union acpi_object *)buffer.pointer;
912 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
913 printk(TPACPI_ERR "Unknown _BCL data, "
914 "please report this to %s\n", TPACPI_MAIL);
915 rc = 0;
916 } else {
917 rc = obj->package.count;
919 } else {
920 return 0;
923 kfree(buffer.pointer);
924 return rc;
927 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
928 u32 lvl, void *context, void **rv)
930 char name[ACPI_PATH_SEGMENT_LENGTH];
931 struct acpi_buffer buffer = { sizeof(name), &name };
933 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
934 !strncmp("_BCL", name, sizeof(name) - 1)) {
935 BUG_ON(!rv || !*rv);
936 **(int **)rv = tpacpi_query_bcl_levels(handle);
937 return AE_CTRL_TERMINATE;
938 } else {
939 return AE_OK;
944 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
946 static int __init tpacpi_check_std_acpi_brightness_support(void)
948 int status;
949 int bcl_levels = 0;
950 void *bcl_ptr = &bcl_levels;
952 if (!vid_handle) {
953 TPACPI_ACPIHANDLE_INIT(vid);
955 if (!vid_handle)
956 return 0;
959 * Search for a _BCL method, and execute it. This is safe on all
960 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
961 * BIOS in ACPI backlight control mode. We do NOT have to care
962 * about calling the _BCL method in an enabled video device, any
963 * will do for our purposes.
966 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
967 tpacpi_acpi_walk_find_bcl, NULL,
968 &bcl_ptr);
970 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
971 tp_features.bright_acpimode = 1;
972 return (bcl_levels - 2);
975 return 0;
978 static int __init tpacpi_new_rfkill(const unsigned int id,
979 struct rfkill **rfk,
980 const enum rfkill_type rfktype,
981 const char *name,
982 const bool set_default,
983 int (*toggle_radio)(void *, enum rfkill_state),
984 int (*get_state)(void *, enum rfkill_state *))
986 int res;
987 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
989 res = get_state(NULL, &initial_state);
990 if (res < 0) {
991 printk(TPACPI_ERR
992 "failed to read initial state for %s, error %d; "
993 "will turn radio off\n", name, res);
994 } else if (set_default) {
995 /* try to set the initial state as the default for the rfkill
996 * type, since we ask the firmware to preserve it across S5 in
997 * NVRAM */
998 rfkill_set_default(rfktype,
999 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1000 RFKILL_STATE_UNBLOCKED :
1001 RFKILL_STATE_SOFT_BLOCKED);
1004 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1005 if (!*rfk) {
1006 printk(TPACPI_ERR
1007 "failed to allocate memory for rfkill class\n");
1008 return -ENOMEM;
1011 (*rfk)->name = name;
1012 (*rfk)->get_state = get_state;
1013 (*rfk)->toggle_radio = toggle_radio;
1014 (*rfk)->state = initial_state;
1016 res = rfkill_register(*rfk);
1017 if (res < 0) {
1018 printk(TPACPI_ERR
1019 "failed to register %s rfkill switch: %d\n",
1020 name, res);
1021 rfkill_free(*rfk);
1022 *rfk = NULL;
1023 return res;
1026 return 0;
1029 /*************************************************************************
1030 * thinkpad-acpi driver attributes
1033 /* interface_version --------------------------------------------------- */
1034 static ssize_t tpacpi_driver_interface_version_show(
1035 struct device_driver *drv,
1036 char *buf)
1038 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1041 static DRIVER_ATTR(interface_version, S_IRUGO,
1042 tpacpi_driver_interface_version_show, NULL);
1044 /* debug_level --------------------------------------------------------- */
1045 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1046 char *buf)
1048 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1051 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1052 const char *buf, size_t count)
1054 unsigned long t;
1056 if (parse_strtoul(buf, 0xffff, &t))
1057 return -EINVAL;
1059 dbg_level = t;
1061 return count;
1064 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1065 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1067 /* version ------------------------------------------------------------- */
1068 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1069 char *buf)
1071 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1072 TPACPI_DESC, TPACPI_VERSION);
1075 static DRIVER_ATTR(version, S_IRUGO,
1076 tpacpi_driver_version_show, NULL);
1078 /* --------------------------------------------------------------------- */
1080 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1082 static void tpacpi_send_radiosw_update(void);
1084 /* wlsw_emulstate ------------------------------------------------------ */
1085 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1086 char *buf)
1088 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1091 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1092 const char *buf, size_t count)
1094 unsigned long t;
1096 if (parse_strtoul(buf, 1, &t))
1097 return -EINVAL;
1099 if (tpacpi_wlsw_emulstate != t) {
1100 tpacpi_wlsw_emulstate = !!t;
1101 tpacpi_send_radiosw_update();
1102 } else
1103 tpacpi_wlsw_emulstate = !!t;
1105 return count;
1108 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1109 tpacpi_driver_wlsw_emulstate_show,
1110 tpacpi_driver_wlsw_emulstate_store);
1112 /* bluetooth_emulstate ------------------------------------------------- */
1113 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1114 struct device_driver *drv,
1115 char *buf)
1117 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1120 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1121 struct device_driver *drv,
1122 const char *buf, size_t count)
1124 unsigned long t;
1126 if (parse_strtoul(buf, 1, &t))
1127 return -EINVAL;
1129 tpacpi_bluetooth_emulstate = !!t;
1131 return count;
1134 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1135 tpacpi_driver_bluetooth_emulstate_show,
1136 tpacpi_driver_bluetooth_emulstate_store);
1138 /* wwan_emulstate ------------------------------------------------- */
1139 static ssize_t tpacpi_driver_wwan_emulstate_show(
1140 struct device_driver *drv,
1141 char *buf)
1143 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1146 static ssize_t tpacpi_driver_wwan_emulstate_store(
1147 struct device_driver *drv,
1148 const char *buf, size_t count)
1150 unsigned long t;
1152 if (parse_strtoul(buf, 1, &t))
1153 return -EINVAL;
1155 tpacpi_wwan_emulstate = !!t;
1157 return count;
1160 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1161 tpacpi_driver_wwan_emulstate_show,
1162 tpacpi_driver_wwan_emulstate_store);
1164 /* uwb_emulstate ------------------------------------------------- */
1165 static ssize_t tpacpi_driver_uwb_emulstate_show(
1166 struct device_driver *drv,
1167 char *buf)
1169 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1172 static ssize_t tpacpi_driver_uwb_emulstate_store(
1173 struct device_driver *drv,
1174 const char *buf, size_t count)
1176 unsigned long t;
1178 if (parse_strtoul(buf, 1, &t))
1179 return -EINVAL;
1181 tpacpi_uwb_emulstate = !!t;
1183 return count;
1186 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1187 tpacpi_driver_uwb_emulstate_show,
1188 tpacpi_driver_uwb_emulstate_store);
1189 #endif
1191 /* --------------------------------------------------------------------- */
1193 static struct driver_attribute *tpacpi_driver_attributes[] = {
1194 &driver_attr_debug_level, &driver_attr_version,
1195 &driver_attr_interface_version,
1198 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1200 int i, res;
1202 i = 0;
1203 res = 0;
1204 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1205 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1206 i++;
1209 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1210 if (!res && dbg_wlswemul)
1211 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1212 if (!res && dbg_bluetoothemul)
1213 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1214 if (!res && dbg_wwanemul)
1215 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1216 if (!res && dbg_uwbemul)
1217 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1218 #endif
1220 return res;
1223 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1225 int i;
1227 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1228 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1230 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1231 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1232 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1233 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1234 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1235 #endif
1238 /****************************************************************************
1239 ****************************************************************************
1241 * Subdrivers
1243 ****************************************************************************
1244 ****************************************************************************/
1246 /*************************************************************************
1247 * thinkpad-acpi init subdriver
1250 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1252 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1253 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1255 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1256 (thinkpad_id.bios_version_str) ?
1257 thinkpad_id.bios_version_str : "unknown",
1258 (thinkpad_id.ec_version_str) ?
1259 thinkpad_id.ec_version_str : "unknown");
1261 if (thinkpad_id.vendor && thinkpad_id.model_str)
1262 printk(TPACPI_INFO "%s %s, model %s\n",
1263 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1264 "IBM" : ((thinkpad_id.vendor ==
1265 PCI_VENDOR_ID_LENOVO) ?
1266 "Lenovo" : "Unknown vendor"),
1267 thinkpad_id.model_str,
1268 (thinkpad_id.nummodel_str) ?
1269 thinkpad_id.nummodel_str : "unknown");
1271 return 0;
1274 static int thinkpad_acpi_driver_read(char *p)
1276 int len = 0;
1278 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1279 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1281 return len;
1284 static struct ibm_struct thinkpad_acpi_driver_data = {
1285 .name = "driver",
1286 .read = thinkpad_acpi_driver_read,
1289 /*************************************************************************
1290 * Hotkey subdriver
1293 enum { /* hot key scan codes (derived from ACPI DSDT) */
1294 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1295 TP_ACPI_HOTKEYSCAN_FNF2,
1296 TP_ACPI_HOTKEYSCAN_FNF3,
1297 TP_ACPI_HOTKEYSCAN_FNF4,
1298 TP_ACPI_HOTKEYSCAN_FNF5,
1299 TP_ACPI_HOTKEYSCAN_FNF6,
1300 TP_ACPI_HOTKEYSCAN_FNF7,
1301 TP_ACPI_HOTKEYSCAN_FNF8,
1302 TP_ACPI_HOTKEYSCAN_FNF9,
1303 TP_ACPI_HOTKEYSCAN_FNF10,
1304 TP_ACPI_HOTKEYSCAN_FNF11,
1305 TP_ACPI_HOTKEYSCAN_FNF12,
1306 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1307 TP_ACPI_HOTKEYSCAN_FNINSERT,
1308 TP_ACPI_HOTKEYSCAN_FNDELETE,
1309 TP_ACPI_HOTKEYSCAN_FNHOME,
1310 TP_ACPI_HOTKEYSCAN_FNEND,
1311 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1312 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1313 TP_ACPI_HOTKEYSCAN_FNSPACE,
1314 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1315 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1316 TP_ACPI_HOTKEYSCAN_MUTE,
1317 TP_ACPI_HOTKEYSCAN_THINKPAD,
1320 enum { /* Keys available through NVRAM polling */
1321 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1322 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1325 enum { /* Positions of some of the keys in hotkey masks */
1326 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1327 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1328 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1329 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1330 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1331 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1332 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1333 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1334 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1335 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1336 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1339 enum { /* NVRAM to ACPI HKEY group map */
1340 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1341 TP_ACPI_HKEY_ZOOM_MASK |
1342 TP_ACPI_HKEY_DISPSWTCH_MASK |
1343 TP_ACPI_HKEY_HIBERNATE_MASK,
1344 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1345 TP_ACPI_HKEY_BRGHTDWN_MASK,
1346 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1347 TP_ACPI_HKEY_VOLDWN_MASK |
1348 TP_ACPI_HKEY_MUTE_MASK,
1351 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1352 struct tp_nvram_state {
1353 u16 thinkpad_toggle:1;
1354 u16 zoom_toggle:1;
1355 u16 display_toggle:1;
1356 u16 thinklight_toggle:1;
1357 u16 hibernate_toggle:1;
1358 u16 displayexp_toggle:1;
1359 u16 display_state:1;
1360 u16 brightness_toggle:1;
1361 u16 volume_toggle:1;
1362 u16 mute:1;
1364 u8 brightness_level;
1365 u8 volume_level;
1368 static struct task_struct *tpacpi_hotkey_task;
1369 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1370 static int hotkey_poll_freq = 10; /* Hz */
1371 static struct mutex hotkey_thread_mutex;
1372 static struct mutex hotkey_thread_data_mutex;
1373 static unsigned int hotkey_config_change;
1375 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1377 #define hotkey_source_mask 0U
1379 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1381 static struct mutex hotkey_mutex;
1383 static enum { /* Reasons for waking up */
1384 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1385 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1386 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1387 } hotkey_wakeup_reason;
1389 static int hotkey_autosleep_ack;
1391 static int hotkey_orig_status;
1392 static u32 hotkey_orig_mask;
1393 static u32 hotkey_all_mask;
1394 static u32 hotkey_reserved_mask;
1395 static u32 hotkey_mask;
1397 static unsigned int hotkey_report_mode;
1399 static u16 *hotkey_keycode_map;
1401 static struct attribute_set *hotkey_dev_attributes;
1403 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1404 #define HOTKEY_CONFIG_CRITICAL_START \
1405 do { \
1406 mutex_lock(&hotkey_thread_data_mutex); \
1407 hotkey_config_change++; \
1408 } while (0);
1409 #define HOTKEY_CONFIG_CRITICAL_END \
1410 mutex_unlock(&hotkey_thread_data_mutex);
1411 #else
1412 #define HOTKEY_CONFIG_CRITICAL_START
1413 #define HOTKEY_CONFIG_CRITICAL_END
1414 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1416 /* HKEY.MHKG() return bits */
1417 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1419 static int hotkey_get_wlsw(int *status)
1421 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1422 if (dbg_wlswemul) {
1423 *status = !!tpacpi_wlsw_emulstate;
1424 return 0;
1426 #endif
1427 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1428 return -EIO;
1429 return 0;
1432 static int hotkey_get_tablet_mode(int *status)
1434 int s;
1436 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1437 return -EIO;
1439 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1440 return 0;
1444 * Call with hotkey_mutex held
1446 static int hotkey_mask_get(void)
1448 u32 m = 0;
1450 if (tp_features.hotkey_mask) {
1451 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1452 return -EIO;
1454 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1456 return 0;
1460 * Call with hotkey_mutex held
1462 static int hotkey_mask_set(u32 mask)
1464 int i;
1465 int rc = 0;
1467 if (tp_features.hotkey_mask) {
1468 if (!tp_warned.hotkey_mask_ff &&
1469 (mask == 0xffff || mask == 0xffffff ||
1470 mask == 0xffffffff)) {
1471 tp_warned.hotkey_mask_ff = 1;
1472 printk(TPACPI_NOTICE
1473 "setting the hotkey mask to 0x%08x is likely "
1474 "not the best way to go about it\n", mask);
1475 printk(TPACPI_NOTICE
1476 "please consider using the driver defaults, "
1477 "and refer to up-to-date thinkpad-acpi "
1478 "documentation\n");
1481 HOTKEY_CONFIG_CRITICAL_START
1482 for (i = 0; i < 32; i++) {
1483 u32 m = 1 << i;
1484 /* enable in firmware mask only keys not in NVRAM
1485 * mode, but enable the key in the cached hotkey_mask
1486 * regardless of mode, or the key will end up
1487 * disabled by hotkey_mask_get() */
1488 if (!acpi_evalf(hkey_handle,
1489 NULL, "MHKM", "vdd", i + 1,
1490 !!((mask & ~hotkey_source_mask) & m))) {
1491 rc = -EIO;
1492 break;
1493 } else {
1494 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1497 HOTKEY_CONFIG_CRITICAL_END
1499 /* hotkey_mask_get must be called unconditionally below */
1500 if (!hotkey_mask_get() && !rc &&
1501 (hotkey_mask & ~hotkey_source_mask) !=
1502 (mask & ~hotkey_source_mask)) {
1503 printk(TPACPI_NOTICE
1504 "requested hot key mask 0x%08x, but "
1505 "firmware forced it to 0x%08x\n",
1506 mask, hotkey_mask);
1508 } else {
1509 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1510 HOTKEY_CONFIG_CRITICAL_START
1511 hotkey_mask = mask & hotkey_source_mask;
1512 HOTKEY_CONFIG_CRITICAL_END
1513 hotkey_mask_get();
1514 if (hotkey_mask != mask) {
1515 printk(TPACPI_NOTICE
1516 "requested hot key mask 0x%08x, "
1517 "forced to 0x%08x (NVRAM poll mask is "
1518 "0x%08x): no firmware mask support\n",
1519 mask, hotkey_mask, hotkey_source_mask);
1521 #else
1522 hotkey_mask_get();
1523 rc = -ENXIO;
1524 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1527 return rc;
1530 static int hotkey_status_get(int *status)
1532 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1533 return -EIO;
1535 return 0;
1538 static int hotkey_status_set(int status)
1540 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1541 return -EIO;
1543 return 0;
1546 static void tpacpi_input_send_tabletsw(void)
1548 int state;
1550 if (tp_features.hotkey_tablet &&
1551 !hotkey_get_tablet_mode(&state)) {
1552 mutex_lock(&tpacpi_inputdev_send_mutex);
1554 input_report_switch(tpacpi_inputdev,
1555 SW_TABLET_MODE, !!state);
1556 input_sync(tpacpi_inputdev);
1558 mutex_unlock(&tpacpi_inputdev_send_mutex);
1562 static void tpacpi_input_send_key(unsigned int scancode)
1564 unsigned int keycode;
1566 keycode = hotkey_keycode_map[scancode];
1568 if (keycode != KEY_RESERVED) {
1569 mutex_lock(&tpacpi_inputdev_send_mutex);
1571 input_report_key(tpacpi_inputdev, keycode, 1);
1572 if (keycode == KEY_UNKNOWN)
1573 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1574 scancode);
1575 input_sync(tpacpi_inputdev);
1577 input_report_key(tpacpi_inputdev, keycode, 0);
1578 if (keycode == KEY_UNKNOWN)
1579 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1580 scancode);
1581 input_sync(tpacpi_inputdev);
1583 mutex_unlock(&tpacpi_inputdev_send_mutex);
1587 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1588 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1590 static void tpacpi_hotkey_send_key(unsigned int scancode)
1592 tpacpi_input_send_key(scancode);
1593 if (hotkey_report_mode < 2) {
1594 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1595 0x80, 0x1001 + scancode);
1599 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1601 u8 d;
1603 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1604 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1605 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1606 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1607 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1608 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1610 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1611 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1612 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1614 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1615 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1616 n->displayexp_toggle =
1617 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1619 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1620 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1621 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1622 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1623 n->brightness_toggle =
1624 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1626 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1627 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1628 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1629 >> TP_NVRAM_POS_LEVEL_VOLUME;
1630 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1631 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1635 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1636 do { \
1637 if ((mask & (1 << __scancode)) && \
1638 oldn->__member != newn->__member) \
1639 tpacpi_hotkey_send_key(__scancode); \
1640 } while (0)
1642 #define TPACPI_MAY_SEND_KEY(__scancode) \
1643 do { if (mask & (1 << __scancode)) \
1644 tpacpi_hotkey_send_key(__scancode); } while (0)
1646 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1647 struct tp_nvram_state *newn,
1648 u32 mask)
1650 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1651 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1652 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1653 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1655 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1657 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1659 /* handle volume */
1660 if (oldn->volume_toggle != newn->volume_toggle) {
1661 if (oldn->mute != newn->mute) {
1662 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1664 if (oldn->volume_level > newn->volume_level) {
1665 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1666 } else if (oldn->volume_level < newn->volume_level) {
1667 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1668 } else if (oldn->mute == newn->mute) {
1669 /* repeated key presses that didn't change state */
1670 if (newn->mute) {
1671 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1672 } else if (newn->volume_level != 0) {
1673 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1674 } else {
1675 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1680 /* handle brightness */
1681 if (oldn->brightness_toggle != newn->brightness_toggle) {
1682 if (oldn->brightness_level < newn->brightness_level) {
1683 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1684 } else if (oldn->brightness_level > newn->brightness_level) {
1685 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1686 } else {
1687 /* repeated key presses that didn't change state */
1688 if (newn->brightness_level != 0) {
1689 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1690 } else {
1691 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1697 #undef TPACPI_COMPARE_KEY
1698 #undef TPACPI_MAY_SEND_KEY
1700 static int hotkey_kthread(void *data)
1702 struct tp_nvram_state s[2];
1703 u32 mask;
1704 unsigned int si, so;
1705 unsigned long t;
1706 unsigned int change_detector, must_reset;
1708 mutex_lock(&hotkey_thread_mutex);
1710 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1711 goto exit;
1713 set_freezable();
1715 so = 0;
1716 si = 1;
1717 t = 0;
1719 /* Initial state for compares */
1720 mutex_lock(&hotkey_thread_data_mutex);
1721 change_detector = hotkey_config_change;
1722 mask = hotkey_source_mask & hotkey_mask;
1723 mutex_unlock(&hotkey_thread_data_mutex);
1724 hotkey_read_nvram(&s[so], mask);
1726 while (!kthread_should_stop() && hotkey_poll_freq) {
1727 if (t == 0)
1728 t = 1000/hotkey_poll_freq;
1729 t = msleep_interruptible(t);
1730 if (unlikely(kthread_should_stop()))
1731 break;
1732 must_reset = try_to_freeze();
1733 if (t > 0 && !must_reset)
1734 continue;
1736 mutex_lock(&hotkey_thread_data_mutex);
1737 if (must_reset || hotkey_config_change != change_detector) {
1738 /* forget old state on thaw or config change */
1739 si = so;
1740 t = 0;
1741 change_detector = hotkey_config_change;
1743 mask = hotkey_source_mask & hotkey_mask;
1744 mutex_unlock(&hotkey_thread_data_mutex);
1746 if (likely(mask)) {
1747 hotkey_read_nvram(&s[si], mask);
1748 if (likely(si != so)) {
1749 hotkey_compare_and_issue_event(&s[so], &s[si],
1750 mask);
1754 so = si;
1755 si ^= 1;
1758 exit:
1759 mutex_unlock(&hotkey_thread_mutex);
1760 return 0;
1763 static void hotkey_poll_stop_sync(void)
1765 if (tpacpi_hotkey_task) {
1766 if (frozen(tpacpi_hotkey_task) ||
1767 freezing(tpacpi_hotkey_task))
1768 thaw_process(tpacpi_hotkey_task);
1770 kthread_stop(tpacpi_hotkey_task);
1771 tpacpi_hotkey_task = NULL;
1772 mutex_lock(&hotkey_thread_mutex);
1773 /* at this point, the thread did exit */
1774 mutex_unlock(&hotkey_thread_mutex);
1778 /* call with hotkey_mutex held */
1779 static void hotkey_poll_setup(int may_warn)
1781 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1782 hotkey_poll_freq > 0 &&
1783 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1784 if (!tpacpi_hotkey_task) {
1785 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1786 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1787 if (IS_ERR(tpacpi_hotkey_task)) {
1788 tpacpi_hotkey_task = NULL;
1789 printk(TPACPI_ERR
1790 "could not create kernel thread "
1791 "for hotkey polling\n");
1794 } else {
1795 hotkey_poll_stop_sync();
1796 if (may_warn &&
1797 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1798 printk(TPACPI_NOTICE
1799 "hot keys 0x%08x require polling, "
1800 "which is currently disabled\n",
1801 hotkey_source_mask);
1806 static void hotkey_poll_setup_safe(int may_warn)
1808 mutex_lock(&hotkey_mutex);
1809 hotkey_poll_setup(may_warn);
1810 mutex_unlock(&hotkey_mutex);
1813 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1815 static void hotkey_poll_setup_safe(int __unused)
1819 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1821 static int hotkey_inputdev_open(struct input_dev *dev)
1823 switch (tpacpi_lifecycle) {
1824 case TPACPI_LIFE_INIT:
1826 * hotkey_init will call hotkey_poll_setup_safe
1827 * at the appropriate moment
1829 return 0;
1830 case TPACPI_LIFE_EXITING:
1831 return -EBUSY;
1832 case TPACPI_LIFE_RUNNING:
1833 hotkey_poll_setup_safe(0);
1834 return 0;
1837 /* Should only happen if tpacpi_lifecycle is corrupt */
1838 BUG();
1839 return -EBUSY;
1842 static void hotkey_inputdev_close(struct input_dev *dev)
1844 /* disable hotkey polling when possible */
1845 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1846 hotkey_poll_setup_safe(0);
1849 /* sysfs hotkey enable ------------------------------------------------- */
1850 static ssize_t hotkey_enable_show(struct device *dev,
1851 struct device_attribute *attr,
1852 char *buf)
1854 int res, status;
1856 res = hotkey_status_get(&status);
1857 if (res)
1858 return res;
1860 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1863 static ssize_t hotkey_enable_store(struct device *dev,
1864 struct device_attribute *attr,
1865 const char *buf, size_t count)
1867 unsigned long t;
1868 int res;
1870 if (parse_strtoul(buf, 1, &t))
1871 return -EINVAL;
1873 res = hotkey_status_set(t);
1875 return (res) ? res : count;
1878 static struct device_attribute dev_attr_hotkey_enable =
1879 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1880 hotkey_enable_show, hotkey_enable_store);
1882 /* sysfs hotkey mask --------------------------------------------------- */
1883 static ssize_t hotkey_mask_show(struct device *dev,
1884 struct device_attribute *attr,
1885 char *buf)
1887 int res;
1889 if (mutex_lock_killable(&hotkey_mutex))
1890 return -ERESTARTSYS;
1891 res = hotkey_mask_get();
1892 mutex_unlock(&hotkey_mutex);
1894 return (res)?
1895 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1898 static ssize_t hotkey_mask_store(struct device *dev,
1899 struct device_attribute *attr,
1900 const char *buf, size_t count)
1902 unsigned long t;
1903 int res;
1905 if (parse_strtoul(buf, 0xffffffffUL, &t))
1906 return -EINVAL;
1908 if (mutex_lock_killable(&hotkey_mutex))
1909 return -ERESTARTSYS;
1911 res = hotkey_mask_set(t);
1913 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1914 hotkey_poll_setup(1);
1915 #endif
1917 mutex_unlock(&hotkey_mutex);
1919 return (res) ? res : count;
1922 static struct device_attribute dev_attr_hotkey_mask =
1923 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1924 hotkey_mask_show, hotkey_mask_store);
1926 /* sysfs hotkey bios_enabled ------------------------------------------- */
1927 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1928 struct device_attribute *attr,
1929 char *buf)
1931 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1934 static struct device_attribute dev_attr_hotkey_bios_enabled =
1935 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1937 /* sysfs hotkey bios_mask ---------------------------------------------- */
1938 static ssize_t hotkey_bios_mask_show(struct device *dev,
1939 struct device_attribute *attr,
1940 char *buf)
1942 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1945 static struct device_attribute dev_attr_hotkey_bios_mask =
1946 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1948 /* sysfs hotkey all_mask ----------------------------------------------- */
1949 static ssize_t hotkey_all_mask_show(struct device *dev,
1950 struct device_attribute *attr,
1951 char *buf)
1953 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1954 hotkey_all_mask | hotkey_source_mask);
1957 static struct device_attribute dev_attr_hotkey_all_mask =
1958 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1960 /* sysfs hotkey recommended_mask --------------------------------------- */
1961 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1962 struct device_attribute *attr,
1963 char *buf)
1965 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1966 (hotkey_all_mask | hotkey_source_mask)
1967 & ~hotkey_reserved_mask);
1970 static struct device_attribute dev_attr_hotkey_recommended_mask =
1971 __ATTR(hotkey_recommended_mask, S_IRUGO,
1972 hotkey_recommended_mask_show, NULL);
1974 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1976 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1977 static ssize_t hotkey_source_mask_show(struct device *dev,
1978 struct device_attribute *attr,
1979 char *buf)
1981 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1984 static ssize_t hotkey_source_mask_store(struct device *dev,
1985 struct device_attribute *attr,
1986 const char *buf, size_t count)
1988 unsigned long t;
1990 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1991 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1992 return -EINVAL;
1994 if (mutex_lock_killable(&hotkey_mutex))
1995 return -ERESTARTSYS;
1997 HOTKEY_CONFIG_CRITICAL_START
1998 hotkey_source_mask = t;
1999 HOTKEY_CONFIG_CRITICAL_END
2001 hotkey_poll_setup(1);
2003 mutex_unlock(&hotkey_mutex);
2005 return count;
2008 static struct device_attribute dev_attr_hotkey_source_mask =
2009 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2010 hotkey_source_mask_show, hotkey_source_mask_store);
2012 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2013 static ssize_t hotkey_poll_freq_show(struct device *dev,
2014 struct device_attribute *attr,
2015 char *buf)
2017 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2020 static ssize_t hotkey_poll_freq_store(struct device *dev,
2021 struct device_attribute *attr,
2022 const char *buf, size_t count)
2024 unsigned long t;
2026 if (parse_strtoul(buf, 25, &t))
2027 return -EINVAL;
2029 if (mutex_lock_killable(&hotkey_mutex))
2030 return -ERESTARTSYS;
2032 hotkey_poll_freq = t;
2034 hotkey_poll_setup(1);
2035 mutex_unlock(&hotkey_mutex);
2037 return count;
2040 static struct device_attribute dev_attr_hotkey_poll_freq =
2041 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2042 hotkey_poll_freq_show, hotkey_poll_freq_store);
2044 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2046 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2047 static ssize_t hotkey_radio_sw_show(struct device *dev,
2048 struct device_attribute *attr,
2049 char *buf)
2051 int res, s;
2052 res = hotkey_get_wlsw(&s);
2053 if (res < 0)
2054 return res;
2056 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2059 static struct device_attribute dev_attr_hotkey_radio_sw =
2060 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2062 static void hotkey_radio_sw_notify_change(void)
2064 if (tp_features.hotkey_wlsw)
2065 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2066 "hotkey_radio_sw");
2069 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2070 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2071 struct device_attribute *attr,
2072 char *buf)
2074 int res, s;
2075 res = hotkey_get_tablet_mode(&s);
2076 if (res < 0)
2077 return res;
2079 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2082 static struct device_attribute dev_attr_hotkey_tablet_mode =
2083 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2085 static void hotkey_tablet_mode_notify_change(void)
2087 if (tp_features.hotkey_tablet)
2088 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2089 "hotkey_tablet_mode");
2092 /* sysfs hotkey report_mode -------------------------------------------- */
2093 static ssize_t hotkey_report_mode_show(struct device *dev,
2094 struct device_attribute *attr,
2095 char *buf)
2097 return snprintf(buf, PAGE_SIZE, "%d\n",
2098 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2101 static struct device_attribute dev_attr_hotkey_report_mode =
2102 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2104 /* sysfs wakeup reason (pollable) -------------------------------------- */
2105 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2106 struct device_attribute *attr,
2107 char *buf)
2109 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2112 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2113 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2115 static void hotkey_wakeup_reason_notify_change(void)
2117 if (tp_features.hotkey_mask)
2118 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2119 "wakeup_reason");
2122 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2123 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2124 struct device_attribute *attr,
2125 char *buf)
2127 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2130 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2131 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2132 hotkey_wakeup_hotunplug_complete_show, NULL);
2134 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2136 if (tp_features.hotkey_mask)
2137 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2138 "wakeup_hotunplug_complete");
2141 /* --------------------------------------------------------------------- */
2143 static struct attribute *hotkey_attributes[] __initdata = {
2144 &dev_attr_hotkey_enable.attr,
2145 &dev_attr_hotkey_bios_enabled.attr,
2146 &dev_attr_hotkey_report_mode.attr,
2147 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2148 &dev_attr_hotkey_mask.attr,
2149 &dev_attr_hotkey_all_mask.attr,
2150 &dev_attr_hotkey_recommended_mask.attr,
2151 &dev_attr_hotkey_source_mask.attr,
2152 &dev_attr_hotkey_poll_freq.attr,
2153 #endif
2156 static struct attribute *hotkey_mask_attributes[] __initdata = {
2157 &dev_attr_hotkey_bios_mask.attr,
2158 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2159 &dev_attr_hotkey_mask.attr,
2160 &dev_attr_hotkey_all_mask.attr,
2161 &dev_attr_hotkey_recommended_mask.attr,
2162 #endif
2163 &dev_attr_hotkey_wakeup_reason.attr,
2164 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2167 static void bluetooth_update_rfk(void);
2168 static void wan_update_rfk(void);
2169 static void uwb_update_rfk(void);
2170 static void tpacpi_send_radiosw_update(void)
2172 int wlsw;
2174 /* Sync these BEFORE sending any rfkill events */
2175 if (tp_features.bluetooth)
2176 bluetooth_update_rfk();
2177 if (tp_features.wan)
2178 wan_update_rfk();
2179 if (tp_features.uwb)
2180 uwb_update_rfk();
2182 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2183 mutex_lock(&tpacpi_inputdev_send_mutex);
2185 input_report_switch(tpacpi_inputdev,
2186 SW_RFKILL_ALL, !!wlsw);
2187 input_sync(tpacpi_inputdev);
2189 mutex_unlock(&tpacpi_inputdev_send_mutex);
2191 hotkey_radio_sw_notify_change();
2194 static void hotkey_exit(void)
2196 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2197 hotkey_poll_stop_sync();
2198 #endif
2200 if (hotkey_dev_attributes)
2201 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2203 kfree(hotkey_keycode_map);
2205 if (tp_features.hotkey) {
2206 dbg_printk(TPACPI_DBG_EXIT,
2207 "restoring original hot key mask\n");
2208 /* no short-circuit boolean operator below! */
2209 if ((hotkey_mask_set(hotkey_orig_mask) |
2210 hotkey_status_set(hotkey_orig_status)) != 0)
2211 printk(TPACPI_ERR
2212 "failed to restore hot key mask "
2213 "to BIOS defaults\n");
2217 static int __init hotkey_init(struct ibm_init_struct *iibm)
2219 /* Requirements for changing the default keymaps:
2221 * 1. Many of the keys are mapped to KEY_RESERVED for very
2222 * good reasons. Do not change them unless you have deep
2223 * knowledge on the IBM and Lenovo ThinkPad firmware for
2224 * the various ThinkPad models. The driver behaves
2225 * differently for KEY_RESERVED: such keys have their
2226 * hot key mask *unset* in mask_recommended, and also
2227 * in the initial hot key mask programmed into the
2228 * firmware at driver load time, which means the firm-
2229 * ware may react very differently if you change them to
2230 * something else;
2232 * 2. You must be subscribed to the linux-thinkpad and
2233 * ibm-acpi-devel mailing lists, and you should read the
2234 * list archives since 2007 if you want to change the
2235 * keymaps. This requirement exists so that you will
2236 * know the past history of problems with the thinkpad-
2237 * acpi driver keymaps, and also that you will be
2238 * listening to any bug reports;
2240 * 3. Do not send thinkpad-acpi specific patches directly to
2241 * for merging, *ever*. Send them to the linux-acpi
2242 * mailinglist for comments. Merging is to be done only
2243 * through acpi-test and the ACPI maintainer.
2245 * If the above is too much to ask, don't change the keymap.
2246 * Ask the thinkpad-acpi maintainer to do it, instead.
2248 static u16 ibm_keycode_map[] __initdata = {
2249 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2250 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2251 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2252 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2254 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2255 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2256 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2257 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2259 /* brightness: firmware always reacts to them, unless
2260 * X.org did some tricks in the radeon BIOS scratch
2261 * registers of *some* models */
2262 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2263 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2265 /* Thinklight: firmware always react to it */
2266 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2268 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2269 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2271 /* Volume: firmware always react to it and reprograms
2272 * the built-in *extra* mixer. Never map it to control
2273 * another mixer by default. */
2274 KEY_RESERVED, /* 0x14: VOLUME UP */
2275 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2276 KEY_RESERVED, /* 0x16: MUTE */
2278 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2280 /* (assignments unknown, please report if found) */
2281 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2282 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2284 static u16 lenovo_keycode_map[] __initdata = {
2285 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2286 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2287 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2288 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2290 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2291 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2292 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2293 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2295 /* These either have to go through ACPI video, or
2296 * act like in the IBM ThinkPads, so don't ever
2297 * enable them by default */
2298 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2299 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2301 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2303 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2304 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2306 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2307 * react to it and reprograms the built-in *extra* mixer.
2308 * Never map it to control another mixer by default.
2310 * T60?, T61, R60?, R61: firmware and EC tries to send
2311 * these over the regular keyboard, so these are no-ops,
2312 * but there are still weird bugs re. MUTE, so do not
2313 * change unless you get test reports from all Lenovo
2314 * models. May cause the BIOS to interfere with the
2315 * HDA mixer.
2317 KEY_RESERVED, /* 0x14: VOLUME UP */
2318 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2319 KEY_RESERVED, /* 0x16: MUTE */
2321 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2323 /* (assignments unknown, please report if found) */
2324 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2328 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2329 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2330 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2332 int res, i;
2333 int status;
2334 int hkeyv;
2336 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2338 BUG_ON(!tpacpi_inputdev);
2339 BUG_ON(tpacpi_inputdev->open != NULL ||
2340 tpacpi_inputdev->close != NULL);
2342 TPACPI_ACPIHANDLE_INIT(hkey);
2343 mutex_init(&hotkey_mutex);
2345 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2346 mutex_init(&hotkey_thread_mutex);
2347 mutex_init(&hotkey_thread_data_mutex);
2348 #endif
2350 /* hotkey not supported on 570 */
2351 tp_features.hotkey = hkey_handle != NULL;
2353 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2354 str_supported(tp_features.hotkey));
2356 if (!tp_features.hotkey)
2357 return 1;
2359 tpacpi_disable_brightness_delay();
2361 hotkey_dev_attributes = create_attr_set(13, NULL);
2362 if (!hotkey_dev_attributes)
2363 return -ENOMEM;
2364 res = add_many_to_attr_set(hotkey_dev_attributes,
2365 hotkey_attributes,
2366 ARRAY_SIZE(hotkey_attributes));
2367 if (res)
2368 goto err_exit;
2370 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2371 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2372 for HKEY interface version 0x100 */
2373 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2374 if ((hkeyv >> 8) != 1) {
2375 printk(TPACPI_ERR "unknown version of the "
2376 "HKEY interface: 0x%x\n", hkeyv);
2377 printk(TPACPI_ERR "please report this to %s\n",
2378 TPACPI_MAIL);
2379 } else {
2381 * MHKV 0x100 in A31, R40, R40e,
2382 * T4x, X31, and later
2384 tp_features.hotkey_mask = 1;
2388 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2389 str_supported(tp_features.hotkey_mask));
2391 if (tp_features.hotkey_mask) {
2392 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2393 "MHKA", "qd")) {
2394 printk(TPACPI_ERR
2395 "missing MHKA handler, "
2396 "please report this to %s\n",
2397 TPACPI_MAIL);
2398 /* FN+F12, FN+F4, FN+F3 */
2399 hotkey_all_mask = 0x080cU;
2403 /* hotkey_source_mask *must* be zero for
2404 * the first hotkey_mask_get */
2405 res = hotkey_status_get(&hotkey_orig_status);
2406 if (res)
2407 goto err_exit;
2409 if (tp_features.hotkey_mask) {
2410 res = hotkey_mask_get();
2411 if (res)
2412 goto err_exit;
2414 hotkey_orig_mask = hotkey_mask;
2415 res = add_many_to_attr_set(
2416 hotkey_dev_attributes,
2417 hotkey_mask_attributes,
2418 ARRAY_SIZE(hotkey_mask_attributes));
2419 if (res)
2420 goto err_exit;
2423 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2424 if (tp_features.hotkey_mask) {
2425 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2426 & ~hotkey_all_mask;
2427 } else {
2428 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2431 vdbg_printk(TPACPI_DBG_INIT,
2432 "hotkey source mask 0x%08x, polling freq %d\n",
2433 hotkey_source_mask, hotkey_poll_freq);
2434 #endif
2436 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2437 if (dbg_wlswemul) {
2438 tp_features.hotkey_wlsw = 1;
2439 printk(TPACPI_INFO
2440 "radio switch emulation enabled\n");
2441 } else
2442 #endif
2443 /* Not all thinkpads have a hardware radio switch */
2444 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2445 tp_features.hotkey_wlsw = 1;
2446 printk(TPACPI_INFO
2447 "radio switch found; radios are %s\n",
2448 enabled(status, 0));
2450 if (tp_features.hotkey_wlsw)
2451 res = add_to_attr_set(hotkey_dev_attributes,
2452 &dev_attr_hotkey_radio_sw.attr);
2454 /* For X41t, X60t, X61t Tablets... */
2455 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2456 tp_features.hotkey_tablet = 1;
2457 printk(TPACPI_INFO
2458 "possible tablet mode switch found; "
2459 "ThinkPad in %s mode\n",
2460 (status & TP_HOTKEY_TABLET_MASK)?
2461 "tablet" : "laptop");
2462 res = add_to_attr_set(hotkey_dev_attributes,
2463 &dev_attr_hotkey_tablet_mode.attr);
2466 if (!res)
2467 res = register_attr_set_with_sysfs(
2468 hotkey_dev_attributes,
2469 &tpacpi_pdev->dev.kobj);
2470 if (res)
2471 goto err_exit;
2473 /* Set up key map */
2475 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2476 GFP_KERNEL);
2477 if (!hotkey_keycode_map) {
2478 printk(TPACPI_ERR
2479 "failed to allocate memory for key map\n");
2480 res = -ENOMEM;
2481 goto err_exit;
2484 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2485 dbg_printk(TPACPI_DBG_INIT,
2486 "using Lenovo default hot key map\n");
2487 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2488 TPACPI_HOTKEY_MAP_SIZE);
2489 } else {
2490 dbg_printk(TPACPI_DBG_INIT,
2491 "using IBM default hot key map\n");
2492 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2493 TPACPI_HOTKEY_MAP_SIZE);
2496 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2497 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2498 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2499 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2500 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2501 tpacpi_inputdev->keycode = hotkey_keycode_map;
2502 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2503 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2504 set_bit(hotkey_keycode_map[i],
2505 tpacpi_inputdev->keybit);
2506 } else {
2507 if (i < sizeof(hotkey_reserved_mask)*8)
2508 hotkey_reserved_mask |= 1 << i;
2512 if (tp_features.hotkey_wlsw) {
2513 set_bit(EV_SW, tpacpi_inputdev->evbit);
2514 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2516 if (tp_features.hotkey_tablet) {
2517 set_bit(EV_SW, tpacpi_inputdev->evbit);
2518 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2521 /* Do not issue duplicate brightness change events to
2522 * userspace */
2523 if (!tp_features.bright_acpimode)
2524 /* update bright_acpimode... */
2525 tpacpi_check_std_acpi_brightness_support();
2527 if (tp_features.bright_acpimode) {
2528 printk(TPACPI_INFO
2529 "This ThinkPad has standard ACPI backlight "
2530 "brightness control, supported by the ACPI "
2531 "video driver\n");
2532 printk(TPACPI_NOTICE
2533 "Disabling thinkpad-acpi brightness events "
2534 "by default...\n");
2536 /* The hotkey_reserved_mask change below is not
2537 * necessary while the keys are at KEY_RESERVED in the
2538 * default map, but better safe than sorry, leave it
2539 * here as a marker of what we have to do, especially
2540 * when we finally become able to set this at runtime
2541 * on response to X.org requests */
2542 hotkey_reserved_mask |=
2543 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2544 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2547 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2548 res = hotkey_status_set(1);
2549 if (res) {
2550 hotkey_exit();
2551 return res;
2553 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2554 & ~hotkey_reserved_mask)
2555 | hotkey_orig_mask);
2556 if (res < 0 && res != -ENXIO) {
2557 hotkey_exit();
2558 return res;
2561 dbg_printk(TPACPI_DBG_INIT,
2562 "legacy hot key reporting over procfs %s\n",
2563 (hotkey_report_mode < 2) ?
2564 "enabled" : "disabled");
2566 tpacpi_inputdev->open = &hotkey_inputdev_open;
2567 tpacpi_inputdev->close = &hotkey_inputdev_close;
2569 hotkey_poll_setup_safe(1);
2570 tpacpi_send_radiosw_update();
2571 tpacpi_input_send_tabletsw();
2573 return 0;
2575 err_exit:
2576 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2577 hotkey_dev_attributes = NULL;
2579 return (res < 0)? res : 1;
2582 static bool hotkey_notify_hotkey(const u32 hkey,
2583 bool *send_acpi_ev,
2584 bool *ignore_acpi_ev)
2586 /* 0x1000-0x1FFF: key presses */
2587 unsigned int scancode = hkey & 0xfff;
2588 *send_acpi_ev = true;
2589 *ignore_acpi_ev = false;
2591 if (scancode > 0 && scancode < 0x21) {
2592 scancode--;
2593 if (!(hotkey_source_mask & (1 << scancode))) {
2594 tpacpi_input_send_key(scancode);
2595 *send_acpi_ev = false;
2596 } else {
2597 *ignore_acpi_ev = true;
2599 return true;
2601 return false;
2604 static bool hotkey_notify_wakeup(const u32 hkey,
2605 bool *send_acpi_ev,
2606 bool *ignore_acpi_ev)
2608 /* 0x2000-0x2FFF: Wakeup reason */
2609 *send_acpi_ev = true;
2610 *ignore_acpi_ev = false;
2612 switch (hkey) {
2613 case 0x2304: /* suspend, undock */
2614 case 0x2404: /* hibernation, undock */
2615 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2616 *ignore_acpi_ev = true;
2617 break;
2619 case 0x2305: /* suspend, bay eject */
2620 case 0x2405: /* hibernation, bay eject */
2621 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2622 *ignore_acpi_ev = true;
2623 break;
2625 case 0x2313: /* Battery on critical low level (S3) */
2626 case 0x2413: /* Battery on critical low level (S4) */
2627 printk(TPACPI_ALERT
2628 "EMERGENCY WAKEUP: battery almost empty\n");
2629 /* how to auto-heal: */
2630 /* 2313: woke up from S3, go to S4/S5 */
2631 /* 2413: woke up from S4, go to S5 */
2632 break;
2634 default:
2635 return false;
2638 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2639 printk(TPACPI_INFO
2640 "woke up due to a hot-unplug "
2641 "request...\n");
2642 hotkey_wakeup_reason_notify_change();
2644 return true;
2647 static bool hotkey_notify_usrevent(const u32 hkey,
2648 bool *send_acpi_ev,
2649 bool *ignore_acpi_ev)
2651 /* 0x5000-0x5FFF: human interface helpers */
2652 *send_acpi_ev = true;
2653 *ignore_acpi_ev = false;
2655 switch (hkey) {
2656 case 0x5010: /* Lenovo new BIOS: brightness changed */
2657 case 0x500b: /* X61t: tablet pen inserted into bay */
2658 case 0x500c: /* X61t: tablet pen removed from bay */
2659 return true;
2661 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2662 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2663 tpacpi_input_send_tabletsw();
2664 hotkey_tablet_mode_notify_change();
2665 *send_acpi_ev = false;
2666 return true;
2668 case 0x5001:
2669 case 0x5002:
2670 /* LID switch events. Do not propagate */
2671 *ignore_acpi_ev = true;
2672 return true;
2674 default:
2675 return false;
2679 static bool hotkey_notify_thermal(const u32 hkey,
2680 bool *send_acpi_ev,
2681 bool *ignore_acpi_ev)
2683 /* 0x6000-0x6FFF: thermal alarms */
2684 *send_acpi_ev = true;
2685 *ignore_acpi_ev = false;
2687 switch (hkey) {
2688 case 0x6011:
2689 printk(TPACPI_CRIT
2690 "THERMAL ALARM: battery is too hot!\n");
2691 /* recommended action: warn user through gui */
2692 return true;
2693 case 0x6012:
2694 printk(TPACPI_ALERT
2695 "THERMAL EMERGENCY: battery is extremely hot!\n");
2696 /* recommended action: immediate sleep/hibernate */
2697 return true;
2698 case 0x6021:
2699 printk(TPACPI_CRIT
2700 "THERMAL ALARM: "
2701 "a sensor reports something is too hot!\n");
2702 /* recommended action: warn user through gui, that */
2703 /* some internal component is too hot */
2704 return true;
2705 case 0x6022:
2706 printk(TPACPI_ALERT
2707 "THERMAL EMERGENCY: "
2708 "a sensor reports something is extremely hot!\n");
2709 /* recommended action: immediate sleep/hibernate */
2710 return true;
2711 case 0x6030:
2712 printk(TPACPI_INFO
2713 "EC reports that Thermal Table has changed\n");
2714 /* recommended action: do nothing, we don't have
2715 * Lenovo ATM information */
2716 return true;
2717 default:
2718 printk(TPACPI_ALERT
2719 "THERMAL ALERT: unknown thermal alarm received\n");
2720 return false;
2724 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2726 u32 hkey;
2727 bool send_acpi_ev;
2728 bool ignore_acpi_ev;
2729 bool known_ev;
2731 if (event != 0x80) {
2732 printk(TPACPI_ERR
2733 "unknown HKEY notification event %d\n", event);
2734 /* forward it to userspace, maybe it knows how to handle it */
2735 acpi_bus_generate_netlink_event(
2736 ibm->acpi->device->pnp.device_class,
2737 dev_name(&ibm->acpi->device->dev),
2738 event, 0);
2739 return;
2742 while (1) {
2743 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2744 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2745 return;
2748 if (hkey == 0) {
2749 /* queue empty */
2750 return;
2753 send_acpi_ev = true;
2754 ignore_acpi_ev = false;
2756 switch (hkey >> 12) {
2757 case 1:
2758 /* 0x1000-0x1FFF: key presses */
2759 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2760 &ignore_acpi_ev);
2761 break;
2762 case 2:
2763 /* 0x2000-0x2FFF: Wakeup reason */
2764 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2765 &ignore_acpi_ev);
2766 break;
2767 case 3:
2768 /* 0x3000-0x3FFF: bay-related wakeups */
2769 if (hkey == 0x3003) {
2770 hotkey_autosleep_ack = 1;
2771 printk(TPACPI_INFO
2772 "bay ejected\n");
2773 hotkey_wakeup_hotunplug_complete_notify_change();
2774 known_ev = true;
2775 } else {
2776 known_ev = false;
2778 break;
2779 case 4:
2780 /* 0x4000-0x4FFF: dock-related wakeups */
2781 if (hkey == 0x4003) {
2782 hotkey_autosleep_ack = 1;
2783 printk(TPACPI_INFO
2784 "undocked\n");
2785 hotkey_wakeup_hotunplug_complete_notify_change();
2786 known_ev = true;
2787 } else {
2788 known_ev = false;
2790 break;
2791 case 5:
2792 /* 0x5000-0x5FFF: human interface helpers */
2793 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2794 &ignore_acpi_ev);
2795 break;
2796 case 6:
2797 /* 0x6000-0x6FFF: thermal alarms */
2798 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2799 &ignore_acpi_ev);
2800 break;
2801 case 7:
2802 /* 0x7000-0x7FFF: misc */
2803 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2804 tpacpi_send_radiosw_update();
2805 send_acpi_ev = 0;
2806 known_ev = true;
2807 break;
2809 /* fallthrough to default */
2810 default:
2811 known_ev = false;
2813 if (!known_ev) {
2814 printk(TPACPI_NOTICE
2815 "unhandled HKEY event 0x%04x\n", hkey);
2816 printk(TPACPI_NOTICE
2817 "please report the conditions when this "
2818 "event happened to %s\n", TPACPI_MAIL);
2821 /* Legacy events */
2822 if (!ignore_acpi_ev &&
2823 (send_acpi_ev || hotkey_report_mode < 2)) {
2824 acpi_bus_generate_proc_event(ibm->acpi->device,
2825 event, hkey);
2828 /* netlink events */
2829 if (!ignore_acpi_ev && send_acpi_ev) {
2830 acpi_bus_generate_netlink_event(
2831 ibm->acpi->device->pnp.device_class,
2832 dev_name(&ibm->acpi->device->dev),
2833 event, hkey);
2838 static void hotkey_suspend(pm_message_t state)
2840 /* Do these on suspend, we get the events on early resume! */
2841 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2842 hotkey_autosleep_ack = 0;
2845 static void hotkey_resume(void)
2847 tpacpi_disable_brightness_delay();
2849 if (hotkey_mask_get())
2850 printk(TPACPI_ERR
2851 "error while trying to read hot key mask "
2852 "from firmware\n");
2853 tpacpi_send_radiosw_update();
2854 hotkey_tablet_mode_notify_change();
2855 hotkey_wakeup_reason_notify_change();
2856 hotkey_wakeup_hotunplug_complete_notify_change();
2857 hotkey_poll_setup_safe(0);
2860 /* procfs -------------------------------------------------------------- */
2861 static int hotkey_read(char *p)
2863 int res, status;
2864 int len = 0;
2866 if (!tp_features.hotkey) {
2867 len += sprintf(p + len, "status:\t\tnot supported\n");
2868 return len;
2871 if (mutex_lock_killable(&hotkey_mutex))
2872 return -ERESTARTSYS;
2873 res = hotkey_status_get(&status);
2874 if (!res)
2875 res = hotkey_mask_get();
2876 mutex_unlock(&hotkey_mutex);
2877 if (res)
2878 return res;
2880 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2881 if (tp_features.hotkey_mask) {
2882 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2883 len += sprintf(p + len,
2884 "commands:\tenable, disable, reset, <mask>\n");
2885 } else {
2886 len += sprintf(p + len, "mask:\t\tnot supported\n");
2887 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2890 return len;
2893 static int hotkey_write(char *buf)
2895 int res, status;
2896 u32 mask;
2897 char *cmd;
2899 if (!tp_features.hotkey)
2900 return -ENODEV;
2902 if (mutex_lock_killable(&hotkey_mutex))
2903 return -ERESTARTSYS;
2905 status = -1;
2906 mask = hotkey_mask;
2908 res = 0;
2909 while ((cmd = next_cmd(&buf))) {
2910 if (strlencmp(cmd, "enable") == 0) {
2911 status = 1;
2912 } else if (strlencmp(cmd, "disable") == 0) {
2913 status = 0;
2914 } else if (strlencmp(cmd, "reset") == 0) {
2915 status = hotkey_orig_status;
2916 mask = hotkey_orig_mask;
2917 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2918 /* mask set */
2919 } else if (sscanf(cmd, "%x", &mask) == 1) {
2920 /* mask set */
2921 } else {
2922 res = -EINVAL;
2923 goto errexit;
2926 if (status != -1)
2927 res = hotkey_status_set(status);
2929 if (!res && mask != hotkey_mask)
2930 res = hotkey_mask_set(mask);
2932 errexit:
2933 mutex_unlock(&hotkey_mutex);
2934 return res;
2937 static const struct acpi_device_id ibm_htk_device_ids[] = {
2938 {TPACPI_ACPI_HKEY_HID, 0},
2939 {"", 0},
2942 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2943 .hid = ibm_htk_device_ids,
2944 .notify = hotkey_notify,
2945 .handle = &hkey_handle,
2946 .type = ACPI_DEVICE_NOTIFY,
2949 static struct ibm_struct hotkey_driver_data = {
2950 .name = "hotkey",
2951 .read = hotkey_read,
2952 .write = hotkey_write,
2953 .exit = hotkey_exit,
2954 .resume = hotkey_resume,
2955 .suspend = hotkey_suspend,
2956 .acpi = &ibm_hotkey_acpidriver,
2959 /*************************************************************************
2960 * Bluetooth subdriver
2963 enum {
2964 /* ACPI GBDC/SBDC bits */
2965 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2966 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2967 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2968 off / last state */
2971 enum {
2972 /* ACPI \BLTH commands */
2973 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2974 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2975 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2976 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2977 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2980 static struct rfkill *tpacpi_bluetooth_rfkill;
2982 static void bluetooth_suspend(pm_message_t state)
2984 /* Try to make sure radio will resume powered off */
2985 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2986 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2989 static int bluetooth_get_radiosw(void)
2991 int status;
2993 if (!tp_features.bluetooth)
2994 return -ENODEV;
2996 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2997 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2998 return RFKILL_STATE_HARD_BLOCKED;
3000 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3001 if (dbg_bluetoothemul)
3002 return (tpacpi_bluetooth_emulstate) ?
3003 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3004 #endif
3006 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3007 return -EIO;
3009 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3010 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3013 static void bluetooth_update_rfk(void)
3015 int status;
3017 if (!tpacpi_bluetooth_rfkill)
3018 return;
3020 status = bluetooth_get_radiosw();
3021 if (status < 0)
3022 return;
3023 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3026 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3028 int status;
3030 if (!tp_features.bluetooth)
3031 return -ENODEV;
3033 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3034 * reason to risk weird behaviour. */
3035 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3036 && radio_on)
3037 return -EPERM;
3039 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3040 if (dbg_bluetoothemul) {
3041 tpacpi_bluetooth_emulstate = !!radio_on;
3042 if (update_rfk)
3043 bluetooth_update_rfk();
3044 return 0;
3046 #endif
3048 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3049 if (radio_on)
3050 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3051 else
3052 status = 0;
3053 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3054 return -EIO;
3056 if (update_rfk)
3057 bluetooth_update_rfk();
3059 return 0;
3062 /* sysfs bluetooth enable ---------------------------------------------- */
3063 static ssize_t bluetooth_enable_show(struct device *dev,
3064 struct device_attribute *attr,
3065 char *buf)
3067 int status;
3069 status = bluetooth_get_radiosw();
3070 if (status < 0)
3071 return status;
3073 return snprintf(buf, PAGE_SIZE, "%d\n",
3074 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3077 static ssize_t bluetooth_enable_store(struct device *dev,
3078 struct device_attribute *attr,
3079 const char *buf, size_t count)
3081 unsigned long t;
3082 int res;
3084 if (parse_strtoul(buf, 1, &t))
3085 return -EINVAL;
3087 res = bluetooth_set_radiosw(t, 1);
3089 return (res) ? res : count;
3092 static struct device_attribute dev_attr_bluetooth_enable =
3093 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3094 bluetooth_enable_show, bluetooth_enable_store);
3096 /* --------------------------------------------------------------------- */
3098 static struct attribute *bluetooth_attributes[] = {
3099 &dev_attr_bluetooth_enable.attr,
3100 NULL
3103 static const struct attribute_group bluetooth_attr_group = {
3104 .attrs = bluetooth_attributes,
3107 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3109 int bts = bluetooth_get_radiosw();
3111 if (bts < 0)
3112 return bts;
3114 *state = bts;
3115 return 0;
3118 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3120 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3123 static void bluetooth_shutdown(void)
3125 /* Order firmware to save current state to NVRAM */
3126 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3127 TP_ACPI_BLTH_SAVE_STATE))
3128 printk(TPACPI_NOTICE
3129 "failed to save bluetooth state to NVRAM\n");
3132 static void bluetooth_exit(void)
3134 bluetooth_shutdown();
3136 if (tpacpi_bluetooth_rfkill)
3137 rfkill_unregister(tpacpi_bluetooth_rfkill);
3139 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3140 &bluetooth_attr_group);
3143 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3145 int res;
3146 int status = 0;
3148 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3150 TPACPI_ACPIHANDLE_INIT(hkey);
3152 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3153 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3154 tp_features.bluetooth = hkey_handle &&
3155 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3157 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3158 str_supported(tp_features.bluetooth),
3159 status);
3161 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3162 if (dbg_bluetoothemul) {
3163 tp_features.bluetooth = 1;
3164 printk(TPACPI_INFO
3165 "bluetooth switch emulation enabled\n");
3166 } else
3167 #endif
3168 if (tp_features.bluetooth &&
3169 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3170 /* no bluetooth hardware present in system */
3171 tp_features.bluetooth = 0;
3172 dbg_printk(TPACPI_DBG_INIT,
3173 "bluetooth hardware not installed\n");
3176 if (!tp_features.bluetooth)
3177 return 1;
3179 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3180 &bluetooth_attr_group);
3181 if (res)
3182 return res;
3184 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3185 &tpacpi_bluetooth_rfkill,
3186 RFKILL_TYPE_BLUETOOTH,
3187 "tpacpi_bluetooth_sw",
3188 true,
3189 tpacpi_bluetooth_rfk_set,
3190 tpacpi_bluetooth_rfk_get);
3191 if (res) {
3192 bluetooth_exit();
3193 return res;
3196 return 0;
3199 /* procfs -------------------------------------------------------------- */
3200 static int bluetooth_read(char *p)
3202 int len = 0;
3203 int status = bluetooth_get_radiosw();
3205 if (!tp_features.bluetooth)
3206 len += sprintf(p + len, "status:\t\tnot supported\n");
3207 else {
3208 len += sprintf(p + len, "status:\t\t%s\n",
3209 (status == RFKILL_STATE_UNBLOCKED) ?
3210 "enabled" : "disabled");
3211 len += sprintf(p + len, "commands:\tenable, disable\n");
3214 return len;
3217 static int bluetooth_write(char *buf)
3219 char *cmd;
3221 if (!tp_features.bluetooth)
3222 return -ENODEV;
3224 while ((cmd = next_cmd(&buf))) {
3225 if (strlencmp(cmd, "enable") == 0) {
3226 bluetooth_set_radiosw(1, 1);
3227 } else if (strlencmp(cmd, "disable") == 0) {
3228 bluetooth_set_radiosw(0, 1);
3229 } else
3230 return -EINVAL;
3233 return 0;
3236 static struct ibm_struct bluetooth_driver_data = {
3237 .name = "bluetooth",
3238 .read = bluetooth_read,
3239 .write = bluetooth_write,
3240 .exit = bluetooth_exit,
3241 .suspend = bluetooth_suspend,
3242 .shutdown = bluetooth_shutdown,
3245 /*************************************************************************
3246 * Wan subdriver
3249 enum {
3250 /* ACPI GWAN/SWAN bits */
3251 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3252 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3253 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3254 off / last state */
3257 static struct rfkill *tpacpi_wan_rfkill;
3259 static void wan_suspend(pm_message_t state)
3261 /* Try to make sure radio will resume powered off */
3262 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3263 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3266 static int wan_get_radiosw(void)
3268 int status;
3270 if (!tp_features.wan)
3271 return -ENODEV;
3273 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3274 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3275 return RFKILL_STATE_HARD_BLOCKED;
3277 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3278 if (dbg_wwanemul)
3279 return (tpacpi_wwan_emulstate) ?
3280 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3281 #endif
3283 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3284 return -EIO;
3286 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3287 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3290 static void wan_update_rfk(void)
3292 int status;
3294 if (!tpacpi_wan_rfkill)
3295 return;
3297 status = wan_get_radiosw();
3298 if (status < 0)
3299 return;
3300 rfkill_force_state(tpacpi_wan_rfkill, status);
3303 static int wan_set_radiosw(int radio_on, int update_rfk)
3305 int status;
3307 if (!tp_features.wan)
3308 return -ENODEV;
3310 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3311 * reason to risk weird behaviour. */
3312 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3313 && radio_on)
3314 return -EPERM;
3316 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3317 if (dbg_wwanemul) {
3318 tpacpi_wwan_emulstate = !!radio_on;
3319 if (update_rfk)
3320 wan_update_rfk();
3321 return 0;
3323 #endif
3325 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3326 if (radio_on)
3327 status = TP_ACPI_WANCARD_RADIOSSW;
3328 else
3329 status = 0;
3330 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3331 return -EIO;
3333 if (update_rfk)
3334 wan_update_rfk();
3336 return 0;
3339 /* sysfs wan enable ---------------------------------------------------- */
3340 static ssize_t wan_enable_show(struct device *dev,
3341 struct device_attribute *attr,
3342 char *buf)
3344 int status;
3346 status = wan_get_radiosw();
3347 if (status < 0)
3348 return status;
3350 return snprintf(buf, PAGE_SIZE, "%d\n",
3351 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3354 static ssize_t wan_enable_store(struct device *dev,
3355 struct device_attribute *attr,
3356 const char *buf, size_t count)
3358 unsigned long t;
3359 int res;
3361 if (parse_strtoul(buf, 1, &t))
3362 return -EINVAL;
3364 res = wan_set_radiosw(t, 1);
3366 return (res) ? res : count;
3369 static struct device_attribute dev_attr_wan_enable =
3370 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3371 wan_enable_show, wan_enable_store);
3373 /* --------------------------------------------------------------------- */
3375 static struct attribute *wan_attributes[] = {
3376 &dev_attr_wan_enable.attr,
3377 NULL
3380 static const struct attribute_group wan_attr_group = {
3381 .attrs = wan_attributes,
3384 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3386 int wans = wan_get_radiosw();
3388 if (wans < 0)
3389 return wans;
3391 *state = wans;
3392 return 0;
3395 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3397 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3400 static void wan_shutdown(void)
3402 /* Order firmware to save current state to NVRAM */
3403 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3404 TP_ACPI_WGSV_SAVE_STATE))
3405 printk(TPACPI_NOTICE
3406 "failed to save WWAN state to NVRAM\n");
3409 static void wan_exit(void)
3411 wan_shutdown();
3413 if (tpacpi_wan_rfkill)
3414 rfkill_unregister(tpacpi_wan_rfkill);
3416 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3417 &wan_attr_group);
3420 static int __init wan_init(struct ibm_init_struct *iibm)
3422 int res;
3423 int status = 0;
3425 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3427 TPACPI_ACPIHANDLE_INIT(hkey);
3429 tp_features.wan = hkey_handle &&
3430 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3432 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3433 str_supported(tp_features.wan),
3434 status);
3436 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3437 if (dbg_wwanemul) {
3438 tp_features.wan = 1;
3439 printk(TPACPI_INFO
3440 "wwan switch emulation enabled\n");
3441 } else
3442 #endif
3443 if (tp_features.wan &&
3444 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3445 /* no wan hardware present in system */
3446 tp_features.wan = 0;
3447 dbg_printk(TPACPI_DBG_INIT,
3448 "wan hardware not installed\n");
3451 if (!tp_features.wan)
3452 return 1;
3454 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3455 &wan_attr_group);
3456 if (res)
3457 return res;
3459 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3460 &tpacpi_wan_rfkill,
3461 RFKILL_TYPE_WWAN,
3462 "tpacpi_wwan_sw",
3463 true,
3464 tpacpi_wan_rfk_set,
3465 tpacpi_wan_rfk_get);
3466 if (res) {
3467 wan_exit();
3468 return res;
3471 return 0;
3474 /* procfs -------------------------------------------------------------- */
3475 static int wan_read(char *p)
3477 int len = 0;
3478 int status = wan_get_radiosw();
3480 if (!tp_features.wan)
3481 len += sprintf(p + len, "status:\t\tnot supported\n");
3482 else {
3483 len += sprintf(p + len, "status:\t\t%s\n",
3484 (status == RFKILL_STATE_UNBLOCKED) ?
3485 "enabled" : "disabled");
3486 len += sprintf(p + len, "commands:\tenable, disable\n");
3489 return len;
3492 static int wan_write(char *buf)
3494 char *cmd;
3496 if (!tp_features.wan)
3497 return -ENODEV;
3499 while ((cmd = next_cmd(&buf))) {
3500 if (strlencmp(cmd, "enable") == 0) {
3501 wan_set_radiosw(1, 1);
3502 } else if (strlencmp(cmd, "disable") == 0) {
3503 wan_set_radiosw(0, 1);
3504 } else
3505 return -EINVAL;
3508 return 0;
3511 static struct ibm_struct wan_driver_data = {
3512 .name = "wan",
3513 .read = wan_read,
3514 .write = wan_write,
3515 .exit = wan_exit,
3516 .suspend = wan_suspend,
3517 .shutdown = wan_shutdown,
3520 /*************************************************************************
3521 * UWB subdriver
3524 enum {
3525 /* ACPI GUWB/SUWB bits */
3526 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3527 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3530 static struct rfkill *tpacpi_uwb_rfkill;
3532 static int uwb_get_radiosw(void)
3534 int status;
3536 if (!tp_features.uwb)
3537 return -ENODEV;
3539 /* WLSW overrides UWB in firmware/hardware, reflect that */
3540 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3541 return RFKILL_STATE_HARD_BLOCKED;
3543 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3544 if (dbg_uwbemul)
3545 return (tpacpi_uwb_emulstate) ?
3546 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3547 #endif
3549 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3550 return -EIO;
3552 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3553 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3556 static void uwb_update_rfk(void)
3558 int status;
3560 if (!tpacpi_uwb_rfkill)
3561 return;
3563 status = uwb_get_radiosw();
3564 if (status < 0)
3565 return;
3566 rfkill_force_state(tpacpi_uwb_rfkill, status);
3569 static int uwb_set_radiosw(int radio_on, int update_rfk)
3571 int status;
3573 if (!tp_features.uwb)
3574 return -ENODEV;
3576 /* WLSW overrides UWB in firmware/hardware, but there is no
3577 * reason to risk weird behaviour. */
3578 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3579 && radio_on)
3580 return -EPERM;
3582 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3583 if (dbg_uwbemul) {
3584 tpacpi_uwb_emulstate = !!radio_on;
3585 if (update_rfk)
3586 uwb_update_rfk();
3587 return 0;
3589 #endif
3591 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3592 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3593 return -EIO;
3595 if (update_rfk)
3596 uwb_update_rfk();
3598 return 0;
3601 /* --------------------------------------------------------------------- */
3603 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3605 int uwbs = uwb_get_radiosw();
3607 if (uwbs < 0)
3608 return uwbs;
3610 *state = uwbs;
3611 return 0;
3614 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3616 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3619 static void uwb_exit(void)
3621 if (tpacpi_uwb_rfkill)
3622 rfkill_unregister(tpacpi_uwb_rfkill);
3625 static int __init uwb_init(struct ibm_init_struct *iibm)
3627 int res;
3628 int status = 0;
3630 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3632 TPACPI_ACPIHANDLE_INIT(hkey);
3634 tp_features.uwb = hkey_handle &&
3635 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3637 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3638 str_supported(tp_features.uwb),
3639 status);
3641 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3642 if (dbg_uwbemul) {
3643 tp_features.uwb = 1;
3644 printk(TPACPI_INFO
3645 "uwb switch emulation enabled\n");
3646 } else
3647 #endif
3648 if (tp_features.uwb &&
3649 !(status & TP_ACPI_UWB_HWPRESENT)) {
3650 /* no uwb hardware present in system */
3651 tp_features.uwb = 0;
3652 dbg_printk(TPACPI_DBG_INIT,
3653 "uwb hardware not installed\n");
3656 if (!tp_features.uwb)
3657 return 1;
3659 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3660 &tpacpi_uwb_rfkill,
3661 RFKILL_TYPE_UWB,
3662 "tpacpi_uwb_sw",
3663 false,
3664 tpacpi_uwb_rfk_set,
3665 tpacpi_uwb_rfk_get);
3667 return res;
3670 static struct ibm_struct uwb_driver_data = {
3671 .name = "uwb",
3672 .exit = uwb_exit,
3673 .flags.experimental = 1,
3676 /*************************************************************************
3677 * Video subdriver
3680 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3682 enum video_access_mode {
3683 TPACPI_VIDEO_NONE = 0,
3684 TPACPI_VIDEO_570, /* 570 */
3685 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3686 TPACPI_VIDEO_NEW, /* all others */
3689 enum { /* video status flags, based on VIDEO_570 */
3690 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3691 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3692 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3695 enum { /* TPACPI_VIDEO_570 constants */
3696 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3697 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3698 * video_status_flags */
3699 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3700 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3703 static enum video_access_mode video_supported;
3704 static int video_orig_autosw;
3706 static int video_autosw_get(void);
3707 static int video_autosw_set(int enable);
3709 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3711 static int __init video_init(struct ibm_init_struct *iibm)
3713 int ivga;
3715 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3717 TPACPI_ACPIHANDLE_INIT(vid);
3718 TPACPI_ACPIHANDLE_INIT(vid2);
3720 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3721 /* G41, assume IVGA doesn't change */
3722 vid_handle = vid2_handle;
3724 if (!vid_handle)
3725 /* video switching not supported on R30, R31 */
3726 video_supported = TPACPI_VIDEO_NONE;
3727 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3728 /* 570 */
3729 video_supported = TPACPI_VIDEO_570;
3730 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3731 /* 600e/x, 770e, 770x */
3732 video_supported = TPACPI_VIDEO_770;
3733 else
3734 /* all others */
3735 video_supported = TPACPI_VIDEO_NEW;
3737 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3738 str_supported(video_supported != TPACPI_VIDEO_NONE),
3739 video_supported);
3741 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3744 static void video_exit(void)
3746 dbg_printk(TPACPI_DBG_EXIT,
3747 "restoring original video autoswitch mode\n");
3748 if (video_autosw_set(video_orig_autosw))
3749 printk(TPACPI_ERR "error while trying to restore original "
3750 "video autoswitch mode\n");
3753 static int video_outputsw_get(void)
3755 int status = 0;
3756 int i;
3758 switch (video_supported) {
3759 case TPACPI_VIDEO_570:
3760 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3761 TP_ACPI_VIDEO_570_PHSCMD))
3762 return -EIO;
3763 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3764 break;
3765 case TPACPI_VIDEO_770:
3766 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3767 return -EIO;
3768 if (i)
3769 status |= TP_ACPI_VIDEO_S_LCD;
3770 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3771 return -EIO;
3772 if (i)
3773 status |= TP_ACPI_VIDEO_S_CRT;
3774 break;
3775 case TPACPI_VIDEO_NEW:
3776 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3777 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3778 return -EIO;
3779 if (i)
3780 status |= TP_ACPI_VIDEO_S_CRT;
3782 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3783 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3784 return -EIO;
3785 if (i)
3786 status |= TP_ACPI_VIDEO_S_LCD;
3787 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3788 return -EIO;
3789 if (i)
3790 status |= TP_ACPI_VIDEO_S_DVI;
3791 break;
3792 default:
3793 return -ENOSYS;
3796 return status;
3799 static int video_outputsw_set(int status)
3801 int autosw;
3802 int res = 0;
3804 switch (video_supported) {
3805 case TPACPI_VIDEO_570:
3806 res = acpi_evalf(NULL, NULL,
3807 "\\_SB.PHS2", "vdd",
3808 TP_ACPI_VIDEO_570_PHS2CMD,
3809 status | TP_ACPI_VIDEO_570_PHS2SET);
3810 break;
3811 case TPACPI_VIDEO_770:
3812 autosw = video_autosw_get();
3813 if (autosw < 0)
3814 return autosw;
3816 res = video_autosw_set(1);
3817 if (res)
3818 return res;
3819 res = acpi_evalf(vid_handle, NULL,
3820 "ASWT", "vdd", status * 0x100, 0);
3821 if (!autosw && video_autosw_set(autosw)) {
3822 printk(TPACPI_ERR
3823 "video auto-switch left enabled due to error\n");
3824 return -EIO;
3826 break;
3827 case TPACPI_VIDEO_NEW:
3828 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3829 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3830 break;
3831 default:
3832 return -ENOSYS;
3835 return (res)? 0 : -EIO;
3838 static int video_autosw_get(void)
3840 int autosw = 0;
3842 switch (video_supported) {
3843 case TPACPI_VIDEO_570:
3844 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3845 return -EIO;
3846 break;
3847 case TPACPI_VIDEO_770:
3848 case TPACPI_VIDEO_NEW:
3849 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3850 return -EIO;
3851 break;
3852 default:
3853 return -ENOSYS;
3856 return autosw & 1;
3859 static int video_autosw_set(int enable)
3861 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3862 return -EIO;
3863 return 0;
3866 static int video_outputsw_cycle(void)
3868 int autosw = video_autosw_get();
3869 int res;
3871 if (autosw < 0)
3872 return autosw;
3874 switch (video_supported) {
3875 case TPACPI_VIDEO_570:
3876 res = video_autosw_set(1);
3877 if (res)
3878 return res;
3879 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3880 break;
3881 case TPACPI_VIDEO_770:
3882 case TPACPI_VIDEO_NEW:
3883 res = video_autosw_set(1);
3884 if (res)
3885 return res;
3886 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3887 break;
3888 default:
3889 return -ENOSYS;
3891 if (!autosw && video_autosw_set(autosw)) {
3892 printk(TPACPI_ERR
3893 "video auto-switch left enabled due to error\n");
3894 return -EIO;
3897 return (res)? 0 : -EIO;
3900 static int video_expand_toggle(void)
3902 switch (video_supported) {
3903 case TPACPI_VIDEO_570:
3904 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3905 0 : -EIO;
3906 case TPACPI_VIDEO_770:
3907 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3908 0 : -EIO;
3909 case TPACPI_VIDEO_NEW:
3910 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3911 0 : -EIO;
3912 default:
3913 return -ENOSYS;
3915 /* not reached */
3918 static int video_read(char *p)
3920 int status, autosw;
3921 int len = 0;
3923 if (video_supported == TPACPI_VIDEO_NONE) {
3924 len += sprintf(p + len, "status:\t\tnot supported\n");
3925 return len;
3928 status = video_outputsw_get();
3929 if (status < 0)
3930 return status;
3932 autosw = video_autosw_get();
3933 if (autosw < 0)
3934 return autosw;
3936 len += sprintf(p + len, "status:\t\tsupported\n");
3937 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3938 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3939 if (video_supported == TPACPI_VIDEO_NEW)
3940 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3941 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3942 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3943 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3944 if (video_supported == TPACPI_VIDEO_NEW)
3945 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3946 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3947 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3949 return len;
3952 static int video_write(char *buf)
3954 char *cmd;
3955 int enable, disable, status;
3956 int res;
3958 if (video_supported == TPACPI_VIDEO_NONE)
3959 return -ENODEV;
3961 enable = 0;
3962 disable = 0;
3964 while ((cmd = next_cmd(&buf))) {
3965 if (strlencmp(cmd, "lcd_enable") == 0) {
3966 enable |= TP_ACPI_VIDEO_S_LCD;
3967 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3968 disable |= TP_ACPI_VIDEO_S_LCD;
3969 } else if (strlencmp(cmd, "crt_enable") == 0) {
3970 enable |= TP_ACPI_VIDEO_S_CRT;
3971 } else if (strlencmp(cmd, "crt_disable") == 0) {
3972 disable |= TP_ACPI_VIDEO_S_CRT;
3973 } else if (video_supported == TPACPI_VIDEO_NEW &&
3974 strlencmp(cmd, "dvi_enable") == 0) {
3975 enable |= TP_ACPI_VIDEO_S_DVI;
3976 } else if (video_supported == TPACPI_VIDEO_NEW &&
3977 strlencmp(cmd, "dvi_disable") == 0) {
3978 disable |= TP_ACPI_VIDEO_S_DVI;
3979 } else if (strlencmp(cmd, "auto_enable") == 0) {
3980 res = video_autosw_set(1);
3981 if (res)
3982 return res;
3983 } else if (strlencmp(cmd, "auto_disable") == 0) {
3984 res = video_autosw_set(0);
3985 if (res)
3986 return res;
3987 } else if (strlencmp(cmd, "video_switch") == 0) {
3988 res = video_outputsw_cycle();
3989 if (res)
3990 return res;
3991 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3992 res = video_expand_toggle();
3993 if (res)
3994 return res;
3995 } else
3996 return -EINVAL;
3999 if (enable || disable) {
4000 status = video_outputsw_get();
4001 if (status < 0)
4002 return status;
4003 res = video_outputsw_set((status & ~disable) | enable);
4004 if (res)
4005 return res;
4008 return 0;
4011 static struct ibm_struct video_driver_data = {
4012 .name = "video",
4013 .read = video_read,
4014 .write = video_write,
4015 .exit = video_exit,
4018 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4020 /*************************************************************************
4021 * Light (thinklight) subdriver
4024 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4025 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4027 static int light_get_status(void)
4029 int status = 0;
4031 if (tp_features.light_status) {
4032 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4033 return -EIO;
4034 return (!!status);
4037 return -ENXIO;
4040 static int light_set_status(int status)
4042 int rc;
4044 if (tp_features.light) {
4045 if (cmos_handle) {
4046 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4047 (status)?
4048 TP_CMOS_THINKLIGHT_ON :
4049 TP_CMOS_THINKLIGHT_OFF);
4050 } else {
4051 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4052 (status)? 1 : 0);
4054 return (rc)? 0 : -EIO;
4057 return -ENXIO;
4060 static void light_set_status_worker(struct work_struct *work)
4062 struct tpacpi_led_classdev *data =
4063 container_of(work, struct tpacpi_led_classdev, work);
4065 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4066 light_set_status((data->new_state != TPACPI_LED_OFF));
4069 static void light_sysfs_set(struct led_classdev *led_cdev,
4070 enum led_brightness brightness)
4072 struct tpacpi_led_classdev *data =
4073 container_of(led_cdev,
4074 struct tpacpi_led_classdev,
4075 led_classdev);
4076 data->new_state = (brightness != LED_OFF) ?
4077 TPACPI_LED_ON : TPACPI_LED_OFF;
4078 queue_work(tpacpi_wq, &data->work);
4081 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4083 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4086 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4087 .led_classdev = {
4088 .name = "tpacpi::thinklight",
4089 .brightness_set = &light_sysfs_set,
4090 .brightness_get = &light_sysfs_get,
4094 static int __init light_init(struct ibm_init_struct *iibm)
4096 int rc;
4098 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4100 TPACPI_ACPIHANDLE_INIT(ledb);
4101 TPACPI_ACPIHANDLE_INIT(lght);
4102 TPACPI_ACPIHANDLE_INIT(cmos);
4103 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4105 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4106 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4108 if (tp_features.light)
4109 /* light status not supported on
4110 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4111 tp_features.light_status =
4112 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4114 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4115 str_supported(tp_features.light),
4116 str_supported(tp_features.light_status));
4118 if (!tp_features.light)
4119 return 1;
4121 rc = led_classdev_register(&tpacpi_pdev->dev,
4122 &tpacpi_led_thinklight.led_classdev);
4124 if (rc < 0) {
4125 tp_features.light = 0;
4126 tp_features.light_status = 0;
4127 } else {
4128 rc = 0;
4131 return rc;
4134 static void light_exit(void)
4136 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4137 if (work_pending(&tpacpi_led_thinklight.work))
4138 flush_workqueue(tpacpi_wq);
4141 static int light_read(char *p)
4143 int len = 0;
4144 int status;
4146 if (!tp_features.light) {
4147 len += sprintf(p + len, "status:\t\tnot supported\n");
4148 } else if (!tp_features.light_status) {
4149 len += sprintf(p + len, "status:\t\tunknown\n");
4150 len += sprintf(p + len, "commands:\ton, off\n");
4151 } else {
4152 status = light_get_status();
4153 if (status < 0)
4154 return status;
4155 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4156 len += sprintf(p + len, "commands:\ton, off\n");
4159 return len;
4162 static int light_write(char *buf)
4164 char *cmd;
4165 int newstatus = 0;
4167 if (!tp_features.light)
4168 return -ENODEV;
4170 while ((cmd = next_cmd(&buf))) {
4171 if (strlencmp(cmd, "on") == 0) {
4172 newstatus = 1;
4173 } else if (strlencmp(cmd, "off") == 0) {
4174 newstatus = 0;
4175 } else
4176 return -EINVAL;
4179 return light_set_status(newstatus);
4182 static struct ibm_struct light_driver_data = {
4183 .name = "light",
4184 .read = light_read,
4185 .write = light_write,
4186 .exit = light_exit,
4189 /*************************************************************************
4190 * Dock subdriver
4193 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4195 static void dock_notify(struct ibm_struct *ibm, u32 event);
4196 static int dock_read(char *p);
4197 static int dock_write(char *buf);
4199 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4200 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4201 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4202 "\\_SB.PCI.ISA.SLCE", /* 570 */
4203 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4205 /* don't list other alternatives as we install a notify handler on the 570 */
4206 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4208 static const struct acpi_device_id ibm_pci_device_ids[] = {
4209 {PCI_ROOT_HID_STRING, 0},
4210 {"", 0},
4213 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4215 .notify = dock_notify,
4216 .handle = &dock_handle,
4217 .type = ACPI_SYSTEM_NOTIFY,
4220 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4221 * We just use it to get notifications of dock hotplug
4222 * in very old thinkpads */
4223 .hid = ibm_pci_device_ids,
4224 .notify = dock_notify,
4225 .handle = &pci_handle,
4226 .type = ACPI_SYSTEM_NOTIFY,
4230 static struct ibm_struct dock_driver_data[2] = {
4232 .name = "dock",
4233 .read = dock_read,
4234 .write = dock_write,
4235 .acpi = &ibm_dock_acpidriver[0],
4238 .name = "dock",
4239 .acpi = &ibm_dock_acpidriver[1],
4243 #define dock_docked() (_sta(dock_handle) & 1)
4245 static int __init dock_init(struct ibm_init_struct *iibm)
4247 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4249 TPACPI_ACPIHANDLE_INIT(dock);
4251 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4252 str_supported(dock_handle != NULL));
4254 return (dock_handle)? 0 : 1;
4257 static int __init dock_init2(struct ibm_init_struct *iibm)
4259 int dock2_needed;
4261 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4263 if (dock_driver_data[0].flags.acpi_driver_registered &&
4264 dock_driver_data[0].flags.acpi_notify_installed) {
4265 TPACPI_ACPIHANDLE_INIT(pci);
4266 dock2_needed = (pci_handle != NULL);
4267 vdbg_printk(TPACPI_DBG_INIT,
4268 "dock PCI handler for the TP 570 is %s\n",
4269 str_supported(dock2_needed));
4270 } else {
4271 vdbg_printk(TPACPI_DBG_INIT,
4272 "dock subdriver part 2 not required\n");
4273 dock2_needed = 0;
4276 return (dock2_needed)? 0 : 1;
4279 static void dock_notify(struct ibm_struct *ibm, u32 event)
4281 int docked = dock_docked();
4282 int pci = ibm->acpi->hid && ibm->acpi->device &&
4283 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4284 int data;
4286 if (event == 1 && !pci) /* 570 */
4287 data = 1; /* button */
4288 else if (event == 1 && pci) /* 570 */
4289 data = 3; /* dock */
4290 else if (event == 3 && docked)
4291 data = 1; /* button */
4292 else if (event == 3 && !docked)
4293 data = 2; /* undock */
4294 else if (event == 0 && docked)
4295 data = 3; /* dock */
4296 else {
4297 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4298 event, _sta(dock_handle));
4299 data = 0; /* unknown */
4301 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4302 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4303 dev_name(&ibm->acpi->device->dev),
4304 event, data);
4307 static int dock_read(char *p)
4309 int len = 0;
4310 int docked = dock_docked();
4312 if (!dock_handle)
4313 len += sprintf(p + len, "status:\t\tnot supported\n");
4314 else if (!docked)
4315 len += sprintf(p + len, "status:\t\tundocked\n");
4316 else {
4317 len += sprintf(p + len, "status:\t\tdocked\n");
4318 len += sprintf(p + len, "commands:\tdock, undock\n");
4321 return len;
4324 static int dock_write(char *buf)
4326 char *cmd;
4328 if (!dock_docked())
4329 return -ENODEV;
4331 while ((cmd = next_cmd(&buf))) {
4332 if (strlencmp(cmd, "undock") == 0) {
4333 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4334 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4335 return -EIO;
4336 } else if (strlencmp(cmd, "dock") == 0) {
4337 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4338 return -EIO;
4339 } else
4340 return -EINVAL;
4343 return 0;
4346 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4348 /*************************************************************************
4349 * Bay subdriver
4352 #ifdef CONFIG_THINKPAD_ACPI_BAY
4354 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4355 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4356 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4357 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4358 ); /* A21e, R30, R31 */
4359 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4360 "_EJ0", /* all others */
4361 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4362 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4363 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4364 ); /* all others */
4365 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4366 "_EJ0", /* 770x */
4367 ); /* all others */
4369 static int __init bay_init(struct ibm_init_struct *iibm)
4371 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4373 TPACPI_ACPIHANDLE_INIT(bay);
4374 if (bay_handle)
4375 TPACPI_ACPIHANDLE_INIT(bay_ej);
4376 TPACPI_ACPIHANDLE_INIT(bay2);
4377 if (bay2_handle)
4378 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4380 tp_features.bay_status = bay_handle &&
4381 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4382 tp_features.bay_status2 = bay2_handle &&
4383 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4385 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4386 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4387 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4388 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4390 vdbg_printk(TPACPI_DBG_INIT,
4391 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4392 str_supported(tp_features.bay_status),
4393 str_supported(tp_features.bay_eject),
4394 str_supported(tp_features.bay_status2),
4395 str_supported(tp_features.bay_eject2));
4397 return (tp_features.bay_status || tp_features.bay_eject ||
4398 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4401 static void bay_notify(struct ibm_struct *ibm, u32 event)
4403 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4404 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4405 dev_name(&ibm->acpi->device->dev),
4406 event, 0);
4409 #define bay_occupied(b) (_sta(b##_handle) & 1)
4411 static int bay_read(char *p)
4413 int len = 0;
4414 int occupied = bay_occupied(bay);
4415 int occupied2 = bay_occupied(bay2);
4416 int eject, eject2;
4418 len += sprintf(p + len, "status:\t\t%s\n",
4419 tp_features.bay_status ?
4420 (occupied ? "occupied" : "unoccupied") :
4421 "not supported");
4422 if (tp_features.bay_status2)
4423 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4424 "occupied" : "unoccupied");
4426 eject = tp_features.bay_eject && occupied;
4427 eject2 = tp_features.bay_eject2 && occupied2;
4429 if (eject && eject2)
4430 len += sprintf(p + len, "commands:\teject, eject2\n");
4431 else if (eject)
4432 len += sprintf(p + len, "commands:\teject\n");
4433 else if (eject2)
4434 len += sprintf(p + len, "commands:\teject2\n");
4436 return len;
4439 static int bay_write(char *buf)
4441 char *cmd;
4443 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4444 return -ENODEV;
4446 while ((cmd = next_cmd(&buf))) {
4447 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4448 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4449 return -EIO;
4450 } else if (tp_features.bay_eject2 &&
4451 strlencmp(cmd, "eject2") == 0) {
4452 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4453 return -EIO;
4454 } else
4455 return -EINVAL;
4458 return 0;
4461 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4462 .notify = bay_notify,
4463 .handle = &bay_handle,
4464 .type = ACPI_SYSTEM_NOTIFY,
4467 static struct ibm_struct bay_driver_data = {
4468 .name = "bay",
4469 .read = bay_read,
4470 .write = bay_write,
4471 .acpi = &ibm_bay_acpidriver,
4474 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4476 /*************************************************************************
4477 * CMOS subdriver
4480 /* sysfs cmos_command -------------------------------------------------- */
4481 static ssize_t cmos_command_store(struct device *dev,
4482 struct device_attribute *attr,
4483 const char *buf, size_t count)
4485 unsigned long cmos_cmd;
4486 int res;
4488 if (parse_strtoul(buf, 21, &cmos_cmd))
4489 return -EINVAL;
4491 res = issue_thinkpad_cmos_command(cmos_cmd);
4492 return (res)? res : count;
4495 static struct device_attribute dev_attr_cmos_command =
4496 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4498 /* --------------------------------------------------------------------- */
4500 static int __init cmos_init(struct ibm_init_struct *iibm)
4502 int res;
4504 vdbg_printk(TPACPI_DBG_INIT,
4505 "initializing cmos commands subdriver\n");
4507 TPACPI_ACPIHANDLE_INIT(cmos);
4509 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4510 str_supported(cmos_handle != NULL));
4512 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4513 if (res)
4514 return res;
4516 return (cmos_handle)? 0 : 1;
4519 static void cmos_exit(void)
4521 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4524 static int cmos_read(char *p)
4526 int len = 0;
4528 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4529 R30, R31, T20-22, X20-21 */
4530 if (!cmos_handle)
4531 len += sprintf(p + len, "status:\t\tnot supported\n");
4532 else {
4533 len += sprintf(p + len, "status:\t\tsupported\n");
4534 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4537 return len;
4540 static int cmos_write(char *buf)
4542 char *cmd;
4543 int cmos_cmd, res;
4545 while ((cmd = next_cmd(&buf))) {
4546 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4547 cmos_cmd >= 0 && cmos_cmd <= 21) {
4548 /* cmos_cmd set */
4549 } else
4550 return -EINVAL;
4552 res = issue_thinkpad_cmos_command(cmos_cmd);
4553 if (res)
4554 return res;
4557 return 0;
4560 static struct ibm_struct cmos_driver_data = {
4561 .name = "cmos",
4562 .read = cmos_read,
4563 .write = cmos_write,
4564 .exit = cmos_exit,
4567 /*************************************************************************
4568 * LED subdriver
4571 enum led_access_mode {
4572 TPACPI_LED_NONE = 0,
4573 TPACPI_LED_570, /* 570 */
4574 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4575 TPACPI_LED_NEW, /* all others */
4578 enum { /* For TPACPI_LED_OLD */
4579 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4580 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4581 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4584 static enum led_access_mode led_supported;
4586 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4587 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4588 /* T20-22, X20-21 */
4589 "LED", /* all others */
4590 ); /* R30, R31 */
4592 #define TPACPI_LED_NUMLEDS 8
4593 static struct tpacpi_led_classdev *tpacpi_leds;
4594 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4595 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4596 /* there's a limit of 19 chars + NULL before 2.6.26 */
4597 "tpacpi::power",
4598 "tpacpi:orange:batt",
4599 "tpacpi:green:batt",
4600 "tpacpi::dock_active",
4601 "tpacpi::bay_active",
4602 "tpacpi::dock_batt",
4603 "tpacpi::unknown_led",
4604 "tpacpi::standby",
4607 static int led_get_status(const unsigned int led)
4609 int status;
4610 enum led_status_t led_s;
4612 switch (led_supported) {
4613 case TPACPI_LED_570:
4614 if (!acpi_evalf(ec_handle,
4615 &status, "GLED", "dd", 1 << led))
4616 return -EIO;
4617 led_s = (status == 0)?
4618 TPACPI_LED_OFF :
4619 ((status == 1)?
4620 TPACPI_LED_ON :
4621 TPACPI_LED_BLINK);
4622 tpacpi_led_state_cache[led] = led_s;
4623 return led_s;
4624 default:
4625 return -ENXIO;
4628 /* not reached */
4631 static int led_set_status(const unsigned int led,
4632 const enum led_status_t ledstatus)
4634 /* off, on, blink. Index is led_status_t */
4635 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4636 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4638 int rc = 0;
4640 switch (led_supported) {
4641 case TPACPI_LED_570:
4642 /* 570 */
4643 if (led > 7)
4644 return -EINVAL;
4645 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4646 (1 << led), led_sled_arg1[ledstatus]))
4647 rc = -EIO;
4648 break;
4649 case TPACPI_LED_OLD:
4650 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4651 if (led > 7)
4652 return -EINVAL;
4653 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4654 if (rc >= 0)
4655 rc = ec_write(TPACPI_LED_EC_HLBL,
4656 (ledstatus == TPACPI_LED_BLINK) << led);
4657 if (rc >= 0)
4658 rc = ec_write(TPACPI_LED_EC_HLCL,
4659 (ledstatus != TPACPI_LED_OFF) << led);
4660 break;
4661 case TPACPI_LED_NEW:
4662 /* all others */
4663 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4664 led, led_led_arg1[ledstatus]))
4665 rc = -EIO;
4666 break;
4667 default:
4668 rc = -ENXIO;
4671 if (!rc)
4672 tpacpi_led_state_cache[led] = ledstatus;
4674 return rc;
4677 static void led_set_status_worker(struct work_struct *work)
4679 struct tpacpi_led_classdev *data =
4680 container_of(work, struct tpacpi_led_classdev, work);
4682 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4683 led_set_status(data->led, data->new_state);
4686 static void led_sysfs_set(struct led_classdev *led_cdev,
4687 enum led_brightness brightness)
4689 struct tpacpi_led_classdev *data = container_of(led_cdev,
4690 struct tpacpi_led_classdev, led_classdev);
4692 if (brightness == LED_OFF)
4693 data->new_state = TPACPI_LED_OFF;
4694 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4695 data->new_state = TPACPI_LED_ON;
4696 else
4697 data->new_state = TPACPI_LED_BLINK;
4699 queue_work(tpacpi_wq, &data->work);
4702 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4703 unsigned long *delay_on, unsigned long *delay_off)
4705 struct tpacpi_led_classdev *data = container_of(led_cdev,
4706 struct tpacpi_led_classdev, led_classdev);
4708 /* Can we choose the flash rate? */
4709 if (*delay_on == 0 && *delay_off == 0) {
4710 /* yes. set them to the hardware blink rate (1 Hz) */
4711 *delay_on = 500; /* ms */
4712 *delay_off = 500; /* ms */
4713 } else if ((*delay_on != 500) || (*delay_off != 500))
4714 return -EINVAL;
4716 data->new_state = TPACPI_LED_BLINK;
4717 queue_work(tpacpi_wq, &data->work);
4719 return 0;
4722 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4724 int rc;
4726 struct tpacpi_led_classdev *data = container_of(led_cdev,
4727 struct tpacpi_led_classdev, led_classdev);
4729 rc = led_get_status(data->led);
4731 if (rc == TPACPI_LED_OFF || rc < 0)
4732 rc = LED_OFF; /* no error handling in led class :( */
4733 else
4734 rc = LED_FULL;
4736 return rc;
4739 static void led_exit(void)
4741 unsigned int i;
4743 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4744 if (tpacpi_leds[i].led_classdev.name)
4745 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4748 kfree(tpacpi_leds);
4751 static int __init led_init(struct ibm_init_struct *iibm)
4753 unsigned int i;
4754 int rc;
4756 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4758 TPACPI_ACPIHANDLE_INIT(led);
4760 if (!led_handle)
4761 /* led not supported on R30, R31 */
4762 led_supported = TPACPI_LED_NONE;
4763 else if (strlencmp(led_path, "SLED") == 0)
4764 /* 570 */
4765 led_supported = TPACPI_LED_570;
4766 else if (strlencmp(led_path, "SYSL") == 0)
4767 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4768 led_supported = TPACPI_LED_OLD;
4769 else
4770 /* all others */
4771 led_supported = TPACPI_LED_NEW;
4773 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4774 str_supported(led_supported), led_supported);
4776 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4777 GFP_KERNEL);
4778 if (!tpacpi_leds) {
4779 printk(TPACPI_ERR "Out of memory for LED data\n");
4780 return -ENOMEM;
4783 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4784 tpacpi_leds[i].led = i;
4786 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4787 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4788 if (led_supported == TPACPI_LED_570)
4789 tpacpi_leds[i].led_classdev.brightness_get =
4790 &led_sysfs_get;
4792 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4794 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4796 rc = led_classdev_register(&tpacpi_pdev->dev,
4797 &tpacpi_leds[i].led_classdev);
4798 if (rc < 0) {
4799 tpacpi_leds[i].led_classdev.name = NULL;
4800 led_exit();
4801 return rc;
4805 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4808 #define str_led_status(s) \
4809 ((s) == TPACPI_LED_OFF ? "off" : \
4810 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4812 static int led_read(char *p)
4814 int len = 0;
4816 if (!led_supported) {
4817 len += sprintf(p + len, "status:\t\tnot supported\n");
4818 return len;
4820 len += sprintf(p + len, "status:\t\tsupported\n");
4822 if (led_supported == TPACPI_LED_570) {
4823 /* 570 */
4824 int i, status;
4825 for (i = 0; i < 8; i++) {
4826 status = led_get_status(i);
4827 if (status < 0)
4828 return -EIO;
4829 len += sprintf(p + len, "%d:\t\t%s\n",
4830 i, str_led_status(status));
4834 len += sprintf(p + len, "commands:\t"
4835 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4837 return len;
4840 static int led_write(char *buf)
4842 char *cmd;
4843 int led, rc;
4844 enum led_status_t s;
4846 if (!led_supported)
4847 return -ENODEV;
4849 while ((cmd = next_cmd(&buf))) {
4850 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4851 return -EINVAL;
4853 if (strstr(cmd, "off")) {
4854 s = TPACPI_LED_OFF;
4855 } else if (strstr(cmd, "on")) {
4856 s = TPACPI_LED_ON;
4857 } else if (strstr(cmd, "blink")) {
4858 s = TPACPI_LED_BLINK;
4859 } else {
4860 return -EINVAL;
4863 rc = led_set_status(led, s);
4864 if (rc < 0)
4865 return rc;
4868 return 0;
4871 static struct ibm_struct led_driver_data = {
4872 .name = "led",
4873 .read = led_read,
4874 .write = led_write,
4875 .exit = led_exit,
4878 /*************************************************************************
4879 * Beep subdriver
4882 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4884 static int __init beep_init(struct ibm_init_struct *iibm)
4886 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4888 TPACPI_ACPIHANDLE_INIT(beep);
4890 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4891 str_supported(beep_handle != NULL));
4893 return (beep_handle)? 0 : 1;
4896 static int beep_read(char *p)
4898 int len = 0;
4900 if (!beep_handle)
4901 len += sprintf(p + len, "status:\t\tnot supported\n");
4902 else {
4903 len += sprintf(p + len, "status:\t\tsupported\n");
4904 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4907 return len;
4910 static int beep_write(char *buf)
4912 char *cmd;
4913 int beep_cmd;
4915 if (!beep_handle)
4916 return -ENODEV;
4918 while ((cmd = next_cmd(&buf))) {
4919 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4920 beep_cmd >= 0 && beep_cmd <= 17) {
4921 /* beep_cmd set */
4922 } else
4923 return -EINVAL;
4924 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4925 return -EIO;
4928 return 0;
4931 static struct ibm_struct beep_driver_data = {
4932 .name = "beep",
4933 .read = beep_read,
4934 .write = beep_write,
4937 /*************************************************************************
4938 * Thermal subdriver
4941 enum thermal_access_mode {
4942 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4943 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4944 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4945 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4946 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4949 enum { /* TPACPI_THERMAL_TPEC_* */
4950 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4951 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4952 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4955 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4956 struct ibm_thermal_sensors_struct {
4957 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4960 static enum thermal_access_mode thermal_read_mode;
4962 /* idx is zero-based */
4963 static int thermal_get_sensor(int idx, s32 *value)
4965 int t;
4966 s8 tmp;
4967 char tmpi[5];
4969 t = TP_EC_THERMAL_TMP0;
4971 switch (thermal_read_mode) {
4972 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4973 case TPACPI_THERMAL_TPEC_16:
4974 if (idx >= 8 && idx <= 15) {
4975 t = TP_EC_THERMAL_TMP8;
4976 idx -= 8;
4978 /* fallthrough */
4979 #endif
4980 case TPACPI_THERMAL_TPEC_8:
4981 if (idx <= 7) {
4982 if (!acpi_ec_read(t + idx, &tmp))
4983 return -EIO;
4984 *value = tmp * 1000;
4985 return 0;
4987 break;
4989 case TPACPI_THERMAL_ACPI_UPDT:
4990 if (idx <= 7) {
4991 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4992 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4993 return -EIO;
4994 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4995 return -EIO;
4996 *value = (t - 2732) * 100;
4997 return 0;
4999 break;
5001 case TPACPI_THERMAL_ACPI_TMP07:
5002 if (idx <= 7) {
5003 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5004 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5005 return -EIO;
5006 if (t > 127 || t < -127)
5007 t = TP_EC_THERMAL_TMP_NA;
5008 *value = t * 1000;
5009 return 0;
5011 break;
5013 case TPACPI_THERMAL_NONE:
5014 default:
5015 return -ENOSYS;
5018 return -EINVAL;
5021 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5023 int res, i;
5024 int n;
5026 n = 8;
5027 i = 0;
5029 if (!s)
5030 return -EINVAL;
5032 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5033 n = 16;
5035 for (i = 0 ; i < n; i++) {
5036 res = thermal_get_sensor(i, &s->temp[i]);
5037 if (res)
5038 return res;
5041 return n;
5044 /* sysfs temp##_input -------------------------------------------------- */
5046 static ssize_t thermal_temp_input_show(struct device *dev,
5047 struct device_attribute *attr,
5048 char *buf)
5050 struct sensor_device_attribute *sensor_attr =
5051 to_sensor_dev_attr(attr);
5052 int idx = sensor_attr->index;
5053 s32 value;
5054 int res;
5056 res = thermal_get_sensor(idx, &value);
5057 if (res)
5058 return res;
5059 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5060 return -ENXIO;
5062 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5065 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5066 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5067 thermal_temp_input_show, NULL, _idxB)
5069 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5070 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5071 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5072 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5073 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5074 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5075 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5076 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5077 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5078 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5079 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5080 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5081 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5082 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5083 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5084 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5085 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5088 #define THERMAL_ATTRS(X) \
5089 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5091 static struct attribute *thermal_temp_input_attr[] = {
5092 THERMAL_ATTRS(8),
5093 THERMAL_ATTRS(9),
5094 THERMAL_ATTRS(10),
5095 THERMAL_ATTRS(11),
5096 THERMAL_ATTRS(12),
5097 THERMAL_ATTRS(13),
5098 THERMAL_ATTRS(14),
5099 THERMAL_ATTRS(15),
5100 THERMAL_ATTRS(0),
5101 THERMAL_ATTRS(1),
5102 THERMAL_ATTRS(2),
5103 THERMAL_ATTRS(3),
5104 THERMAL_ATTRS(4),
5105 THERMAL_ATTRS(5),
5106 THERMAL_ATTRS(6),
5107 THERMAL_ATTRS(7),
5108 NULL
5111 static const struct attribute_group thermal_temp_input16_group = {
5112 .attrs = thermal_temp_input_attr
5115 static const struct attribute_group thermal_temp_input8_group = {
5116 .attrs = &thermal_temp_input_attr[8]
5119 #undef THERMAL_SENSOR_ATTR_TEMP
5120 #undef THERMAL_ATTRS
5122 /* --------------------------------------------------------------------- */
5124 static int __init thermal_init(struct ibm_init_struct *iibm)
5126 u8 t, ta1, ta2;
5127 int i;
5128 int acpi_tmp7;
5129 int res;
5131 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5133 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5135 if (thinkpad_id.ec_model) {
5137 * Direct EC access mode: sensors at registers
5138 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5139 * non-implemented, thermal sensors return 0x80 when
5140 * not available
5143 ta1 = ta2 = 0;
5144 for (i = 0; i < 8; i++) {
5145 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5146 ta1 |= t;
5147 } else {
5148 ta1 = 0;
5149 break;
5151 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5152 ta2 |= t;
5153 } else {
5154 ta1 = 0;
5155 break;
5158 if (ta1 == 0) {
5159 /* This is sheer paranoia, but we handle it anyway */
5160 if (acpi_tmp7) {
5161 printk(TPACPI_ERR
5162 "ThinkPad ACPI EC access misbehaving, "
5163 "falling back to ACPI TMPx access "
5164 "mode\n");
5165 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5166 } else {
5167 printk(TPACPI_ERR
5168 "ThinkPad ACPI EC access misbehaving, "
5169 "disabling thermal sensors access\n");
5170 thermal_read_mode = TPACPI_THERMAL_NONE;
5172 } else {
5173 thermal_read_mode =
5174 (ta2 != 0) ?
5175 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5177 } else if (acpi_tmp7) {
5178 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5179 /* 600e/x, 770e, 770x */
5180 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5181 } else {
5182 /* Standard ACPI TMPx access, max 8 sensors */
5183 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5185 } else {
5186 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5187 thermal_read_mode = TPACPI_THERMAL_NONE;
5190 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5191 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5192 thermal_read_mode);
5194 switch (thermal_read_mode) {
5195 case TPACPI_THERMAL_TPEC_16:
5196 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5197 &thermal_temp_input16_group);
5198 if (res)
5199 return res;
5200 break;
5201 case TPACPI_THERMAL_TPEC_8:
5202 case TPACPI_THERMAL_ACPI_TMP07:
5203 case TPACPI_THERMAL_ACPI_UPDT:
5204 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5205 &thermal_temp_input8_group);
5206 if (res)
5207 return res;
5208 break;
5209 case TPACPI_THERMAL_NONE:
5210 default:
5211 return 1;
5214 return 0;
5217 static void thermal_exit(void)
5219 switch (thermal_read_mode) {
5220 case TPACPI_THERMAL_TPEC_16:
5221 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5222 &thermal_temp_input16_group);
5223 break;
5224 case TPACPI_THERMAL_TPEC_8:
5225 case TPACPI_THERMAL_ACPI_TMP07:
5226 case TPACPI_THERMAL_ACPI_UPDT:
5227 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5228 &thermal_temp_input16_group);
5229 break;
5230 case TPACPI_THERMAL_NONE:
5231 default:
5232 break;
5236 static int thermal_read(char *p)
5238 int len = 0;
5239 int n, i;
5240 struct ibm_thermal_sensors_struct t;
5242 n = thermal_get_sensors(&t);
5243 if (unlikely(n < 0))
5244 return n;
5246 len += sprintf(p + len, "temperatures:\t");
5248 if (n > 0) {
5249 for (i = 0; i < (n - 1); i++)
5250 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5251 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5252 } else
5253 len += sprintf(p + len, "not supported\n");
5255 return len;
5258 static struct ibm_struct thermal_driver_data = {
5259 .name = "thermal",
5260 .read = thermal_read,
5261 .exit = thermal_exit,
5264 /*************************************************************************
5265 * EC Dump subdriver
5268 static u8 ecdump_regs[256];
5270 static int ecdump_read(char *p)
5272 int len = 0;
5273 int i, j;
5274 u8 v;
5276 len += sprintf(p + len, "EC "
5277 " +00 +01 +02 +03 +04 +05 +06 +07"
5278 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5279 for (i = 0; i < 256; i += 16) {
5280 len += sprintf(p + len, "EC 0x%02x:", i);
5281 for (j = 0; j < 16; j++) {
5282 if (!acpi_ec_read(i + j, &v))
5283 break;
5284 if (v != ecdump_regs[i + j])
5285 len += sprintf(p + len, " *%02x", v);
5286 else
5287 len += sprintf(p + len, " %02x", v);
5288 ecdump_regs[i + j] = v;
5290 len += sprintf(p + len, "\n");
5291 if (j != 16)
5292 break;
5295 /* These are way too dangerous to advertise openly... */
5296 #if 0
5297 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5298 " (<offset> is 00-ff, <value> is 00-ff)\n");
5299 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5300 " (<offset> is 00-ff, <value> is 0-255)\n");
5301 #endif
5302 return len;
5305 static int ecdump_write(char *buf)
5307 char *cmd;
5308 int i, v;
5310 while ((cmd = next_cmd(&buf))) {
5311 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5312 /* i and v set */
5313 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5314 /* i and v set */
5315 } else
5316 return -EINVAL;
5317 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5318 if (!acpi_ec_write(i, v))
5319 return -EIO;
5320 } else
5321 return -EINVAL;
5324 return 0;
5327 static struct ibm_struct ecdump_driver_data = {
5328 .name = "ecdump",
5329 .read = ecdump_read,
5330 .write = ecdump_write,
5331 .flags.experimental = 1,
5334 /*************************************************************************
5335 * Backlight/brightness subdriver
5338 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5340 enum {
5341 TP_EC_BACKLIGHT = 0x31,
5343 /* TP_EC_BACKLIGHT bitmasks */
5344 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5345 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5346 TP_EC_BACKLIGHT_MAPSW = 0x20,
5349 static struct backlight_device *ibm_backlight_device;
5350 static int brightness_mode;
5351 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5353 static struct mutex brightness_mutex;
5356 * ThinkPads can read brightness from two places: EC 0x31, or
5357 * CMOS NVRAM byte 0x5E, bits 0-3.
5359 * EC 0x31 has the following layout
5360 * Bit 7: unknown function
5361 * Bit 6: unknown function
5362 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5363 * Bit 4: must be set to zero to avoid problems
5364 * Bit 3-0: backlight brightness level
5366 * brightness_get_raw returns status data in the EC 0x31 layout
5368 static int brightness_get_raw(int *status)
5370 u8 lec = 0, lcmos = 0, level = 0;
5372 if (brightness_mode & 1) {
5373 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5374 return -EIO;
5375 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5377 if (brightness_mode & 2) {
5378 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5379 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5380 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5381 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5382 level = lcmos;
5385 if (brightness_mode == 3) {
5386 *status = lec; /* Prefer EC, CMOS is just a backing store */
5387 lec &= TP_EC_BACKLIGHT_LVLMSK;
5388 if (lec == lcmos)
5389 tp_warned.bright_cmos_ec_unsync = 0;
5390 else {
5391 if (!tp_warned.bright_cmos_ec_unsync) {
5392 printk(TPACPI_ERR
5393 "CMOS NVRAM (%u) and EC (%u) do not "
5394 "agree on display brightness level\n",
5395 (unsigned int) lcmos,
5396 (unsigned int) lec);
5397 tp_warned.bright_cmos_ec_unsync = 1;
5399 return -EIO;
5401 } else {
5402 *status = level;
5405 return 0;
5408 /* May return EINTR which can always be mapped to ERESTARTSYS */
5409 static int brightness_set(int value)
5411 int cmos_cmd, inc, i, res;
5412 int current_value;
5413 int command_bits;
5415 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5416 value < 0)
5417 return -EINVAL;
5419 res = mutex_lock_killable(&brightness_mutex);
5420 if (res < 0)
5421 return res;
5423 res = brightness_get_raw(&current_value);
5424 if (res < 0)
5425 goto errout;
5427 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5428 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5430 cmos_cmd = value > current_value ?
5431 TP_CMOS_BRIGHTNESS_UP :
5432 TP_CMOS_BRIGHTNESS_DOWN;
5433 inc = (value > current_value)? 1 : -1;
5435 res = 0;
5436 for (i = current_value; i != value; i += inc) {
5437 if ((brightness_mode & 2) &&
5438 issue_thinkpad_cmos_command(cmos_cmd)) {
5439 res = -EIO;
5440 goto errout;
5442 if ((brightness_mode & 1) &&
5443 !acpi_ec_write(TP_EC_BACKLIGHT,
5444 (i + inc) | command_bits)) {
5445 res = -EIO;
5446 goto errout;;
5450 errout:
5451 mutex_unlock(&brightness_mutex);
5452 return res;
5455 /* sysfs backlight class ----------------------------------------------- */
5457 static int brightness_update_status(struct backlight_device *bd)
5459 /* it is the backlight class's job (caller) to handle
5460 * EINTR and other errors properly */
5461 return brightness_set(
5462 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5463 bd->props.power == FB_BLANK_UNBLANK) ?
5464 bd->props.brightness : 0);
5467 static int brightness_get(struct backlight_device *bd)
5469 int status, res;
5471 res = brightness_get_raw(&status);
5472 if (res < 0)
5473 return 0; /* FIXME: teach backlight about error handling */
5475 return status & TP_EC_BACKLIGHT_LVLMSK;
5478 static struct backlight_ops ibm_backlight_data = {
5479 .get_brightness = brightness_get,
5480 .update_status = brightness_update_status,
5483 /* --------------------------------------------------------------------- */
5485 static int __init brightness_init(struct ibm_init_struct *iibm)
5487 int b;
5489 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5491 mutex_init(&brightness_mutex);
5494 * We always attempt to detect acpi support, so as to switch
5495 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5496 * going to publish a backlight interface
5498 b = tpacpi_check_std_acpi_brightness_support();
5499 if (b > 0) {
5501 if (acpi_video_backlight_support()) {
5502 if (brightness_enable > 1) {
5503 printk(TPACPI_NOTICE
5504 "Standard ACPI backlight interface "
5505 "available, not loading native one.\n");
5506 return 1;
5507 } else if (brightness_enable == 1) {
5508 printk(TPACPI_NOTICE
5509 "Backlight control force enabled, even if standard "
5510 "ACPI backlight interface is available\n");
5512 } else {
5513 if (brightness_enable > 1) {
5514 printk(TPACPI_NOTICE
5515 "Standard ACPI backlight interface not "
5516 "available, thinkpad_acpi native "
5517 "brightness control enabled\n");
5522 if (!brightness_enable) {
5523 dbg_printk(TPACPI_DBG_INIT,
5524 "brightness support disabled by "
5525 "module parameter\n");
5526 return 1;
5529 if (b > 16) {
5530 printk(TPACPI_ERR
5531 "Unsupported brightness interface, "
5532 "please contact %s\n", TPACPI_MAIL);
5533 return 1;
5535 if (b == 16)
5536 tp_features.bright_16levels = 1;
5538 if (!brightness_mode) {
5539 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5540 brightness_mode = 2;
5541 else
5542 brightness_mode = 3;
5544 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5545 brightness_mode);
5548 if (brightness_mode > 3)
5549 return -EINVAL;
5551 if (brightness_get_raw(&b) < 0)
5552 return 1;
5554 if (tp_features.bright_16levels)
5555 printk(TPACPI_INFO
5556 "detected a 16-level brightness capable ThinkPad\n");
5558 ibm_backlight_device = backlight_device_register(
5559 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5560 &ibm_backlight_data);
5561 if (IS_ERR(ibm_backlight_device)) {
5562 printk(TPACPI_ERR "Could not register backlight device\n");
5563 return PTR_ERR(ibm_backlight_device);
5565 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5567 ibm_backlight_device->props.max_brightness =
5568 (tp_features.bright_16levels)? 15 : 7;
5569 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5570 backlight_update_status(ibm_backlight_device);
5572 return 0;
5575 static void brightness_exit(void)
5577 if (ibm_backlight_device) {
5578 vdbg_printk(TPACPI_DBG_EXIT,
5579 "calling backlight_device_unregister()\n");
5580 backlight_device_unregister(ibm_backlight_device);
5584 static int brightness_read(char *p)
5586 int len = 0;
5587 int level;
5589 level = brightness_get(NULL);
5590 if (level < 0) {
5591 len += sprintf(p + len, "level:\t\tunreadable\n");
5592 } else {
5593 len += sprintf(p + len, "level:\t\t%d\n", level);
5594 len += sprintf(p + len, "commands:\tup, down\n");
5595 len += sprintf(p + len, "commands:\tlevel <level>"
5596 " (<level> is 0-%d)\n",
5597 (tp_features.bright_16levels) ? 15 : 7);
5600 return len;
5603 static int brightness_write(char *buf)
5605 int level;
5606 int rc;
5607 char *cmd;
5608 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5610 level = brightness_get(NULL);
5611 if (level < 0)
5612 return level;
5614 while ((cmd = next_cmd(&buf))) {
5615 if (strlencmp(cmd, "up") == 0) {
5616 if (level < max_level)
5617 level++;
5618 } else if (strlencmp(cmd, "down") == 0) {
5619 if (level > 0)
5620 level--;
5621 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5622 level >= 0 && level <= max_level) {
5623 /* new level set */
5624 } else
5625 return -EINVAL;
5629 * Now we know what the final level should be, so we try to set it.
5630 * Doing it this way makes the syscall restartable in case of EINTR
5632 rc = brightness_set(level);
5633 return (rc == -EINTR)? ERESTARTSYS : rc;
5636 static struct ibm_struct brightness_driver_data = {
5637 .name = "brightness",
5638 .read = brightness_read,
5639 .write = brightness_write,
5640 .exit = brightness_exit,
5643 /*************************************************************************
5644 * Volume subdriver
5647 static int volume_offset = 0x30;
5649 static int volume_read(char *p)
5651 int len = 0;
5652 u8 level;
5654 if (!acpi_ec_read(volume_offset, &level)) {
5655 len += sprintf(p + len, "level:\t\tunreadable\n");
5656 } else {
5657 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5658 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5659 len += sprintf(p + len, "commands:\tup, down, mute\n");
5660 len += sprintf(p + len, "commands:\tlevel <level>"
5661 " (<level> is 0-15)\n");
5664 return len;
5667 static int volume_write(char *buf)
5669 int cmos_cmd, inc, i;
5670 u8 level, mute;
5671 int new_level, new_mute;
5672 char *cmd;
5674 while ((cmd = next_cmd(&buf))) {
5675 if (!acpi_ec_read(volume_offset, &level))
5676 return -EIO;
5677 new_mute = mute = level & 0x40;
5678 new_level = level = level & 0xf;
5680 if (strlencmp(cmd, "up") == 0) {
5681 if (mute)
5682 new_mute = 0;
5683 else
5684 new_level = level == 15 ? 15 : level + 1;
5685 } else if (strlencmp(cmd, "down") == 0) {
5686 if (mute)
5687 new_mute = 0;
5688 else
5689 new_level = level == 0 ? 0 : level - 1;
5690 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5691 new_level >= 0 && new_level <= 15) {
5692 /* new_level set */
5693 } else if (strlencmp(cmd, "mute") == 0) {
5694 new_mute = 0x40;
5695 } else
5696 return -EINVAL;
5698 if (new_level != level) {
5699 /* mute doesn't change */
5701 cmos_cmd = (new_level > level) ?
5702 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5703 inc = new_level > level ? 1 : -1;
5705 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5706 !acpi_ec_write(volume_offset, level)))
5707 return -EIO;
5709 for (i = level; i != new_level; i += inc)
5710 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5711 !acpi_ec_write(volume_offset, i + inc))
5712 return -EIO;
5714 if (mute &&
5715 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5716 !acpi_ec_write(volume_offset, new_level + mute))) {
5717 return -EIO;
5721 if (new_mute != mute) {
5722 /* level doesn't change */
5724 cmos_cmd = (new_mute) ?
5725 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5727 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5728 !acpi_ec_write(volume_offset, level + new_mute))
5729 return -EIO;
5733 return 0;
5736 static struct ibm_struct volume_driver_data = {
5737 .name = "volume",
5738 .read = volume_read,
5739 .write = volume_write,
5742 /*************************************************************************
5743 * Fan subdriver
5747 * FAN ACCESS MODES
5749 * TPACPI_FAN_RD_ACPI_GFAN:
5750 * ACPI GFAN method: returns fan level
5752 * see TPACPI_FAN_WR_ACPI_SFAN
5753 * EC 0x2f (HFSP) not available if GFAN exists
5755 * TPACPI_FAN_WR_ACPI_SFAN:
5756 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5758 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5759 * it for writing.
5761 * TPACPI_FAN_WR_TPEC:
5762 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5763 * Supported on almost all ThinkPads
5765 * Fan speed changes of any sort (including those caused by the
5766 * disengaged mode) are usually done slowly by the firmware as the
5767 * maximum ammount of fan duty cycle change per second seems to be
5768 * limited.
5770 * Reading is not available if GFAN exists.
5771 * Writing is not available if SFAN exists.
5773 * Bits
5774 * 7 automatic mode engaged;
5775 * (default operation mode of the ThinkPad)
5776 * fan level is ignored in this mode.
5777 * 6 full speed mode (takes precedence over bit 7);
5778 * not available on all thinkpads. May disable
5779 * the tachometer while the fan controller ramps up
5780 * the speed (which can take up to a few *minutes*).
5781 * Speeds up fan to 100% duty-cycle, which is far above
5782 * the standard RPM levels. It is not impossible that
5783 * it could cause hardware damage.
5784 * 5-3 unused in some models. Extra bits for fan level
5785 * in others, but still useless as all values above
5786 * 7 map to the same speed as level 7 in these models.
5787 * 2-0 fan level (0..7 usually)
5788 * 0x00 = stop
5789 * 0x07 = max (set when temperatures critical)
5790 * Some ThinkPads may have other levels, see
5791 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5793 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5794 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5795 * does so, its initial value is meaningless (0x07).
5797 * For firmware bugs, refer to:
5798 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5800 * ----
5802 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5803 * Main fan tachometer reading (in RPM)
5805 * This register is present on all ThinkPads with a new-style EC, and
5806 * it is known not to be present on the A21m/e, and T22, as there is
5807 * something else in offset 0x84 according to the ACPI DSDT. Other
5808 * ThinkPads from this same time period (and earlier) probably lack the
5809 * tachometer as well.
5811 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5812 * was never fixed by IBM to report the EC firmware version string
5813 * probably support the tachometer (like the early X models), so
5814 * detecting it is quite hard. We need more data to know for sure.
5816 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5817 * might result.
5819 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5820 * mode.
5822 * For firmware bugs, refer to:
5823 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5825 * TPACPI_FAN_WR_ACPI_FANS:
5826 * ThinkPad X31, X40, X41. Not available in the X60.
5828 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5829 * high speed. ACPI DSDT seems to map these three speeds to levels
5830 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5831 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5833 * The speeds are stored on handles
5834 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5836 * There are three default speed sets, acessible as handles:
5837 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5839 * ACPI DSDT switches which set is in use depending on various
5840 * factors.
5842 * TPACPI_FAN_WR_TPEC is also available and should be used to
5843 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5844 * but the ACPI tables just mention level 7.
5847 enum { /* Fan control constants */
5848 fan_status_offset = 0x2f, /* EC register 0x2f */
5849 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5850 * 0x84 must be read before 0x85 */
5852 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5853 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5855 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5858 enum fan_status_access_mode {
5859 TPACPI_FAN_NONE = 0, /* No fan status or control */
5860 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5861 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5864 enum fan_control_access_mode {
5865 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5866 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5867 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5868 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5871 enum fan_control_commands {
5872 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5873 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5874 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5875 * and also watchdog cmd */
5878 static int fan_control_allowed;
5880 static enum fan_status_access_mode fan_status_access_mode;
5881 static enum fan_control_access_mode fan_control_access_mode;
5882 static enum fan_control_commands fan_control_commands;
5884 static u8 fan_control_initial_status;
5885 static u8 fan_control_desired_level;
5886 static u8 fan_control_resume_level;
5887 static int fan_watchdog_maxinterval;
5889 static struct mutex fan_mutex;
5891 static void fan_watchdog_fire(struct work_struct *ignored);
5892 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5894 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5895 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5896 "\\FSPD", /* 600e/x, 770e, 770x */
5897 ); /* all others */
5898 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5899 "JFNS", /* 770x-JL */
5900 ); /* all others */
5903 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5904 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5905 * be in auto mode (0x80).
5907 * This is corrected by any write to HFSP either by the driver, or
5908 * by the firmware.
5910 * We assume 0x07 really means auto mode while this quirk is active,
5911 * as this is far more likely than the ThinkPad being in level 7,
5912 * which is only used by the firmware during thermal emergencies.
5915 static void fan_quirk1_detect(void)
5917 /* In some ThinkPads, neither the EC nor the ACPI
5918 * DSDT initialize the HFSP register, and it ends up
5919 * being initially set to 0x07 when it *could* be
5920 * either 0x07 or 0x80.
5922 * Enable for TP-1Y (T43), TP-78 (R51e),
5923 * TP-76 (R52), TP-70 (T43, R52), which are known
5924 * to be buggy. */
5925 if (fan_control_initial_status == 0x07) {
5926 switch (thinkpad_id.ec_model) {
5927 case 0x5931: /* TP-1Y */
5928 case 0x3837: /* TP-78 */
5929 case 0x3637: /* TP-76 */
5930 case 0x3037: /* TP-70 */
5931 printk(TPACPI_NOTICE
5932 "fan_init: initial fan status is unknown, "
5933 "assuming it is in auto mode\n");
5934 tp_features.fan_ctrl_status_undef = 1;
5940 static void fan_quirk1_handle(u8 *fan_status)
5942 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5943 if (*fan_status != fan_control_initial_status) {
5944 /* something changed the HFSP regisnter since
5945 * driver init time, so it is not undefined
5946 * anymore */
5947 tp_features.fan_ctrl_status_undef = 0;
5948 } else {
5949 /* Return most likely status. In fact, it
5950 * might be the only possible status */
5951 *fan_status = TP_EC_FAN_AUTO;
5957 * Call with fan_mutex held
5959 static void fan_update_desired_level(u8 status)
5961 if ((status &
5962 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5963 if (status > 7)
5964 fan_control_desired_level = 7;
5965 else
5966 fan_control_desired_level = status;
5970 static int fan_get_status(u8 *status)
5972 u8 s;
5974 /* TODO:
5975 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5977 switch (fan_status_access_mode) {
5978 case TPACPI_FAN_RD_ACPI_GFAN:
5979 /* 570, 600e/x, 770e, 770x */
5981 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5982 return -EIO;
5984 if (likely(status))
5985 *status = s & 0x07;
5987 break;
5989 case TPACPI_FAN_RD_TPEC:
5990 /* all except 570, 600e/x, 770e, 770x */
5991 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5992 return -EIO;
5994 if (likely(status)) {
5995 *status = s;
5996 fan_quirk1_handle(status);
5999 break;
6001 default:
6002 return -ENXIO;
6005 return 0;
6008 static int fan_get_status_safe(u8 *status)
6010 int rc;
6011 u8 s;
6013 if (mutex_lock_killable(&fan_mutex))
6014 return -ERESTARTSYS;
6015 rc = fan_get_status(&s);
6016 if (!rc)
6017 fan_update_desired_level(s);
6018 mutex_unlock(&fan_mutex);
6020 if (status)
6021 *status = s;
6023 return rc;
6026 static int fan_get_speed(unsigned int *speed)
6028 u8 hi, lo;
6030 switch (fan_status_access_mode) {
6031 case TPACPI_FAN_RD_TPEC:
6032 /* all except 570, 600e/x, 770e, 770x */
6033 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6034 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6035 return -EIO;
6037 if (likely(speed))
6038 *speed = (hi << 8) | lo;
6040 break;
6042 default:
6043 return -ENXIO;
6046 return 0;
6049 static int fan_set_level(int level)
6051 if (!fan_control_allowed)
6052 return -EPERM;
6054 switch (fan_control_access_mode) {
6055 case TPACPI_FAN_WR_ACPI_SFAN:
6056 if (level >= 0 && level <= 7) {
6057 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6058 return -EIO;
6059 } else
6060 return -EINVAL;
6061 break;
6063 case TPACPI_FAN_WR_ACPI_FANS:
6064 case TPACPI_FAN_WR_TPEC:
6065 if (!(level & TP_EC_FAN_AUTO) &&
6066 !(level & TP_EC_FAN_FULLSPEED) &&
6067 ((level < 0) || (level > 7)))
6068 return -EINVAL;
6070 /* safety net should the EC not support AUTO
6071 * or FULLSPEED mode bits and just ignore them */
6072 if (level & TP_EC_FAN_FULLSPEED)
6073 level |= 7; /* safety min speed 7 */
6074 else if (level & TP_EC_FAN_AUTO)
6075 level |= 4; /* safety min speed 4 */
6077 if (!acpi_ec_write(fan_status_offset, level))
6078 return -EIO;
6079 else
6080 tp_features.fan_ctrl_status_undef = 0;
6081 break;
6083 default:
6084 return -ENXIO;
6086 return 0;
6089 static int fan_set_level_safe(int level)
6091 int rc;
6093 if (!fan_control_allowed)
6094 return -EPERM;
6096 if (mutex_lock_killable(&fan_mutex))
6097 return -ERESTARTSYS;
6099 if (level == TPACPI_FAN_LAST_LEVEL)
6100 level = fan_control_desired_level;
6102 rc = fan_set_level(level);
6103 if (!rc)
6104 fan_update_desired_level(level);
6106 mutex_unlock(&fan_mutex);
6107 return rc;
6110 static int fan_set_enable(void)
6112 u8 s;
6113 int rc;
6115 if (!fan_control_allowed)
6116 return -EPERM;
6118 if (mutex_lock_killable(&fan_mutex))
6119 return -ERESTARTSYS;
6121 switch (fan_control_access_mode) {
6122 case TPACPI_FAN_WR_ACPI_FANS:
6123 case TPACPI_FAN_WR_TPEC:
6124 rc = fan_get_status(&s);
6125 if (rc < 0)
6126 break;
6128 /* Don't go out of emergency fan mode */
6129 if (s != 7) {
6130 s &= 0x07;
6131 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6134 if (!acpi_ec_write(fan_status_offset, s))
6135 rc = -EIO;
6136 else {
6137 tp_features.fan_ctrl_status_undef = 0;
6138 rc = 0;
6140 break;
6142 case TPACPI_FAN_WR_ACPI_SFAN:
6143 rc = fan_get_status(&s);
6144 if (rc < 0)
6145 break;
6147 s &= 0x07;
6149 /* Set fan to at least level 4 */
6150 s |= 4;
6152 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6153 rc = -EIO;
6154 else
6155 rc = 0;
6156 break;
6158 default:
6159 rc = -ENXIO;
6162 mutex_unlock(&fan_mutex);
6163 return rc;
6166 static int fan_set_disable(void)
6168 int rc;
6170 if (!fan_control_allowed)
6171 return -EPERM;
6173 if (mutex_lock_killable(&fan_mutex))
6174 return -ERESTARTSYS;
6176 rc = 0;
6177 switch (fan_control_access_mode) {
6178 case TPACPI_FAN_WR_ACPI_FANS:
6179 case TPACPI_FAN_WR_TPEC:
6180 if (!acpi_ec_write(fan_status_offset, 0x00))
6181 rc = -EIO;
6182 else {
6183 fan_control_desired_level = 0;
6184 tp_features.fan_ctrl_status_undef = 0;
6186 break;
6188 case TPACPI_FAN_WR_ACPI_SFAN:
6189 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6190 rc = -EIO;
6191 else
6192 fan_control_desired_level = 0;
6193 break;
6195 default:
6196 rc = -ENXIO;
6200 mutex_unlock(&fan_mutex);
6201 return rc;
6204 static int fan_set_speed(int speed)
6206 int rc;
6208 if (!fan_control_allowed)
6209 return -EPERM;
6211 if (mutex_lock_killable(&fan_mutex))
6212 return -ERESTARTSYS;
6214 rc = 0;
6215 switch (fan_control_access_mode) {
6216 case TPACPI_FAN_WR_ACPI_FANS:
6217 if (speed >= 0 && speed <= 65535) {
6218 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6219 speed, speed, speed))
6220 rc = -EIO;
6221 } else
6222 rc = -EINVAL;
6223 break;
6225 default:
6226 rc = -ENXIO;
6229 mutex_unlock(&fan_mutex);
6230 return rc;
6233 static void fan_watchdog_reset(void)
6235 static int fan_watchdog_active;
6237 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6238 return;
6240 if (fan_watchdog_active)
6241 cancel_delayed_work(&fan_watchdog_task);
6243 if (fan_watchdog_maxinterval > 0 &&
6244 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6245 fan_watchdog_active = 1;
6246 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6247 msecs_to_jiffies(fan_watchdog_maxinterval
6248 * 1000))) {
6249 printk(TPACPI_ERR
6250 "failed to queue the fan watchdog, "
6251 "watchdog will not trigger\n");
6253 } else
6254 fan_watchdog_active = 0;
6257 static void fan_watchdog_fire(struct work_struct *ignored)
6259 int rc;
6261 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6262 return;
6264 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6265 rc = fan_set_enable();
6266 if (rc < 0) {
6267 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6268 "will try again later...\n", -rc);
6269 /* reschedule for later */
6270 fan_watchdog_reset();
6275 * SYSFS fan layout: hwmon compatible (device)
6277 * pwm*_enable:
6278 * 0: "disengaged" mode
6279 * 1: manual mode
6280 * 2: native EC "auto" mode (recommended, hardware default)
6282 * pwm*: set speed in manual mode, ignored otherwise.
6283 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6284 * interpolation.
6286 * fan*_input: tachometer reading, RPM
6289 * SYSFS fan layout: extensions
6291 * fan_watchdog (driver):
6292 * fan watchdog interval in seconds, 0 disables (default), max 120
6295 /* sysfs fan pwm1_enable ----------------------------------------------- */
6296 static ssize_t fan_pwm1_enable_show(struct device *dev,
6297 struct device_attribute *attr,
6298 char *buf)
6300 int res, mode;
6301 u8 status;
6303 res = fan_get_status_safe(&status);
6304 if (res)
6305 return res;
6307 if (status & TP_EC_FAN_FULLSPEED) {
6308 mode = 0;
6309 } else if (status & TP_EC_FAN_AUTO) {
6310 mode = 2;
6311 } else
6312 mode = 1;
6314 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6317 static ssize_t fan_pwm1_enable_store(struct device *dev,
6318 struct device_attribute *attr,
6319 const char *buf, size_t count)
6321 unsigned long t;
6322 int res, level;
6324 if (parse_strtoul(buf, 2, &t))
6325 return -EINVAL;
6327 switch (t) {
6328 case 0:
6329 level = TP_EC_FAN_FULLSPEED;
6330 break;
6331 case 1:
6332 level = TPACPI_FAN_LAST_LEVEL;
6333 break;
6334 case 2:
6335 level = TP_EC_FAN_AUTO;
6336 break;
6337 case 3:
6338 /* reserved for software-controlled auto mode */
6339 return -ENOSYS;
6340 default:
6341 return -EINVAL;
6344 res = fan_set_level_safe(level);
6345 if (res == -ENXIO)
6346 return -EINVAL;
6347 else if (res < 0)
6348 return res;
6350 fan_watchdog_reset();
6352 return count;
6355 static struct device_attribute dev_attr_fan_pwm1_enable =
6356 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6357 fan_pwm1_enable_show, fan_pwm1_enable_store);
6359 /* sysfs fan pwm1 ------------------------------------------------------ */
6360 static ssize_t fan_pwm1_show(struct device *dev,
6361 struct device_attribute *attr,
6362 char *buf)
6364 int res;
6365 u8 status;
6367 res = fan_get_status_safe(&status);
6368 if (res)
6369 return res;
6371 if ((status &
6372 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6373 status = fan_control_desired_level;
6375 if (status > 7)
6376 status = 7;
6378 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6381 static ssize_t fan_pwm1_store(struct device *dev,
6382 struct device_attribute *attr,
6383 const char *buf, size_t count)
6385 unsigned long s;
6386 int rc;
6387 u8 status, newlevel;
6389 if (parse_strtoul(buf, 255, &s))
6390 return -EINVAL;
6392 /* scale down from 0-255 to 0-7 */
6393 newlevel = (s >> 5) & 0x07;
6395 if (mutex_lock_killable(&fan_mutex))
6396 return -ERESTARTSYS;
6398 rc = fan_get_status(&status);
6399 if (!rc && (status &
6400 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6401 rc = fan_set_level(newlevel);
6402 if (rc == -ENXIO)
6403 rc = -EINVAL;
6404 else if (!rc) {
6405 fan_update_desired_level(newlevel);
6406 fan_watchdog_reset();
6410 mutex_unlock(&fan_mutex);
6411 return (rc)? rc : count;
6414 static struct device_attribute dev_attr_fan_pwm1 =
6415 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6416 fan_pwm1_show, fan_pwm1_store);
6418 /* sysfs fan fan1_input ------------------------------------------------ */
6419 static ssize_t fan_fan1_input_show(struct device *dev,
6420 struct device_attribute *attr,
6421 char *buf)
6423 int res;
6424 unsigned int speed;
6426 res = fan_get_speed(&speed);
6427 if (res < 0)
6428 return res;
6430 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6433 static struct device_attribute dev_attr_fan_fan1_input =
6434 __ATTR(fan1_input, S_IRUGO,
6435 fan_fan1_input_show, NULL);
6437 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6438 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6439 char *buf)
6441 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6444 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6445 const char *buf, size_t count)
6447 unsigned long t;
6449 if (parse_strtoul(buf, 120, &t))
6450 return -EINVAL;
6452 if (!fan_control_allowed)
6453 return -EPERM;
6455 fan_watchdog_maxinterval = t;
6456 fan_watchdog_reset();
6458 return count;
6461 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6462 fan_fan_watchdog_show, fan_fan_watchdog_store);
6464 /* --------------------------------------------------------------------- */
6465 static struct attribute *fan_attributes[] = {
6466 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6467 &dev_attr_fan_fan1_input.attr,
6468 NULL
6471 static const struct attribute_group fan_attr_group = {
6472 .attrs = fan_attributes,
6475 static int __init fan_init(struct ibm_init_struct *iibm)
6477 int rc;
6479 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6481 mutex_init(&fan_mutex);
6482 fan_status_access_mode = TPACPI_FAN_NONE;
6483 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6484 fan_control_commands = 0;
6485 fan_watchdog_maxinterval = 0;
6486 tp_features.fan_ctrl_status_undef = 0;
6487 fan_control_desired_level = 7;
6489 TPACPI_ACPIHANDLE_INIT(fans);
6490 TPACPI_ACPIHANDLE_INIT(gfan);
6491 TPACPI_ACPIHANDLE_INIT(sfan);
6493 if (gfan_handle) {
6494 /* 570, 600e/x, 770e, 770x */
6495 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6496 } else {
6497 /* all other ThinkPads: note that even old-style
6498 * ThinkPad ECs supports the fan control register */
6499 if (likely(acpi_ec_read(fan_status_offset,
6500 &fan_control_initial_status))) {
6501 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6502 fan_quirk1_detect();
6503 } else {
6504 printk(TPACPI_ERR
6505 "ThinkPad ACPI EC access misbehaving, "
6506 "fan status and control unavailable\n");
6507 return 1;
6511 if (sfan_handle) {
6512 /* 570, 770x-JL */
6513 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6514 fan_control_commands |=
6515 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6516 } else {
6517 if (!gfan_handle) {
6518 /* gfan without sfan means no fan control */
6519 /* all other models implement TP EC 0x2f control */
6521 if (fans_handle) {
6522 /* X31, X40, X41 */
6523 fan_control_access_mode =
6524 TPACPI_FAN_WR_ACPI_FANS;
6525 fan_control_commands |=
6526 TPACPI_FAN_CMD_SPEED |
6527 TPACPI_FAN_CMD_LEVEL |
6528 TPACPI_FAN_CMD_ENABLE;
6529 } else {
6530 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6531 fan_control_commands |=
6532 TPACPI_FAN_CMD_LEVEL |
6533 TPACPI_FAN_CMD_ENABLE;
6538 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6539 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6540 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6541 fan_status_access_mode, fan_control_access_mode);
6543 /* fan control master switch */
6544 if (!fan_control_allowed) {
6545 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6546 fan_control_commands = 0;
6547 dbg_printk(TPACPI_DBG_INIT,
6548 "fan control features disabled by parameter\n");
6551 /* update fan_control_desired_level */
6552 if (fan_status_access_mode != TPACPI_FAN_NONE)
6553 fan_get_status_safe(NULL);
6555 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6556 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6557 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6558 &fan_attr_group);
6559 if (rc < 0)
6560 return rc;
6562 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6563 &driver_attr_fan_watchdog);
6564 if (rc < 0) {
6565 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6566 &fan_attr_group);
6567 return rc;
6569 return 0;
6570 } else
6571 return 1;
6574 static void fan_exit(void)
6576 vdbg_printk(TPACPI_DBG_EXIT,
6577 "cancelling any pending fan watchdog tasks\n");
6579 /* FIXME: can we really do this unconditionally? */
6580 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6581 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6582 &driver_attr_fan_watchdog);
6584 cancel_delayed_work(&fan_watchdog_task);
6585 flush_workqueue(tpacpi_wq);
6588 static void fan_suspend(pm_message_t state)
6590 int rc;
6592 if (!fan_control_allowed)
6593 return;
6595 /* Store fan status in cache */
6596 fan_control_resume_level = 0;
6597 rc = fan_get_status_safe(&fan_control_resume_level);
6598 if (rc < 0)
6599 printk(TPACPI_NOTICE
6600 "failed to read fan level for later "
6601 "restore during resume: %d\n", rc);
6603 /* if it is undefined, don't attempt to restore it.
6604 * KEEP THIS LAST */
6605 if (tp_features.fan_ctrl_status_undef)
6606 fan_control_resume_level = 0;
6609 static void fan_resume(void)
6611 u8 current_level = 7;
6612 bool do_set = false;
6613 int rc;
6615 /* DSDT *always* updates status on resume */
6616 tp_features.fan_ctrl_status_undef = 0;
6618 if (!fan_control_allowed ||
6619 !fan_control_resume_level ||
6620 (fan_get_status_safe(&current_level) < 0))
6621 return;
6623 switch (fan_control_access_mode) {
6624 case TPACPI_FAN_WR_ACPI_SFAN:
6625 /* never decrease fan level */
6626 do_set = (fan_control_resume_level > current_level);
6627 break;
6628 case TPACPI_FAN_WR_ACPI_FANS:
6629 case TPACPI_FAN_WR_TPEC:
6630 /* never decrease fan level, scale is:
6631 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6633 * We expect the firmware to set either 7 or AUTO, but we
6634 * handle FULLSPEED out of paranoia.
6636 * So, we can safely only restore FULLSPEED or 7, anything
6637 * else could slow the fan. Restoring AUTO is useless, at
6638 * best that's exactly what the DSDT already set (it is the
6639 * slower it uses).
6641 * Always keep in mind that the DSDT *will* have set the
6642 * fans to what the vendor supposes is the best level. We
6643 * muck with it only to speed the fan up.
6645 if (fan_control_resume_level != 7 &&
6646 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6647 return;
6648 else
6649 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6650 (current_level != fan_control_resume_level);
6651 break;
6652 default:
6653 return;
6655 if (do_set) {
6656 printk(TPACPI_NOTICE
6657 "restoring fan level to 0x%02x\n",
6658 fan_control_resume_level);
6659 rc = fan_set_level_safe(fan_control_resume_level);
6660 if (rc < 0)
6661 printk(TPACPI_NOTICE
6662 "failed to restore fan level: %d\n", rc);
6666 static int fan_read(char *p)
6668 int len = 0;
6669 int rc;
6670 u8 status;
6671 unsigned int speed = 0;
6673 switch (fan_status_access_mode) {
6674 case TPACPI_FAN_RD_ACPI_GFAN:
6675 /* 570, 600e/x, 770e, 770x */
6676 rc = fan_get_status_safe(&status);
6677 if (rc < 0)
6678 return rc;
6680 len += sprintf(p + len, "status:\t\t%s\n"
6681 "level:\t\t%d\n",
6682 (status != 0) ? "enabled" : "disabled", status);
6683 break;
6685 case TPACPI_FAN_RD_TPEC:
6686 /* all except 570, 600e/x, 770e, 770x */
6687 rc = fan_get_status_safe(&status);
6688 if (rc < 0)
6689 return rc;
6691 len += sprintf(p + len, "status:\t\t%s\n",
6692 (status != 0) ? "enabled" : "disabled");
6694 rc = fan_get_speed(&speed);
6695 if (rc < 0)
6696 return rc;
6698 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6700 if (status & TP_EC_FAN_FULLSPEED)
6701 /* Disengaged mode takes precedence */
6702 len += sprintf(p + len, "level:\t\tdisengaged\n");
6703 else if (status & TP_EC_FAN_AUTO)
6704 len += sprintf(p + len, "level:\t\tauto\n");
6705 else
6706 len += sprintf(p + len, "level:\t\t%d\n", status);
6707 break;
6709 case TPACPI_FAN_NONE:
6710 default:
6711 len += sprintf(p + len, "status:\t\tnot supported\n");
6714 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6715 len += sprintf(p + len, "commands:\tlevel <level>");
6717 switch (fan_control_access_mode) {
6718 case TPACPI_FAN_WR_ACPI_SFAN:
6719 len += sprintf(p + len, " (<level> is 0-7)\n");
6720 break;
6722 default:
6723 len += sprintf(p + len, " (<level> is 0-7, "
6724 "auto, disengaged, full-speed)\n");
6725 break;
6729 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6730 len += sprintf(p + len, "commands:\tenable, disable\n"
6731 "commands:\twatchdog <timeout> (<timeout> "
6732 "is 0 (off), 1-120 (seconds))\n");
6734 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6735 len += sprintf(p + len, "commands:\tspeed <speed>"
6736 " (<speed> is 0-65535)\n");
6738 return len;
6741 static int fan_write_cmd_level(const char *cmd, int *rc)
6743 int level;
6745 if (strlencmp(cmd, "level auto") == 0)
6746 level = TP_EC_FAN_AUTO;
6747 else if ((strlencmp(cmd, "level disengaged") == 0) |
6748 (strlencmp(cmd, "level full-speed") == 0))
6749 level = TP_EC_FAN_FULLSPEED;
6750 else if (sscanf(cmd, "level %d", &level) != 1)
6751 return 0;
6753 *rc = fan_set_level_safe(level);
6754 if (*rc == -ENXIO)
6755 printk(TPACPI_ERR "level command accepted for unsupported "
6756 "access mode %d", fan_control_access_mode);
6758 return 1;
6761 static int fan_write_cmd_enable(const char *cmd, int *rc)
6763 if (strlencmp(cmd, "enable") != 0)
6764 return 0;
6766 *rc = fan_set_enable();
6767 if (*rc == -ENXIO)
6768 printk(TPACPI_ERR "enable command accepted for unsupported "
6769 "access mode %d", fan_control_access_mode);
6771 return 1;
6774 static int fan_write_cmd_disable(const char *cmd, int *rc)
6776 if (strlencmp(cmd, "disable") != 0)
6777 return 0;
6779 *rc = fan_set_disable();
6780 if (*rc == -ENXIO)
6781 printk(TPACPI_ERR "disable command accepted for unsupported "
6782 "access mode %d", fan_control_access_mode);
6784 return 1;
6787 static int fan_write_cmd_speed(const char *cmd, int *rc)
6789 int speed;
6791 /* TODO:
6792 * Support speed <low> <medium> <high> ? */
6794 if (sscanf(cmd, "speed %d", &speed) != 1)
6795 return 0;
6797 *rc = fan_set_speed(speed);
6798 if (*rc == -ENXIO)
6799 printk(TPACPI_ERR "speed command accepted for unsupported "
6800 "access mode %d", fan_control_access_mode);
6802 return 1;
6805 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6807 int interval;
6809 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6810 return 0;
6812 if (interval < 0 || interval > 120)
6813 *rc = -EINVAL;
6814 else
6815 fan_watchdog_maxinterval = interval;
6817 return 1;
6820 static int fan_write(char *buf)
6822 char *cmd;
6823 int rc = 0;
6825 while (!rc && (cmd = next_cmd(&buf))) {
6826 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6827 fan_write_cmd_level(cmd, &rc)) &&
6828 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6829 (fan_write_cmd_enable(cmd, &rc) ||
6830 fan_write_cmd_disable(cmd, &rc) ||
6831 fan_write_cmd_watchdog(cmd, &rc))) &&
6832 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6833 fan_write_cmd_speed(cmd, &rc))
6835 rc = -EINVAL;
6836 else if (!rc)
6837 fan_watchdog_reset();
6840 return rc;
6843 static struct ibm_struct fan_driver_data = {
6844 .name = "fan",
6845 .read = fan_read,
6846 .write = fan_write,
6847 .exit = fan_exit,
6848 .suspend = fan_suspend,
6849 .resume = fan_resume,
6852 /****************************************************************************
6853 ****************************************************************************
6855 * Infrastructure
6857 ****************************************************************************
6858 ****************************************************************************/
6860 /* sysfs name ---------------------------------------------------------- */
6861 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6862 struct device_attribute *attr,
6863 char *buf)
6865 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6868 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6869 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6871 /* --------------------------------------------------------------------- */
6873 /* /proc support */
6874 static struct proc_dir_entry *proc_dir;
6877 * Module and infrastructure proble, init and exit handling
6880 static int force_load;
6882 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6883 static const char * __init str_supported(int is_supported)
6885 static char text_unsupported[] __initdata = "not supported";
6887 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6889 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6891 static void ibm_exit(struct ibm_struct *ibm)
6893 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6895 list_del_init(&ibm->all_drivers);
6897 if (ibm->flags.acpi_notify_installed) {
6898 dbg_printk(TPACPI_DBG_EXIT,
6899 "%s: acpi_remove_notify_handler\n", ibm->name);
6900 BUG_ON(!ibm->acpi);
6901 acpi_remove_notify_handler(*ibm->acpi->handle,
6902 ibm->acpi->type,
6903 dispatch_acpi_notify);
6904 ibm->flags.acpi_notify_installed = 0;
6905 ibm->flags.acpi_notify_installed = 0;
6908 if (ibm->flags.proc_created) {
6909 dbg_printk(TPACPI_DBG_EXIT,
6910 "%s: remove_proc_entry\n", ibm->name);
6911 remove_proc_entry(ibm->name, proc_dir);
6912 ibm->flags.proc_created = 0;
6915 if (ibm->flags.acpi_driver_registered) {
6916 dbg_printk(TPACPI_DBG_EXIT,
6917 "%s: acpi_bus_unregister_driver\n", ibm->name);
6918 BUG_ON(!ibm->acpi);
6919 acpi_bus_unregister_driver(ibm->acpi->driver);
6920 kfree(ibm->acpi->driver);
6921 ibm->acpi->driver = NULL;
6922 ibm->flags.acpi_driver_registered = 0;
6925 if (ibm->flags.init_called && ibm->exit) {
6926 ibm->exit();
6927 ibm->flags.init_called = 0;
6930 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6933 static int __init ibm_init(struct ibm_init_struct *iibm)
6935 int ret;
6936 struct ibm_struct *ibm = iibm->data;
6937 struct proc_dir_entry *entry;
6939 BUG_ON(ibm == NULL);
6941 INIT_LIST_HEAD(&ibm->all_drivers);
6943 if (ibm->flags.experimental && !experimental)
6944 return 0;
6946 dbg_printk(TPACPI_DBG_INIT,
6947 "probing for %s\n", ibm->name);
6949 if (iibm->init) {
6950 ret = iibm->init(iibm);
6951 if (ret > 0)
6952 return 0; /* probe failed */
6953 if (ret)
6954 return ret;
6956 ibm->flags.init_called = 1;
6959 if (ibm->acpi) {
6960 if (ibm->acpi->hid) {
6961 ret = register_tpacpi_subdriver(ibm);
6962 if (ret)
6963 goto err_out;
6966 if (ibm->acpi->notify) {
6967 ret = setup_acpi_notify(ibm);
6968 if (ret == -ENODEV) {
6969 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6970 ibm->name);
6971 ret = 0;
6972 goto err_out;
6974 if (ret < 0)
6975 goto err_out;
6979 dbg_printk(TPACPI_DBG_INIT,
6980 "%s installed\n", ibm->name);
6982 if (ibm->read) {
6983 entry = create_proc_entry(ibm->name,
6984 S_IFREG | S_IRUGO | S_IWUSR,
6985 proc_dir);
6986 if (!entry) {
6987 printk(TPACPI_ERR "unable to create proc entry %s\n",
6988 ibm->name);
6989 ret = -ENODEV;
6990 goto err_out;
6992 entry->owner = THIS_MODULE;
6993 entry->data = ibm;
6994 entry->read_proc = &dispatch_procfs_read;
6995 if (ibm->write)
6996 entry->write_proc = &dispatch_procfs_write;
6997 ibm->flags.proc_created = 1;
7000 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7002 return 0;
7004 err_out:
7005 dbg_printk(TPACPI_DBG_INIT,
7006 "%s: at error exit path with result %d\n",
7007 ibm->name, ret);
7009 ibm_exit(ibm);
7010 return (ret < 0)? ret : 0;
7013 /* Probing */
7015 /* returns 0 - probe ok, or < 0 - probe error.
7016 * Probe ok doesn't mean thinkpad found.
7017 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7018 static int __must_check __init get_thinkpad_model_data(
7019 struct thinkpad_id_data *tp)
7021 const struct dmi_device *dev = NULL;
7022 char ec_fw_string[18];
7023 char const *s;
7025 if (!tp)
7026 return -EINVAL;
7028 memset(tp, 0, sizeof(*tp));
7030 if (dmi_name_in_vendors("IBM"))
7031 tp->vendor = PCI_VENDOR_ID_IBM;
7032 else if (dmi_name_in_vendors("LENOVO"))
7033 tp->vendor = PCI_VENDOR_ID_LENOVO;
7034 else
7035 return 0;
7037 s = dmi_get_system_info(DMI_BIOS_VERSION);
7038 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7039 if (s && !tp->bios_version_str)
7040 return -ENOMEM;
7041 if (!tp->bios_version_str)
7042 return 0;
7043 tp->bios_model = tp->bios_version_str[0]
7044 | (tp->bios_version_str[1] << 8);
7047 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7048 * X32 or newer, all Z series; Some models must have an
7049 * up-to-date BIOS or they will not be detected.
7051 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7053 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7054 if (sscanf(dev->name,
7055 "IBM ThinkPad Embedded Controller -[%17c",
7056 ec_fw_string) == 1) {
7057 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7058 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7060 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7061 if (!tp->ec_version_str)
7062 return -ENOMEM;
7063 tp->ec_model = ec_fw_string[0]
7064 | (ec_fw_string[1] << 8);
7065 break;
7069 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7070 if (s && !strnicmp(s, "ThinkPad", 8)) {
7071 tp->model_str = kstrdup(s, GFP_KERNEL);
7072 if (!tp->model_str)
7073 return -ENOMEM;
7076 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7077 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7078 if (s && !tp->nummodel_str)
7079 return -ENOMEM;
7081 return 0;
7084 static int __init probe_for_thinkpad(void)
7086 int is_thinkpad;
7088 if (acpi_disabled)
7089 return -ENODEV;
7092 * Non-ancient models have better DMI tagging, but very old models
7093 * don't.
7095 is_thinkpad = (thinkpad_id.model_str != NULL);
7097 /* ec is required because many other handles are relative to it */
7098 TPACPI_ACPIHANDLE_INIT(ec);
7099 if (!ec_handle) {
7100 if (is_thinkpad)
7101 printk(TPACPI_ERR
7102 "Not yet supported ThinkPad detected!\n");
7103 return -ENODEV;
7107 * Risks a regression on very old machines, but reduces potential
7108 * false positives a damn great deal
7110 if (!is_thinkpad)
7111 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7113 if (!is_thinkpad && !force_load)
7114 return -ENODEV;
7116 return 0;
7120 /* Module init, exit, parameters */
7122 static struct ibm_init_struct ibms_init[] __initdata = {
7124 .init = thinkpad_acpi_driver_init,
7125 .data = &thinkpad_acpi_driver_data,
7128 .init = hotkey_init,
7129 .data = &hotkey_driver_data,
7132 .init = bluetooth_init,
7133 .data = &bluetooth_driver_data,
7136 .init = wan_init,
7137 .data = &wan_driver_data,
7140 .init = uwb_init,
7141 .data = &uwb_driver_data,
7143 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7145 .init = video_init,
7146 .data = &video_driver_data,
7148 #endif
7150 .init = light_init,
7151 .data = &light_driver_data,
7153 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7155 .init = dock_init,
7156 .data = &dock_driver_data[0],
7159 .init = dock_init2,
7160 .data = &dock_driver_data[1],
7162 #endif
7163 #ifdef CONFIG_THINKPAD_ACPI_BAY
7165 .init = bay_init,
7166 .data = &bay_driver_data,
7168 #endif
7170 .init = cmos_init,
7171 .data = &cmos_driver_data,
7174 .init = led_init,
7175 .data = &led_driver_data,
7178 .init = beep_init,
7179 .data = &beep_driver_data,
7182 .init = thermal_init,
7183 .data = &thermal_driver_data,
7186 .data = &ecdump_driver_data,
7189 .init = brightness_init,
7190 .data = &brightness_driver_data,
7193 .data = &volume_driver_data,
7196 .init = fan_init,
7197 .data = &fan_driver_data,
7201 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7203 unsigned int i;
7204 struct ibm_struct *ibm;
7206 if (!kp || !kp->name || !val)
7207 return -EINVAL;
7209 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7210 ibm = ibms_init[i].data;
7211 WARN_ON(ibm == NULL);
7213 if (!ibm || !ibm->name)
7214 continue;
7216 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7217 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7218 return -ENOSPC;
7219 strcpy(ibms_init[i].param, val);
7220 strcat(ibms_init[i].param, ",");
7221 return 0;
7225 return -EINVAL;
7228 module_param(experimental, int, 0);
7229 MODULE_PARM_DESC(experimental,
7230 "Enables experimental features when non-zero");
7232 module_param_named(debug, dbg_level, uint, 0);
7233 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7235 module_param(force_load, bool, 0);
7236 MODULE_PARM_DESC(force_load,
7237 "Attempts to load the driver even on a "
7238 "mis-identified ThinkPad when true");
7240 module_param_named(fan_control, fan_control_allowed, bool, 0);
7241 MODULE_PARM_DESC(fan_control,
7242 "Enables setting fan parameters features when true");
7244 module_param_named(brightness_mode, brightness_mode, int, 0);
7245 MODULE_PARM_DESC(brightness_mode,
7246 "Selects brightness control strategy: "
7247 "0=auto, 1=EC, 2=CMOS, 3=both");
7249 module_param(brightness_enable, uint, 0);
7250 MODULE_PARM_DESC(brightness_enable,
7251 "Enables backlight control when 1, disables when 0");
7253 module_param(hotkey_report_mode, uint, 0);
7254 MODULE_PARM_DESC(hotkey_report_mode,
7255 "used for backwards compatibility with userspace, "
7256 "see documentation");
7258 #define TPACPI_PARAM(feature) \
7259 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7260 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7261 "at module load, see documentation")
7263 TPACPI_PARAM(hotkey);
7264 TPACPI_PARAM(bluetooth);
7265 TPACPI_PARAM(video);
7266 TPACPI_PARAM(light);
7267 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7268 TPACPI_PARAM(dock);
7269 #endif
7270 #ifdef CONFIG_THINKPAD_ACPI_BAY
7271 TPACPI_PARAM(bay);
7272 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7273 TPACPI_PARAM(cmos);
7274 TPACPI_PARAM(led);
7275 TPACPI_PARAM(beep);
7276 TPACPI_PARAM(ecdump);
7277 TPACPI_PARAM(brightness);
7278 TPACPI_PARAM(volume);
7279 TPACPI_PARAM(fan);
7281 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7282 module_param(dbg_wlswemul, uint, 0);
7283 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7284 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7285 MODULE_PARM_DESC(wlsw_state,
7286 "Initial state of the emulated WLSW switch");
7288 module_param(dbg_bluetoothemul, uint, 0);
7289 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7290 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7291 MODULE_PARM_DESC(bluetooth_state,
7292 "Initial state of the emulated bluetooth switch");
7294 module_param(dbg_wwanemul, uint, 0);
7295 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7296 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7297 MODULE_PARM_DESC(wwan_state,
7298 "Initial state of the emulated WWAN switch");
7300 module_param(dbg_uwbemul, uint, 0);
7301 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7302 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7303 MODULE_PARM_DESC(uwb_state,
7304 "Initial state of the emulated UWB switch");
7305 #endif
7307 static void thinkpad_acpi_module_exit(void)
7309 struct ibm_struct *ibm, *itmp;
7311 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7313 list_for_each_entry_safe_reverse(ibm, itmp,
7314 &tpacpi_all_drivers,
7315 all_drivers) {
7316 ibm_exit(ibm);
7319 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7321 if (tpacpi_inputdev) {
7322 if (tp_features.input_device_registered)
7323 input_unregister_device(tpacpi_inputdev);
7324 else
7325 input_free_device(tpacpi_inputdev);
7328 if (tpacpi_hwmon)
7329 hwmon_device_unregister(tpacpi_hwmon);
7331 if (tp_features.sensors_pdev_attrs_registered)
7332 device_remove_file(&tpacpi_sensors_pdev->dev,
7333 &dev_attr_thinkpad_acpi_pdev_name);
7334 if (tpacpi_sensors_pdev)
7335 platform_device_unregister(tpacpi_sensors_pdev);
7336 if (tpacpi_pdev)
7337 platform_device_unregister(tpacpi_pdev);
7339 if (tp_features.sensors_pdrv_attrs_registered)
7340 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7341 if (tp_features.platform_drv_attrs_registered)
7342 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7344 if (tp_features.sensors_pdrv_registered)
7345 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7347 if (tp_features.platform_drv_registered)
7348 platform_driver_unregister(&tpacpi_pdriver);
7350 if (proc_dir)
7351 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7353 if (tpacpi_wq)
7354 destroy_workqueue(tpacpi_wq);
7356 kfree(thinkpad_id.bios_version_str);
7357 kfree(thinkpad_id.ec_version_str);
7358 kfree(thinkpad_id.model_str);
7362 static int __init thinkpad_acpi_module_init(void)
7364 int ret, i;
7366 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7368 /* Parameter checking */
7369 if (hotkey_report_mode > 2)
7370 return -EINVAL;
7372 /* Driver-level probe */
7374 ret = get_thinkpad_model_data(&thinkpad_id);
7375 if (ret) {
7376 printk(TPACPI_ERR
7377 "unable to get DMI data: %d\n", ret);
7378 thinkpad_acpi_module_exit();
7379 return ret;
7381 ret = probe_for_thinkpad();
7382 if (ret) {
7383 thinkpad_acpi_module_exit();
7384 return ret;
7387 /* Driver initialization */
7389 TPACPI_ACPIHANDLE_INIT(ecrd);
7390 TPACPI_ACPIHANDLE_INIT(ecwr);
7392 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7393 if (!tpacpi_wq) {
7394 thinkpad_acpi_module_exit();
7395 return -ENOMEM;
7398 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7399 if (!proc_dir) {
7400 printk(TPACPI_ERR
7401 "unable to create proc dir " TPACPI_PROC_DIR);
7402 thinkpad_acpi_module_exit();
7403 return -ENODEV;
7405 proc_dir->owner = THIS_MODULE;
7407 ret = platform_driver_register(&tpacpi_pdriver);
7408 if (ret) {
7409 printk(TPACPI_ERR
7410 "unable to register main platform driver\n");
7411 thinkpad_acpi_module_exit();
7412 return ret;
7414 tp_features.platform_drv_registered = 1;
7416 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7417 if (ret) {
7418 printk(TPACPI_ERR
7419 "unable to register hwmon platform driver\n");
7420 thinkpad_acpi_module_exit();
7421 return ret;
7423 tp_features.sensors_pdrv_registered = 1;
7425 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7426 if (!ret) {
7427 tp_features.platform_drv_attrs_registered = 1;
7428 ret = tpacpi_create_driver_attributes(
7429 &tpacpi_hwmon_pdriver.driver);
7431 if (ret) {
7432 printk(TPACPI_ERR
7433 "unable to create sysfs driver attributes\n");
7434 thinkpad_acpi_module_exit();
7435 return ret;
7437 tp_features.sensors_pdrv_attrs_registered = 1;
7440 /* Device initialization */
7441 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7442 NULL, 0);
7443 if (IS_ERR(tpacpi_pdev)) {
7444 ret = PTR_ERR(tpacpi_pdev);
7445 tpacpi_pdev = NULL;
7446 printk(TPACPI_ERR "unable to register platform device\n");
7447 thinkpad_acpi_module_exit();
7448 return ret;
7450 tpacpi_sensors_pdev = platform_device_register_simple(
7451 TPACPI_HWMON_DRVR_NAME,
7452 -1, NULL, 0);
7453 if (IS_ERR(tpacpi_sensors_pdev)) {
7454 ret = PTR_ERR(tpacpi_sensors_pdev);
7455 tpacpi_sensors_pdev = NULL;
7456 printk(TPACPI_ERR
7457 "unable to register hwmon platform device\n");
7458 thinkpad_acpi_module_exit();
7459 return ret;
7461 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7462 &dev_attr_thinkpad_acpi_pdev_name);
7463 if (ret) {
7464 printk(TPACPI_ERR
7465 "unable to create sysfs hwmon device attributes\n");
7466 thinkpad_acpi_module_exit();
7467 return ret;
7469 tp_features.sensors_pdev_attrs_registered = 1;
7470 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7471 if (IS_ERR(tpacpi_hwmon)) {
7472 ret = PTR_ERR(tpacpi_hwmon);
7473 tpacpi_hwmon = NULL;
7474 printk(TPACPI_ERR "unable to register hwmon device\n");
7475 thinkpad_acpi_module_exit();
7476 return ret;
7478 mutex_init(&tpacpi_inputdev_send_mutex);
7479 tpacpi_inputdev = input_allocate_device();
7480 if (!tpacpi_inputdev) {
7481 printk(TPACPI_ERR "unable to allocate input device\n");
7482 thinkpad_acpi_module_exit();
7483 return -ENOMEM;
7484 } else {
7485 /* Prepare input device, but don't register */
7486 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7487 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7488 tpacpi_inputdev->id.bustype = BUS_HOST;
7489 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7490 thinkpad_id.vendor :
7491 PCI_VENDOR_ID_IBM;
7492 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7493 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7495 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7496 ret = ibm_init(&ibms_init[i]);
7497 if (ret >= 0 && *ibms_init[i].param)
7498 ret = ibms_init[i].data->write(ibms_init[i].param);
7499 if (ret < 0) {
7500 thinkpad_acpi_module_exit();
7501 return ret;
7504 ret = input_register_device(tpacpi_inputdev);
7505 if (ret < 0) {
7506 printk(TPACPI_ERR "unable to register input device\n");
7507 thinkpad_acpi_module_exit();
7508 return ret;
7509 } else {
7510 tp_features.input_device_registered = 1;
7513 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7514 return 0;
7517 /* Please remove this in year 2009 */
7518 MODULE_ALIAS("ibm_acpi");
7520 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7523 * DMI matching for module autoloading
7525 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7526 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7528 * Only models listed in thinkwiki will be supported, so add yours
7529 * if it is not there yet.
7531 #define IBM_BIOS_MODULE_ALIAS(__type) \
7532 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7534 /* Non-ancient thinkpads */
7535 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7536 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7538 /* Ancient thinkpad BIOSes have to be identified by
7539 * BIOS type or model number, and there are far less
7540 * BIOS types than model numbers... */
7541 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7542 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7543 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7545 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7546 MODULE_DESCRIPTION(TPACPI_DESC);
7547 MODULE_VERSION(TPACPI_VERSION);
7548 MODULE_LICENSE("GPL");
7550 module_init(thinkpad_acpi_module_init);
7551 module_exit(thinkpad_acpi_module_exit);