thinkpad-acpi: cleanup debug helpers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob2014b6df0433c01a1f6415818f370ce887aac620
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
188 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
189 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
190 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
193 /****************************************************************************
194 * Driver-wide structs and misc. variables
197 struct ibm_struct;
199 struct tp_acpi_drv_struct {
200 const struct acpi_device_id *hid;
201 struct acpi_driver *driver;
203 void (*notify) (struct ibm_struct *, u32);
204 acpi_handle *handle;
205 u32 type;
206 struct acpi_device *device;
209 struct ibm_struct {
210 char *name;
212 int (*read) (char *);
213 int (*write) (char *);
214 void (*exit) (void);
215 void (*resume) (void);
216 void (*suspend) (pm_message_t state);
217 void (*shutdown) (void);
219 struct list_head all_drivers;
221 struct tp_acpi_drv_struct *acpi;
223 struct {
224 u8 acpi_driver_registered:1;
225 u8 acpi_notify_installed:1;
226 u8 proc_created:1;
227 u8 init_called:1;
228 u8 experimental:1;
229 } flags;
232 struct ibm_init_struct {
233 char param[32];
235 int (*init) (struct ibm_init_struct *);
236 struct ibm_struct *data;
239 static struct {
240 #ifdef CONFIG_THINKPAD_ACPI_BAY
241 u32 bay_status:1;
242 u32 bay_eject:1;
243 u32 bay_status2:1;
244 u32 bay_eject2:1;
245 #endif
246 u32 bluetooth:1;
247 u32 hotkey:1;
248 u32 hotkey_mask:1;
249 u32 hotkey_wlsw:1;
250 u32 hotkey_tablet:1;
251 u32 light:1;
252 u32 light_status:1;
253 u32 bright_16levels:1;
254 u32 bright_acpimode:1;
255 u32 wan:1;
256 u32 uwb:1;
257 u32 fan_ctrl_status_undef:1;
258 u32 input_device_registered:1;
259 u32 platform_drv_registered:1;
260 u32 platform_drv_attrs_registered:1;
261 u32 sensors_pdrv_registered:1;
262 u32 sensors_pdrv_attrs_registered:1;
263 u32 sensors_pdev_attrs_registered:1;
264 u32 hotkey_poll_active:1;
265 } tp_features;
267 static struct {
268 u16 hotkey_mask_ff:1;
269 u16 bright_cmos_ec_unsync:1;
270 } tp_warned;
272 struct thinkpad_id_data {
273 unsigned int vendor; /* ThinkPad vendor:
274 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
276 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
277 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
279 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
280 u16 ec_model;
282 char *model_str; /* ThinkPad T43 */
283 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
285 static struct thinkpad_id_data thinkpad_id;
287 static enum {
288 TPACPI_LIFE_INIT = 0,
289 TPACPI_LIFE_RUNNING,
290 TPACPI_LIFE_EXITING,
291 } tpacpi_lifecycle;
293 static int experimental;
294 static u32 dbg_level;
296 static struct workqueue_struct *tpacpi_wq;
298 enum led_status_t {
299 TPACPI_LED_OFF = 0,
300 TPACPI_LED_ON,
301 TPACPI_LED_BLINK,
304 /* Special LED class that can defer work */
305 struct tpacpi_led_classdev {
306 struct led_classdev led_classdev;
307 struct work_struct work;
308 enum led_status_t new_state;
309 unsigned int led;
312 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
313 static int dbg_wlswemul;
314 static int tpacpi_wlsw_emulstate;
315 static int dbg_bluetoothemul;
316 static int tpacpi_bluetooth_emulstate;
317 static int dbg_wwanemul;
318 static int tpacpi_wwan_emulstate;
319 static int dbg_uwbemul;
320 static int tpacpi_uwb_emulstate;
321 #endif
324 /*************************************************************************
325 * Debugging helpers
328 #define dbg_printk(a_dbg_level, format, arg...) \
329 do { if (dbg_level & (a_dbg_level)) \
330 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
331 } while (0)
333 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
334 #define vdbg_printk dbg_printk
335 static const char *str_supported(int is_supported);
336 #else
337 #define vdbg_printk(a_dbg_level, format, arg...) \
338 do { } while (0)
339 #endif
342 /****************************************************************************
343 ****************************************************************************
345 * ACPI Helpers and device model
347 ****************************************************************************
348 ****************************************************************************/
350 /*************************************************************************
351 * ACPI basic handles
354 static acpi_handle root_handle;
356 #define TPACPI_HANDLE(object, parent, paths...) \
357 static acpi_handle object##_handle; \
358 static acpi_handle *object##_parent = &parent##_handle; \
359 static char *object##_path; \
360 static char *object##_paths[] = { paths }
362 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
363 "\\_SB.PCI.ISA.EC", /* 570 */
364 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
365 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
366 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
367 "\\_SB.PCI0.ICH3.EC0", /* R31 */
368 "\\_SB.PCI0.LPC.EC", /* all others */
371 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
372 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
374 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
375 /* T4x, X31, X40 */
376 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
377 "\\CMS", /* R40, R40e */
378 ); /* all others */
380 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
381 "^HKEY", /* R30, R31 */
382 "HKEY", /* all others */
383 ); /* 570 */
385 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
386 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
387 "\\_SB.PCI0.VID0", /* 770e */
388 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
389 "\\_SB.PCI0.AGP.VID", /* all others */
390 ); /* R30, R31 */
393 /*************************************************************************
394 * ACPI helpers
397 static int acpi_evalf(acpi_handle handle,
398 void *res, char *method, char *fmt, ...)
400 char *fmt0 = fmt;
401 struct acpi_object_list params;
402 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
403 struct acpi_buffer result, *resultp;
404 union acpi_object out_obj;
405 acpi_status status;
406 va_list ap;
407 char res_type;
408 int success;
409 int quiet;
411 if (!*fmt) {
412 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
413 return 0;
416 if (*fmt == 'q') {
417 quiet = 1;
418 fmt++;
419 } else
420 quiet = 0;
422 res_type = *(fmt++);
424 params.count = 0;
425 params.pointer = &in_objs[0];
427 va_start(ap, fmt);
428 while (*fmt) {
429 char c = *(fmt++);
430 switch (c) {
431 case 'd': /* int */
432 in_objs[params.count].integer.value = va_arg(ap, int);
433 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
434 break;
435 /* add more types as needed */
436 default:
437 printk(TPACPI_ERR "acpi_evalf() called "
438 "with invalid format character '%c'\n", c);
439 return 0;
442 va_end(ap);
444 if (res_type != 'v') {
445 result.length = sizeof(out_obj);
446 result.pointer = &out_obj;
447 resultp = &result;
448 } else
449 resultp = NULL;
451 status = acpi_evaluate_object(handle, method, &params, resultp);
453 switch (res_type) {
454 case 'd': /* int */
455 if (res)
456 *(int *)res = out_obj.integer.value;
457 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
458 break;
459 case 'v': /* void */
460 success = status == AE_OK;
461 break;
462 /* add more types as needed */
463 default:
464 printk(TPACPI_ERR "acpi_evalf() called "
465 "with invalid format character '%c'\n", res_type);
466 return 0;
469 if (!success && !quiet)
470 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
471 method, fmt0, status);
473 return success;
476 static int acpi_ec_read(int i, u8 *p)
478 int v;
480 if (ecrd_handle) {
481 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
482 return 0;
483 *p = v;
484 } else {
485 if (ec_read(i, p) < 0)
486 return 0;
489 return 1;
492 static int acpi_ec_write(int i, u8 v)
494 if (ecwr_handle) {
495 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
496 return 0;
497 } else {
498 if (ec_write(i, v) < 0)
499 return 0;
502 return 1;
505 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
506 static int _sta(acpi_handle handle)
508 int status;
510 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
511 status = 0;
513 return status;
515 #endif
517 static int issue_thinkpad_cmos_command(int cmos_cmd)
519 if (!cmos_handle)
520 return -ENXIO;
522 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
523 return -EIO;
525 return 0;
528 /*************************************************************************
529 * ACPI device model
532 #define TPACPI_ACPIHANDLE_INIT(object) \
533 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
534 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
536 static void drv_acpi_handle_init(char *name,
537 acpi_handle *handle, acpi_handle parent,
538 char **paths, int num_paths, char **path)
540 int i;
541 acpi_status status;
543 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
544 name);
546 for (i = 0; i < num_paths; i++) {
547 status = acpi_get_handle(parent, paths[i], handle);
548 if (ACPI_SUCCESS(status)) {
549 *path = paths[i];
550 dbg_printk(TPACPI_DBG_INIT,
551 "Found ACPI handle %s for %s\n",
552 *path, name);
553 return;
557 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
558 name);
559 *handle = NULL;
562 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
564 struct ibm_struct *ibm = data;
566 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
567 return;
569 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
570 return;
572 ibm->acpi->notify(ibm, event);
575 static int __init setup_acpi_notify(struct ibm_struct *ibm)
577 acpi_status status;
578 int rc;
580 BUG_ON(!ibm->acpi);
582 if (!*ibm->acpi->handle)
583 return 0;
585 vdbg_printk(TPACPI_DBG_INIT,
586 "setting up ACPI notify for %s\n", ibm->name);
588 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
589 if (rc < 0) {
590 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
591 ibm->name, rc);
592 return -ENODEV;
595 ibm->acpi->device->driver_data = ibm;
596 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
597 TPACPI_ACPI_EVENT_PREFIX,
598 ibm->name);
600 status = acpi_install_notify_handler(*ibm->acpi->handle,
601 ibm->acpi->type, dispatch_acpi_notify, ibm);
602 if (ACPI_FAILURE(status)) {
603 if (status == AE_ALREADY_EXISTS) {
604 printk(TPACPI_NOTICE
605 "another device driver is already "
606 "handling %s events\n", ibm->name);
607 } else {
608 printk(TPACPI_ERR
609 "acpi_install_notify_handler(%s) failed: %d\n",
610 ibm->name, status);
612 return -ENODEV;
614 ibm->flags.acpi_notify_installed = 1;
615 return 0;
618 static int __init tpacpi_device_add(struct acpi_device *device)
620 return 0;
623 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
625 int rc;
627 dbg_printk(TPACPI_DBG_INIT,
628 "registering %s as an ACPI driver\n", ibm->name);
630 BUG_ON(!ibm->acpi);
632 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
633 if (!ibm->acpi->driver) {
634 printk(TPACPI_ERR
635 "failed to allocate memory for ibm->acpi->driver\n");
636 return -ENOMEM;
639 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
640 ibm->acpi->driver->ids = ibm->acpi->hid;
642 ibm->acpi->driver->ops.add = &tpacpi_device_add;
644 rc = acpi_bus_register_driver(ibm->acpi->driver);
645 if (rc < 0) {
646 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
647 ibm->name, rc);
648 kfree(ibm->acpi->driver);
649 ibm->acpi->driver = NULL;
650 } else if (!rc)
651 ibm->flags.acpi_driver_registered = 1;
653 return rc;
657 /****************************************************************************
658 ****************************************************************************
660 * Procfs Helpers
662 ****************************************************************************
663 ****************************************************************************/
665 static int dispatch_procfs_read(char *page, char **start, off_t off,
666 int count, int *eof, void *data)
668 struct ibm_struct *ibm = data;
669 int len;
671 if (!ibm || !ibm->read)
672 return -EINVAL;
674 len = ibm->read(page);
675 if (len < 0)
676 return len;
678 if (len <= off + count)
679 *eof = 1;
680 *start = page + off;
681 len -= off;
682 if (len > count)
683 len = count;
684 if (len < 0)
685 len = 0;
687 return len;
690 static int dispatch_procfs_write(struct file *file,
691 const char __user *userbuf,
692 unsigned long count, void *data)
694 struct ibm_struct *ibm = data;
695 char *kernbuf;
696 int ret;
698 if (!ibm || !ibm->write)
699 return -EINVAL;
701 kernbuf = kmalloc(count + 2, GFP_KERNEL);
702 if (!kernbuf)
703 return -ENOMEM;
705 if (copy_from_user(kernbuf, userbuf, count)) {
706 kfree(kernbuf);
707 return -EFAULT;
710 kernbuf[count] = 0;
711 strcat(kernbuf, ",");
712 ret = ibm->write(kernbuf);
713 if (ret == 0)
714 ret = count;
716 kfree(kernbuf);
718 return ret;
721 static char *next_cmd(char **cmds)
723 char *start = *cmds;
724 char *end;
726 while ((end = strchr(start, ',')) && end == start)
727 start = end + 1;
729 if (!end)
730 return NULL;
732 *end = 0;
733 *cmds = end + 1;
734 return start;
738 /****************************************************************************
739 ****************************************************************************
741 * Device model: input, hwmon and platform
743 ****************************************************************************
744 ****************************************************************************/
746 static struct platform_device *tpacpi_pdev;
747 static struct platform_device *tpacpi_sensors_pdev;
748 static struct device *tpacpi_hwmon;
749 static struct input_dev *tpacpi_inputdev;
750 static struct mutex tpacpi_inputdev_send_mutex;
751 static LIST_HEAD(tpacpi_all_drivers);
753 static int tpacpi_suspend_handler(struct platform_device *pdev,
754 pm_message_t state)
756 struct ibm_struct *ibm, *itmp;
758 list_for_each_entry_safe(ibm, itmp,
759 &tpacpi_all_drivers,
760 all_drivers) {
761 if (ibm->suspend)
762 (ibm->suspend)(state);
765 return 0;
768 static int tpacpi_resume_handler(struct platform_device *pdev)
770 struct ibm_struct *ibm, *itmp;
772 list_for_each_entry_safe(ibm, itmp,
773 &tpacpi_all_drivers,
774 all_drivers) {
775 if (ibm->resume)
776 (ibm->resume)();
779 return 0;
782 static void tpacpi_shutdown_handler(struct platform_device *pdev)
784 struct ibm_struct *ibm, *itmp;
786 list_for_each_entry_safe(ibm, itmp,
787 &tpacpi_all_drivers,
788 all_drivers) {
789 if (ibm->shutdown)
790 (ibm->shutdown)();
794 static struct platform_driver tpacpi_pdriver = {
795 .driver = {
796 .name = TPACPI_DRVR_NAME,
797 .owner = THIS_MODULE,
799 .suspend = tpacpi_suspend_handler,
800 .resume = tpacpi_resume_handler,
801 .shutdown = tpacpi_shutdown_handler,
804 static struct platform_driver tpacpi_hwmon_pdriver = {
805 .driver = {
806 .name = TPACPI_HWMON_DRVR_NAME,
807 .owner = THIS_MODULE,
811 /*************************************************************************
812 * sysfs support helpers
815 struct attribute_set {
816 unsigned int members, max_members;
817 struct attribute_group group;
820 struct attribute_set_obj {
821 struct attribute_set s;
822 struct attribute *a;
823 } __attribute__((packed));
825 static struct attribute_set *create_attr_set(unsigned int max_members,
826 const char *name)
828 struct attribute_set_obj *sobj;
830 if (max_members == 0)
831 return NULL;
833 /* Allocates space for implicit NULL at the end too */
834 sobj = kzalloc(sizeof(struct attribute_set_obj) +
835 max_members * sizeof(struct attribute *),
836 GFP_KERNEL);
837 if (!sobj)
838 return NULL;
839 sobj->s.max_members = max_members;
840 sobj->s.group.attrs = &sobj->a;
841 sobj->s.group.name = name;
843 return &sobj->s;
846 #define destroy_attr_set(_set) \
847 kfree(_set);
849 /* not multi-threaded safe, use it in a single thread per set */
850 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
852 if (!s || !attr)
853 return -EINVAL;
855 if (s->members >= s->max_members)
856 return -ENOMEM;
858 s->group.attrs[s->members] = attr;
859 s->members++;
861 return 0;
864 static int add_many_to_attr_set(struct attribute_set *s,
865 struct attribute **attr,
866 unsigned int count)
868 int i, res;
870 for (i = 0; i < count; i++) {
871 res = add_to_attr_set(s, attr[i]);
872 if (res)
873 return res;
876 return 0;
879 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
881 sysfs_remove_group(kobj, &s->group);
882 destroy_attr_set(s);
885 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
886 sysfs_create_group(_kobj, &_attr_set->group)
888 static int parse_strtoul(const char *buf,
889 unsigned long max, unsigned long *value)
891 char *endp;
893 while (*buf && isspace(*buf))
894 buf++;
895 *value = simple_strtoul(buf, &endp, 0);
896 while (*endp && isspace(*endp))
897 endp++;
898 if (*endp || *value > max)
899 return -EINVAL;
901 return 0;
904 static void tpacpi_disable_brightness_delay(void)
906 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
907 printk(TPACPI_NOTICE
908 "ACPI backlight control delay disabled\n");
911 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
913 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
914 union acpi_object *obj;
915 int rc;
917 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
918 obj = (union acpi_object *)buffer.pointer;
919 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
920 printk(TPACPI_ERR "Unknown _BCL data, "
921 "please report this to %s\n", TPACPI_MAIL);
922 rc = 0;
923 } else {
924 rc = obj->package.count;
926 } else {
927 return 0;
930 kfree(buffer.pointer);
931 return rc;
934 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
935 u32 lvl, void *context, void **rv)
937 char name[ACPI_PATH_SEGMENT_LENGTH];
938 struct acpi_buffer buffer = { sizeof(name), &name };
940 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
941 !strncmp("_BCL", name, sizeof(name) - 1)) {
942 BUG_ON(!rv || !*rv);
943 **(int **)rv = tpacpi_query_bcl_levels(handle);
944 return AE_CTRL_TERMINATE;
945 } else {
946 return AE_OK;
951 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
953 static int __init tpacpi_check_std_acpi_brightness_support(void)
955 int status;
956 int bcl_levels = 0;
957 void *bcl_ptr = &bcl_levels;
959 if (!vid_handle) {
960 TPACPI_ACPIHANDLE_INIT(vid);
962 if (!vid_handle)
963 return 0;
966 * Search for a _BCL method, and execute it. This is safe on all
967 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
968 * BIOS in ACPI backlight control mode. We do NOT have to care
969 * about calling the _BCL method in an enabled video device, any
970 * will do for our purposes.
973 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
974 tpacpi_acpi_walk_find_bcl, NULL,
975 &bcl_ptr);
977 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
978 tp_features.bright_acpimode = 1;
979 return (bcl_levels - 2);
982 return 0;
985 static int __init tpacpi_new_rfkill(const unsigned int id,
986 struct rfkill **rfk,
987 const enum rfkill_type rfktype,
988 const char *name,
989 const bool set_default,
990 int (*toggle_radio)(void *, enum rfkill_state),
991 int (*get_state)(void *, enum rfkill_state *))
993 int res;
994 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
996 res = get_state(NULL, &initial_state);
997 if (res < 0) {
998 printk(TPACPI_ERR
999 "failed to read initial state for %s, error %d; "
1000 "will turn radio off\n", name, res);
1001 } else if (set_default) {
1002 /* try to set the initial state as the default for the rfkill
1003 * type, since we ask the firmware to preserve it across S5 in
1004 * NVRAM */
1005 rfkill_set_default(rfktype,
1006 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1007 RFKILL_STATE_UNBLOCKED :
1008 RFKILL_STATE_SOFT_BLOCKED);
1011 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1012 if (!*rfk) {
1013 printk(TPACPI_ERR
1014 "failed to allocate memory for rfkill class\n");
1015 return -ENOMEM;
1018 (*rfk)->name = name;
1019 (*rfk)->get_state = get_state;
1020 (*rfk)->toggle_radio = toggle_radio;
1021 (*rfk)->state = initial_state;
1023 res = rfkill_register(*rfk);
1024 if (res < 0) {
1025 printk(TPACPI_ERR
1026 "failed to register %s rfkill switch: %d\n",
1027 name, res);
1028 rfkill_free(*rfk);
1029 *rfk = NULL;
1030 return res;
1033 return 0;
1036 /*************************************************************************
1037 * thinkpad-acpi driver attributes
1040 /* interface_version --------------------------------------------------- */
1041 static ssize_t tpacpi_driver_interface_version_show(
1042 struct device_driver *drv,
1043 char *buf)
1045 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1048 static DRIVER_ATTR(interface_version, S_IRUGO,
1049 tpacpi_driver_interface_version_show, NULL);
1051 /* debug_level --------------------------------------------------------- */
1052 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1053 char *buf)
1055 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1058 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1059 const char *buf, size_t count)
1061 unsigned long t;
1063 if (parse_strtoul(buf, 0xffff, &t))
1064 return -EINVAL;
1066 dbg_level = t;
1068 return count;
1071 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1072 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1074 /* version ------------------------------------------------------------- */
1075 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1076 char *buf)
1078 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1079 TPACPI_DESC, TPACPI_VERSION);
1082 static DRIVER_ATTR(version, S_IRUGO,
1083 tpacpi_driver_version_show, NULL);
1085 /* --------------------------------------------------------------------- */
1087 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1089 static void tpacpi_send_radiosw_update(void);
1091 /* wlsw_emulstate ------------------------------------------------------ */
1092 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1093 char *buf)
1095 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1098 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1099 const char *buf, size_t count)
1101 unsigned long t;
1103 if (parse_strtoul(buf, 1, &t))
1104 return -EINVAL;
1106 if (tpacpi_wlsw_emulstate != t) {
1107 tpacpi_wlsw_emulstate = !!t;
1108 tpacpi_send_radiosw_update();
1109 } else
1110 tpacpi_wlsw_emulstate = !!t;
1112 return count;
1115 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1116 tpacpi_driver_wlsw_emulstate_show,
1117 tpacpi_driver_wlsw_emulstate_store);
1119 /* bluetooth_emulstate ------------------------------------------------- */
1120 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1121 struct device_driver *drv,
1122 char *buf)
1124 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1127 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1128 struct device_driver *drv,
1129 const char *buf, size_t count)
1131 unsigned long t;
1133 if (parse_strtoul(buf, 1, &t))
1134 return -EINVAL;
1136 tpacpi_bluetooth_emulstate = !!t;
1138 return count;
1141 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1142 tpacpi_driver_bluetooth_emulstate_show,
1143 tpacpi_driver_bluetooth_emulstate_store);
1145 /* wwan_emulstate ------------------------------------------------- */
1146 static ssize_t tpacpi_driver_wwan_emulstate_show(
1147 struct device_driver *drv,
1148 char *buf)
1150 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1153 static ssize_t tpacpi_driver_wwan_emulstate_store(
1154 struct device_driver *drv,
1155 const char *buf, size_t count)
1157 unsigned long t;
1159 if (parse_strtoul(buf, 1, &t))
1160 return -EINVAL;
1162 tpacpi_wwan_emulstate = !!t;
1164 return count;
1167 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1168 tpacpi_driver_wwan_emulstate_show,
1169 tpacpi_driver_wwan_emulstate_store);
1171 /* uwb_emulstate ------------------------------------------------- */
1172 static ssize_t tpacpi_driver_uwb_emulstate_show(
1173 struct device_driver *drv,
1174 char *buf)
1176 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1179 static ssize_t tpacpi_driver_uwb_emulstate_store(
1180 struct device_driver *drv,
1181 const char *buf, size_t count)
1183 unsigned long t;
1185 if (parse_strtoul(buf, 1, &t))
1186 return -EINVAL;
1188 tpacpi_uwb_emulstate = !!t;
1190 return count;
1193 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1194 tpacpi_driver_uwb_emulstate_show,
1195 tpacpi_driver_uwb_emulstate_store);
1196 #endif
1198 /* --------------------------------------------------------------------- */
1200 static struct driver_attribute *tpacpi_driver_attributes[] = {
1201 &driver_attr_debug_level, &driver_attr_version,
1202 &driver_attr_interface_version,
1205 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1207 int i, res;
1209 i = 0;
1210 res = 0;
1211 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1212 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1213 i++;
1216 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1217 if (!res && dbg_wlswemul)
1218 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1219 if (!res && dbg_bluetoothemul)
1220 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1221 if (!res && dbg_wwanemul)
1222 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1223 if (!res && dbg_uwbemul)
1224 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1225 #endif
1227 return res;
1230 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1232 int i;
1234 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1235 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1237 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1238 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1239 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1240 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1241 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1242 #endif
1245 /****************************************************************************
1246 ****************************************************************************
1248 * Subdrivers
1250 ****************************************************************************
1251 ****************************************************************************/
1253 /*************************************************************************
1254 * thinkpad-acpi init subdriver
1257 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1259 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1260 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1262 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1263 (thinkpad_id.bios_version_str) ?
1264 thinkpad_id.bios_version_str : "unknown",
1265 (thinkpad_id.ec_version_str) ?
1266 thinkpad_id.ec_version_str : "unknown");
1268 if (thinkpad_id.vendor && thinkpad_id.model_str)
1269 printk(TPACPI_INFO "%s %s, model %s\n",
1270 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1271 "IBM" : ((thinkpad_id.vendor ==
1272 PCI_VENDOR_ID_LENOVO) ?
1273 "Lenovo" : "Unknown vendor"),
1274 thinkpad_id.model_str,
1275 (thinkpad_id.nummodel_str) ?
1276 thinkpad_id.nummodel_str : "unknown");
1278 return 0;
1281 static int thinkpad_acpi_driver_read(char *p)
1283 int len = 0;
1285 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1286 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1288 return len;
1291 static struct ibm_struct thinkpad_acpi_driver_data = {
1292 .name = "driver",
1293 .read = thinkpad_acpi_driver_read,
1296 /*************************************************************************
1297 * Hotkey subdriver
1300 enum { /* hot key scan codes (derived from ACPI DSDT) */
1301 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1302 TP_ACPI_HOTKEYSCAN_FNF2,
1303 TP_ACPI_HOTKEYSCAN_FNF3,
1304 TP_ACPI_HOTKEYSCAN_FNF4,
1305 TP_ACPI_HOTKEYSCAN_FNF5,
1306 TP_ACPI_HOTKEYSCAN_FNF6,
1307 TP_ACPI_HOTKEYSCAN_FNF7,
1308 TP_ACPI_HOTKEYSCAN_FNF8,
1309 TP_ACPI_HOTKEYSCAN_FNF9,
1310 TP_ACPI_HOTKEYSCAN_FNF10,
1311 TP_ACPI_HOTKEYSCAN_FNF11,
1312 TP_ACPI_HOTKEYSCAN_FNF12,
1313 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1314 TP_ACPI_HOTKEYSCAN_FNINSERT,
1315 TP_ACPI_HOTKEYSCAN_FNDELETE,
1316 TP_ACPI_HOTKEYSCAN_FNHOME,
1317 TP_ACPI_HOTKEYSCAN_FNEND,
1318 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1319 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1320 TP_ACPI_HOTKEYSCAN_FNSPACE,
1321 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1322 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1323 TP_ACPI_HOTKEYSCAN_MUTE,
1324 TP_ACPI_HOTKEYSCAN_THINKPAD,
1327 enum { /* Keys available through NVRAM polling */
1328 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1329 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1332 enum { /* Positions of some of the keys in hotkey masks */
1333 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1334 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1335 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1336 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1337 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1338 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1339 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1340 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1341 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1342 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1343 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1346 enum { /* NVRAM to ACPI HKEY group map */
1347 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1348 TP_ACPI_HKEY_ZOOM_MASK |
1349 TP_ACPI_HKEY_DISPSWTCH_MASK |
1350 TP_ACPI_HKEY_HIBERNATE_MASK,
1351 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1352 TP_ACPI_HKEY_BRGHTDWN_MASK,
1353 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1354 TP_ACPI_HKEY_VOLDWN_MASK |
1355 TP_ACPI_HKEY_MUTE_MASK,
1358 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1359 struct tp_nvram_state {
1360 u16 thinkpad_toggle:1;
1361 u16 zoom_toggle:1;
1362 u16 display_toggle:1;
1363 u16 thinklight_toggle:1;
1364 u16 hibernate_toggle:1;
1365 u16 displayexp_toggle:1;
1366 u16 display_state:1;
1367 u16 brightness_toggle:1;
1368 u16 volume_toggle:1;
1369 u16 mute:1;
1371 u8 brightness_level;
1372 u8 volume_level;
1375 static struct task_struct *tpacpi_hotkey_task;
1376 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1377 static int hotkey_poll_freq = 10; /* Hz */
1378 static struct mutex hotkey_thread_mutex;
1379 static struct mutex hotkey_thread_data_mutex;
1380 static unsigned int hotkey_config_change;
1382 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1384 #define hotkey_source_mask 0U
1386 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1388 static struct mutex hotkey_mutex;
1390 static enum { /* Reasons for waking up */
1391 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1392 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1393 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1394 } hotkey_wakeup_reason;
1396 static int hotkey_autosleep_ack;
1398 static int hotkey_orig_status;
1399 static u32 hotkey_orig_mask;
1400 static u32 hotkey_all_mask;
1401 static u32 hotkey_reserved_mask;
1402 static u32 hotkey_mask;
1404 static unsigned int hotkey_report_mode;
1406 static u16 *hotkey_keycode_map;
1408 static struct attribute_set *hotkey_dev_attributes;
1410 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1411 #define HOTKEY_CONFIG_CRITICAL_START \
1412 do { \
1413 mutex_lock(&hotkey_thread_data_mutex); \
1414 hotkey_config_change++; \
1415 } while (0);
1416 #define HOTKEY_CONFIG_CRITICAL_END \
1417 mutex_unlock(&hotkey_thread_data_mutex);
1418 #else
1419 #define HOTKEY_CONFIG_CRITICAL_START
1420 #define HOTKEY_CONFIG_CRITICAL_END
1421 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1423 /* HKEY.MHKG() return bits */
1424 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1426 static int hotkey_get_wlsw(int *status)
1428 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1429 if (dbg_wlswemul) {
1430 *status = !!tpacpi_wlsw_emulstate;
1431 return 0;
1433 #endif
1434 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1435 return -EIO;
1436 return 0;
1439 static int hotkey_get_tablet_mode(int *status)
1441 int s;
1443 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1444 return -EIO;
1446 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1447 return 0;
1451 * Call with hotkey_mutex held
1453 static int hotkey_mask_get(void)
1455 u32 m = 0;
1457 if (tp_features.hotkey_mask) {
1458 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1459 return -EIO;
1461 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1463 return 0;
1467 * Call with hotkey_mutex held
1469 static int hotkey_mask_set(u32 mask)
1471 int i;
1472 int rc = 0;
1474 if (tp_features.hotkey_mask) {
1475 if (!tp_warned.hotkey_mask_ff &&
1476 (mask == 0xffff || mask == 0xffffff ||
1477 mask == 0xffffffff)) {
1478 tp_warned.hotkey_mask_ff = 1;
1479 printk(TPACPI_NOTICE
1480 "setting the hotkey mask to 0x%08x is likely "
1481 "not the best way to go about it\n", mask);
1482 printk(TPACPI_NOTICE
1483 "please consider using the driver defaults, "
1484 "and refer to up-to-date thinkpad-acpi "
1485 "documentation\n");
1488 HOTKEY_CONFIG_CRITICAL_START
1489 for (i = 0; i < 32; i++) {
1490 u32 m = 1 << i;
1491 /* enable in firmware mask only keys not in NVRAM
1492 * mode, but enable the key in the cached hotkey_mask
1493 * regardless of mode, or the key will end up
1494 * disabled by hotkey_mask_get() */
1495 if (!acpi_evalf(hkey_handle,
1496 NULL, "MHKM", "vdd", i + 1,
1497 !!((mask & ~hotkey_source_mask) & m))) {
1498 rc = -EIO;
1499 break;
1500 } else {
1501 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1504 HOTKEY_CONFIG_CRITICAL_END
1506 /* hotkey_mask_get must be called unconditionally below */
1507 if (!hotkey_mask_get() && !rc &&
1508 (hotkey_mask & ~hotkey_source_mask) !=
1509 (mask & ~hotkey_source_mask)) {
1510 printk(TPACPI_NOTICE
1511 "requested hot key mask 0x%08x, but "
1512 "firmware forced it to 0x%08x\n",
1513 mask, hotkey_mask);
1515 } else {
1516 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1517 HOTKEY_CONFIG_CRITICAL_START
1518 hotkey_mask = mask & hotkey_source_mask;
1519 HOTKEY_CONFIG_CRITICAL_END
1520 hotkey_mask_get();
1521 if (hotkey_mask != mask) {
1522 printk(TPACPI_NOTICE
1523 "requested hot key mask 0x%08x, "
1524 "forced to 0x%08x (NVRAM poll mask is "
1525 "0x%08x): no firmware mask support\n",
1526 mask, hotkey_mask, hotkey_source_mask);
1528 #else
1529 hotkey_mask_get();
1530 rc = -ENXIO;
1531 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1534 return rc;
1537 static int hotkey_status_get(int *status)
1539 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1540 return -EIO;
1542 return 0;
1545 static int hotkey_status_set(int status)
1547 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1548 return -EIO;
1550 return 0;
1553 static void tpacpi_input_send_tabletsw(void)
1555 int state;
1557 if (tp_features.hotkey_tablet &&
1558 !hotkey_get_tablet_mode(&state)) {
1559 mutex_lock(&tpacpi_inputdev_send_mutex);
1561 input_report_switch(tpacpi_inputdev,
1562 SW_TABLET_MODE, !!state);
1563 input_sync(tpacpi_inputdev);
1565 mutex_unlock(&tpacpi_inputdev_send_mutex);
1569 static void tpacpi_input_send_key(unsigned int scancode)
1571 unsigned int keycode;
1573 keycode = hotkey_keycode_map[scancode];
1575 if (keycode != KEY_RESERVED) {
1576 mutex_lock(&tpacpi_inputdev_send_mutex);
1578 input_report_key(tpacpi_inputdev, keycode, 1);
1579 if (keycode == KEY_UNKNOWN)
1580 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1581 scancode);
1582 input_sync(tpacpi_inputdev);
1584 input_report_key(tpacpi_inputdev, keycode, 0);
1585 if (keycode == KEY_UNKNOWN)
1586 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1587 scancode);
1588 input_sync(tpacpi_inputdev);
1590 mutex_unlock(&tpacpi_inputdev_send_mutex);
1594 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1595 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1597 static void tpacpi_hotkey_send_key(unsigned int scancode)
1599 tpacpi_input_send_key(scancode);
1600 if (hotkey_report_mode < 2) {
1601 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1602 0x80, 0x1001 + scancode);
1606 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1608 u8 d;
1610 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1611 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1612 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1613 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1614 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1615 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1617 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1618 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1619 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1621 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1622 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1623 n->displayexp_toggle =
1624 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1626 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1627 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1628 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1629 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1630 n->brightness_toggle =
1631 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1633 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1634 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1635 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1636 >> TP_NVRAM_POS_LEVEL_VOLUME;
1637 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1638 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1642 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1643 do { \
1644 if ((mask & (1 << __scancode)) && \
1645 oldn->__member != newn->__member) \
1646 tpacpi_hotkey_send_key(__scancode); \
1647 } while (0)
1649 #define TPACPI_MAY_SEND_KEY(__scancode) \
1650 do { if (mask & (1 << __scancode)) \
1651 tpacpi_hotkey_send_key(__scancode); } while (0)
1653 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1654 struct tp_nvram_state *newn,
1655 u32 mask)
1657 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1658 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1659 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1660 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1662 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1664 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1666 /* handle volume */
1667 if (oldn->volume_toggle != newn->volume_toggle) {
1668 if (oldn->mute != newn->mute) {
1669 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1671 if (oldn->volume_level > newn->volume_level) {
1672 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1673 } else if (oldn->volume_level < newn->volume_level) {
1674 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1675 } else if (oldn->mute == newn->mute) {
1676 /* repeated key presses that didn't change state */
1677 if (newn->mute) {
1678 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1679 } else if (newn->volume_level != 0) {
1680 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1681 } else {
1682 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1687 /* handle brightness */
1688 if (oldn->brightness_toggle != newn->brightness_toggle) {
1689 if (oldn->brightness_level < newn->brightness_level) {
1690 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1691 } else if (oldn->brightness_level > newn->brightness_level) {
1692 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1693 } else {
1694 /* repeated key presses that didn't change state */
1695 if (newn->brightness_level != 0) {
1696 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1697 } else {
1698 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1704 #undef TPACPI_COMPARE_KEY
1705 #undef TPACPI_MAY_SEND_KEY
1707 static int hotkey_kthread(void *data)
1709 struct tp_nvram_state s[2];
1710 u32 mask;
1711 unsigned int si, so;
1712 unsigned long t;
1713 unsigned int change_detector, must_reset;
1715 mutex_lock(&hotkey_thread_mutex);
1717 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1718 goto exit;
1720 set_freezable();
1722 so = 0;
1723 si = 1;
1724 t = 0;
1726 /* Initial state for compares */
1727 mutex_lock(&hotkey_thread_data_mutex);
1728 change_detector = hotkey_config_change;
1729 mask = hotkey_source_mask & hotkey_mask;
1730 mutex_unlock(&hotkey_thread_data_mutex);
1731 hotkey_read_nvram(&s[so], mask);
1733 while (!kthread_should_stop() && hotkey_poll_freq) {
1734 if (t == 0)
1735 t = 1000/hotkey_poll_freq;
1736 t = msleep_interruptible(t);
1737 if (unlikely(kthread_should_stop()))
1738 break;
1739 must_reset = try_to_freeze();
1740 if (t > 0 && !must_reset)
1741 continue;
1743 mutex_lock(&hotkey_thread_data_mutex);
1744 if (must_reset || hotkey_config_change != change_detector) {
1745 /* forget old state on thaw or config change */
1746 si = so;
1747 t = 0;
1748 change_detector = hotkey_config_change;
1750 mask = hotkey_source_mask & hotkey_mask;
1751 mutex_unlock(&hotkey_thread_data_mutex);
1753 if (likely(mask)) {
1754 hotkey_read_nvram(&s[si], mask);
1755 if (likely(si != so)) {
1756 hotkey_compare_and_issue_event(&s[so], &s[si],
1757 mask);
1761 so = si;
1762 si ^= 1;
1765 exit:
1766 mutex_unlock(&hotkey_thread_mutex);
1767 return 0;
1770 static void hotkey_poll_stop_sync(void)
1772 if (tpacpi_hotkey_task) {
1773 if (frozen(tpacpi_hotkey_task) ||
1774 freezing(tpacpi_hotkey_task))
1775 thaw_process(tpacpi_hotkey_task);
1777 kthread_stop(tpacpi_hotkey_task);
1778 tpacpi_hotkey_task = NULL;
1779 mutex_lock(&hotkey_thread_mutex);
1780 /* at this point, the thread did exit */
1781 mutex_unlock(&hotkey_thread_mutex);
1785 /* call with hotkey_mutex held */
1786 static void hotkey_poll_setup(int may_warn)
1788 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1789 hotkey_poll_freq > 0 &&
1790 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1791 if (!tpacpi_hotkey_task) {
1792 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1793 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1794 if (IS_ERR(tpacpi_hotkey_task)) {
1795 tpacpi_hotkey_task = NULL;
1796 printk(TPACPI_ERR
1797 "could not create kernel thread "
1798 "for hotkey polling\n");
1801 } else {
1802 hotkey_poll_stop_sync();
1803 if (may_warn &&
1804 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1805 printk(TPACPI_NOTICE
1806 "hot keys 0x%08x require polling, "
1807 "which is currently disabled\n",
1808 hotkey_source_mask);
1813 static void hotkey_poll_setup_safe(int may_warn)
1815 mutex_lock(&hotkey_mutex);
1816 hotkey_poll_setup(may_warn);
1817 mutex_unlock(&hotkey_mutex);
1820 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1822 static void hotkey_poll_setup_safe(int __unused)
1826 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1828 static int hotkey_inputdev_open(struct input_dev *dev)
1830 switch (tpacpi_lifecycle) {
1831 case TPACPI_LIFE_INIT:
1833 * hotkey_init will call hotkey_poll_setup_safe
1834 * at the appropriate moment
1836 return 0;
1837 case TPACPI_LIFE_EXITING:
1838 return -EBUSY;
1839 case TPACPI_LIFE_RUNNING:
1840 hotkey_poll_setup_safe(0);
1841 return 0;
1844 /* Should only happen if tpacpi_lifecycle is corrupt */
1845 BUG();
1846 return -EBUSY;
1849 static void hotkey_inputdev_close(struct input_dev *dev)
1851 /* disable hotkey polling when possible */
1852 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1853 hotkey_poll_setup_safe(0);
1856 /* sysfs hotkey enable ------------------------------------------------- */
1857 static ssize_t hotkey_enable_show(struct device *dev,
1858 struct device_attribute *attr,
1859 char *buf)
1861 int res, status;
1863 res = hotkey_status_get(&status);
1864 if (res)
1865 return res;
1867 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1870 static ssize_t hotkey_enable_store(struct device *dev,
1871 struct device_attribute *attr,
1872 const char *buf, size_t count)
1874 unsigned long t;
1875 int res;
1877 if (parse_strtoul(buf, 1, &t))
1878 return -EINVAL;
1880 res = hotkey_status_set(t);
1882 return (res) ? res : count;
1885 static struct device_attribute dev_attr_hotkey_enable =
1886 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1887 hotkey_enable_show, hotkey_enable_store);
1889 /* sysfs hotkey mask --------------------------------------------------- */
1890 static ssize_t hotkey_mask_show(struct device *dev,
1891 struct device_attribute *attr,
1892 char *buf)
1894 int res;
1896 if (mutex_lock_killable(&hotkey_mutex))
1897 return -ERESTARTSYS;
1898 res = hotkey_mask_get();
1899 mutex_unlock(&hotkey_mutex);
1901 return (res)?
1902 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1905 static ssize_t hotkey_mask_store(struct device *dev,
1906 struct device_attribute *attr,
1907 const char *buf, size_t count)
1909 unsigned long t;
1910 int res;
1912 if (parse_strtoul(buf, 0xffffffffUL, &t))
1913 return -EINVAL;
1915 if (mutex_lock_killable(&hotkey_mutex))
1916 return -ERESTARTSYS;
1918 res = hotkey_mask_set(t);
1920 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1921 hotkey_poll_setup(1);
1922 #endif
1924 mutex_unlock(&hotkey_mutex);
1926 return (res) ? res : count;
1929 static struct device_attribute dev_attr_hotkey_mask =
1930 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1931 hotkey_mask_show, hotkey_mask_store);
1933 /* sysfs hotkey bios_enabled ------------------------------------------- */
1934 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1935 struct device_attribute *attr,
1936 char *buf)
1938 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1941 static struct device_attribute dev_attr_hotkey_bios_enabled =
1942 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1944 /* sysfs hotkey bios_mask ---------------------------------------------- */
1945 static ssize_t hotkey_bios_mask_show(struct device *dev,
1946 struct device_attribute *attr,
1947 char *buf)
1949 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1952 static struct device_attribute dev_attr_hotkey_bios_mask =
1953 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1955 /* sysfs hotkey all_mask ----------------------------------------------- */
1956 static ssize_t hotkey_all_mask_show(struct device *dev,
1957 struct device_attribute *attr,
1958 char *buf)
1960 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1961 hotkey_all_mask | hotkey_source_mask);
1964 static struct device_attribute dev_attr_hotkey_all_mask =
1965 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1967 /* sysfs hotkey recommended_mask --------------------------------------- */
1968 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1969 struct device_attribute *attr,
1970 char *buf)
1972 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1973 (hotkey_all_mask | hotkey_source_mask)
1974 & ~hotkey_reserved_mask);
1977 static struct device_attribute dev_attr_hotkey_recommended_mask =
1978 __ATTR(hotkey_recommended_mask, S_IRUGO,
1979 hotkey_recommended_mask_show, NULL);
1981 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1983 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1984 static ssize_t hotkey_source_mask_show(struct device *dev,
1985 struct device_attribute *attr,
1986 char *buf)
1988 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1991 static ssize_t hotkey_source_mask_store(struct device *dev,
1992 struct device_attribute *attr,
1993 const char *buf, size_t count)
1995 unsigned long t;
1997 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1998 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1999 return -EINVAL;
2001 if (mutex_lock_killable(&hotkey_mutex))
2002 return -ERESTARTSYS;
2004 HOTKEY_CONFIG_CRITICAL_START
2005 hotkey_source_mask = t;
2006 HOTKEY_CONFIG_CRITICAL_END
2008 hotkey_poll_setup(1);
2010 mutex_unlock(&hotkey_mutex);
2012 return count;
2015 static struct device_attribute dev_attr_hotkey_source_mask =
2016 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2017 hotkey_source_mask_show, hotkey_source_mask_store);
2019 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2020 static ssize_t hotkey_poll_freq_show(struct device *dev,
2021 struct device_attribute *attr,
2022 char *buf)
2024 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2027 static ssize_t hotkey_poll_freq_store(struct device *dev,
2028 struct device_attribute *attr,
2029 const char *buf, size_t count)
2031 unsigned long t;
2033 if (parse_strtoul(buf, 25, &t))
2034 return -EINVAL;
2036 if (mutex_lock_killable(&hotkey_mutex))
2037 return -ERESTARTSYS;
2039 hotkey_poll_freq = t;
2041 hotkey_poll_setup(1);
2042 mutex_unlock(&hotkey_mutex);
2044 return count;
2047 static struct device_attribute dev_attr_hotkey_poll_freq =
2048 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2049 hotkey_poll_freq_show, hotkey_poll_freq_store);
2051 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2053 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2054 static ssize_t hotkey_radio_sw_show(struct device *dev,
2055 struct device_attribute *attr,
2056 char *buf)
2058 int res, s;
2059 res = hotkey_get_wlsw(&s);
2060 if (res < 0)
2061 return res;
2063 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2066 static struct device_attribute dev_attr_hotkey_radio_sw =
2067 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2069 static void hotkey_radio_sw_notify_change(void)
2071 if (tp_features.hotkey_wlsw)
2072 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2073 "hotkey_radio_sw");
2076 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2077 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2078 struct device_attribute *attr,
2079 char *buf)
2081 int res, s;
2082 res = hotkey_get_tablet_mode(&s);
2083 if (res < 0)
2084 return res;
2086 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2089 static struct device_attribute dev_attr_hotkey_tablet_mode =
2090 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2092 static void hotkey_tablet_mode_notify_change(void)
2094 if (tp_features.hotkey_tablet)
2095 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2096 "hotkey_tablet_mode");
2099 /* sysfs hotkey report_mode -------------------------------------------- */
2100 static ssize_t hotkey_report_mode_show(struct device *dev,
2101 struct device_attribute *attr,
2102 char *buf)
2104 return snprintf(buf, PAGE_SIZE, "%d\n",
2105 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2108 static struct device_attribute dev_attr_hotkey_report_mode =
2109 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2111 /* sysfs wakeup reason (pollable) -------------------------------------- */
2112 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2113 struct device_attribute *attr,
2114 char *buf)
2116 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2119 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2120 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2122 static void hotkey_wakeup_reason_notify_change(void)
2124 if (tp_features.hotkey_mask)
2125 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2126 "wakeup_reason");
2129 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2130 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2131 struct device_attribute *attr,
2132 char *buf)
2134 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2137 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2138 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2139 hotkey_wakeup_hotunplug_complete_show, NULL);
2141 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2143 if (tp_features.hotkey_mask)
2144 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2145 "wakeup_hotunplug_complete");
2148 /* --------------------------------------------------------------------- */
2150 static struct attribute *hotkey_attributes[] __initdata = {
2151 &dev_attr_hotkey_enable.attr,
2152 &dev_attr_hotkey_bios_enabled.attr,
2153 &dev_attr_hotkey_report_mode.attr,
2154 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2155 &dev_attr_hotkey_mask.attr,
2156 &dev_attr_hotkey_all_mask.attr,
2157 &dev_attr_hotkey_recommended_mask.attr,
2158 &dev_attr_hotkey_source_mask.attr,
2159 &dev_attr_hotkey_poll_freq.attr,
2160 #endif
2163 static struct attribute *hotkey_mask_attributes[] __initdata = {
2164 &dev_attr_hotkey_bios_mask.attr,
2165 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2166 &dev_attr_hotkey_mask.attr,
2167 &dev_attr_hotkey_all_mask.attr,
2168 &dev_attr_hotkey_recommended_mask.attr,
2169 #endif
2170 &dev_attr_hotkey_wakeup_reason.attr,
2171 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2174 static void bluetooth_update_rfk(void);
2175 static void wan_update_rfk(void);
2176 static void uwb_update_rfk(void);
2177 static void tpacpi_send_radiosw_update(void)
2179 int wlsw;
2181 /* Sync these BEFORE sending any rfkill events */
2182 if (tp_features.bluetooth)
2183 bluetooth_update_rfk();
2184 if (tp_features.wan)
2185 wan_update_rfk();
2186 if (tp_features.uwb)
2187 uwb_update_rfk();
2189 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2190 mutex_lock(&tpacpi_inputdev_send_mutex);
2192 input_report_switch(tpacpi_inputdev,
2193 SW_RFKILL_ALL, !!wlsw);
2194 input_sync(tpacpi_inputdev);
2196 mutex_unlock(&tpacpi_inputdev_send_mutex);
2198 hotkey_radio_sw_notify_change();
2201 static void hotkey_exit(void)
2203 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2204 hotkey_poll_stop_sync();
2205 #endif
2207 if (hotkey_dev_attributes)
2208 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2210 kfree(hotkey_keycode_map);
2212 if (tp_features.hotkey) {
2213 dbg_printk(TPACPI_DBG_EXIT,
2214 "restoring original hot key mask\n");
2215 /* no short-circuit boolean operator below! */
2216 if ((hotkey_mask_set(hotkey_orig_mask) |
2217 hotkey_status_set(hotkey_orig_status)) != 0)
2218 printk(TPACPI_ERR
2219 "failed to restore hot key mask "
2220 "to BIOS defaults\n");
2224 static int __init hotkey_init(struct ibm_init_struct *iibm)
2226 /* Requirements for changing the default keymaps:
2228 * 1. Many of the keys are mapped to KEY_RESERVED for very
2229 * good reasons. Do not change them unless you have deep
2230 * knowledge on the IBM and Lenovo ThinkPad firmware for
2231 * the various ThinkPad models. The driver behaves
2232 * differently for KEY_RESERVED: such keys have their
2233 * hot key mask *unset* in mask_recommended, and also
2234 * in the initial hot key mask programmed into the
2235 * firmware at driver load time, which means the firm-
2236 * ware may react very differently if you change them to
2237 * something else;
2239 * 2. You must be subscribed to the linux-thinkpad and
2240 * ibm-acpi-devel mailing lists, and you should read the
2241 * list archives since 2007 if you want to change the
2242 * keymaps. This requirement exists so that you will
2243 * know the past history of problems with the thinkpad-
2244 * acpi driver keymaps, and also that you will be
2245 * listening to any bug reports;
2247 * 3. Do not send thinkpad-acpi specific patches directly to
2248 * for merging, *ever*. Send them to the linux-acpi
2249 * mailinglist for comments. Merging is to be done only
2250 * through acpi-test and the ACPI maintainer.
2252 * If the above is too much to ask, don't change the keymap.
2253 * Ask the thinkpad-acpi maintainer to do it, instead.
2255 static u16 ibm_keycode_map[] __initdata = {
2256 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2257 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2258 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2259 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2261 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2262 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2263 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2264 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2266 /* brightness: firmware always reacts to them, unless
2267 * X.org did some tricks in the radeon BIOS scratch
2268 * registers of *some* models */
2269 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2270 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2272 /* Thinklight: firmware always react to it */
2273 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2275 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2276 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2278 /* Volume: firmware always react to it and reprograms
2279 * the built-in *extra* mixer. Never map it to control
2280 * another mixer by default. */
2281 KEY_RESERVED, /* 0x14: VOLUME UP */
2282 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2283 KEY_RESERVED, /* 0x16: MUTE */
2285 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2287 /* (assignments unknown, please report if found) */
2288 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2289 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2291 static u16 lenovo_keycode_map[] __initdata = {
2292 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2293 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2294 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2295 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2297 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2298 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2299 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2300 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2302 /* These either have to go through ACPI video, or
2303 * act like in the IBM ThinkPads, so don't ever
2304 * enable them by default */
2305 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2306 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2308 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2310 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2311 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2313 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2314 * react to it and reprograms the built-in *extra* mixer.
2315 * Never map it to control another mixer by default.
2317 * T60?, T61, R60?, R61: firmware and EC tries to send
2318 * these over the regular keyboard, so these are no-ops,
2319 * but there are still weird bugs re. MUTE, so do not
2320 * change unless you get test reports from all Lenovo
2321 * models. May cause the BIOS to interfere with the
2322 * HDA mixer.
2324 KEY_RESERVED, /* 0x14: VOLUME UP */
2325 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2326 KEY_RESERVED, /* 0x16: MUTE */
2328 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2330 /* (assignments unknown, please report if found) */
2331 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2332 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2335 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2336 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2337 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2339 int res, i;
2340 int status;
2341 int hkeyv;
2343 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2345 BUG_ON(!tpacpi_inputdev);
2346 BUG_ON(tpacpi_inputdev->open != NULL ||
2347 tpacpi_inputdev->close != NULL);
2349 TPACPI_ACPIHANDLE_INIT(hkey);
2350 mutex_init(&hotkey_mutex);
2352 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2353 mutex_init(&hotkey_thread_mutex);
2354 mutex_init(&hotkey_thread_data_mutex);
2355 #endif
2357 /* hotkey not supported on 570 */
2358 tp_features.hotkey = hkey_handle != NULL;
2360 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2361 str_supported(tp_features.hotkey));
2363 if (!tp_features.hotkey)
2364 return 1;
2366 tpacpi_disable_brightness_delay();
2368 hotkey_dev_attributes = create_attr_set(13, NULL);
2369 if (!hotkey_dev_attributes)
2370 return -ENOMEM;
2371 res = add_many_to_attr_set(hotkey_dev_attributes,
2372 hotkey_attributes,
2373 ARRAY_SIZE(hotkey_attributes));
2374 if (res)
2375 goto err_exit;
2377 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2378 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2379 for HKEY interface version 0x100 */
2380 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2381 if ((hkeyv >> 8) != 1) {
2382 printk(TPACPI_ERR "unknown version of the "
2383 "HKEY interface: 0x%x\n", hkeyv);
2384 printk(TPACPI_ERR "please report this to %s\n",
2385 TPACPI_MAIL);
2386 } else {
2388 * MHKV 0x100 in A31, R40, R40e,
2389 * T4x, X31, and later
2391 tp_features.hotkey_mask = 1;
2395 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2396 str_supported(tp_features.hotkey_mask));
2398 if (tp_features.hotkey_mask) {
2399 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2400 "MHKA", "qd")) {
2401 printk(TPACPI_ERR
2402 "missing MHKA handler, "
2403 "please report this to %s\n",
2404 TPACPI_MAIL);
2405 /* FN+F12, FN+F4, FN+F3 */
2406 hotkey_all_mask = 0x080cU;
2410 /* hotkey_source_mask *must* be zero for
2411 * the first hotkey_mask_get */
2412 res = hotkey_status_get(&hotkey_orig_status);
2413 if (res)
2414 goto err_exit;
2416 if (tp_features.hotkey_mask) {
2417 res = hotkey_mask_get();
2418 if (res)
2419 goto err_exit;
2421 hotkey_orig_mask = hotkey_mask;
2422 res = add_many_to_attr_set(
2423 hotkey_dev_attributes,
2424 hotkey_mask_attributes,
2425 ARRAY_SIZE(hotkey_mask_attributes));
2426 if (res)
2427 goto err_exit;
2430 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2431 if (tp_features.hotkey_mask) {
2432 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2433 & ~hotkey_all_mask;
2434 } else {
2435 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2438 vdbg_printk(TPACPI_DBG_INIT,
2439 "hotkey source mask 0x%08x, polling freq %d\n",
2440 hotkey_source_mask, hotkey_poll_freq);
2441 #endif
2443 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2444 if (dbg_wlswemul) {
2445 tp_features.hotkey_wlsw = 1;
2446 printk(TPACPI_INFO
2447 "radio switch emulation enabled\n");
2448 } else
2449 #endif
2450 /* Not all thinkpads have a hardware radio switch */
2451 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2452 tp_features.hotkey_wlsw = 1;
2453 printk(TPACPI_INFO
2454 "radio switch found; radios are %s\n",
2455 enabled(status, 0));
2457 if (tp_features.hotkey_wlsw)
2458 res = add_to_attr_set(hotkey_dev_attributes,
2459 &dev_attr_hotkey_radio_sw.attr);
2461 /* For X41t, X60t, X61t Tablets... */
2462 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2463 tp_features.hotkey_tablet = 1;
2464 printk(TPACPI_INFO
2465 "possible tablet mode switch found; "
2466 "ThinkPad in %s mode\n",
2467 (status & TP_HOTKEY_TABLET_MASK)?
2468 "tablet" : "laptop");
2469 res = add_to_attr_set(hotkey_dev_attributes,
2470 &dev_attr_hotkey_tablet_mode.attr);
2473 if (!res)
2474 res = register_attr_set_with_sysfs(
2475 hotkey_dev_attributes,
2476 &tpacpi_pdev->dev.kobj);
2477 if (res)
2478 goto err_exit;
2480 /* Set up key map */
2482 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2483 GFP_KERNEL);
2484 if (!hotkey_keycode_map) {
2485 printk(TPACPI_ERR
2486 "failed to allocate memory for key map\n");
2487 res = -ENOMEM;
2488 goto err_exit;
2491 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2492 dbg_printk(TPACPI_DBG_INIT,
2493 "using Lenovo default hot key map\n");
2494 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2495 TPACPI_HOTKEY_MAP_SIZE);
2496 } else {
2497 dbg_printk(TPACPI_DBG_INIT,
2498 "using IBM default hot key map\n");
2499 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2500 TPACPI_HOTKEY_MAP_SIZE);
2503 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2504 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2505 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2506 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2507 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2508 tpacpi_inputdev->keycode = hotkey_keycode_map;
2509 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2510 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2511 set_bit(hotkey_keycode_map[i],
2512 tpacpi_inputdev->keybit);
2513 } else {
2514 if (i < sizeof(hotkey_reserved_mask)*8)
2515 hotkey_reserved_mask |= 1 << i;
2519 if (tp_features.hotkey_wlsw) {
2520 set_bit(EV_SW, tpacpi_inputdev->evbit);
2521 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2523 if (tp_features.hotkey_tablet) {
2524 set_bit(EV_SW, tpacpi_inputdev->evbit);
2525 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2528 /* Do not issue duplicate brightness change events to
2529 * userspace */
2530 if (!tp_features.bright_acpimode)
2531 /* update bright_acpimode... */
2532 tpacpi_check_std_acpi_brightness_support();
2534 if (tp_features.bright_acpimode) {
2535 printk(TPACPI_INFO
2536 "This ThinkPad has standard ACPI backlight "
2537 "brightness control, supported by the ACPI "
2538 "video driver\n");
2539 printk(TPACPI_NOTICE
2540 "Disabling thinkpad-acpi brightness events "
2541 "by default...\n");
2543 /* The hotkey_reserved_mask change below is not
2544 * necessary while the keys are at KEY_RESERVED in the
2545 * default map, but better safe than sorry, leave it
2546 * here as a marker of what we have to do, especially
2547 * when we finally become able to set this at runtime
2548 * on response to X.org requests */
2549 hotkey_reserved_mask |=
2550 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2551 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2554 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2555 res = hotkey_status_set(1);
2556 if (res) {
2557 hotkey_exit();
2558 return res;
2560 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2561 & ~hotkey_reserved_mask)
2562 | hotkey_orig_mask);
2563 if (res < 0 && res != -ENXIO) {
2564 hotkey_exit();
2565 return res;
2568 dbg_printk(TPACPI_DBG_INIT,
2569 "legacy hot key reporting over procfs %s\n",
2570 (hotkey_report_mode < 2) ?
2571 "enabled" : "disabled");
2573 tpacpi_inputdev->open = &hotkey_inputdev_open;
2574 tpacpi_inputdev->close = &hotkey_inputdev_close;
2576 hotkey_poll_setup_safe(1);
2577 tpacpi_send_radiosw_update();
2578 tpacpi_input_send_tabletsw();
2580 return 0;
2582 err_exit:
2583 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2584 hotkey_dev_attributes = NULL;
2586 return (res < 0)? res : 1;
2589 static bool hotkey_notify_hotkey(const u32 hkey,
2590 bool *send_acpi_ev,
2591 bool *ignore_acpi_ev)
2593 /* 0x1000-0x1FFF: key presses */
2594 unsigned int scancode = hkey & 0xfff;
2595 *send_acpi_ev = true;
2596 *ignore_acpi_ev = false;
2598 if (scancode > 0 && scancode < 0x21) {
2599 scancode--;
2600 if (!(hotkey_source_mask & (1 << scancode))) {
2601 tpacpi_input_send_key(scancode);
2602 *send_acpi_ev = false;
2603 } else {
2604 *ignore_acpi_ev = true;
2606 return true;
2608 return false;
2611 static bool hotkey_notify_wakeup(const u32 hkey,
2612 bool *send_acpi_ev,
2613 bool *ignore_acpi_ev)
2615 /* 0x2000-0x2FFF: Wakeup reason */
2616 *send_acpi_ev = true;
2617 *ignore_acpi_ev = false;
2619 switch (hkey) {
2620 case 0x2304: /* suspend, undock */
2621 case 0x2404: /* hibernation, undock */
2622 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2623 *ignore_acpi_ev = true;
2624 break;
2626 case 0x2305: /* suspend, bay eject */
2627 case 0x2405: /* hibernation, bay eject */
2628 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2629 *ignore_acpi_ev = true;
2630 break;
2632 case 0x2313: /* Battery on critical low level (S3) */
2633 case 0x2413: /* Battery on critical low level (S4) */
2634 printk(TPACPI_ALERT
2635 "EMERGENCY WAKEUP: battery almost empty\n");
2636 /* how to auto-heal: */
2637 /* 2313: woke up from S3, go to S4/S5 */
2638 /* 2413: woke up from S4, go to S5 */
2639 break;
2641 default:
2642 return false;
2645 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2646 printk(TPACPI_INFO
2647 "woke up due to a hot-unplug "
2648 "request...\n");
2649 hotkey_wakeup_reason_notify_change();
2651 return true;
2654 static bool hotkey_notify_usrevent(const u32 hkey,
2655 bool *send_acpi_ev,
2656 bool *ignore_acpi_ev)
2658 /* 0x5000-0x5FFF: human interface helpers */
2659 *send_acpi_ev = true;
2660 *ignore_acpi_ev = false;
2662 switch (hkey) {
2663 case 0x5010: /* Lenovo new BIOS: brightness changed */
2664 case 0x500b: /* X61t: tablet pen inserted into bay */
2665 case 0x500c: /* X61t: tablet pen removed from bay */
2666 return true;
2668 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2669 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2670 tpacpi_input_send_tabletsw();
2671 hotkey_tablet_mode_notify_change();
2672 *send_acpi_ev = false;
2673 return true;
2675 case 0x5001:
2676 case 0x5002:
2677 /* LID switch events. Do not propagate */
2678 *ignore_acpi_ev = true;
2679 return true;
2681 default:
2682 return false;
2686 static bool hotkey_notify_thermal(const u32 hkey,
2687 bool *send_acpi_ev,
2688 bool *ignore_acpi_ev)
2690 /* 0x6000-0x6FFF: thermal alarms */
2691 *send_acpi_ev = true;
2692 *ignore_acpi_ev = false;
2694 switch (hkey) {
2695 case 0x6011:
2696 printk(TPACPI_CRIT
2697 "THERMAL ALARM: battery is too hot!\n");
2698 /* recommended action: warn user through gui */
2699 return true;
2700 case 0x6012:
2701 printk(TPACPI_ALERT
2702 "THERMAL EMERGENCY: battery is extremely hot!\n");
2703 /* recommended action: immediate sleep/hibernate */
2704 return true;
2705 case 0x6021:
2706 printk(TPACPI_CRIT
2707 "THERMAL ALARM: "
2708 "a sensor reports something is too hot!\n");
2709 /* recommended action: warn user through gui, that */
2710 /* some internal component is too hot */
2711 return true;
2712 case 0x6022:
2713 printk(TPACPI_ALERT
2714 "THERMAL EMERGENCY: "
2715 "a sensor reports something is extremely hot!\n");
2716 /* recommended action: immediate sleep/hibernate */
2717 return true;
2718 case 0x6030:
2719 printk(TPACPI_INFO
2720 "EC reports that Thermal Table has changed\n");
2721 /* recommended action: do nothing, we don't have
2722 * Lenovo ATM information */
2723 return true;
2724 default:
2725 printk(TPACPI_ALERT
2726 "THERMAL ALERT: unknown thermal alarm received\n");
2727 return false;
2731 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2733 u32 hkey;
2734 bool send_acpi_ev;
2735 bool ignore_acpi_ev;
2736 bool known_ev;
2738 if (event != 0x80) {
2739 printk(TPACPI_ERR
2740 "unknown HKEY notification event %d\n", event);
2741 /* forward it to userspace, maybe it knows how to handle it */
2742 acpi_bus_generate_netlink_event(
2743 ibm->acpi->device->pnp.device_class,
2744 dev_name(&ibm->acpi->device->dev),
2745 event, 0);
2746 return;
2749 while (1) {
2750 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2751 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2752 return;
2755 if (hkey == 0) {
2756 /* queue empty */
2757 return;
2760 send_acpi_ev = true;
2761 ignore_acpi_ev = false;
2763 switch (hkey >> 12) {
2764 case 1:
2765 /* 0x1000-0x1FFF: key presses */
2766 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2767 &ignore_acpi_ev);
2768 break;
2769 case 2:
2770 /* 0x2000-0x2FFF: Wakeup reason */
2771 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2772 &ignore_acpi_ev);
2773 break;
2774 case 3:
2775 /* 0x3000-0x3FFF: bay-related wakeups */
2776 if (hkey == 0x3003) {
2777 hotkey_autosleep_ack = 1;
2778 printk(TPACPI_INFO
2779 "bay ejected\n");
2780 hotkey_wakeup_hotunplug_complete_notify_change();
2781 known_ev = true;
2782 } else {
2783 known_ev = false;
2785 break;
2786 case 4:
2787 /* 0x4000-0x4FFF: dock-related wakeups */
2788 if (hkey == 0x4003) {
2789 hotkey_autosleep_ack = 1;
2790 printk(TPACPI_INFO
2791 "undocked\n");
2792 hotkey_wakeup_hotunplug_complete_notify_change();
2793 known_ev = true;
2794 } else {
2795 known_ev = false;
2797 break;
2798 case 5:
2799 /* 0x5000-0x5FFF: human interface helpers */
2800 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2801 &ignore_acpi_ev);
2802 break;
2803 case 6:
2804 /* 0x6000-0x6FFF: thermal alarms */
2805 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2806 &ignore_acpi_ev);
2807 break;
2808 case 7:
2809 /* 0x7000-0x7FFF: misc */
2810 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2811 tpacpi_send_radiosw_update();
2812 send_acpi_ev = 0;
2813 known_ev = true;
2814 break;
2816 /* fallthrough to default */
2817 default:
2818 known_ev = false;
2820 if (!known_ev) {
2821 printk(TPACPI_NOTICE
2822 "unhandled HKEY event 0x%04x\n", hkey);
2823 printk(TPACPI_NOTICE
2824 "please report the conditions when this "
2825 "event happened to %s\n", TPACPI_MAIL);
2828 /* Legacy events */
2829 if (!ignore_acpi_ev &&
2830 (send_acpi_ev || hotkey_report_mode < 2)) {
2831 acpi_bus_generate_proc_event(ibm->acpi->device,
2832 event, hkey);
2835 /* netlink events */
2836 if (!ignore_acpi_ev && send_acpi_ev) {
2837 acpi_bus_generate_netlink_event(
2838 ibm->acpi->device->pnp.device_class,
2839 dev_name(&ibm->acpi->device->dev),
2840 event, hkey);
2845 static void hotkey_suspend(pm_message_t state)
2847 /* Do these on suspend, we get the events on early resume! */
2848 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2849 hotkey_autosleep_ack = 0;
2852 static void hotkey_resume(void)
2854 tpacpi_disable_brightness_delay();
2856 if (hotkey_mask_get())
2857 printk(TPACPI_ERR
2858 "error while trying to read hot key mask "
2859 "from firmware\n");
2860 tpacpi_send_radiosw_update();
2861 hotkey_tablet_mode_notify_change();
2862 hotkey_wakeup_reason_notify_change();
2863 hotkey_wakeup_hotunplug_complete_notify_change();
2864 hotkey_poll_setup_safe(0);
2867 /* procfs -------------------------------------------------------------- */
2868 static int hotkey_read(char *p)
2870 int res, status;
2871 int len = 0;
2873 if (!tp_features.hotkey) {
2874 len += sprintf(p + len, "status:\t\tnot supported\n");
2875 return len;
2878 if (mutex_lock_killable(&hotkey_mutex))
2879 return -ERESTARTSYS;
2880 res = hotkey_status_get(&status);
2881 if (!res)
2882 res = hotkey_mask_get();
2883 mutex_unlock(&hotkey_mutex);
2884 if (res)
2885 return res;
2887 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2888 if (tp_features.hotkey_mask) {
2889 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2890 len += sprintf(p + len,
2891 "commands:\tenable, disable, reset, <mask>\n");
2892 } else {
2893 len += sprintf(p + len, "mask:\t\tnot supported\n");
2894 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2897 return len;
2900 static int hotkey_write(char *buf)
2902 int res, status;
2903 u32 mask;
2904 char *cmd;
2906 if (!tp_features.hotkey)
2907 return -ENODEV;
2909 if (mutex_lock_killable(&hotkey_mutex))
2910 return -ERESTARTSYS;
2912 status = -1;
2913 mask = hotkey_mask;
2915 res = 0;
2916 while ((cmd = next_cmd(&buf))) {
2917 if (strlencmp(cmd, "enable") == 0) {
2918 status = 1;
2919 } else if (strlencmp(cmd, "disable") == 0) {
2920 status = 0;
2921 } else if (strlencmp(cmd, "reset") == 0) {
2922 status = hotkey_orig_status;
2923 mask = hotkey_orig_mask;
2924 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2925 /* mask set */
2926 } else if (sscanf(cmd, "%x", &mask) == 1) {
2927 /* mask set */
2928 } else {
2929 res = -EINVAL;
2930 goto errexit;
2933 if (status != -1)
2934 res = hotkey_status_set(status);
2936 if (!res && mask != hotkey_mask)
2937 res = hotkey_mask_set(mask);
2939 errexit:
2940 mutex_unlock(&hotkey_mutex);
2941 return res;
2944 static const struct acpi_device_id ibm_htk_device_ids[] = {
2945 {TPACPI_ACPI_HKEY_HID, 0},
2946 {"", 0},
2949 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2950 .hid = ibm_htk_device_ids,
2951 .notify = hotkey_notify,
2952 .handle = &hkey_handle,
2953 .type = ACPI_DEVICE_NOTIFY,
2956 static struct ibm_struct hotkey_driver_data = {
2957 .name = "hotkey",
2958 .read = hotkey_read,
2959 .write = hotkey_write,
2960 .exit = hotkey_exit,
2961 .resume = hotkey_resume,
2962 .suspend = hotkey_suspend,
2963 .acpi = &ibm_hotkey_acpidriver,
2966 /*************************************************************************
2967 * Bluetooth subdriver
2970 enum {
2971 /* ACPI GBDC/SBDC bits */
2972 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2973 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2974 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2975 off / last state */
2978 enum {
2979 /* ACPI \BLTH commands */
2980 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2981 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2982 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2983 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2984 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2987 static struct rfkill *tpacpi_bluetooth_rfkill;
2989 static void bluetooth_suspend(pm_message_t state)
2991 /* Try to make sure radio will resume powered off */
2992 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2993 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2996 static int bluetooth_get_radiosw(void)
2998 int status;
3000 if (!tp_features.bluetooth)
3001 return -ENODEV;
3003 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3004 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3005 return RFKILL_STATE_HARD_BLOCKED;
3007 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3008 if (dbg_bluetoothemul)
3009 return (tpacpi_bluetooth_emulstate) ?
3010 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3011 #endif
3013 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3014 return -EIO;
3016 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3017 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3020 static void bluetooth_update_rfk(void)
3022 int status;
3024 if (!tpacpi_bluetooth_rfkill)
3025 return;
3027 status = bluetooth_get_radiosw();
3028 if (status < 0)
3029 return;
3030 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3033 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3035 int status;
3037 if (!tp_features.bluetooth)
3038 return -ENODEV;
3040 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3041 * reason to risk weird behaviour. */
3042 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3043 && radio_on)
3044 return -EPERM;
3046 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3047 if (dbg_bluetoothemul) {
3048 tpacpi_bluetooth_emulstate = !!radio_on;
3049 if (update_rfk)
3050 bluetooth_update_rfk();
3051 return 0;
3053 #endif
3055 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3056 if (radio_on)
3057 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3058 else
3059 status = 0;
3060 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3061 return -EIO;
3063 if (update_rfk)
3064 bluetooth_update_rfk();
3066 return 0;
3069 /* sysfs bluetooth enable ---------------------------------------------- */
3070 static ssize_t bluetooth_enable_show(struct device *dev,
3071 struct device_attribute *attr,
3072 char *buf)
3074 int status;
3076 status = bluetooth_get_radiosw();
3077 if (status < 0)
3078 return status;
3080 return snprintf(buf, PAGE_SIZE, "%d\n",
3081 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3084 static ssize_t bluetooth_enable_store(struct device *dev,
3085 struct device_attribute *attr,
3086 const char *buf, size_t count)
3088 unsigned long t;
3089 int res;
3091 if (parse_strtoul(buf, 1, &t))
3092 return -EINVAL;
3094 res = bluetooth_set_radiosw(t, 1);
3096 return (res) ? res : count;
3099 static struct device_attribute dev_attr_bluetooth_enable =
3100 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3101 bluetooth_enable_show, bluetooth_enable_store);
3103 /* --------------------------------------------------------------------- */
3105 static struct attribute *bluetooth_attributes[] = {
3106 &dev_attr_bluetooth_enable.attr,
3107 NULL
3110 static const struct attribute_group bluetooth_attr_group = {
3111 .attrs = bluetooth_attributes,
3114 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3116 int bts = bluetooth_get_radiosw();
3118 if (bts < 0)
3119 return bts;
3121 *state = bts;
3122 return 0;
3125 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3127 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3130 static void bluetooth_shutdown(void)
3132 /* Order firmware to save current state to NVRAM */
3133 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3134 TP_ACPI_BLTH_SAVE_STATE))
3135 printk(TPACPI_NOTICE
3136 "failed to save bluetooth state to NVRAM\n");
3139 static void bluetooth_exit(void)
3141 bluetooth_shutdown();
3143 if (tpacpi_bluetooth_rfkill)
3144 rfkill_unregister(tpacpi_bluetooth_rfkill);
3146 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3147 &bluetooth_attr_group);
3150 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3152 int res;
3153 int status = 0;
3155 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3157 TPACPI_ACPIHANDLE_INIT(hkey);
3159 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3160 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3161 tp_features.bluetooth = hkey_handle &&
3162 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3164 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3165 str_supported(tp_features.bluetooth),
3166 status);
3168 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3169 if (dbg_bluetoothemul) {
3170 tp_features.bluetooth = 1;
3171 printk(TPACPI_INFO
3172 "bluetooth switch emulation enabled\n");
3173 } else
3174 #endif
3175 if (tp_features.bluetooth &&
3176 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3177 /* no bluetooth hardware present in system */
3178 tp_features.bluetooth = 0;
3179 dbg_printk(TPACPI_DBG_INIT,
3180 "bluetooth hardware not installed\n");
3183 if (!tp_features.bluetooth)
3184 return 1;
3186 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3187 &bluetooth_attr_group);
3188 if (res)
3189 return res;
3191 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3192 &tpacpi_bluetooth_rfkill,
3193 RFKILL_TYPE_BLUETOOTH,
3194 "tpacpi_bluetooth_sw",
3195 true,
3196 tpacpi_bluetooth_rfk_set,
3197 tpacpi_bluetooth_rfk_get);
3198 if (res) {
3199 bluetooth_exit();
3200 return res;
3203 return 0;
3206 /* procfs -------------------------------------------------------------- */
3207 static int bluetooth_read(char *p)
3209 int len = 0;
3210 int status = bluetooth_get_radiosw();
3212 if (!tp_features.bluetooth)
3213 len += sprintf(p + len, "status:\t\tnot supported\n");
3214 else {
3215 len += sprintf(p + len, "status:\t\t%s\n",
3216 (status == RFKILL_STATE_UNBLOCKED) ?
3217 "enabled" : "disabled");
3218 len += sprintf(p + len, "commands:\tenable, disable\n");
3221 return len;
3224 static int bluetooth_write(char *buf)
3226 char *cmd;
3228 if (!tp_features.bluetooth)
3229 return -ENODEV;
3231 while ((cmd = next_cmd(&buf))) {
3232 if (strlencmp(cmd, "enable") == 0) {
3233 bluetooth_set_radiosw(1, 1);
3234 } else if (strlencmp(cmd, "disable") == 0) {
3235 bluetooth_set_radiosw(0, 1);
3236 } else
3237 return -EINVAL;
3240 return 0;
3243 static struct ibm_struct bluetooth_driver_data = {
3244 .name = "bluetooth",
3245 .read = bluetooth_read,
3246 .write = bluetooth_write,
3247 .exit = bluetooth_exit,
3248 .suspend = bluetooth_suspend,
3249 .shutdown = bluetooth_shutdown,
3252 /*************************************************************************
3253 * Wan subdriver
3256 enum {
3257 /* ACPI GWAN/SWAN bits */
3258 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3259 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3260 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3261 off / last state */
3264 static struct rfkill *tpacpi_wan_rfkill;
3266 static void wan_suspend(pm_message_t state)
3268 /* Try to make sure radio will resume powered off */
3269 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3270 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3273 static int wan_get_radiosw(void)
3275 int status;
3277 if (!tp_features.wan)
3278 return -ENODEV;
3280 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3281 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3282 return RFKILL_STATE_HARD_BLOCKED;
3284 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3285 if (dbg_wwanemul)
3286 return (tpacpi_wwan_emulstate) ?
3287 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3288 #endif
3290 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3291 return -EIO;
3293 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3294 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3297 static void wan_update_rfk(void)
3299 int status;
3301 if (!tpacpi_wan_rfkill)
3302 return;
3304 status = wan_get_radiosw();
3305 if (status < 0)
3306 return;
3307 rfkill_force_state(tpacpi_wan_rfkill, status);
3310 static int wan_set_radiosw(int radio_on, int update_rfk)
3312 int status;
3314 if (!tp_features.wan)
3315 return -ENODEV;
3317 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3318 * reason to risk weird behaviour. */
3319 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3320 && radio_on)
3321 return -EPERM;
3323 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3324 if (dbg_wwanemul) {
3325 tpacpi_wwan_emulstate = !!radio_on;
3326 if (update_rfk)
3327 wan_update_rfk();
3328 return 0;
3330 #endif
3332 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3333 if (radio_on)
3334 status = TP_ACPI_WANCARD_RADIOSSW;
3335 else
3336 status = 0;
3337 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3338 return -EIO;
3340 if (update_rfk)
3341 wan_update_rfk();
3343 return 0;
3346 /* sysfs wan enable ---------------------------------------------------- */
3347 static ssize_t wan_enable_show(struct device *dev,
3348 struct device_attribute *attr,
3349 char *buf)
3351 int status;
3353 status = wan_get_radiosw();
3354 if (status < 0)
3355 return status;
3357 return snprintf(buf, PAGE_SIZE, "%d\n",
3358 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3361 static ssize_t wan_enable_store(struct device *dev,
3362 struct device_attribute *attr,
3363 const char *buf, size_t count)
3365 unsigned long t;
3366 int res;
3368 if (parse_strtoul(buf, 1, &t))
3369 return -EINVAL;
3371 res = wan_set_radiosw(t, 1);
3373 return (res) ? res : count;
3376 static struct device_attribute dev_attr_wan_enable =
3377 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3378 wan_enable_show, wan_enable_store);
3380 /* --------------------------------------------------------------------- */
3382 static struct attribute *wan_attributes[] = {
3383 &dev_attr_wan_enable.attr,
3384 NULL
3387 static const struct attribute_group wan_attr_group = {
3388 .attrs = wan_attributes,
3391 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3393 int wans = wan_get_radiosw();
3395 if (wans < 0)
3396 return wans;
3398 *state = wans;
3399 return 0;
3402 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3404 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3407 static void wan_shutdown(void)
3409 /* Order firmware to save current state to NVRAM */
3410 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3411 TP_ACPI_WGSV_SAVE_STATE))
3412 printk(TPACPI_NOTICE
3413 "failed to save WWAN state to NVRAM\n");
3416 static void wan_exit(void)
3418 wan_shutdown();
3420 if (tpacpi_wan_rfkill)
3421 rfkill_unregister(tpacpi_wan_rfkill);
3423 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3424 &wan_attr_group);
3427 static int __init wan_init(struct ibm_init_struct *iibm)
3429 int res;
3430 int status = 0;
3432 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3434 TPACPI_ACPIHANDLE_INIT(hkey);
3436 tp_features.wan = hkey_handle &&
3437 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3439 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3440 str_supported(tp_features.wan),
3441 status);
3443 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3444 if (dbg_wwanemul) {
3445 tp_features.wan = 1;
3446 printk(TPACPI_INFO
3447 "wwan switch emulation enabled\n");
3448 } else
3449 #endif
3450 if (tp_features.wan &&
3451 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3452 /* no wan hardware present in system */
3453 tp_features.wan = 0;
3454 dbg_printk(TPACPI_DBG_INIT,
3455 "wan hardware not installed\n");
3458 if (!tp_features.wan)
3459 return 1;
3461 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3462 &wan_attr_group);
3463 if (res)
3464 return res;
3466 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3467 &tpacpi_wan_rfkill,
3468 RFKILL_TYPE_WWAN,
3469 "tpacpi_wwan_sw",
3470 true,
3471 tpacpi_wan_rfk_set,
3472 tpacpi_wan_rfk_get);
3473 if (res) {
3474 wan_exit();
3475 return res;
3478 return 0;
3481 /* procfs -------------------------------------------------------------- */
3482 static int wan_read(char *p)
3484 int len = 0;
3485 int status = wan_get_radiosw();
3487 if (!tp_features.wan)
3488 len += sprintf(p + len, "status:\t\tnot supported\n");
3489 else {
3490 len += sprintf(p + len, "status:\t\t%s\n",
3491 (status == RFKILL_STATE_UNBLOCKED) ?
3492 "enabled" : "disabled");
3493 len += sprintf(p + len, "commands:\tenable, disable\n");
3496 return len;
3499 static int wan_write(char *buf)
3501 char *cmd;
3503 if (!tp_features.wan)
3504 return -ENODEV;
3506 while ((cmd = next_cmd(&buf))) {
3507 if (strlencmp(cmd, "enable") == 0) {
3508 wan_set_radiosw(1, 1);
3509 } else if (strlencmp(cmd, "disable") == 0) {
3510 wan_set_radiosw(0, 1);
3511 } else
3512 return -EINVAL;
3515 return 0;
3518 static struct ibm_struct wan_driver_data = {
3519 .name = "wan",
3520 .read = wan_read,
3521 .write = wan_write,
3522 .exit = wan_exit,
3523 .suspend = wan_suspend,
3524 .shutdown = wan_shutdown,
3527 /*************************************************************************
3528 * UWB subdriver
3531 enum {
3532 /* ACPI GUWB/SUWB bits */
3533 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3534 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3537 static struct rfkill *tpacpi_uwb_rfkill;
3539 static int uwb_get_radiosw(void)
3541 int status;
3543 if (!tp_features.uwb)
3544 return -ENODEV;
3546 /* WLSW overrides UWB in firmware/hardware, reflect that */
3547 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3548 return RFKILL_STATE_HARD_BLOCKED;
3550 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3551 if (dbg_uwbemul)
3552 return (tpacpi_uwb_emulstate) ?
3553 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3554 #endif
3556 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3557 return -EIO;
3559 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3560 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3563 static void uwb_update_rfk(void)
3565 int status;
3567 if (!tpacpi_uwb_rfkill)
3568 return;
3570 status = uwb_get_radiosw();
3571 if (status < 0)
3572 return;
3573 rfkill_force_state(tpacpi_uwb_rfkill, status);
3576 static int uwb_set_radiosw(int radio_on, int update_rfk)
3578 int status;
3580 if (!tp_features.uwb)
3581 return -ENODEV;
3583 /* WLSW overrides UWB in firmware/hardware, but there is no
3584 * reason to risk weird behaviour. */
3585 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3586 && radio_on)
3587 return -EPERM;
3589 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3590 if (dbg_uwbemul) {
3591 tpacpi_uwb_emulstate = !!radio_on;
3592 if (update_rfk)
3593 uwb_update_rfk();
3594 return 0;
3596 #endif
3598 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3599 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3600 return -EIO;
3602 if (update_rfk)
3603 uwb_update_rfk();
3605 return 0;
3608 /* --------------------------------------------------------------------- */
3610 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3612 int uwbs = uwb_get_radiosw();
3614 if (uwbs < 0)
3615 return uwbs;
3617 *state = uwbs;
3618 return 0;
3621 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3623 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3626 static void uwb_exit(void)
3628 if (tpacpi_uwb_rfkill)
3629 rfkill_unregister(tpacpi_uwb_rfkill);
3632 static int __init uwb_init(struct ibm_init_struct *iibm)
3634 int res;
3635 int status = 0;
3637 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3639 TPACPI_ACPIHANDLE_INIT(hkey);
3641 tp_features.uwb = hkey_handle &&
3642 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3644 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3645 str_supported(tp_features.uwb),
3646 status);
3648 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3649 if (dbg_uwbemul) {
3650 tp_features.uwb = 1;
3651 printk(TPACPI_INFO
3652 "uwb switch emulation enabled\n");
3653 } else
3654 #endif
3655 if (tp_features.uwb &&
3656 !(status & TP_ACPI_UWB_HWPRESENT)) {
3657 /* no uwb hardware present in system */
3658 tp_features.uwb = 0;
3659 dbg_printk(TPACPI_DBG_INIT,
3660 "uwb hardware not installed\n");
3663 if (!tp_features.uwb)
3664 return 1;
3666 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3667 &tpacpi_uwb_rfkill,
3668 RFKILL_TYPE_UWB,
3669 "tpacpi_uwb_sw",
3670 false,
3671 tpacpi_uwb_rfk_set,
3672 tpacpi_uwb_rfk_get);
3674 return res;
3677 static struct ibm_struct uwb_driver_data = {
3678 .name = "uwb",
3679 .exit = uwb_exit,
3680 .flags.experimental = 1,
3683 /*************************************************************************
3684 * Video subdriver
3687 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3689 enum video_access_mode {
3690 TPACPI_VIDEO_NONE = 0,
3691 TPACPI_VIDEO_570, /* 570 */
3692 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3693 TPACPI_VIDEO_NEW, /* all others */
3696 enum { /* video status flags, based on VIDEO_570 */
3697 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3698 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3699 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3702 enum { /* TPACPI_VIDEO_570 constants */
3703 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3704 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3705 * video_status_flags */
3706 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3707 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3710 static enum video_access_mode video_supported;
3711 static int video_orig_autosw;
3713 static int video_autosw_get(void);
3714 static int video_autosw_set(int enable);
3716 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3718 static int __init video_init(struct ibm_init_struct *iibm)
3720 int ivga;
3722 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3724 TPACPI_ACPIHANDLE_INIT(vid);
3725 TPACPI_ACPIHANDLE_INIT(vid2);
3727 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3728 /* G41, assume IVGA doesn't change */
3729 vid_handle = vid2_handle;
3731 if (!vid_handle)
3732 /* video switching not supported on R30, R31 */
3733 video_supported = TPACPI_VIDEO_NONE;
3734 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3735 /* 570 */
3736 video_supported = TPACPI_VIDEO_570;
3737 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3738 /* 600e/x, 770e, 770x */
3739 video_supported = TPACPI_VIDEO_770;
3740 else
3741 /* all others */
3742 video_supported = TPACPI_VIDEO_NEW;
3744 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3745 str_supported(video_supported != TPACPI_VIDEO_NONE),
3746 video_supported);
3748 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3751 static void video_exit(void)
3753 dbg_printk(TPACPI_DBG_EXIT,
3754 "restoring original video autoswitch mode\n");
3755 if (video_autosw_set(video_orig_autosw))
3756 printk(TPACPI_ERR "error while trying to restore original "
3757 "video autoswitch mode\n");
3760 static int video_outputsw_get(void)
3762 int status = 0;
3763 int i;
3765 switch (video_supported) {
3766 case TPACPI_VIDEO_570:
3767 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3768 TP_ACPI_VIDEO_570_PHSCMD))
3769 return -EIO;
3770 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3771 break;
3772 case TPACPI_VIDEO_770:
3773 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3774 return -EIO;
3775 if (i)
3776 status |= TP_ACPI_VIDEO_S_LCD;
3777 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3778 return -EIO;
3779 if (i)
3780 status |= TP_ACPI_VIDEO_S_CRT;
3781 break;
3782 case TPACPI_VIDEO_NEW:
3783 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3784 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3785 return -EIO;
3786 if (i)
3787 status |= TP_ACPI_VIDEO_S_CRT;
3789 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3790 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3791 return -EIO;
3792 if (i)
3793 status |= TP_ACPI_VIDEO_S_LCD;
3794 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3795 return -EIO;
3796 if (i)
3797 status |= TP_ACPI_VIDEO_S_DVI;
3798 break;
3799 default:
3800 return -ENOSYS;
3803 return status;
3806 static int video_outputsw_set(int status)
3808 int autosw;
3809 int res = 0;
3811 switch (video_supported) {
3812 case TPACPI_VIDEO_570:
3813 res = acpi_evalf(NULL, NULL,
3814 "\\_SB.PHS2", "vdd",
3815 TP_ACPI_VIDEO_570_PHS2CMD,
3816 status | TP_ACPI_VIDEO_570_PHS2SET);
3817 break;
3818 case TPACPI_VIDEO_770:
3819 autosw = video_autosw_get();
3820 if (autosw < 0)
3821 return autosw;
3823 res = video_autosw_set(1);
3824 if (res)
3825 return res;
3826 res = acpi_evalf(vid_handle, NULL,
3827 "ASWT", "vdd", status * 0x100, 0);
3828 if (!autosw && video_autosw_set(autosw)) {
3829 printk(TPACPI_ERR
3830 "video auto-switch left enabled due to error\n");
3831 return -EIO;
3833 break;
3834 case TPACPI_VIDEO_NEW:
3835 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3836 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3837 break;
3838 default:
3839 return -ENOSYS;
3842 return (res)? 0 : -EIO;
3845 static int video_autosw_get(void)
3847 int autosw = 0;
3849 switch (video_supported) {
3850 case TPACPI_VIDEO_570:
3851 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3852 return -EIO;
3853 break;
3854 case TPACPI_VIDEO_770:
3855 case TPACPI_VIDEO_NEW:
3856 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3857 return -EIO;
3858 break;
3859 default:
3860 return -ENOSYS;
3863 return autosw & 1;
3866 static int video_autosw_set(int enable)
3868 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3869 return -EIO;
3870 return 0;
3873 static int video_outputsw_cycle(void)
3875 int autosw = video_autosw_get();
3876 int res;
3878 if (autosw < 0)
3879 return autosw;
3881 switch (video_supported) {
3882 case TPACPI_VIDEO_570:
3883 res = video_autosw_set(1);
3884 if (res)
3885 return res;
3886 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3887 break;
3888 case TPACPI_VIDEO_770:
3889 case TPACPI_VIDEO_NEW:
3890 res = video_autosw_set(1);
3891 if (res)
3892 return res;
3893 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3894 break;
3895 default:
3896 return -ENOSYS;
3898 if (!autosw && video_autosw_set(autosw)) {
3899 printk(TPACPI_ERR
3900 "video auto-switch left enabled due to error\n");
3901 return -EIO;
3904 return (res)? 0 : -EIO;
3907 static int video_expand_toggle(void)
3909 switch (video_supported) {
3910 case TPACPI_VIDEO_570:
3911 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3912 0 : -EIO;
3913 case TPACPI_VIDEO_770:
3914 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3915 0 : -EIO;
3916 case TPACPI_VIDEO_NEW:
3917 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3918 0 : -EIO;
3919 default:
3920 return -ENOSYS;
3922 /* not reached */
3925 static int video_read(char *p)
3927 int status, autosw;
3928 int len = 0;
3930 if (video_supported == TPACPI_VIDEO_NONE) {
3931 len += sprintf(p + len, "status:\t\tnot supported\n");
3932 return len;
3935 status = video_outputsw_get();
3936 if (status < 0)
3937 return status;
3939 autosw = video_autosw_get();
3940 if (autosw < 0)
3941 return autosw;
3943 len += sprintf(p + len, "status:\t\tsupported\n");
3944 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3945 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3946 if (video_supported == TPACPI_VIDEO_NEW)
3947 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3948 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3949 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3950 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3951 if (video_supported == TPACPI_VIDEO_NEW)
3952 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3953 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3954 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3956 return len;
3959 static int video_write(char *buf)
3961 char *cmd;
3962 int enable, disable, status;
3963 int res;
3965 if (video_supported == TPACPI_VIDEO_NONE)
3966 return -ENODEV;
3968 enable = 0;
3969 disable = 0;
3971 while ((cmd = next_cmd(&buf))) {
3972 if (strlencmp(cmd, "lcd_enable") == 0) {
3973 enable |= TP_ACPI_VIDEO_S_LCD;
3974 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3975 disable |= TP_ACPI_VIDEO_S_LCD;
3976 } else if (strlencmp(cmd, "crt_enable") == 0) {
3977 enable |= TP_ACPI_VIDEO_S_CRT;
3978 } else if (strlencmp(cmd, "crt_disable") == 0) {
3979 disable |= TP_ACPI_VIDEO_S_CRT;
3980 } else if (video_supported == TPACPI_VIDEO_NEW &&
3981 strlencmp(cmd, "dvi_enable") == 0) {
3982 enable |= TP_ACPI_VIDEO_S_DVI;
3983 } else if (video_supported == TPACPI_VIDEO_NEW &&
3984 strlencmp(cmd, "dvi_disable") == 0) {
3985 disable |= TP_ACPI_VIDEO_S_DVI;
3986 } else if (strlencmp(cmd, "auto_enable") == 0) {
3987 res = video_autosw_set(1);
3988 if (res)
3989 return res;
3990 } else if (strlencmp(cmd, "auto_disable") == 0) {
3991 res = video_autosw_set(0);
3992 if (res)
3993 return res;
3994 } else if (strlencmp(cmd, "video_switch") == 0) {
3995 res = video_outputsw_cycle();
3996 if (res)
3997 return res;
3998 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3999 res = video_expand_toggle();
4000 if (res)
4001 return res;
4002 } else
4003 return -EINVAL;
4006 if (enable || disable) {
4007 status = video_outputsw_get();
4008 if (status < 0)
4009 return status;
4010 res = video_outputsw_set((status & ~disable) | enable);
4011 if (res)
4012 return res;
4015 return 0;
4018 static struct ibm_struct video_driver_data = {
4019 .name = "video",
4020 .read = video_read,
4021 .write = video_write,
4022 .exit = video_exit,
4025 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4027 /*************************************************************************
4028 * Light (thinklight) subdriver
4031 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4032 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4034 static int light_get_status(void)
4036 int status = 0;
4038 if (tp_features.light_status) {
4039 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4040 return -EIO;
4041 return (!!status);
4044 return -ENXIO;
4047 static int light_set_status(int status)
4049 int rc;
4051 if (tp_features.light) {
4052 if (cmos_handle) {
4053 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4054 (status)?
4055 TP_CMOS_THINKLIGHT_ON :
4056 TP_CMOS_THINKLIGHT_OFF);
4057 } else {
4058 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4059 (status)? 1 : 0);
4061 return (rc)? 0 : -EIO;
4064 return -ENXIO;
4067 static void light_set_status_worker(struct work_struct *work)
4069 struct tpacpi_led_classdev *data =
4070 container_of(work, struct tpacpi_led_classdev, work);
4072 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4073 light_set_status((data->new_state != TPACPI_LED_OFF));
4076 static void light_sysfs_set(struct led_classdev *led_cdev,
4077 enum led_brightness brightness)
4079 struct tpacpi_led_classdev *data =
4080 container_of(led_cdev,
4081 struct tpacpi_led_classdev,
4082 led_classdev);
4083 data->new_state = (brightness != LED_OFF) ?
4084 TPACPI_LED_ON : TPACPI_LED_OFF;
4085 queue_work(tpacpi_wq, &data->work);
4088 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4090 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4093 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4094 .led_classdev = {
4095 .name = "tpacpi::thinklight",
4096 .brightness_set = &light_sysfs_set,
4097 .brightness_get = &light_sysfs_get,
4101 static int __init light_init(struct ibm_init_struct *iibm)
4103 int rc;
4105 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4107 TPACPI_ACPIHANDLE_INIT(ledb);
4108 TPACPI_ACPIHANDLE_INIT(lght);
4109 TPACPI_ACPIHANDLE_INIT(cmos);
4110 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4112 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4113 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4115 if (tp_features.light)
4116 /* light status not supported on
4117 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4118 tp_features.light_status =
4119 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4121 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4122 str_supported(tp_features.light),
4123 str_supported(tp_features.light_status));
4125 if (!tp_features.light)
4126 return 1;
4128 rc = led_classdev_register(&tpacpi_pdev->dev,
4129 &tpacpi_led_thinklight.led_classdev);
4131 if (rc < 0) {
4132 tp_features.light = 0;
4133 tp_features.light_status = 0;
4134 } else {
4135 rc = 0;
4138 return rc;
4141 static void light_exit(void)
4143 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4144 if (work_pending(&tpacpi_led_thinklight.work))
4145 flush_workqueue(tpacpi_wq);
4148 static int light_read(char *p)
4150 int len = 0;
4151 int status;
4153 if (!tp_features.light) {
4154 len += sprintf(p + len, "status:\t\tnot supported\n");
4155 } else if (!tp_features.light_status) {
4156 len += sprintf(p + len, "status:\t\tunknown\n");
4157 len += sprintf(p + len, "commands:\ton, off\n");
4158 } else {
4159 status = light_get_status();
4160 if (status < 0)
4161 return status;
4162 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4163 len += sprintf(p + len, "commands:\ton, off\n");
4166 return len;
4169 static int light_write(char *buf)
4171 char *cmd;
4172 int newstatus = 0;
4174 if (!tp_features.light)
4175 return -ENODEV;
4177 while ((cmd = next_cmd(&buf))) {
4178 if (strlencmp(cmd, "on") == 0) {
4179 newstatus = 1;
4180 } else if (strlencmp(cmd, "off") == 0) {
4181 newstatus = 0;
4182 } else
4183 return -EINVAL;
4186 return light_set_status(newstatus);
4189 static struct ibm_struct light_driver_data = {
4190 .name = "light",
4191 .read = light_read,
4192 .write = light_write,
4193 .exit = light_exit,
4196 /*************************************************************************
4197 * Dock subdriver
4200 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4202 static void dock_notify(struct ibm_struct *ibm, u32 event);
4203 static int dock_read(char *p);
4204 static int dock_write(char *buf);
4206 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4207 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4208 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4209 "\\_SB.PCI.ISA.SLCE", /* 570 */
4210 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4212 /* don't list other alternatives as we install a notify handler on the 570 */
4213 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4215 static const struct acpi_device_id ibm_pci_device_ids[] = {
4216 {PCI_ROOT_HID_STRING, 0},
4217 {"", 0},
4220 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4222 .notify = dock_notify,
4223 .handle = &dock_handle,
4224 .type = ACPI_SYSTEM_NOTIFY,
4227 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4228 * We just use it to get notifications of dock hotplug
4229 * in very old thinkpads */
4230 .hid = ibm_pci_device_ids,
4231 .notify = dock_notify,
4232 .handle = &pci_handle,
4233 .type = ACPI_SYSTEM_NOTIFY,
4237 static struct ibm_struct dock_driver_data[2] = {
4239 .name = "dock",
4240 .read = dock_read,
4241 .write = dock_write,
4242 .acpi = &ibm_dock_acpidriver[0],
4245 .name = "dock",
4246 .acpi = &ibm_dock_acpidriver[1],
4250 #define dock_docked() (_sta(dock_handle) & 1)
4252 static int __init dock_init(struct ibm_init_struct *iibm)
4254 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4256 TPACPI_ACPIHANDLE_INIT(dock);
4258 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4259 str_supported(dock_handle != NULL));
4261 return (dock_handle)? 0 : 1;
4264 static int __init dock_init2(struct ibm_init_struct *iibm)
4266 int dock2_needed;
4268 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4270 if (dock_driver_data[0].flags.acpi_driver_registered &&
4271 dock_driver_data[0].flags.acpi_notify_installed) {
4272 TPACPI_ACPIHANDLE_INIT(pci);
4273 dock2_needed = (pci_handle != NULL);
4274 vdbg_printk(TPACPI_DBG_INIT,
4275 "dock PCI handler for the TP 570 is %s\n",
4276 str_supported(dock2_needed));
4277 } else {
4278 vdbg_printk(TPACPI_DBG_INIT,
4279 "dock subdriver part 2 not required\n");
4280 dock2_needed = 0;
4283 return (dock2_needed)? 0 : 1;
4286 static void dock_notify(struct ibm_struct *ibm, u32 event)
4288 int docked = dock_docked();
4289 int pci = ibm->acpi->hid && ibm->acpi->device &&
4290 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4291 int data;
4293 if (event == 1 && !pci) /* 570 */
4294 data = 1; /* button */
4295 else if (event == 1 && pci) /* 570 */
4296 data = 3; /* dock */
4297 else if (event == 3 && docked)
4298 data = 1; /* button */
4299 else if (event == 3 && !docked)
4300 data = 2; /* undock */
4301 else if (event == 0 && docked)
4302 data = 3; /* dock */
4303 else {
4304 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4305 event, _sta(dock_handle));
4306 data = 0; /* unknown */
4308 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4309 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4310 dev_name(&ibm->acpi->device->dev),
4311 event, data);
4314 static int dock_read(char *p)
4316 int len = 0;
4317 int docked = dock_docked();
4319 if (!dock_handle)
4320 len += sprintf(p + len, "status:\t\tnot supported\n");
4321 else if (!docked)
4322 len += sprintf(p + len, "status:\t\tundocked\n");
4323 else {
4324 len += sprintf(p + len, "status:\t\tdocked\n");
4325 len += sprintf(p + len, "commands:\tdock, undock\n");
4328 return len;
4331 static int dock_write(char *buf)
4333 char *cmd;
4335 if (!dock_docked())
4336 return -ENODEV;
4338 while ((cmd = next_cmd(&buf))) {
4339 if (strlencmp(cmd, "undock") == 0) {
4340 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4341 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4342 return -EIO;
4343 } else if (strlencmp(cmd, "dock") == 0) {
4344 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4345 return -EIO;
4346 } else
4347 return -EINVAL;
4350 return 0;
4353 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4355 /*************************************************************************
4356 * Bay subdriver
4359 #ifdef CONFIG_THINKPAD_ACPI_BAY
4361 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4362 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4363 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4364 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4365 ); /* A21e, R30, R31 */
4366 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4367 "_EJ0", /* all others */
4368 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4369 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4370 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4371 ); /* all others */
4372 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4373 "_EJ0", /* 770x */
4374 ); /* all others */
4376 static int __init bay_init(struct ibm_init_struct *iibm)
4378 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4380 TPACPI_ACPIHANDLE_INIT(bay);
4381 if (bay_handle)
4382 TPACPI_ACPIHANDLE_INIT(bay_ej);
4383 TPACPI_ACPIHANDLE_INIT(bay2);
4384 if (bay2_handle)
4385 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4387 tp_features.bay_status = bay_handle &&
4388 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4389 tp_features.bay_status2 = bay2_handle &&
4390 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4392 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4393 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4394 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4395 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4397 vdbg_printk(TPACPI_DBG_INIT,
4398 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4399 str_supported(tp_features.bay_status),
4400 str_supported(tp_features.bay_eject),
4401 str_supported(tp_features.bay_status2),
4402 str_supported(tp_features.bay_eject2));
4404 return (tp_features.bay_status || tp_features.bay_eject ||
4405 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4408 static void bay_notify(struct ibm_struct *ibm, u32 event)
4410 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4411 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4412 dev_name(&ibm->acpi->device->dev),
4413 event, 0);
4416 #define bay_occupied(b) (_sta(b##_handle) & 1)
4418 static int bay_read(char *p)
4420 int len = 0;
4421 int occupied = bay_occupied(bay);
4422 int occupied2 = bay_occupied(bay2);
4423 int eject, eject2;
4425 len += sprintf(p + len, "status:\t\t%s\n",
4426 tp_features.bay_status ?
4427 (occupied ? "occupied" : "unoccupied") :
4428 "not supported");
4429 if (tp_features.bay_status2)
4430 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4431 "occupied" : "unoccupied");
4433 eject = tp_features.bay_eject && occupied;
4434 eject2 = tp_features.bay_eject2 && occupied2;
4436 if (eject && eject2)
4437 len += sprintf(p + len, "commands:\teject, eject2\n");
4438 else if (eject)
4439 len += sprintf(p + len, "commands:\teject\n");
4440 else if (eject2)
4441 len += sprintf(p + len, "commands:\teject2\n");
4443 return len;
4446 static int bay_write(char *buf)
4448 char *cmd;
4450 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4451 return -ENODEV;
4453 while ((cmd = next_cmd(&buf))) {
4454 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4455 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4456 return -EIO;
4457 } else if (tp_features.bay_eject2 &&
4458 strlencmp(cmd, "eject2") == 0) {
4459 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4460 return -EIO;
4461 } else
4462 return -EINVAL;
4465 return 0;
4468 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4469 .notify = bay_notify,
4470 .handle = &bay_handle,
4471 .type = ACPI_SYSTEM_NOTIFY,
4474 static struct ibm_struct bay_driver_data = {
4475 .name = "bay",
4476 .read = bay_read,
4477 .write = bay_write,
4478 .acpi = &ibm_bay_acpidriver,
4481 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4483 /*************************************************************************
4484 * CMOS subdriver
4487 /* sysfs cmos_command -------------------------------------------------- */
4488 static ssize_t cmos_command_store(struct device *dev,
4489 struct device_attribute *attr,
4490 const char *buf, size_t count)
4492 unsigned long cmos_cmd;
4493 int res;
4495 if (parse_strtoul(buf, 21, &cmos_cmd))
4496 return -EINVAL;
4498 res = issue_thinkpad_cmos_command(cmos_cmd);
4499 return (res)? res : count;
4502 static struct device_attribute dev_attr_cmos_command =
4503 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4505 /* --------------------------------------------------------------------- */
4507 static int __init cmos_init(struct ibm_init_struct *iibm)
4509 int res;
4511 vdbg_printk(TPACPI_DBG_INIT,
4512 "initializing cmos commands subdriver\n");
4514 TPACPI_ACPIHANDLE_INIT(cmos);
4516 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4517 str_supported(cmos_handle != NULL));
4519 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4520 if (res)
4521 return res;
4523 return (cmos_handle)? 0 : 1;
4526 static void cmos_exit(void)
4528 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4531 static int cmos_read(char *p)
4533 int len = 0;
4535 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4536 R30, R31, T20-22, X20-21 */
4537 if (!cmos_handle)
4538 len += sprintf(p + len, "status:\t\tnot supported\n");
4539 else {
4540 len += sprintf(p + len, "status:\t\tsupported\n");
4541 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4544 return len;
4547 static int cmos_write(char *buf)
4549 char *cmd;
4550 int cmos_cmd, res;
4552 while ((cmd = next_cmd(&buf))) {
4553 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4554 cmos_cmd >= 0 && cmos_cmd <= 21) {
4555 /* cmos_cmd set */
4556 } else
4557 return -EINVAL;
4559 res = issue_thinkpad_cmos_command(cmos_cmd);
4560 if (res)
4561 return res;
4564 return 0;
4567 static struct ibm_struct cmos_driver_data = {
4568 .name = "cmos",
4569 .read = cmos_read,
4570 .write = cmos_write,
4571 .exit = cmos_exit,
4574 /*************************************************************************
4575 * LED subdriver
4578 enum led_access_mode {
4579 TPACPI_LED_NONE = 0,
4580 TPACPI_LED_570, /* 570 */
4581 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4582 TPACPI_LED_NEW, /* all others */
4585 enum { /* For TPACPI_LED_OLD */
4586 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4587 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4588 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4591 static enum led_access_mode led_supported;
4593 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4594 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4595 /* T20-22, X20-21 */
4596 "LED", /* all others */
4597 ); /* R30, R31 */
4599 #define TPACPI_LED_NUMLEDS 8
4600 static struct tpacpi_led_classdev *tpacpi_leds;
4601 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4602 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4603 /* there's a limit of 19 chars + NULL before 2.6.26 */
4604 "tpacpi::power",
4605 "tpacpi:orange:batt",
4606 "tpacpi:green:batt",
4607 "tpacpi::dock_active",
4608 "tpacpi::bay_active",
4609 "tpacpi::dock_batt",
4610 "tpacpi::unknown_led",
4611 "tpacpi::standby",
4614 static int led_get_status(const unsigned int led)
4616 int status;
4617 enum led_status_t led_s;
4619 switch (led_supported) {
4620 case TPACPI_LED_570:
4621 if (!acpi_evalf(ec_handle,
4622 &status, "GLED", "dd", 1 << led))
4623 return -EIO;
4624 led_s = (status == 0)?
4625 TPACPI_LED_OFF :
4626 ((status == 1)?
4627 TPACPI_LED_ON :
4628 TPACPI_LED_BLINK);
4629 tpacpi_led_state_cache[led] = led_s;
4630 return led_s;
4631 default:
4632 return -ENXIO;
4635 /* not reached */
4638 static int led_set_status(const unsigned int led,
4639 const enum led_status_t ledstatus)
4641 /* off, on, blink. Index is led_status_t */
4642 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4643 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4645 int rc = 0;
4647 switch (led_supported) {
4648 case TPACPI_LED_570:
4649 /* 570 */
4650 if (led > 7)
4651 return -EINVAL;
4652 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4653 (1 << led), led_sled_arg1[ledstatus]))
4654 rc = -EIO;
4655 break;
4656 case TPACPI_LED_OLD:
4657 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4658 if (led > 7)
4659 return -EINVAL;
4660 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4661 if (rc >= 0)
4662 rc = ec_write(TPACPI_LED_EC_HLBL,
4663 (ledstatus == TPACPI_LED_BLINK) << led);
4664 if (rc >= 0)
4665 rc = ec_write(TPACPI_LED_EC_HLCL,
4666 (ledstatus != TPACPI_LED_OFF) << led);
4667 break;
4668 case TPACPI_LED_NEW:
4669 /* all others */
4670 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4671 led, led_led_arg1[ledstatus]))
4672 rc = -EIO;
4673 break;
4674 default:
4675 rc = -ENXIO;
4678 if (!rc)
4679 tpacpi_led_state_cache[led] = ledstatus;
4681 return rc;
4684 static void led_set_status_worker(struct work_struct *work)
4686 struct tpacpi_led_classdev *data =
4687 container_of(work, struct tpacpi_led_classdev, work);
4689 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4690 led_set_status(data->led, data->new_state);
4693 static void led_sysfs_set(struct led_classdev *led_cdev,
4694 enum led_brightness brightness)
4696 struct tpacpi_led_classdev *data = container_of(led_cdev,
4697 struct tpacpi_led_classdev, led_classdev);
4699 if (brightness == LED_OFF)
4700 data->new_state = TPACPI_LED_OFF;
4701 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4702 data->new_state = TPACPI_LED_ON;
4703 else
4704 data->new_state = TPACPI_LED_BLINK;
4706 queue_work(tpacpi_wq, &data->work);
4709 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4710 unsigned long *delay_on, unsigned long *delay_off)
4712 struct tpacpi_led_classdev *data = container_of(led_cdev,
4713 struct tpacpi_led_classdev, led_classdev);
4715 /* Can we choose the flash rate? */
4716 if (*delay_on == 0 && *delay_off == 0) {
4717 /* yes. set them to the hardware blink rate (1 Hz) */
4718 *delay_on = 500; /* ms */
4719 *delay_off = 500; /* ms */
4720 } else if ((*delay_on != 500) || (*delay_off != 500))
4721 return -EINVAL;
4723 data->new_state = TPACPI_LED_BLINK;
4724 queue_work(tpacpi_wq, &data->work);
4726 return 0;
4729 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4731 int rc;
4733 struct tpacpi_led_classdev *data = container_of(led_cdev,
4734 struct tpacpi_led_classdev, led_classdev);
4736 rc = led_get_status(data->led);
4738 if (rc == TPACPI_LED_OFF || rc < 0)
4739 rc = LED_OFF; /* no error handling in led class :( */
4740 else
4741 rc = LED_FULL;
4743 return rc;
4746 static void led_exit(void)
4748 unsigned int i;
4750 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4751 if (tpacpi_leds[i].led_classdev.name)
4752 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4755 kfree(tpacpi_leds);
4758 static int __init led_init(struct ibm_init_struct *iibm)
4760 unsigned int i;
4761 int rc;
4763 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4765 TPACPI_ACPIHANDLE_INIT(led);
4767 if (!led_handle)
4768 /* led not supported on R30, R31 */
4769 led_supported = TPACPI_LED_NONE;
4770 else if (strlencmp(led_path, "SLED") == 0)
4771 /* 570 */
4772 led_supported = TPACPI_LED_570;
4773 else if (strlencmp(led_path, "SYSL") == 0)
4774 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4775 led_supported = TPACPI_LED_OLD;
4776 else
4777 /* all others */
4778 led_supported = TPACPI_LED_NEW;
4780 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4781 str_supported(led_supported), led_supported);
4783 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4784 GFP_KERNEL);
4785 if (!tpacpi_leds) {
4786 printk(TPACPI_ERR "Out of memory for LED data\n");
4787 return -ENOMEM;
4790 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4791 tpacpi_leds[i].led = i;
4793 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4794 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4795 if (led_supported == TPACPI_LED_570)
4796 tpacpi_leds[i].led_classdev.brightness_get =
4797 &led_sysfs_get;
4799 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4801 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4803 rc = led_classdev_register(&tpacpi_pdev->dev,
4804 &tpacpi_leds[i].led_classdev);
4805 if (rc < 0) {
4806 tpacpi_leds[i].led_classdev.name = NULL;
4807 led_exit();
4808 return rc;
4812 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4815 #define str_led_status(s) \
4816 ((s) == TPACPI_LED_OFF ? "off" : \
4817 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4819 static int led_read(char *p)
4821 int len = 0;
4823 if (!led_supported) {
4824 len += sprintf(p + len, "status:\t\tnot supported\n");
4825 return len;
4827 len += sprintf(p + len, "status:\t\tsupported\n");
4829 if (led_supported == TPACPI_LED_570) {
4830 /* 570 */
4831 int i, status;
4832 for (i = 0; i < 8; i++) {
4833 status = led_get_status(i);
4834 if (status < 0)
4835 return -EIO;
4836 len += sprintf(p + len, "%d:\t\t%s\n",
4837 i, str_led_status(status));
4841 len += sprintf(p + len, "commands:\t"
4842 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4844 return len;
4847 static int led_write(char *buf)
4849 char *cmd;
4850 int led, rc;
4851 enum led_status_t s;
4853 if (!led_supported)
4854 return -ENODEV;
4856 while ((cmd = next_cmd(&buf))) {
4857 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4858 return -EINVAL;
4860 if (strstr(cmd, "off")) {
4861 s = TPACPI_LED_OFF;
4862 } else if (strstr(cmd, "on")) {
4863 s = TPACPI_LED_ON;
4864 } else if (strstr(cmd, "blink")) {
4865 s = TPACPI_LED_BLINK;
4866 } else {
4867 return -EINVAL;
4870 rc = led_set_status(led, s);
4871 if (rc < 0)
4872 return rc;
4875 return 0;
4878 static struct ibm_struct led_driver_data = {
4879 .name = "led",
4880 .read = led_read,
4881 .write = led_write,
4882 .exit = led_exit,
4885 /*************************************************************************
4886 * Beep subdriver
4889 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4891 static int __init beep_init(struct ibm_init_struct *iibm)
4893 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4895 TPACPI_ACPIHANDLE_INIT(beep);
4897 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4898 str_supported(beep_handle != NULL));
4900 return (beep_handle)? 0 : 1;
4903 static int beep_read(char *p)
4905 int len = 0;
4907 if (!beep_handle)
4908 len += sprintf(p + len, "status:\t\tnot supported\n");
4909 else {
4910 len += sprintf(p + len, "status:\t\tsupported\n");
4911 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4914 return len;
4917 static int beep_write(char *buf)
4919 char *cmd;
4920 int beep_cmd;
4922 if (!beep_handle)
4923 return -ENODEV;
4925 while ((cmd = next_cmd(&buf))) {
4926 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4927 beep_cmd >= 0 && beep_cmd <= 17) {
4928 /* beep_cmd set */
4929 } else
4930 return -EINVAL;
4931 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4932 return -EIO;
4935 return 0;
4938 static struct ibm_struct beep_driver_data = {
4939 .name = "beep",
4940 .read = beep_read,
4941 .write = beep_write,
4944 /*************************************************************************
4945 * Thermal subdriver
4948 enum thermal_access_mode {
4949 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4950 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4951 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4952 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4953 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4956 enum { /* TPACPI_THERMAL_TPEC_* */
4957 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4958 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4959 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4962 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4963 struct ibm_thermal_sensors_struct {
4964 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4967 static enum thermal_access_mode thermal_read_mode;
4969 /* idx is zero-based */
4970 static int thermal_get_sensor(int idx, s32 *value)
4972 int t;
4973 s8 tmp;
4974 char tmpi[5];
4976 t = TP_EC_THERMAL_TMP0;
4978 switch (thermal_read_mode) {
4979 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4980 case TPACPI_THERMAL_TPEC_16:
4981 if (idx >= 8 && idx <= 15) {
4982 t = TP_EC_THERMAL_TMP8;
4983 idx -= 8;
4985 /* fallthrough */
4986 #endif
4987 case TPACPI_THERMAL_TPEC_8:
4988 if (idx <= 7) {
4989 if (!acpi_ec_read(t + idx, &tmp))
4990 return -EIO;
4991 *value = tmp * 1000;
4992 return 0;
4994 break;
4996 case TPACPI_THERMAL_ACPI_UPDT:
4997 if (idx <= 7) {
4998 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4999 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5000 return -EIO;
5001 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5002 return -EIO;
5003 *value = (t - 2732) * 100;
5004 return 0;
5006 break;
5008 case TPACPI_THERMAL_ACPI_TMP07:
5009 if (idx <= 7) {
5010 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5011 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5012 return -EIO;
5013 if (t > 127 || t < -127)
5014 t = TP_EC_THERMAL_TMP_NA;
5015 *value = t * 1000;
5016 return 0;
5018 break;
5020 case TPACPI_THERMAL_NONE:
5021 default:
5022 return -ENOSYS;
5025 return -EINVAL;
5028 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5030 int res, i;
5031 int n;
5033 n = 8;
5034 i = 0;
5036 if (!s)
5037 return -EINVAL;
5039 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5040 n = 16;
5042 for (i = 0 ; i < n; i++) {
5043 res = thermal_get_sensor(i, &s->temp[i]);
5044 if (res)
5045 return res;
5048 return n;
5051 /* sysfs temp##_input -------------------------------------------------- */
5053 static ssize_t thermal_temp_input_show(struct device *dev,
5054 struct device_attribute *attr,
5055 char *buf)
5057 struct sensor_device_attribute *sensor_attr =
5058 to_sensor_dev_attr(attr);
5059 int idx = sensor_attr->index;
5060 s32 value;
5061 int res;
5063 res = thermal_get_sensor(idx, &value);
5064 if (res)
5065 return res;
5066 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5067 return -ENXIO;
5069 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5072 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5073 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5074 thermal_temp_input_show, NULL, _idxB)
5076 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5077 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5078 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5079 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5080 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5081 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5082 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5083 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5084 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5085 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5086 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5087 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5088 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5089 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5090 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5091 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5092 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5095 #define THERMAL_ATTRS(X) \
5096 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5098 static struct attribute *thermal_temp_input_attr[] = {
5099 THERMAL_ATTRS(8),
5100 THERMAL_ATTRS(9),
5101 THERMAL_ATTRS(10),
5102 THERMAL_ATTRS(11),
5103 THERMAL_ATTRS(12),
5104 THERMAL_ATTRS(13),
5105 THERMAL_ATTRS(14),
5106 THERMAL_ATTRS(15),
5107 THERMAL_ATTRS(0),
5108 THERMAL_ATTRS(1),
5109 THERMAL_ATTRS(2),
5110 THERMAL_ATTRS(3),
5111 THERMAL_ATTRS(4),
5112 THERMAL_ATTRS(5),
5113 THERMAL_ATTRS(6),
5114 THERMAL_ATTRS(7),
5115 NULL
5118 static const struct attribute_group thermal_temp_input16_group = {
5119 .attrs = thermal_temp_input_attr
5122 static const struct attribute_group thermal_temp_input8_group = {
5123 .attrs = &thermal_temp_input_attr[8]
5126 #undef THERMAL_SENSOR_ATTR_TEMP
5127 #undef THERMAL_ATTRS
5129 /* --------------------------------------------------------------------- */
5131 static int __init thermal_init(struct ibm_init_struct *iibm)
5133 u8 t, ta1, ta2;
5134 int i;
5135 int acpi_tmp7;
5136 int res;
5138 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5140 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5142 if (thinkpad_id.ec_model) {
5144 * Direct EC access mode: sensors at registers
5145 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5146 * non-implemented, thermal sensors return 0x80 when
5147 * not available
5150 ta1 = ta2 = 0;
5151 for (i = 0; i < 8; i++) {
5152 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5153 ta1 |= t;
5154 } else {
5155 ta1 = 0;
5156 break;
5158 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5159 ta2 |= t;
5160 } else {
5161 ta1 = 0;
5162 break;
5165 if (ta1 == 0) {
5166 /* This is sheer paranoia, but we handle it anyway */
5167 if (acpi_tmp7) {
5168 printk(TPACPI_ERR
5169 "ThinkPad ACPI EC access misbehaving, "
5170 "falling back to ACPI TMPx access "
5171 "mode\n");
5172 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5173 } else {
5174 printk(TPACPI_ERR
5175 "ThinkPad ACPI EC access misbehaving, "
5176 "disabling thermal sensors access\n");
5177 thermal_read_mode = TPACPI_THERMAL_NONE;
5179 } else {
5180 thermal_read_mode =
5181 (ta2 != 0) ?
5182 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5184 } else if (acpi_tmp7) {
5185 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5186 /* 600e/x, 770e, 770x */
5187 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5188 } else {
5189 /* Standard ACPI TMPx access, max 8 sensors */
5190 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5192 } else {
5193 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5194 thermal_read_mode = TPACPI_THERMAL_NONE;
5197 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5198 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5199 thermal_read_mode);
5201 switch (thermal_read_mode) {
5202 case TPACPI_THERMAL_TPEC_16:
5203 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5204 &thermal_temp_input16_group);
5205 if (res)
5206 return res;
5207 break;
5208 case TPACPI_THERMAL_TPEC_8:
5209 case TPACPI_THERMAL_ACPI_TMP07:
5210 case TPACPI_THERMAL_ACPI_UPDT:
5211 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5212 &thermal_temp_input8_group);
5213 if (res)
5214 return res;
5215 break;
5216 case TPACPI_THERMAL_NONE:
5217 default:
5218 return 1;
5221 return 0;
5224 static void thermal_exit(void)
5226 switch (thermal_read_mode) {
5227 case TPACPI_THERMAL_TPEC_16:
5228 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5229 &thermal_temp_input16_group);
5230 break;
5231 case TPACPI_THERMAL_TPEC_8:
5232 case TPACPI_THERMAL_ACPI_TMP07:
5233 case TPACPI_THERMAL_ACPI_UPDT:
5234 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5235 &thermal_temp_input16_group);
5236 break;
5237 case TPACPI_THERMAL_NONE:
5238 default:
5239 break;
5243 static int thermal_read(char *p)
5245 int len = 0;
5246 int n, i;
5247 struct ibm_thermal_sensors_struct t;
5249 n = thermal_get_sensors(&t);
5250 if (unlikely(n < 0))
5251 return n;
5253 len += sprintf(p + len, "temperatures:\t");
5255 if (n > 0) {
5256 for (i = 0; i < (n - 1); i++)
5257 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5258 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5259 } else
5260 len += sprintf(p + len, "not supported\n");
5262 return len;
5265 static struct ibm_struct thermal_driver_data = {
5266 .name = "thermal",
5267 .read = thermal_read,
5268 .exit = thermal_exit,
5271 /*************************************************************************
5272 * EC Dump subdriver
5275 static u8 ecdump_regs[256];
5277 static int ecdump_read(char *p)
5279 int len = 0;
5280 int i, j;
5281 u8 v;
5283 len += sprintf(p + len, "EC "
5284 " +00 +01 +02 +03 +04 +05 +06 +07"
5285 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5286 for (i = 0; i < 256; i += 16) {
5287 len += sprintf(p + len, "EC 0x%02x:", i);
5288 for (j = 0; j < 16; j++) {
5289 if (!acpi_ec_read(i + j, &v))
5290 break;
5291 if (v != ecdump_regs[i + j])
5292 len += sprintf(p + len, " *%02x", v);
5293 else
5294 len += sprintf(p + len, " %02x", v);
5295 ecdump_regs[i + j] = v;
5297 len += sprintf(p + len, "\n");
5298 if (j != 16)
5299 break;
5302 /* These are way too dangerous to advertise openly... */
5303 #if 0
5304 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5305 " (<offset> is 00-ff, <value> is 00-ff)\n");
5306 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5307 " (<offset> is 00-ff, <value> is 0-255)\n");
5308 #endif
5309 return len;
5312 static int ecdump_write(char *buf)
5314 char *cmd;
5315 int i, v;
5317 while ((cmd = next_cmd(&buf))) {
5318 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5319 /* i and v set */
5320 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5321 /* i and v set */
5322 } else
5323 return -EINVAL;
5324 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5325 if (!acpi_ec_write(i, v))
5326 return -EIO;
5327 } else
5328 return -EINVAL;
5331 return 0;
5334 static struct ibm_struct ecdump_driver_data = {
5335 .name = "ecdump",
5336 .read = ecdump_read,
5337 .write = ecdump_write,
5338 .flags.experimental = 1,
5341 /*************************************************************************
5342 * Backlight/brightness subdriver
5345 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5347 enum {
5348 TP_EC_BACKLIGHT = 0x31,
5350 /* TP_EC_BACKLIGHT bitmasks */
5351 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5352 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5353 TP_EC_BACKLIGHT_MAPSW = 0x20,
5356 static struct backlight_device *ibm_backlight_device;
5357 static int brightness_mode;
5358 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5360 static struct mutex brightness_mutex;
5363 * ThinkPads can read brightness from two places: EC 0x31, or
5364 * CMOS NVRAM byte 0x5E, bits 0-3.
5366 * EC 0x31 has the following layout
5367 * Bit 7: unknown function
5368 * Bit 6: unknown function
5369 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5370 * Bit 4: must be set to zero to avoid problems
5371 * Bit 3-0: backlight brightness level
5373 * brightness_get_raw returns status data in the EC 0x31 layout
5375 static int brightness_get_raw(int *status)
5377 u8 lec = 0, lcmos = 0, level = 0;
5379 if (brightness_mode & 1) {
5380 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5381 return -EIO;
5382 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5384 if (brightness_mode & 2) {
5385 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5386 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5387 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5388 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5389 level = lcmos;
5392 if (brightness_mode == 3) {
5393 *status = lec; /* Prefer EC, CMOS is just a backing store */
5394 lec &= TP_EC_BACKLIGHT_LVLMSK;
5395 if (lec == lcmos)
5396 tp_warned.bright_cmos_ec_unsync = 0;
5397 else {
5398 if (!tp_warned.bright_cmos_ec_unsync) {
5399 printk(TPACPI_ERR
5400 "CMOS NVRAM (%u) and EC (%u) do not "
5401 "agree on display brightness level\n",
5402 (unsigned int) lcmos,
5403 (unsigned int) lec);
5404 tp_warned.bright_cmos_ec_unsync = 1;
5406 return -EIO;
5408 } else {
5409 *status = level;
5412 return 0;
5415 /* May return EINTR which can always be mapped to ERESTARTSYS */
5416 static int brightness_set(int value)
5418 int cmos_cmd, inc, i, res;
5419 int current_value;
5420 int command_bits;
5422 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5423 value < 0)
5424 return -EINVAL;
5426 res = mutex_lock_killable(&brightness_mutex);
5427 if (res < 0)
5428 return res;
5430 res = brightness_get_raw(&current_value);
5431 if (res < 0)
5432 goto errout;
5434 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5435 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5437 cmos_cmd = value > current_value ?
5438 TP_CMOS_BRIGHTNESS_UP :
5439 TP_CMOS_BRIGHTNESS_DOWN;
5440 inc = (value > current_value)? 1 : -1;
5442 res = 0;
5443 for (i = current_value; i != value; i += inc) {
5444 if ((brightness_mode & 2) &&
5445 issue_thinkpad_cmos_command(cmos_cmd)) {
5446 res = -EIO;
5447 goto errout;
5449 if ((brightness_mode & 1) &&
5450 !acpi_ec_write(TP_EC_BACKLIGHT,
5451 (i + inc) | command_bits)) {
5452 res = -EIO;
5453 goto errout;;
5457 errout:
5458 mutex_unlock(&brightness_mutex);
5459 return res;
5462 /* sysfs backlight class ----------------------------------------------- */
5464 static int brightness_update_status(struct backlight_device *bd)
5466 /* it is the backlight class's job (caller) to handle
5467 * EINTR and other errors properly */
5468 return brightness_set(
5469 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5470 bd->props.power == FB_BLANK_UNBLANK) ?
5471 bd->props.brightness : 0);
5474 static int brightness_get(struct backlight_device *bd)
5476 int status, res;
5478 res = brightness_get_raw(&status);
5479 if (res < 0)
5480 return 0; /* FIXME: teach backlight about error handling */
5482 return status & TP_EC_BACKLIGHT_LVLMSK;
5485 static struct backlight_ops ibm_backlight_data = {
5486 .get_brightness = brightness_get,
5487 .update_status = brightness_update_status,
5490 /* --------------------------------------------------------------------- */
5492 static int __init brightness_init(struct ibm_init_struct *iibm)
5494 int b;
5496 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5498 mutex_init(&brightness_mutex);
5501 * We always attempt to detect acpi support, so as to switch
5502 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5503 * going to publish a backlight interface
5505 b = tpacpi_check_std_acpi_brightness_support();
5506 if (b > 0) {
5508 if (acpi_video_backlight_support()) {
5509 if (brightness_enable > 1) {
5510 printk(TPACPI_NOTICE
5511 "Standard ACPI backlight interface "
5512 "available, not loading native one.\n");
5513 return 1;
5514 } else if (brightness_enable == 1) {
5515 printk(TPACPI_NOTICE
5516 "Backlight control force enabled, even if standard "
5517 "ACPI backlight interface is available\n");
5519 } else {
5520 if (brightness_enable > 1) {
5521 printk(TPACPI_NOTICE
5522 "Standard ACPI backlight interface not "
5523 "available, thinkpad_acpi native "
5524 "brightness control enabled\n");
5529 if (!brightness_enable) {
5530 dbg_printk(TPACPI_DBG_INIT,
5531 "brightness support disabled by "
5532 "module parameter\n");
5533 return 1;
5536 if (b > 16) {
5537 printk(TPACPI_ERR
5538 "Unsupported brightness interface, "
5539 "please contact %s\n", TPACPI_MAIL);
5540 return 1;
5542 if (b == 16)
5543 tp_features.bright_16levels = 1;
5545 if (!brightness_mode) {
5546 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5547 brightness_mode = 2;
5548 else
5549 brightness_mode = 3;
5551 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5552 brightness_mode);
5555 if (brightness_mode > 3)
5556 return -EINVAL;
5558 if (brightness_get_raw(&b) < 0)
5559 return 1;
5561 if (tp_features.bright_16levels)
5562 printk(TPACPI_INFO
5563 "detected a 16-level brightness capable ThinkPad\n");
5565 ibm_backlight_device = backlight_device_register(
5566 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5567 &ibm_backlight_data);
5568 if (IS_ERR(ibm_backlight_device)) {
5569 printk(TPACPI_ERR "Could not register backlight device\n");
5570 return PTR_ERR(ibm_backlight_device);
5572 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5574 ibm_backlight_device->props.max_brightness =
5575 (tp_features.bright_16levels)? 15 : 7;
5576 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5577 backlight_update_status(ibm_backlight_device);
5579 return 0;
5582 static void brightness_exit(void)
5584 if (ibm_backlight_device) {
5585 vdbg_printk(TPACPI_DBG_EXIT,
5586 "calling backlight_device_unregister()\n");
5587 backlight_device_unregister(ibm_backlight_device);
5591 static int brightness_read(char *p)
5593 int len = 0;
5594 int level;
5596 level = brightness_get(NULL);
5597 if (level < 0) {
5598 len += sprintf(p + len, "level:\t\tunreadable\n");
5599 } else {
5600 len += sprintf(p + len, "level:\t\t%d\n", level);
5601 len += sprintf(p + len, "commands:\tup, down\n");
5602 len += sprintf(p + len, "commands:\tlevel <level>"
5603 " (<level> is 0-%d)\n",
5604 (tp_features.bright_16levels) ? 15 : 7);
5607 return len;
5610 static int brightness_write(char *buf)
5612 int level;
5613 int rc;
5614 char *cmd;
5615 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5617 level = brightness_get(NULL);
5618 if (level < 0)
5619 return level;
5621 while ((cmd = next_cmd(&buf))) {
5622 if (strlencmp(cmd, "up") == 0) {
5623 if (level < max_level)
5624 level++;
5625 } else if (strlencmp(cmd, "down") == 0) {
5626 if (level > 0)
5627 level--;
5628 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5629 level >= 0 && level <= max_level) {
5630 /* new level set */
5631 } else
5632 return -EINVAL;
5636 * Now we know what the final level should be, so we try to set it.
5637 * Doing it this way makes the syscall restartable in case of EINTR
5639 rc = brightness_set(level);
5640 return (rc == -EINTR)? ERESTARTSYS : rc;
5643 static struct ibm_struct brightness_driver_data = {
5644 .name = "brightness",
5645 .read = brightness_read,
5646 .write = brightness_write,
5647 .exit = brightness_exit,
5650 /*************************************************************************
5651 * Volume subdriver
5654 static int volume_offset = 0x30;
5656 static int volume_read(char *p)
5658 int len = 0;
5659 u8 level;
5661 if (!acpi_ec_read(volume_offset, &level)) {
5662 len += sprintf(p + len, "level:\t\tunreadable\n");
5663 } else {
5664 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5665 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5666 len += sprintf(p + len, "commands:\tup, down, mute\n");
5667 len += sprintf(p + len, "commands:\tlevel <level>"
5668 " (<level> is 0-15)\n");
5671 return len;
5674 static int volume_write(char *buf)
5676 int cmos_cmd, inc, i;
5677 u8 level, mute;
5678 int new_level, new_mute;
5679 char *cmd;
5681 while ((cmd = next_cmd(&buf))) {
5682 if (!acpi_ec_read(volume_offset, &level))
5683 return -EIO;
5684 new_mute = mute = level & 0x40;
5685 new_level = level = level & 0xf;
5687 if (strlencmp(cmd, "up") == 0) {
5688 if (mute)
5689 new_mute = 0;
5690 else
5691 new_level = level == 15 ? 15 : level + 1;
5692 } else if (strlencmp(cmd, "down") == 0) {
5693 if (mute)
5694 new_mute = 0;
5695 else
5696 new_level = level == 0 ? 0 : level - 1;
5697 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5698 new_level >= 0 && new_level <= 15) {
5699 /* new_level set */
5700 } else if (strlencmp(cmd, "mute") == 0) {
5701 new_mute = 0x40;
5702 } else
5703 return -EINVAL;
5705 if (new_level != level) {
5706 /* mute doesn't change */
5708 cmos_cmd = (new_level > level) ?
5709 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5710 inc = new_level > level ? 1 : -1;
5712 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5713 !acpi_ec_write(volume_offset, level)))
5714 return -EIO;
5716 for (i = level; i != new_level; i += inc)
5717 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5718 !acpi_ec_write(volume_offset, i + inc))
5719 return -EIO;
5721 if (mute &&
5722 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5723 !acpi_ec_write(volume_offset, new_level + mute))) {
5724 return -EIO;
5728 if (new_mute != mute) {
5729 /* level doesn't change */
5731 cmos_cmd = (new_mute) ?
5732 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5734 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5735 !acpi_ec_write(volume_offset, level + new_mute))
5736 return -EIO;
5740 return 0;
5743 static struct ibm_struct volume_driver_data = {
5744 .name = "volume",
5745 .read = volume_read,
5746 .write = volume_write,
5749 /*************************************************************************
5750 * Fan subdriver
5754 * FAN ACCESS MODES
5756 * TPACPI_FAN_RD_ACPI_GFAN:
5757 * ACPI GFAN method: returns fan level
5759 * see TPACPI_FAN_WR_ACPI_SFAN
5760 * EC 0x2f (HFSP) not available if GFAN exists
5762 * TPACPI_FAN_WR_ACPI_SFAN:
5763 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5765 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5766 * it for writing.
5768 * TPACPI_FAN_WR_TPEC:
5769 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5770 * Supported on almost all ThinkPads
5772 * Fan speed changes of any sort (including those caused by the
5773 * disengaged mode) are usually done slowly by the firmware as the
5774 * maximum ammount of fan duty cycle change per second seems to be
5775 * limited.
5777 * Reading is not available if GFAN exists.
5778 * Writing is not available if SFAN exists.
5780 * Bits
5781 * 7 automatic mode engaged;
5782 * (default operation mode of the ThinkPad)
5783 * fan level is ignored in this mode.
5784 * 6 full speed mode (takes precedence over bit 7);
5785 * not available on all thinkpads. May disable
5786 * the tachometer while the fan controller ramps up
5787 * the speed (which can take up to a few *minutes*).
5788 * Speeds up fan to 100% duty-cycle, which is far above
5789 * the standard RPM levels. It is not impossible that
5790 * it could cause hardware damage.
5791 * 5-3 unused in some models. Extra bits for fan level
5792 * in others, but still useless as all values above
5793 * 7 map to the same speed as level 7 in these models.
5794 * 2-0 fan level (0..7 usually)
5795 * 0x00 = stop
5796 * 0x07 = max (set when temperatures critical)
5797 * Some ThinkPads may have other levels, see
5798 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5800 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5801 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5802 * does so, its initial value is meaningless (0x07).
5804 * For firmware bugs, refer to:
5805 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5807 * ----
5809 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5810 * Main fan tachometer reading (in RPM)
5812 * This register is present on all ThinkPads with a new-style EC, and
5813 * it is known not to be present on the A21m/e, and T22, as there is
5814 * something else in offset 0x84 according to the ACPI DSDT. Other
5815 * ThinkPads from this same time period (and earlier) probably lack the
5816 * tachometer as well.
5818 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5819 * was never fixed by IBM to report the EC firmware version string
5820 * probably support the tachometer (like the early X models), so
5821 * detecting it is quite hard. We need more data to know for sure.
5823 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5824 * might result.
5826 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5827 * mode.
5829 * For firmware bugs, refer to:
5830 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5832 * TPACPI_FAN_WR_ACPI_FANS:
5833 * ThinkPad X31, X40, X41. Not available in the X60.
5835 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5836 * high speed. ACPI DSDT seems to map these three speeds to levels
5837 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5838 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5840 * The speeds are stored on handles
5841 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5843 * There are three default speed sets, acessible as handles:
5844 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5846 * ACPI DSDT switches which set is in use depending on various
5847 * factors.
5849 * TPACPI_FAN_WR_TPEC is also available and should be used to
5850 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5851 * but the ACPI tables just mention level 7.
5854 enum { /* Fan control constants */
5855 fan_status_offset = 0x2f, /* EC register 0x2f */
5856 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5857 * 0x84 must be read before 0x85 */
5859 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5860 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5862 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5865 enum fan_status_access_mode {
5866 TPACPI_FAN_NONE = 0, /* No fan status or control */
5867 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5868 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5871 enum fan_control_access_mode {
5872 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5873 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5874 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5875 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5878 enum fan_control_commands {
5879 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5880 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5881 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5882 * and also watchdog cmd */
5885 static int fan_control_allowed;
5887 static enum fan_status_access_mode fan_status_access_mode;
5888 static enum fan_control_access_mode fan_control_access_mode;
5889 static enum fan_control_commands fan_control_commands;
5891 static u8 fan_control_initial_status;
5892 static u8 fan_control_desired_level;
5893 static u8 fan_control_resume_level;
5894 static int fan_watchdog_maxinterval;
5896 static struct mutex fan_mutex;
5898 static void fan_watchdog_fire(struct work_struct *ignored);
5899 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5901 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5902 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5903 "\\FSPD", /* 600e/x, 770e, 770x */
5904 ); /* all others */
5905 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5906 "JFNS", /* 770x-JL */
5907 ); /* all others */
5910 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5911 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5912 * be in auto mode (0x80).
5914 * This is corrected by any write to HFSP either by the driver, or
5915 * by the firmware.
5917 * We assume 0x07 really means auto mode while this quirk is active,
5918 * as this is far more likely than the ThinkPad being in level 7,
5919 * which is only used by the firmware during thermal emergencies.
5922 static void fan_quirk1_detect(void)
5924 /* In some ThinkPads, neither the EC nor the ACPI
5925 * DSDT initialize the HFSP register, and it ends up
5926 * being initially set to 0x07 when it *could* be
5927 * either 0x07 or 0x80.
5929 * Enable for TP-1Y (T43), TP-78 (R51e),
5930 * TP-76 (R52), TP-70 (T43, R52), which are known
5931 * to be buggy. */
5932 if (fan_control_initial_status == 0x07) {
5933 switch (thinkpad_id.ec_model) {
5934 case 0x5931: /* TP-1Y */
5935 case 0x3837: /* TP-78 */
5936 case 0x3637: /* TP-76 */
5937 case 0x3037: /* TP-70 */
5938 printk(TPACPI_NOTICE
5939 "fan_init: initial fan status is unknown, "
5940 "assuming it is in auto mode\n");
5941 tp_features.fan_ctrl_status_undef = 1;
5947 static void fan_quirk1_handle(u8 *fan_status)
5949 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5950 if (*fan_status != fan_control_initial_status) {
5951 /* something changed the HFSP regisnter since
5952 * driver init time, so it is not undefined
5953 * anymore */
5954 tp_features.fan_ctrl_status_undef = 0;
5955 } else {
5956 /* Return most likely status. In fact, it
5957 * might be the only possible status */
5958 *fan_status = TP_EC_FAN_AUTO;
5964 * Call with fan_mutex held
5966 static void fan_update_desired_level(u8 status)
5968 if ((status &
5969 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5970 if (status > 7)
5971 fan_control_desired_level = 7;
5972 else
5973 fan_control_desired_level = status;
5977 static int fan_get_status(u8 *status)
5979 u8 s;
5981 /* TODO:
5982 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5984 switch (fan_status_access_mode) {
5985 case TPACPI_FAN_RD_ACPI_GFAN:
5986 /* 570, 600e/x, 770e, 770x */
5988 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5989 return -EIO;
5991 if (likely(status))
5992 *status = s & 0x07;
5994 break;
5996 case TPACPI_FAN_RD_TPEC:
5997 /* all except 570, 600e/x, 770e, 770x */
5998 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5999 return -EIO;
6001 if (likely(status)) {
6002 *status = s;
6003 fan_quirk1_handle(status);
6006 break;
6008 default:
6009 return -ENXIO;
6012 return 0;
6015 static int fan_get_status_safe(u8 *status)
6017 int rc;
6018 u8 s;
6020 if (mutex_lock_killable(&fan_mutex))
6021 return -ERESTARTSYS;
6022 rc = fan_get_status(&s);
6023 if (!rc)
6024 fan_update_desired_level(s);
6025 mutex_unlock(&fan_mutex);
6027 if (status)
6028 *status = s;
6030 return rc;
6033 static int fan_get_speed(unsigned int *speed)
6035 u8 hi, lo;
6037 switch (fan_status_access_mode) {
6038 case TPACPI_FAN_RD_TPEC:
6039 /* all except 570, 600e/x, 770e, 770x */
6040 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6041 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6042 return -EIO;
6044 if (likely(speed))
6045 *speed = (hi << 8) | lo;
6047 break;
6049 default:
6050 return -ENXIO;
6053 return 0;
6056 static int fan_set_level(int level)
6058 if (!fan_control_allowed)
6059 return -EPERM;
6061 switch (fan_control_access_mode) {
6062 case TPACPI_FAN_WR_ACPI_SFAN:
6063 if (level >= 0 && level <= 7) {
6064 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6065 return -EIO;
6066 } else
6067 return -EINVAL;
6068 break;
6070 case TPACPI_FAN_WR_ACPI_FANS:
6071 case TPACPI_FAN_WR_TPEC:
6072 if (!(level & TP_EC_FAN_AUTO) &&
6073 !(level & TP_EC_FAN_FULLSPEED) &&
6074 ((level < 0) || (level > 7)))
6075 return -EINVAL;
6077 /* safety net should the EC not support AUTO
6078 * or FULLSPEED mode bits and just ignore them */
6079 if (level & TP_EC_FAN_FULLSPEED)
6080 level |= 7; /* safety min speed 7 */
6081 else if (level & TP_EC_FAN_AUTO)
6082 level |= 4; /* safety min speed 4 */
6084 if (!acpi_ec_write(fan_status_offset, level))
6085 return -EIO;
6086 else
6087 tp_features.fan_ctrl_status_undef = 0;
6088 break;
6090 default:
6091 return -ENXIO;
6093 return 0;
6096 static int fan_set_level_safe(int level)
6098 int rc;
6100 if (!fan_control_allowed)
6101 return -EPERM;
6103 if (mutex_lock_killable(&fan_mutex))
6104 return -ERESTARTSYS;
6106 if (level == TPACPI_FAN_LAST_LEVEL)
6107 level = fan_control_desired_level;
6109 rc = fan_set_level(level);
6110 if (!rc)
6111 fan_update_desired_level(level);
6113 mutex_unlock(&fan_mutex);
6114 return rc;
6117 static int fan_set_enable(void)
6119 u8 s;
6120 int rc;
6122 if (!fan_control_allowed)
6123 return -EPERM;
6125 if (mutex_lock_killable(&fan_mutex))
6126 return -ERESTARTSYS;
6128 switch (fan_control_access_mode) {
6129 case TPACPI_FAN_WR_ACPI_FANS:
6130 case TPACPI_FAN_WR_TPEC:
6131 rc = fan_get_status(&s);
6132 if (rc < 0)
6133 break;
6135 /* Don't go out of emergency fan mode */
6136 if (s != 7) {
6137 s &= 0x07;
6138 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6141 if (!acpi_ec_write(fan_status_offset, s))
6142 rc = -EIO;
6143 else {
6144 tp_features.fan_ctrl_status_undef = 0;
6145 rc = 0;
6147 break;
6149 case TPACPI_FAN_WR_ACPI_SFAN:
6150 rc = fan_get_status(&s);
6151 if (rc < 0)
6152 break;
6154 s &= 0x07;
6156 /* Set fan to at least level 4 */
6157 s |= 4;
6159 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6160 rc = -EIO;
6161 else
6162 rc = 0;
6163 break;
6165 default:
6166 rc = -ENXIO;
6169 mutex_unlock(&fan_mutex);
6170 return rc;
6173 static int fan_set_disable(void)
6175 int rc;
6177 if (!fan_control_allowed)
6178 return -EPERM;
6180 if (mutex_lock_killable(&fan_mutex))
6181 return -ERESTARTSYS;
6183 rc = 0;
6184 switch (fan_control_access_mode) {
6185 case TPACPI_FAN_WR_ACPI_FANS:
6186 case TPACPI_FAN_WR_TPEC:
6187 if (!acpi_ec_write(fan_status_offset, 0x00))
6188 rc = -EIO;
6189 else {
6190 fan_control_desired_level = 0;
6191 tp_features.fan_ctrl_status_undef = 0;
6193 break;
6195 case TPACPI_FAN_WR_ACPI_SFAN:
6196 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6197 rc = -EIO;
6198 else
6199 fan_control_desired_level = 0;
6200 break;
6202 default:
6203 rc = -ENXIO;
6207 mutex_unlock(&fan_mutex);
6208 return rc;
6211 static int fan_set_speed(int speed)
6213 int rc;
6215 if (!fan_control_allowed)
6216 return -EPERM;
6218 if (mutex_lock_killable(&fan_mutex))
6219 return -ERESTARTSYS;
6221 rc = 0;
6222 switch (fan_control_access_mode) {
6223 case TPACPI_FAN_WR_ACPI_FANS:
6224 if (speed >= 0 && speed <= 65535) {
6225 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6226 speed, speed, speed))
6227 rc = -EIO;
6228 } else
6229 rc = -EINVAL;
6230 break;
6232 default:
6233 rc = -ENXIO;
6236 mutex_unlock(&fan_mutex);
6237 return rc;
6240 static void fan_watchdog_reset(void)
6242 static int fan_watchdog_active;
6244 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6245 return;
6247 if (fan_watchdog_active)
6248 cancel_delayed_work(&fan_watchdog_task);
6250 if (fan_watchdog_maxinterval > 0 &&
6251 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6252 fan_watchdog_active = 1;
6253 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6254 msecs_to_jiffies(fan_watchdog_maxinterval
6255 * 1000))) {
6256 printk(TPACPI_ERR
6257 "failed to queue the fan watchdog, "
6258 "watchdog will not trigger\n");
6260 } else
6261 fan_watchdog_active = 0;
6264 static void fan_watchdog_fire(struct work_struct *ignored)
6266 int rc;
6268 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6269 return;
6271 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6272 rc = fan_set_enable();
6273 if (rc < 0) {
6274 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6275 "will try again later...\n", -rc);
6276 /* reschedule for later */
6277 fan_watchdog_reset();
6282 * SYSFS fan layout: hwmon compatible (device)
6284 * pwm*_enable:
6285 * 0: "disengaged" mode
6286 * 1: manual mode
6287 * 2: native EC "auto" mode (recommended, hardware default)
6289 * pwm*: set speed in manual mode, ignored otherwise.
6290 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6291 * interpolation.
6293 * fan*_input: tachometer reading, RPM
6296 * SYSFS fan layout: extensions
6298 * fan_watchdog (driver):
6299 * fan watchdog interval in seconds, 0 disables (default), max 120
6302 /* sysfs fan pwm1_enable ----------------------------------------------- */
6303 static ssize_t fan_pwm1_enable_show(struct device *dev,
6304 struct device_attribute *attr,
6305 char *buf)
6307 int res, mode;
6308 u8 status;
6310 res = fan_get_status_safe(&status);
6311 if (res)
6312 return res;
6314 if (status & TP_EC_FAN_FULLSPEED) {
6315 mode = 0;
6316 } else if (status & TP_EC_FAN_AUTO) {
6317 mode = 2;
6318 } else
6319 mode = 1;
6321 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6324 static ssize_t fan_pwm1_enable_store(struct device *dev,
6325 struct device_attribute *attr,
6326 const char *buf, size_t count)
6328 unsigned long t;
6329 int res, level;
6331 if (parse_strtoul(buf, 2, &t))
6332 return -EINVAL;
6334 switch (t) {
6335 case 0:
6336 level = TP_EC_FAN_FULLSPEED;
6337 break;
6338 case 1:
6339 level = TPACPI_FAN_LAST_LEVEL;
6340 break;
6341 case 2:
6342 level = TP_EC_FAN_AUTO;
6343 break;
6344 case 3:
6345 /* reserved for software-controlled auto mode */
6346 return -ENOSYS;
6347 default:
6348 return -EINVAL;
6351 res = fan_set_level_safe(level);
6352 if (res == -ENXIO)
6353 return -EINVAL;
6354 else if (res < 0)
6355 return res;
6357 fan_watchdog_reset();
6359 return count;
6362 static struct device_attribute dev_attr_fan_pwm1_enable =
6363 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6364 fan_pwm1_enable_show, fan_pwm1_enable_store);
6366 /* sysfs fan pwm1 ------------------------------------------------------ */
6367 static ssize_t fan_pwm1_show(struct device *dev,
6368 struct device_attribute *attr,
6369 char *buf)
6371 int res;
6372 u8 status;
6374 res = fan_get_status_safe(&status);
6375 if (res)
6376 return res;
6378 if ((status &
6379 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6380 status = fan_control_desired_level;
6382 if (status > 7)
6383 status = 7;
6385 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6388 static ssize_t fan_pwm1_store(struct device *dev,
6389 struct device_attribute *attr,
6390 const char *buf, size_t count)
6392 unsigned long s;
6393 int rc;
6394 u8 status, newlevel;
6396 if (parse_strtoul(buf, 255, &s))
6397 return -EINVAL;
6399 /* scale down from 0-255 to 0-7 */
6400 newlevel = (s >> 5) & 0x07;
6402 if (mutex_lock_killable(&fan_mutex))
6403 return -ERESTARTSYS;
6405 rc = fan_get_status(&status);
6406 if (!rc && (status &
6407 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6408 rc = fan_set_level(newlevel);
6409 if (rc == -ENXIO)
6410 rc = -EINVAL;
6411 else if (!rc) {
6412 fan_update_desired_level(newlevel);
6413 fan_watchdog_reset();
6417 mutex_unlock(&fan_mutex);
6418 return (rc)? rc : count;
6421 static struct device_attribute dev_attr_fan_pwm1 =
6422 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6423 fan_pwm1_show, fan_pwm1_store);
6425 /* sysfs fan fan1_input ------------------------------------------------ */
6426 static ssize_t fan_fan1_input_show(struct device *dev,
6427 struct device_attribute *attr,
6428 char *buf)
6430 int res;
6431 unsigned int speed;
6433 res = fan_get_speed(&speed);
6434 if (res < 0)
6435 return res;
6437 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6440 static struct device_attribute dev_attr_fan_fan1_input =
6441 __ATTR(fan1_input, S_IRUGO,
6442 fan_fan1_input_show, NULL);
6444 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6445 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6446 char *buf)
6448 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6451 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6452 const char *buf, size_t count)
6454 unsigned long t;
6456 if (parse_strtoul(buf, 120, &t))
6457 return -EINVAL;
6459 if (!fan_control_allowed)
6460 return -EPERM;
6462 fan_watchdog_maxinterval = t;
6463 fan_watchdog_reset();
6465 return count;
6468 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6469 fan_fan_watchdog_show, fan_fan_watchdog_store);
6471 /* --------------------------------------------------------------------- */
6472 static struct attribute *fan_attributes[] = {
6473 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6474 &dev_attr_fan_fan1_input.attr,
6475 NULL
6478 static const struct attribute_group fan_attr_group = {
6479 .attrs = fan_attributes,
6482 static int __init fan_init(struct ibm_init_struct *iibm)
6484 int rc;
6486 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6488 mutex_init(&fan_mutex);
6489 fan_status_access_mode = TPACPI_FAN_NONE;
6490 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6491 fan_control_commands = 0;
6492 fan_watchdog_maxinterval = 0;
6493 tp_features.fan_ctrl_status_undef = 0;
6494 fan_control_desired_level = 7;
6496 TPACPI_ACPIHANDLE_INIT(fans);
6497 TPACPI_ACPIHANDLE_INIT(gfan);
6498 TPACPI_ACPIHANDLE_INIT(sfan);
6500 if (gfan_handle) {
6501 /* 570, 600e/x, 770e, 770x */
6502 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6503 } else {
6504 /* all other ThinkPads: note that even old-style
6505 * ThinkPad ECs supports the fan control register */
6506 if (likely(acpi_ec_read(fan_status_offset,
6507 &fan_control_initial_status))) {
6508 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6509 fan_quirk1_detect();
6510 } else {
6511 printk(TPACPI_ERR
6512 "ThinkPad ACPI EC access misbehaving, "
6513 "fan status and control unavailable\n");
6514 return 1;
6518 if (sfan_handle) {
6519 /* 570, 770x-JL */
6520 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6521 fan_control_commands |=
6522 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6523 } else {
6524 if (!gfan_handle) {
6525 /* gfan without sfan means no fan control */
6526 /* all other models implement TP EC 0x2f control */
6528 if (fans_handle) {
6529 /* X31, X40, X41 */
6530 fan_control_access_mode =
6531 TPACPI_FAN_WR_ACPI_FANS;
6532 fan_control_commands |=
6533 TPACPI_FAN_CMD_SPEED |
6534 TPACPI_FAN_CMD_LEVEL |
6535 TPACPI_FAN_CMD_ENABLE;
6536 } else {
6537 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6538 fan_control_commands |=
6539 TPACPI_FAN_CMD_LEVEL |
6540 TPACPI_FAN_CMD_ENABLE;
6545 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6546 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6547 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6548 fan_status_access_mode, fan_control_access_mode);
6550 /* fan control master switch */
6551 if (!fan_control_allowed) {
6552 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6553 fan_control_commands = 0;
6554 dbg_printk(TPACPI_DBG_INIT,
6555 "fan control features disabled by parameter\n");
6558 /* update fan_control_desired_level */
6559 if (fan_status_access_mode != TPACPI_FAN_NONE)
6560 fan_get_status_safe(NULL);
6562 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6563 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6564 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6565 &fan_attr_group);
6566 if (rc < 0)
6567 return rc;
6569 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6570 &driver_attr_fan_watchdog);
6571 if (rc < 0) {
6572 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6573 &fan_attr_group);
6574 return rc;
6576 return 0;
6577 } else
6578 return 1;
6581 static void fan_exit(void)
6583 vdbg_printk(TPACPI_DBG_EXIT,
6584 "cancelling any pending fan watchdog tasks\n");
6586 /* FIXME: can we really do this unconditionally? */
6587 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6588 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6589 &driver_attr_fan_watchdog);
6591 cancel_delayed_work(&fan_watchdog_task);
6592 flush_workqueue(tpacpi_wq);
6595 static void fan_suspend(pm_message_t state)
6597 int rc;
6599 if (!fan_control_allowed)
6600 return;
6602 /* Store fan status in cache */
6603 fan_control_resume_level = 0;
6604 rc = fan_get_status_safe(&fan_control_resume_level);
6605 if (rc < 0)
6606 printk(TPACPI_NOTICE
6607 "failed to read fan level for later "
6608 "restore during resume: %d\n", rc);
6610 /* if it is undefined, don't attempt to restore it.
6611 * KEEP THIS LAST */
6612 if (tp_features.fan_ctrl_status_undef)
6613 fan_control_resume_level = 0;
6616 static void fan_resume(void)
6618 u8 current_level = 7;
6619 bool do_set = false;
6620 int rc;
6622 /* DSDT *always* updates status on resume */
6623 tp_features.fan_ctrl_status_undef = 0;
6625 if (!fan_control_allowed ||
6626 !fan_control_resume_level ||
6627 (fan_get_status_safe(&current_level) < 0))
6628 return;
6630 switch (fan_control_access_mode) {
6631 case TPACPI_FAN_WR_ACPI_SFAN:
6632 /* never decrease fan level */
6633 do_set = (fan_control_resume_level > current_level);
6634 break;
6635 case TPACPI_FAN_WR_ACPI_FANS:
6636 case TPACPI_FAN_WR_TPEC:
6637 /* never decrease fan level, scale is:
6638 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6640 * We expect the firmware to set either 7 or AUTO, but we
6641 * handle FULLSPEED out of paranoia.
6643 * So, we can safely only restore FULLSPEED or 7, anything
6644 * else could slow the fan. Restoring AUTO is useless, at
6645 * best that's exactly what the DSDT already set (it is the
6646 * slower it uses).
6648 * Always keep in mind that the DSDT *will* have set the
6649 * fans to what the vendor supposes is the best level. We
6650 * muck with it only to speed the fan up.
6652 if (fan_control_resume_level != 7 &&
6653 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6654 return;
6655 else
6656 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6657 (current_level != fan_control_resume_level);
6658 break;
6659 default:
6660 return;
6662 if (do_set) {
6663 printk(TPACPI_NOTICE
6664 "restoring fan level to 0x%02x\n",
6665 fan_control_resume_level);
6666 rc = fan_set_level_safe(fan_control_resume_level);
6667 if (rc < 0)
6668 printk(TPACPI_NOTICE
6669 "failed to restore fan level: %d\n", rc);
6673 static int fan_read(char *p)
6675 int len = 0;
6676 int rc;
6677 u8 status;
6678 unsigned int speed = 0;
6680 switch (fan_status_access_mode) {
6681 case TPACPI_FAN_RD_ACPI_GFAN:
6682 /* 570, 600e/x, 770e, 770x */
6683 rc = fan_get_status_safe(&status);
6684 if (rc < 0)
6685 return rc;
6687 len += sprintf(p + len, "status:\t\t%s\n"
6688 "level:\t\t%d\n",
6689 (status != 0) ? "enabled" : "disabled", status);
6690 break;
6692 case TPACPI_FAN_RD_TPEC:
6693 /* all except 570, 600e/x, 770e, 770x */
6694 rc = fan_get_status_safe(&status);
6695 if (rc < 0)
6696 return rc;
6698 len += sprintf(p + len, "status:\t\t%s\n",
6699 (status != 0) ? "enabled" : "disabled");
6701 rc = fan_get_speed(&speed);
6702 if (rc < 0)
6703 return rc;
6705 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6707 if (status & TP_EC_FAN_FULLSPEED)
6708 /* Disengaged mode takes precedence */
6709 len += sprintf(p + len, "level:\t\tdisengaged\n");
6710 else if (status & TP_EC_FAN_AUTO)
6711 len += sprintf(p + len, "level:\t\tauto\n");
6712 else
6713 len += sprintf(p + len, "level:\t\t%d\n", status);
6714 break;
6716 case TPACPI_FAN_NONE:
6717 default:
6718 len += sprintf(p + len, "status:\t\tnot supported\n");
6721 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6722 len += sprintf(p + len, "commands:\tlevel <level>");
6724 switch (fan_control_access_mode) {
6725 case TPACPI_FAN_WR_ACPI_SFAN:
6726 len += sprintf(p + len, " (<level> is 0-7)\n");
6727 break;
6729 default:
6730 len += sprintf(p + len, " (<level> is 0-7, "
6731 "auto, disengaged, full-speed)\n");
6732 break;
6736 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6737 len += sprintf(p + len, "commands:\tenable, disable\n"
6738 "commands:\twatchdog <timeout> (<timeout> "
6739 "is 0 (off), 1-120 (seconds))\n");
6741 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6742 len += sprintf(p + len, "commands:\tspeed <speed>"
6743 " (<speed> is 0-65535)\n");
6745 return len;
6748 static int fan_write_cmd_level(const char *cmd, int *rc)
6750 int level;
6752 if (strlencmp(cmd, "level auto") == 0)
6753 level = TP_EC_FAN_AUTO;
6754 else if ((strlencmp(cmd, "level disengaged") == 0) |
6755 (strlencmp(cmd, "level full-speed") == 0))
6756 level = TP_EC_FAN_FULLSPEED;
6757 else if (sscanf(cmd, "level %d", &level) != 1)
6758 return 0;
6760 *rc = fan_set_level_safe(level);
6761 if (*rc == -ENXIO)
6762 printk(TPACPI_ERR "level command accepted for unsupported "
6763 "access mode %d", fan_control_access_mode);
6765 return 1;
6768 static int fan_write_cmd_enable(const char *cmd, int *rc)
6770 if (strlencmp(cmd, "enable") != 0)
6771 return 0;
6773 *rc = fan_set_enable();
6774 if (*rc == -ENXIO)
6775 printk(TPACPI_ERR "enable command accepted for unsupported "
6776 "access mode %d", fan_control_access_mode);
6778 return 1;
6781 static int fan_write_cmd_disable(const char *cmd, int *rc)
6783 if (strlencmp(cmd, "disable") != 0)
6784 return 0;
6786 *rc = fan_set_disable();
6787 if (*rc == -ENXIO)
6788 printk(TPACPI_ERR "disable command accepted for unsupported "
6789 "access mode %d", fan_control_access_mode);
6791 return 1;
6794 static int fan_write_cmd_speed(const char *cmd, int *rc)
6796 int speed;
6798 /* TODO:
6799 * Support speed <low> <medium> <high> ? */
6801 if (sscanf(cmd, "speed %d", &speed) != 1)
6802 return 0;
6804 *rc = fan_set_speed(speed);
6805 if (*rc == -ENXIO)
6806 printk(TPACPI_ERR "speed command accepted for unsupported "
6807 "access mode %d", fan_control_access_mode);
6809 return 1;
6812 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6814 int interval;
6816 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6817 return 0;
6819 if (interval < 0 || interval > 120)
6820 *rc = -EINVAL;
6821 else
6822 fan_watchdog_maxinterval = interval;
6824 return 1;
6827 static int fan_write(char *buf)
6829 char *cmd;
6830 int rc = 0;
6832 while (!rc && (cmd = next_cmd(&buf))) {
6833 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6834 fan_write_cmd_level(cmd, &rc)) &&
6835 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6836 (fan_write_cmd_enable(cmd, &rc) ||
6837 fan_write_cmd_disable(cmd, &rc) ||
6838 fan_write_cmd_watchdog(cmd, &rc))) &&
6839 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6840 fan_write_cmd_speed(cmd, &rc))
6842 rc = -EINVAL;
6843 else if (!rc)
6844 fan_watchdog_reset();
6847 return rc;
6850 static struct ibm_struct fan_driver_data = {
6851 .name = "fan",
6852 .read = fan_read,
6853 .write = fan_write,
6854 .exit = fan_exit,
6855 .suspend = fan_suspend,
6856 .resume = fan_resume,
6859 /****************************************************************************
6860 ****************************************************************************
6862 * Infrastructure
6864 ****************************************************************************
6865 ****************************************************************************/
6867 /* sysfs name ---------------------------------------------------------- */
6868 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6869 struct device_attribute *attr,
6870 char *buf)
6872 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6875 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6876 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6878 /* --------------------------------------------------------------------- */
6880 /* /proc support */
6881 static struct proc_dir_entry *proc_dir;
6884 * Module and infrastructure proble, init and exit handling
6887 static int force_load;
6889 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6890 static const char * __init str_supported(int is_supported)
6892 static char text_unsupported[] __initdata = "not supported";
6894 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6896 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6898 static void ibm_exit(struct ibm_struct *ibm)
6900 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6902 list_del_init(&ibm->all_drivers);
6904 if (ibm->flags.acpi_notify_installed) {
6905 dbg_printk(TPACPI_DBG_EXIT,
6906 "%s: acpi_remove_notify_handler\n", ibm->name);
6907 BUG_ON(!ibm->acpi);
6908 acpi_remove_notify_handler(*ibm->acpi->handle,
6909 ibm->acpi->type,
6910 dispatch_acpi_notify);
6911 ibm->flags.acpi_notify_installed = 0;
6912 ibm->flags.acpi_notify_installed = 0;
6915 if (ibm->flags.proc_created) {
6916 dbg_printk(TPACPI_DBG_EXIT,
6917 "%s: remove_proc_entry\n", ibm->name);
6918 remove_proc_entry(ibm->name, proc_dir);
6919 ibm->flags.proc_created = 0;
6922 if (ibm->flags.acpi_driver_registered) {
6923 dbg_printk(TPACPI_DBG_EXIT,
6924 "%s: acpi_bus_unregister_driver\n", ibm->name);
6925 BUG_ON(!ibm->acpi);
6926 acpi_bus_unregister_driver(ibm->acpi->driver);
6927 kfree(ibm->acpi->driver);
6928 ibm->acpi->driver = NULL;
6929 ibm->flags.acpi_driver_registered = 0;
6932 if (ibm->flags.init_called && ibm->exit) {
6933 ibm->exit();
6934 ibm->flags.init_called = 0;
6937 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6940 static int __init ibm_init(struct ibm_init_struct *iibm)
6942 int ret;
6943 struct ibm_struct *ibm = iibm->data;
6944 struct proc_dir_entry *entry;
6946 BUG_ON(ibm == NULL);
6948 INIT_LIST_HEAD(&ibm->all_drivers);
6950 if (ibm->flags.experimental && !experimental)
6951 return 0;
6953 dbg_printk(TPACPI_DBG_INIT,
6954 "probing for %s\n", ibm->name);
6956 if (iibm->init) {
6957 ret = iibm->init(iibm);
6958 if (ret > 0)
6959 return 0; /* probe failed */
6960 if (ret)
6961 return ret;
6963 ibm->flags.init_called = 1;
6966 if (ibm->acpi) {
6967 if (ibm->acpi->hid) {
6968 ret = register_tpacpi_subdriver(ibm);
6969 if (ret)
6970 goto err_out;
6973 if (ibm->acpi->notify) {
6974 ret = setup_acpi_notify(ibm);
6975 if (ret == -ENODEV) {
6976 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6977 ibm->name);
6978 ret = 0;
6979 goto err_out;
6981 if (ret < 0)
6982 goto err_out;
6986 dbg_printk(TPACPI_DBG_INIT,
6987 "%s installed\n", ibm->name);
6989 if (ibm->read) {
6990 entry = create_proc_entry(ibm->name,
6991 S_IFREG | S_IRUGO | S_IWUSR,
6992 proc_dir);
6993 if (!entry) {
6994 printk(TPACPI_ERR "unable to create proc entry %s\n",
6995 ibm->name);
6996 ret = -ENODEV;
6997 goto err_out;
6999 entry->owner = THIS_MODULE;
7000 entry->data = ibm;
7001 entry->read_proc = &dispatch_procfs_read;
7002 if (ibm->write)
7003 entry->write_proc = &dispatch_procfs_write;
7004 ibm->flags.proc_created = 1;
7007 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7009 return 0;
7011 err_out:
7012 dbg_printk(TPACPI_DBG_INIT,
7013 "%s: at error exit path with result %d\n",
7014 ibm->name, ret);
7016 ibm_exit(ibm);
7017 return (ret < 0)? ret : 0;
7020 /* Probing */
7022 /* returns 0 - probe ok, or < 0 - probe error.
7023 * Probe ok doesn't mean thinkpad found.
7024 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7025 static int __must_check __init get_thinkpad_model_data(
7026 struct thinkpad_id_data *tp)
7028 const struct dmi_device *dev = NULL;
7029 char ec_fw_string[18];
7030 char const *s;
7032 if (!tp)
7033 return -EINVAL;
7035 memset(tp, 0, sizeof(*tp));
7037 if (dmi_name_in_vendors("IBM"))
7038 tp->vendor = PCI_VENDOR_ID_IBM;
7039 else if (dmi_name_in_vendors("LENOVO"))
7040 tp->vendor = PCI_VENDOR_ID_LENOVO;
7041 else
7042 return 0;
7044 s = dmi_get_system_info(DMI_BIOS_VERSION);
7045 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7046 if (s && !tp->bios_version_str)
7047 return -ENOMEM;
7048 if (!tp->bios_version_str)
7049 return 0;
7050 tp->bios_model = tp->bios_version_str[0]
7051 | (tp->bios_version_str[1] << 8);
7054 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7055 * X32 or newer, all Z series; Some models must have an
7056 * up-to-date BIOS or they will not be detected.
7058 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7060 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7061 if (sscanf(dev->name,
7062 "IBM ThinkPad Embedded Controller -[%17c",
7063 ec_fw_string) == 1) {
7064 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7065 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7067 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7068 if (!tp->ec_version_str)
7069 return -ENOMEM;
7070 tp->ec_model = ec_fw_string[0]
7071 | (ec_fw_string[1] << 8);
7072 break;
7076 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7077 if (s && !strnicmp(s, "ThinkPad", 8)) {
7078 tp->model_str = kstrdup(s, GFP_KERNEL);
7079 if (!tp->model_str)
7080 return -ENOMEM;
7083 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7084 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7085 if (s && !tp->nummodel_str)
7086 return -ENOMEM;
7088 return 0;
7091 static int __init probe_for_thinkpad(void)
7093 int is_thinkpad;
7095 if (acpi_disabled)
7096 return -ENODEV;
7099 * Non-ancient models have better DMI tagging, but very old models
7100 * don't.
7102 is_thinkpad = (thinkpad_id.model_str != NULL);
7104 /* ec is required because many other handles are relative to it */
7105 TPACPI_ACPIHANDLE_INIT(ec);
7106 if (!ec_handle) {
7107 if (is_thinkpad)
7108 printk(TPACPI_ERR
7109 "Not yet supported ThinkPad detected!\n");
7110 return -ENODEV;
7114 * Risks a regression on very old machines, but reduces potential
7115 * false positives a damn great deal
7117 if (!is_thinkpad)
7118 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7120 if (!is_thinkpad && !force_load)
7121 return -ENODEV;
7123 return 0;
7127 /* Module init, exit, parameters */
7129 static struct ibm_init_struct ibms_init[] __initdata = {
7131 .init = thinkpad_acpi_driver_init,
7132 .data = &thinkpad_acpi_driver_data,
7135 .init = hotkey_init,
7136 .data = &hotkey_driver_data,
7139 .init = bluetooth_init,
7140 .data = &bluetooth_driver_data,
7143 .init = wan_init,
7144 .data = &wan_driver_data,
7147 .init = uwb_init,
7148 .data = &uwb_driver_data,
7150 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7152 .init = video_init,
7153 .data = &video_driver_data,
7155 #endif
7157 .init = light_init,
7158 .data = &light_driver_data,
7160 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7162 .init = dock_init,
7163 .data = &dock_driver_data[0],
7166 .init = dock_init2,
7167 .data = &dock_driver_data[1],
7169 #endif
7170 #ifdef CONFIG_THINKPAD_ACPI_BAY
7172 .init = bay_init,
7173 .data = &bay_driver_data,
7175 #endif
7177 .init = cmos_init,
7178 .data = &cmos_driver_data,
7181 .init = led_init,
7182 .data = &led_driver_data,
7185 .init = beep_init,
7186 .data = &beep_driver_data,
7189 .init = thermal_init,
7190 .data = &thermal_driver_data,
7193 .data = &ecdump_driver_data,
7196 .init = brightness_init,
7197 .data = &brightness_driver_data,
7200 .data = &volume_driver_data,
7203 .init = fan_init,
7204 .data = &fan_driver_data,
7208 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7210 unsigned int i;
7211 struct ibm_struct *ibm;
7213 if (!kp || !kp->name || !val)
7214 return -EINVAL;
7216 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7217 ibm = ibms_init[i].data;
7218 WARN_ON(ibm == NULL);
7220 if (!ibm || !ibm->name)
7221 continue;
7223 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7224 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7225 return -ENOSPC;
7226 strcpy(ibms_init[i].param, val);
7227 strcat(ibms_init[i].param, ",");
7228 return 0;
7232 return -EINVAL;
7235 module_param(experimental, int, 0);
7236 MODULE_PARM_DESC(experimental,
7237 "Enables experimental features when non-zero");
7239 module_param_named(debug, dbg_level, uint, 0);
7240 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7242 module_param(force_load, bool, 0);
7243 MODULE_PARM_DESC(force_load,
7244 "Attempts to load the driver even on a "
7245 "mis-identified ThinkPad when true");
7247 module_param_named(fan_control, fan_control_allowed, bool, 0);
7248 MODULE_PARM_DESC(fan_control,
7249 "Enables setting fan parameters features when true");
7251 module_param_named(brightness_mode, brightness_mode, int, 0);
7252 MODULE_PARM_DESC(brightness_mode,
7253 "Selects brightness control strategy: "
7254 "0=auto, 1=EC, 2=CMOS, 3=both");
7256 module_param(brightness_enable, uint, 0);
7257 MODULE_PARM_DESC(brightness_enable,
7258 "Enables backlight control when 1, disables when 0");
7260 module_param(hotkey_report_mode, uint, 0);
7261 MODULE_PARM_DESC(hotkey_report_mode,
7262 "used for backwards compatibility with userspace, "
7263 "see documentation");
7265 #define TPACPI_PARAM(feature) \
7266 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7267 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7268 "at module load, see documentation")
7270 TPACPI_PARAM(hotkey);
7271 TPACPI_PARAM(bluetooth);
7272 TPACPI_PARAM(video);
7273 TPACPI_PARAM(light);
7274 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7275 TPACPI_PARAM(dock);
7276 #endif
7277 #ifdef CONFIG_THINKPAD_ACPI_BAY
7278 TPACPI_PARAM(bay);
7279 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7280 TPACPI_PARAM(cmos);
7281 TPACPI_PARAM(led);
7282 TPACPI_PARAM(beep);
7283 TPACPI_PARAM(ecdump);
7284 TPACPI_PARAM(brightness);
7285 TPACPI_PARAM(volume);
7286 TPACPI_PARAM(fan);
7288 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7289 module_param(dbg_wlswemul, uint, 0);
7290 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7291 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7292 MODULE_PARM_DESC(wlsw_state,
7293 "Initial state of the emulated WLSW switch");
7295 module_param(dbg_bluetoothemul, uint, 0);
7296 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7297 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7298 MODULE_PARM_DESC(bluetooth_state,
7299 "Initial state of the emulated bluetooth switch");
7301 module_param(dbg_wwanemul, uint, 0);
7302 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7303 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7304 MODULE_PARM_DESC(wwan_state,
7305 "Initial state of the emulated WWAN switch");
7307 module_param(dbg_uwbemul, uint, 0);
7308 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7309 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7310 MODULE_PARM_DESC(uwb_state,
7311 "Initial state of the emulated UWB switch");
7312 #endif
7314 static void thinkpad_acpi_module_exit(void)
7316 struct ibm_struct *ibm, *itmp;
7318 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7320 list_for_each_entry_safe_reverse(ibm, itmp,
7321 &tpacpi_all_drivers,
7322 all_drivers) {
7323 ibm_exit(ibm);
7326 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7328 if (tpacpi_inputdev) {
7329 if (tp_features.input_device_registered)
7330 input_unregister_device(tpacpi_inputdev);
7331 else
7332 input_free_device(tpacpi_inputdev);
7335 if (tpacpi_hwmon)
7336 hwmon_device_unregister(tpacpi_hwmon);
7338 if (tp_features.sensors_pdev_attrs_registered)
7339 device_remove_file(&tpacpi_sensors_pdev->dev,
7340 &dev_attr_thinkpad_acpi_pdev_name);
7341 if (tpacpi_sensors_pdev)
7342 platform_device_unregister(tpacpi_sensors_pdev);
7343 if (tpacpi_pdev)
7344 platform_device_unregister(tpacpi_pdev);
7346 if (tp_features.sensors_pdrv_attrs_registered)
7347 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7348 if (tp_features.platform_drv_attrs_registered)
7349 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7351 if (tp_features.sensors_pdrv_registered)
7352 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7354 if (tp_features.platform_drv_registered)
7355 platform_driver_unregister(&tpacpi_pdriver);
7357 if (proc_dir)
7358 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7360 if (tpacpi_wq)
7361 destroy_workqueue(tpacpi_wq);
7363 kfree(thinkpad_id.bios_version_str);
7364 kfree(thinkpad_id.ec_version_str);
7365 kfree(thinkpad_id.model_str);
7369 static int __init thinkpad_acpi_module_init(void)
7371 int ret, i;
7373 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7375 /* Parameter checking */
7376 if (hotkey_report_mode > 2)
7377 return -EINVAL;
7379 /* Driver-level probe */
7381 ret = get_thinkpad_model_data(&thinkpad_id);
7382 if (ret) {
7383 printk(TPACPI_ERR
7384 "unable to get DMI data: %d\n", ret);
7385 thinkpad_acpi_module_exit();
7386 return ret;
7388 ret = probe_for_thinkpad();
7389 if (ret) {
7390 thinkpad_acpi_module_exit();
7391 return ret;
7394 /* Driver initialization */
7396 TPACPI_ACPIHANDLE_INIT(ecrd);
7397 TPACPI_ACPIHANDLE_INIT(ecwr);
7399 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7400 if (!tpacpi_wq) {
7401 thinkpad_acpi_module_exit();
7402 return -ENOMEM;
7405 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7406 if (!proc_dir) {
7407 printk(TPACPI_ERR
7408 "unable to create proc dir " TPACPI_PROC_DIR);
7409 thinkpad_acpi_module_exit();
7410 return -ENODEV;
7412 proc_dir->owner = THIS_MODULE;
7414 ret = platform_driver_register(&tpacpi_pdriver);
7415 if (ret) {
7416 printk(TPACPI_ERR
7417 "unable to register main platform driver\n");
7418 thinkpad_acpi_module_exit();
7419 return ret;
7421 tp_features.platform_drv_registered = 1;
7423 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7424 if (ret) {
7425 printk(TPACPI_ERR
7426 "unable to register hwmon platform driver\n");
7427 thinkpad_acpi_module_exit();
7428 return ret;
7430 tp_features.sensors_pdrv_registered = 1;
7432 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7433 if (!ret) {
7434 tp_features.platform_drv_attrs_registered = 1;
7435 ret = tpacpi_create_driver_attributes(
7436 &tpacpi_hwmon_pdriver.driver);
7438 if (ret) {
7439 printk(TPACPI_ERR
7440 "unable to create sysfs driver attributes\n");
7441 thinkpad_acpi_module_exit();
7442 return ret;
7444 tp_features.sensors_pdrv_attrs_registered = 1;
7447 /* Device initialization */
7448 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7449 NULL, 0);
7450 if (IS_ERR(tpacpi_pdev)) {
7451 ret = PTR_ERR(tpacpi_pdev);
7452 tpacpi_pdev = NULL;
7453 printk(TPACPI_ERR "unable to register platform device\n");
7454 thinkpad_acpi_module_exit();
7455 return ret;
7457 tpacpi_sensors_pdev = platform_device_register_simple(
7458 TPACPI_HWMON_DRVR_NAME,
7459 -1, NULL, 0);
7460 if (IS_ERR(tpacpi_sensors_pdev)) {
7461 ret = PTR_ERR(tpacpi_sensors_pdev);
7462 tpacpi_sensors_pdev = NULL;
7463 printk(TPACPI_ERR
7464 "unable to register hwmon platform device\n");
7465 thinkpad_acpi_module_exit();
7466 return ret;
7468 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7469 &dev_attr_thinkpad_acpi_pdev_name);
7470 if (ret) {
7471 printk(TPACPI_ERR
7472 "unable to create sysfs hwmon device attributes\n");
7473 thinkpad_acpi_module_exit();
7474 return ret;
7476 tp_features.sensors_pdev_attrs_registered = 1;
7477 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7478 if (IS_ERR(tpacpi_hwmon)) {
7479 ret = PTR_ERR(tpacpi_hwmon);
7480 tpacpi_hwmon = NULL;
7481 printk(TPACPI_ERR "unable to register hwmon device\n");
7482 thinkpad_acpi_module_exit();
7483 return ret;
7485 mutex_init(&tpacpi_inputdev_send_mutex);
7486 tpacpi_inputdev = input_allocate_device();
7487 if (!tpacpi_inputdev) {
7488 printk(TPACPI_ERR "unable to allocate input device\n");
7489 thinkpad_acpi_module_exit();
7490 return -ENOMEM;
7491 } else {
7492 /* Prepare input device, but don't register */
7493 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7494 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7495 tpacpi_inputdev->id.bustype = BUS_HOST;
7496 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7497 thinkpad_id.vendor :
7498 PCI_VENDOR_ID_IBM;
7499 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7500 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7502 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7503 ret = ibm_init(&ibms_init[i]);
7504 if (ret >= 0 && *ibms_init[i].param)
7505 ret = ibms_init[i].data->write(ibms_init[i].param);
7506 if (ret < 0) {
7507 thinkpad_acpi_module_exit();
7508 return ret;
7511 ret = input_register_device(tpacpi_inputdev);
7512 if (ret < 0) {
7513 printk(TPACPI_ERR "unable to register input device\n");
7514 thinkpad_acpi_module_exit();
7515 return ret;
7516 } else {
7517 tp_features.input_device_registered = 1;
7520 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7521 return 0;
7524 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7527 * DMI matching for module autoloading
7529 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7530 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7532 * Only models listed in thinkwiki will be supported, so add yours
7533 * if it is not there yet.
7535 #define IBM_BIOS_MODULE_ALIAS(__type) \
7536 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7538 /* Non-ancient thinkpads */
7539 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7540 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7542 /* Ancient thinkpad BIOSes have to be identified by
7543 * BIOS type or model number, and there are far less
7544 * BIOS types than model numbers... */
7545 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7546 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7547 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7549 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7550 MODULE_DESCRIPTION(TPACPI_DESC);
7551 MODULE_VERSION(TPACPI_VERSION);
7552 MODULE_LICENSE("GPL");
7554 module_init(thinkpad_acpi_module_init);
7555 module_exit(thinkpad_acpi_module_exit);