thinkpad-acpi: add missing log levels
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobea34898d25970560e16459e02d42cb04a2c18fb2
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 /* printk headers */
176 #define TPACPI_LOG TPACPI_FILE ": "
177 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
178 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
179 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
180 #define TPACPI_ERR KERN_ERR TPACPI_LOG
181 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
182 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
183 #define TPACPI_INFO KERN_INFO TPACPI_LOG
184 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
186 /* Debugging printk groups */
187 #define TPACPI_DBG_ALL 0xffff
188 #define TPACPI_DBG_INIT 0x0001
189 #define TPACPI_DBG_EXIT 0x0002
191 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
192 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
193 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
196 /****************************************************************************
197 * Driver-wide structs and misc. variables
200 struct ibm_struct;
202 struct tp_acpi_drv_struct {
203 const struct acpi_device_id *hid;
204 struct acpi_driver *driver;
206 void (*notify) (struct ibm_struct *, u32);
207 acpi_handle *handle;
208 u32 type;
209 struct acpi_device *device;
212 struct ibm_struct {
213 char *name;
215 int (*read) (char *);
216 int (*write) (char *);
217 void (*exit) (void);
218 void (*resume) (void);
219 void (*suspend) (pm_message_t state);
220 void (*shutdown) (void);
222 struct list_head all_drivers;
224 struct tp_acpi_drv_struct *acpi;
226 struct {
227 u8 acpi_driver_registered:1;
228 u8 acpi_notify_installed:1;
229 u8 proc_created:1;
230 u8 init_called:1;
231 u8 experimental:1;
232 } flags;
235 struct ibm_init_struct {
236 char param[32];
238 int (*init) (struct ibm_init_struct *);
239 struct ibm_struct *data;
242 static struct {
243 #ifdef CONFIG_THINKPAD_ACPI_BAY
244 u32 bay_status:1;
245 u32 bay_eject:1;
246 u32 bay_status2:1;
247 u32 bay_eject2:1;
248 #endif
249 u32 bluetooth:1;
250 u32 hotkey:1;
251 u32 hotkey_mask:1;
252 u32 hotkey_wlsw:1;
253 u32 hotkey_tablet:1;
254 u32 light:1;
255 u32 light_status:1;
256 u32 bright_16levels:1;
257 u32 bright_acpimode:1;
258 u32 wan:1;
259 u32 uwb:1;
260 u32 fan_ctrl_status_undef:1;
261 u32 input_device_registered:1;
262 u32 platform_drv_registered:1;
263 u32 platform_drv_attrs_registered:1;
264 u32 sensors_pdrv_registered:1;
265 u32 sensors_pdrv_attrs_registered:1;
266 u32 sensors_pdev_attrs_registered:1;
267 u32 hotkey_poll_active:1;
268 } tp_features;
270 static struct {
271 u16 hotkey_mask_ff:1;
272 u16 bright_cmos_ec_unsync:1;
273 } tp_warned;
275 struct thinkpad_id_data {
276 unsigned int vendor; /* ThinkPad vendor:
277 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
279 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
280 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
282 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
283 u16 ec_model;
285 char *model_str; /* ThinkPad T43 */
286 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
288 static struct thinkpad_id_data thinkpad_id;
290 static enum {
291 TPACPI_LIFE_INIT = 0,
292 TPACPI_LIFE_RUNNING,
293 TPACPI_LIFE_EXITING,
294 } tpacpi_lifecycle;
296 static int experimental;
297 static u32 dbg_level;
299 static struct workqueue_struct *tpacpi_wq;
301 enum led_status_t {
302 TPACPI_LED_OFF = 0,
303 TPACPI_LED_ON,
304 TPACPI_LED_BLINK,
307 /* Special LED class that can defer work */
308 struct tpacpi_led_classdev {
309 struct led_classdev led_classdev;
310 struct work_struct work;
311 enum led_status_t new_state;
312 unsigned int led;
315 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
316 static int dbg_wlswemul;
317 static int tpacpi_wlsw_emulstate;
318 static int dbg_bluetoothemul;
319 static int tpacpi_bluetooth_emulstate;
320 static int dbg_wwanemul;
321 static int tpacpi_wwan_emulstate;
322 static int dbg_uwbemul;
323 static int tpacpi_uwb_emulstate;
324 #endif
327 /*************************************************************************
328 * Debugging helpers
331 #define dbg_printk(a_dbg_level, format, arg...) \
332 do { if (dbg_level & (a_dbg_level)) \
333 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
334 } while (0)
336 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
337 #define vdbg_printk dbg_printk
338 static const char *str_supported(int is_supported);
339 #else
340 #define vdbg_printk(a_dbg_level, format, arg...) \
341 do { } while (0)
342 #endif
345 /****************************************************************************
346 ****************************************************************************
348 * ACPI Helpers and device model
350 ****************************************************************************
351 ****************************************************************************/
353 /*************************************************************************
354 * ACPI basic handles
357 static acpi_handle root_handle;
359 #define TPACPI_HANDLE(object, parent, paths...) \
360 static acpi_handle object##_handle; \
361 static acpi_handle *object##_parent = &parent##_handle; \
362 static char *object##_path; \
363 static char *object##_paths[] = { paths }
365 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
366 "\\_SB.PCI.ISA.EC", /* 570 */
367 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
368 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
369 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
370 "\\_SB.PCI0.ICH3.EC0", /* R31 */
371 "\\_SB.PCI0.LPC.EC", /* all others */
374 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
375 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
377 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
378 /* T4x, X31, X40 */
379 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
380 "\\CMS", /* R40, R40e */
381 ); /* all others */
383 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
384 "^HKEY", /* R30, R31 */
385 "HKEY", /* all others */
386 ); /* 570 */
388 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
389 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
390 "\\_SB.PCI0.VID0", /* 770e */
391 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
392 "\\_SB.PCI0.AGP.VID", /* all others */
393 ); /* R30, R31 */
396 /*************************************************************************
397 * ACPI helpers
400 static int acpi_evalf(acpi_handle handle,
401 void *res, char *method, char *fmt, ...)
403 char *fmt0 = fmt;
404 struct acpi_object_list params;
405 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
406 struct acpi_buffer result, *resultp;
407 union acpi_object out_obj;
408 acpi_status status;
409 va_list ap;
410 char res_type;
411 int success;
412 int quiet;
414 if (!*fmt) {
415 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
416 return 0;
419 if (*fmt == 'q') {
420 quiet = 1;
421 fmt++;
422 } else
423 quiet = 0;
425 res_type = *(fmt++);
427 params.count = 0;
428 params.pointer = &in_objs[0];
430 va_start(ap, fmt);
431 while (*fmt) {
432 char c = *(fmt++);
433 switch (c) {
434 case 'd': /* int */
435 in_objs[params.count].integer.value = va_arg(ap, int);
436 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
437 break;
438 /* add more types as needed */
439 default:
440 printk(TPACPI_ERR "acpi_evalf() called "
441 "with invalid format character '%c'\n", c);
442 return 0;
445 va_end(ap);
447 if (res_type != 'v') {
448 result.length = sizeof(out_obj);
449 result.pointer = &out_obj;
450 resultp = &result;
451 } else
452 resultp = NULL;
454 status = acpi_evaluate_object(handle, method, &params, resultp);
456 switch (res_type) {
457 case 'd': /* int */
458 if (res)
459 *(int *)res = out_obj.integer.value;
460 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
461 break;
462 case 'v': /* void */
463 success = status == AE_OK;
464 break;
465 /* add more types as needed */
466 default:
467 printk(TPACPI_ERR "acpi_evalf() called "
468 "with invalid format character '%c'\n", res_type);
469 return 0;
472 if (!success && !quiet)
473 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
474 method, fmt0, status);
476 return success;
479 static int acpi_ec_read(int i, u8 *p)
481 int v;
483 if (ecrd_handle) {
484 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
485 return 0;
486 *p = v;
487 } else {
488 if (ec_read(i, p) < 0)
489 return 0;
492 return 1;
495 static int acpi_ec_write(int i, u8 v)
497 if (ecwr_handle) {
498 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
499 return 0;
500 } else {
501 if (ec_write(i, v) < 0)
502 return 0;
505 return 1;
508 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
509 static int _sta(acpi_handle handle)
511 int status;
513 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
514 status = 0;
516 return status;
518 #endif
520 static int issue_thinkpad_cmos_command(int cmos_cmd)
522 if (!cmos_handle)
523 return -ENXIO;
525 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
526 return -EIO;
528 return 0;
531 /*************************************************************************
532 * ACPI device model
535 #define TPACPI_ACPIHANDLE_INIT(object) \
536 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
537 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
539 static void drv_acpi_handle_init(char *name,
540 acpi_handle *handle, acpi_handle parent,
541 char **paths, int num_paths, char **path)
543 int i;
544 acpi_status status;
546 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
547 name);
549 for (i = 0; i < num_paths; i++) {
550 status = acpi_get_handle(parent, paths[i], handle);
551 if (ACPI_SUCCESS(status)) {
552 *path = paths[i];
553 dbg_printk(TPACPI_DBG_INIT,
554 "Found ACPI handle %s for %s\n",
555 *path, name);
556 return;
560 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
561 name);
562 *handle = NULL;
565 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
567 struct ibm_struct *ibm = data;
569 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
570 return;
572 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
573 return;
575 ibm->acpi->notify(ibm, event);
578 static int __init setup_acpi_notify(struct ibm_struct *ibm)
580 acpi_status status;
581 int rc;
583 BUG_ON(!ibm->acpi);
585 if (!*ibm->acpi->handle)
586 return 0;
588 vdbg_printk(TPACPI_DBG_INIT,
589 "setting up ACPI notify for %s\n", ibm->name);
591 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
592 if (rc < 0) {
593 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
594 ibm->name, rc);
595 return -ENODEV;
598 acpi_driver_data(ibm->acpi->device) = ibm;
599 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
600 TPACPI_ACPI_EVENT_PREFIX,
601 ibm->name);
603 status = acpi_install_notify_handler(*ibm->acpi->handle,
604 ibm->acpi->type, dispatch_acpi_notify, ibm);
605 if (ACPI_FAILURE(status)) {
606 if (status == AE_ALREADY_EXISTS) {
607 printk(TPACPI_NOTICE
608 "another device driver is already "
609 "handling %s events\n", ibm->name);
610 } else {
611 printk(TPACPI_ERR
612 "acpi_install_notify_handler(%s) failed: %d\n",
613 ibm->name, status);
615 return -ENODEV;
617 ibm->flags.acpi_notify_installed = 1;
618 return 0;
621 static int __init tpacpi_device_add(struct acpi_device *device)
623 return 0;
626 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
628 int rc;
630 dbg_printk(TPACPI_DBG_INIT,
631 "registering %s as an ACPI driver\n", ibm->name);
633 BUG_ON(!ibm->acpi);
635 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
636 if (!ibm->acpi->driver) {
637 printk(TPACPI_ERR
638 "failed to allocate memory for ibm->acpi->driver\n");
639 return -ENOMEM;
642 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
643 ibm->acpi->driver->ids = ibm->acpi->hid;
645 ibm->acpi->driver->ops.add = &tpacpi_device_add;
647 rc = acpi_bus_register_driver(ibm->acpi->driver);
648 if (rc < 0) {
649 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
650 ibm->name, rc);
651 kfree(ibm->acpi->driver);
652 ibm->acpi->driver = NULL;
653 } else if (!rc)
654 ibm->flags.acpi_driver_registered = 1;
656 return rc;
660 /****************************************************************************
661 ****************************************************************************
663 * Procfs Helpers
665 ****************************************************************************
666 ****************************************************************************/
668 static int dispatch_procfs_read(char *page, char **start, off_t off,
669 int count, int *eof, void *data)
671 struct ibm_struct *ibm = data;
672 int len;
674 if (!ibm || !ibm->read)
675 return -EINVAL;
677 len = ibm->read(page);
678 if (len < 0)
679 return len;
681 if (len <= off + count)
682 *eof = 1;
683 *start = page + off;
684 len -= off;
685 if (len > count)
686 len = count;
687 if (len < 0)
688 len = 0;
690 return len;
693 static int dispatch_procfs_write(struct file *file,
694 const char __user *userbuf,
695 unsigned long count, void *data)
697 struct ibm_struct *ibm = data;
698 char *kernbuf;
699 int ret;
701 if (!ibm || !ibm->write)
702 return -EINVAL;
704 kernbuf = kmalloc(count + 2, GFP_KERNEL);
705 if (!kernbuf)
706 return -ENOMEM;
708 if (copy_from_user(kernbuf, userbuf, count)) {
709 kfree(kernbuf);
710 return -EFAULT;
713 kernbuf[count] = 0;
714 strcat(kernbuf, ",");
715 ret = ibm->write(kernbuf);
716 if (ret == 0)
717 ret = count;
719 kfree(kernbuf);
721 return ret;
724 static char *next_cmd(char **cmds)
726 char *start = *cmds;
727 char *end;
729 while ((end = strchr(start, ',')) && end == start)
730 start = end + 1;
732 if (!end)
733 return NULL;
735 *end = 0;
736 *cmds = end + 1;
737 return start;
741 /****************************************************************************
742 ****************************************************************************
744 * Device model: input, hwmon and platform
746 ****************************************************************************
747 ****************************************************************************/
749 static struct platform_device *tpacpi_pdev;
750 static struct platform_device *tpacpi_sensors_pdev;
751 static struct device *tpacpi_hwmon;
752 static struct input_dev *tpacpi_inputdev;
753 static struct mutex tpacpi_inputdev_send_mutex;
754 static LIST_HEAD(tpacpi_all_drivers);
756 static int tpacpi_suspend_handler(struct platform_device *pdev,
757 pm_message_t state)
759 struct ibm_struct *ibm, *itmp;
761 list_for_each_entry_safe(ibm, itmp,
762 &tpacpi_all_drivers,
763 all_drivers) {
764 if (ibm->suspend)
765 (ibm->suspend)(state);
768 return 0;
771 static int tpacpi_resume_handler(struct platform_device *pdev)
773 struct ibm_struct *ibm, *itmp;
775 list_for_each_entry_safe(ibm, itmp,
776 &tpacpi_all_drivers,
777 all_drivers) {
778 if (ibm->resume)
779 (ibm->resume)();
782 return 0;
785 static void tpacpi_shutdown_handler(struct platform_device *pdev)
787 struct ibm_struct *ibm, *itmp;
789 list_for_each_entry_safe(ibm, itmp,
790 &tpacpi_all_drivers,
791 all_drivers) {
792 if (ibm->shutdown)
793 (ibm->shutdown)();
797 static struct platform_driver tpacpi_pdriver = {
798 .driver = {
799 .name = TPACPI_DRVR_NAME,
800 .owner = THIS_MODULE,
802 .suspend = tpacpi_suspend_handler,
803 .resume = tpacpi_resume_handler,
804 .shutdown = tpacpi_shutdown_handler,
807 static struct platform_driver tpacpi_hwmon_pdriver = {
808 .driver = {
809 .name = TPACPI_HWMON_DRVR_NAME,
810 .owner = THIS_MODULE,
814 /*************************************************************************
815 * sysfs support helpers
818 struct attribute_set {
819 unsigned int members, max_members;
820 struct attribute_group group;
823 struct attribute_set_obj {
824 struct attribute_set s;
825 struct attribute *a;
826 } __attribute__((packed));
828 static struct attribute_set *create_attr_set(unsigned int max_members,
829 const char *name)
831 struct attribute_set_obj *sobj;
833 if (max_members == 0)
834 return NULL;
836 /* Allocates space for implicit NULL at the end too */
837 sobj = kzalloc(sizeof(struct attribute_set_obj) +
838 max_members * sizeof(struct attribute *),
839 GFP_KERNEL);
840 if (!sobj)
841 return NULL;
842 sobj->s.max_members = max_members;
843 sobj->s.group.attrs = &sobj->a;
844 sobj->s.group.name = name;
846 return &sobj->s;
849 #define destroy_attr_set(_set) \
850 kfree(_set);
852 /* not multi-threaded safe, use it in a single thread per set */
853 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
855 if (!s || !attr)
856 return -EINVAL;
858 if (s->members >= s->max_members)
859 return -ENOMEM;
861 s->group.attrs[s->members] = attr;
862 s->members++;
864 return 0;
867 static int add_many_to_attr_set(struct attribute_set *s,
868 struct attribute **attr,
869 unsigned int count)
871 int i, res;
873 for (i = 0; i < count; i++) {
874 res = add_to_attr_set(s, attr[i]);
875 if (res)
876 return res;
879 return 0;
882 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
884 sysfs_remove_group(kobj, &s->group);
885 destroy_attr_set(s);
888 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
889 sysfs_create_group(_kobj, &_attr_set->group)
891 static int parse_strtoul(const char *buf,
892 unsigned long max, unsigned long *value)
894 char *endp;
896 while (*buf && isspace(*buf))
897 buf++;
898 *value = simple_strtoul(buf, &endp, 0);
899 while (*endp && isspace(*endp))
900 endp++;
901 if (*endp || *value > max)
902 return -EINVAL;
904 return 0;
907 static void tpacpi_disable_brightness_delay(void)
909 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
910 printk(TPACPI_NOTICE
911 "ACPI backlight control delay disabled\n");
914 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
916 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
917 union acpi_object *obj;
918 int rc;
920 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
921 obj = (union acpi_object *)buffer.pointer;
922 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
923 printk(TPACPI_ERR "Unknown _BCL data, "
924 "please report this to %s\n", TPACPI_MAIL);
925 rc = 0;
926 } else {
927 rc = obj->package.count;
929 } else {
930 return 0;
933 kfree(buffer.pointer);
934 return rc;
937 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
938 u32 lvl, void *context, void **rv)
940 char name[ACPI_PATH_SEGMENT_LENGTH];
941 struct acpi_buffer buffer = { sizeof(name), &name };
943 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
944 !strncmp("_BCL", name, sizeof(name) - 1)) {
945 BUG_ON(!rv || !*rv);
946 **(int **)rv = tpacpi_query_bcl_levels(handle);
947 return AE_CTRL_TERMINATE;
948 } else {
949 return AE_OK;
954 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
956 static int __init tpacpi_check_std_acpi_brightness_support(void)
958 int status;
959 int bcl_levels = 0;
960 void *bcl_ptr = &bcl_levels;
962 if (!vid_handle) {
963 TPACPI_ACPIHANDLE_INIT(vid);
965 if (!vid_handle)
966 return 0;
969 * Search for a _BCL method, and execute it. This is safe on all
970 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
971 * BIOS in ACPI backlight control mode. We do NOT have to care
972 * about calling the _BCL method in an enabled video device, any
973 * will do for our purposes.
976 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
977 tpacpi_acpi_walk_find_bcl, NULL,
978 &bcl_ptr);
980 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
981 tp_features.bright_acpimode = 1;
982 return (bcl_levels - 2);
985 return 0;
988 static int __init tpacpi_new_rfkill(const unsigned int id,
989 struct rfkill **rfk,
990 const enum rfkill_type rfktype,
991 const char *name,
992 const bool set_default,
993 int (*toggle_radio)(void *, enum rfkill_state),
994 int (*get_state)(void *, enum rfkill_state *))
996 int res;
997 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
999 res = get_state(NULL, &initial_state);
1000 if (res < 0) {
1001 printk(TPACPI_ERR
1002 "failed to read initial state for %s, error %d; "
1003 "will turn radio off\n", name, res);
1004 } else if (set_default) {
1005 /* try to set the initial state as the default for the rfkill
1006 * type, since we ask the firmware to preserve it across S5 in
1007 * NVRAM */
1008 rfkill_set_default(rfktype,
1009 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1010 RFKILL_STATE_UNBLOCKED :
1011 RFKILL_STATE_SOFT_BLOCKED);
1014 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1015 if (!*rfk) {
1016 printk(TPACPI_ERR
1017 "failed to allocate memory for rfkill class\n");
1018 return -ENOMEM;
1021 (*rfk)->name = name;
1022 (*rfk)->get_state = get_state;
1023 (*rfk)->toggle_radio = toggle_radio;
1024 (*rfk)->state = initial_state;
1026 res = rfkill_register(*rfk);
1027 if (res < 0) {
1028 printk(TPACPI_ERR
1029 "failed to register %s rfkill switch: %d\n",
1030 name, res);
1031 rfkill_free(*rfk);
1032 *rfk = NULL;
1033 return res;
1036 return 0;
1039 /*************************************************************************
1040 * thinkpad-acpi driver attributes
1043 /* interface_version --------------------------------------------------- */
1044 static ssize_t tpacpi_driver_interface_version_show(
1045 struct device_driver *drv,
1046 char *buf)
1048 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1051 static DRIVER_ATTR(interface_version, S_IRUGO,
1052 tpacpi_driver_interface_version_show, NULL);
1054 /* debug_level --------------------------------------------------------- */
1055 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1056 char *buf)
1058 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1061 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1062 const char *buf, size_t count)
1064 unsigned long t;
1066 if (parse_strtoul(buf, 0xffff, &t))
1067 return -EINVAL;
1069 dbg_level = t;
1071 return count;
1074 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1075 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1077 /* version ------------------------------------------------------------- */
1078 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1079 char *buf)
1081 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1082 TPACPI_DESC, TPACPI_VERSION);
1085 static DRIVER_ATTR(version, S_IRUGO,
1086 tpacpi_driver_version_show, NULL);
1088 /* --------------------------------------------------------------------- */
1090 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1092 static void tpacpi_send_radiosw_update(void);
1094 /* wlsw_emulstate ------------------------------------------------------ */
1095 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1096 char *buf)
1098 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1101 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1102 const char *buf, size_t count)
1104 unsigned long t;
1106 if (parse_strtoul(buf, 1, &t))
1107 return -EINVAL;
1109 if (tpacpi_wlsw_emulstate != t) {
1110 tpacpi_wlsw_emulstate = !!t;
1111 tpacpi_send_radiosw_update();
1112 } else
1113 tpacpi_wlsw_emulstate = !!t;
1115 return count;
1118 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1119 tpacpi_driver_wlsw_emulstate_show,
1120 tpacpi_driver_wlsw_emulstate_store);
1122 /* bluetooth_emulstate ------------------------------------------------- */
1123 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1124 struct device_driver *drv,
1125 char *buf)
1127 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1130 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1131 struct device_driver *drv,
1132 const char *buf, size_t count)
1134 unsigned long t;
1136 if (parse_strtoul(buf, 1, &t))
1137 return -EINVAL;
1139 tpacpi_bluetooth_emulstate = !!t;
1141 return count;
1144 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1145 tpacpi_driver_bluetooth_emulstate_show,
1146 tpacpi_driver_bluetooth_emulstate_store);
1148 /* wwan_emulstate ------------------------------------------------- */
1149 static ssize_t tpacpi_driver_wwan_emulstate_show(
1150 struct device_driver *drv,
1151 char *buf)
1153 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1156 static ssize_t tpacpi_driver_wwan_emulstate_store(
1157 struct device_driver *drv,
1158 const char *buf, size_t count)
1160 unsigned long t;
1162 if (parse_strtoul(buf, 1, &t))
1163 return -EINVAL;
1165 tpacpi_wwan_emulstate = !!t;
1167 return count;
1170 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1171 tpacpi_driver_wwan_emulstate_show,
1172 tpacpi_driver_wwan_emulstate_store);
1174 /* uwb_emulstate ------------------------------------------------- */
1175 static ssize_t tpacpi_driver_uwb_emulstate_show(
1176 struct device_driver *drv,
1177 char *buf)
1179 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1182 static ssize_t tpacpi_driver_uwb_emulstate_store(
1183 struct device_driver *drv,
1184 const char *buf, size_t count)
1186 unsigned long t;
1188 if (parse_strtoul(buf, 1, &t))
1189 return -EINVAL;
1191 tpacpi_uwb_emulstate = !!t;
1193 return count;
1196 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1197 tpacpi_driver_uwb_emulstate_show,
1198 tpacpi_driver_uwb_emulstate_store);
1199 #endif
1201 /* --------------------------------------------------------------------- */
1203 static struct driver_attribute *tpacpi_driver_attributes[] = {
1204 &driver_attr_debug_level, &driver_attr_version,
1205 &driver_attr_interface_version,
1208 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1210 int i, res;
1212 i = 0;
1213 res = 0;
1214 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1215 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1216 i++;
1219 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1220 if (!res && dbg_wlswemul)
1221 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1222 if (!res && dbg_bluetoothemul)
1223 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1224 if (!res && dbg_wwanemul)
1225 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1226 if (!res && dbg_uwbemul)
1227 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1228 #endif
1230 return res;
1233 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1235 int i;
1237 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1238 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1240 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1241 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1242 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1243 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1244 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1245 #endif
1248 /****************************************************************************
1249 ****************************************************************************
1251 * Subdrivers
1253 ****************************************************************************
1254 ****************************************************************************/
1256 /*************************************************************************
1257 * thinkpad-acpi init subdriver
1260 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1262 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1263 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1265 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1266 (thinkpad_id.bios_version_str) ?
1267 thinkpad_id.bios_version_str : "unknown",
1268 (thinkpad_id.ec_version_str) ?
1269 thinkpad_id.ec_version_str : "unknown");
1271 if (thinkpad_id.vendor && thinkpad_id.model_str)
1272 printk(TPACPI_INFO "%s %s, model %s\n",
1273 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1274 "IBM" : ((thinkpad_id.vendor ==
1275 PCI_VENDOR_ID_LENOVO) ?
1276 "Lenovo" : "Unknown vendor"),
1277 thinkpad_id.model_str,
1278 (thinkpad_id.nummodel_str) ?
1279 thinkpad_id.nummodel_str : "unknown");
1281 return 0;
1284 static int thinkpad_acpi_driver_read(char *p)
1286 int len = 0;
1288 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1289 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1291 return len;
1294 static struct ibm_struct thinkpad_acpi_driver_data = {
1295 .name = "driver",
1296 .read = thinkpad_acpi_driver_read,
1299 /*************************************************************************
1300 * Hotkey subdriver
1303 enum { /* hot key scan codes (derived from ACPI DSDT) */
1304 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1305 TP_ACPI_HOTKEYSCAN_FNF2,
1306 TP_ACPI_HOTKEYSCAN_FNF3,
1307 TP_ACPI_HOTKEYSCAN_FNF4,
1308 TP_ACPI_HOTKEYSCAN_FNF5,
1309 TP_ACPI_HOTKEYSCAN_FNF6,
1310 TP_ACPI_HOTKEYSCAN_FNF7,
1311 TP_ACPI_HOTKEYSCAN_FNF8,
1312 TP_ACPI_HOTKEYSCAN_FNF9,
1313 TP_ACPI_HOTKEYSCAN_FNF10,
1314 TP_ACPI_HOTKEYSCAN_FNF11,
1315 TP_ACPI_HOTKEYSCAN_FNF12,
1316 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1317 TP_ACPI_HOTKEYSCAN_FNINSERT,
1318 TP_ACPI_HOTKEYSCAN_FNDELETE,
1319 TP_ACPI_HOTKEYSCAN_FNHOME,
1320 TP_ACPI_HOTKEYSCAN_FNEND,
1321 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1322 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1323 TP_ACPI_HOTKEYSCAN_FNSPACE,
1324 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1325 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1326 TP_ACPI_HOTKEYSCAN_MUTE,
1327 TP_ACPI_HOTKEYSCAN_THINKPAD,
1330 enum { /* Keys available through NVRAM polling */
1331 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1332 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1335 enum { /* Positions of some of the keys in hotkey masks */
1336 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1337 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1338 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1339 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1340 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1341 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1342 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1343 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1344 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1345 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1346 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1349 enum { /* NVRAM to ACPI HKEY group map */
1350 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1351 TP_ACPI_HKEY_ZOOM_MASK |
1352 TP_ACPI_HKEY_DISPSWTCH_MASK |
1353 TP_ACPI_HKEY_HIBERNATE_MASK,
1354 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1355 TP_ACPI_HKEY_BRGHTDWN_MASK,
1356 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1357 TP_ACPI_HKEY_VOLDWN_MASK |
1358 TP_ACPI_HKEY_MUTE_MASK,
1361 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1362 struct tp_nvram_state {
1363 u16 thinkpad_toggle:1;
1364 u16 zoom_toggle:1;
1365 u16 display_toggle:1;
1366 u16 thinklight_toggle:1;
1367 u16 hibernate_toggle:1;
1368 u16 displayexp_toggle:1;
1369 u16 display_state:1;
1370 u16 brightness_toggle:1;
1371 u16 volume_toggle:1;
1372 u16 mute:1;
1374 u8 brightness_level;
1375 u8 volume_level;
1378 static struct task_struct *tpacpi_hotkey_task;
1379 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1380 static int hotkey_poll_freq = 10; /* Hz */
1381 static struct mutex hotkey_thread_mutex;
1382 static struct mutex hotkey_thread_data_mutex;
1383 static unsigned int hotkey_config_change;
1385 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1387 #define hotkey_source_mask 0U
1389 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1391 static struct mutex hotkey_mutex;
1393 static enum { /* Reasons for waking up */
1394 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1395 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1396 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1397 } hotkey_wakeup_reason;
1399 static int hotkey_autosleep_ack;
1401 static int hotkey_orig_status;
1402 static u32 hotkey_orig_mask;
1403 static u32 hotkey_all_mask;
1404 static u32 hotkey_reserved_mask;
1405 static u32 hotkey_mask;
1407 static unsigned int hotkey_report_mode;
1409 static u16 *hotkey_keycode_map;
1411 static struct attribute_set *hotkey_dev_attributes;
1413 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1414 #define HOTKEY_CONFIG_CRITICAL_START \
1415 do { \
1416 mutex_lock(&hotkey_thread_data_mutex); \
1417 hotkey_config_change++; \
1418 } while (0);
1419 #define HOTKEY_CONFIG_CRITICAL_END \
1420 mutex_unlock(&hotkey_thread_data_mutex);
1421 #else
1422 #define HOTKEY_CONFIG_CRITICAL_START
1423 #define HOTKEY_CONFIG_CRITICAL_END
1424 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1426 /* HKEY.MHKG() return bits */
1427 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1429 static int hotkey_get_wlsw(int *status)
1431 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1432 if (dbg_wlswemul) {
1433 *status = !!tpacpi_wlsw_emulstate;
1434 return 0;
1436 #endif
1437 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1438 return -EIO;
1439 return 0;
1442 static int hotkey_get_tablet_mode(int *status)
1444 int s;
1446 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1447 return -EIO;
1449 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1450 return 0;
1454 * Call with hotkey_mutex held
1456 static int hotkey_mask_get(void)
1458 u32 m = 0;
1460 if (tp_features.hotkey_mask) {
1461 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1462 return -EIO;
1464 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1466 return 0;
1470 * Call with hotkey_mutex held
1472 static int hotkey_mask_set(u32 mask)
1474 int i;
1475 int rc = 0;
1477 if (tp_features.hotkey_mask) {
1478 if (!tp_warned.hotkey_mask_ff &&
1479 (mask == 0xffff || mask == 0xffffff ||
1480 mask == 0xffffffff)) {
1481 tp_warned.hotkey_mask_ff = 1;
1482 printk(TPACPI_NOTICE
1483 "setting the hotkey mask to 0x%08x is likely "
1484 "not the best way to go about it\n", mask);
1485 printk(TPACPI_NOTICE
1486 "please consider using the driver defaults, "
1487 "and refer to up-to-date thinkpad-acpi "
1488 "documentation\n");
1491 HOTKEY_CONFIG_CRITICAL_START
1492 for (i = 0; i < 32; i++) {
1493 u32 m = 1 << i;
1494 /* enable in firmware mask only keys not in NVRAM
1495 * mode, but enable the key in the cached hotkey_mask
1496 * regardless of mode, or the key will end up
1497 * disabled by hotkey_mask_get() */
1498 if (!acpi_evalf(hkey_handle,
1499 NULL, "MHKM", "vdd", i + 1,
1500 !!((mask & ~hotkey_source_mask) & m))) {
1501 rc = -EIO;
1502 break;
1503 } else {
1504 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1507 HOTKEY_CONFIG_CRITICAL_END
1509 /* hotkey_mask_get must be called unconditionally below */
1510 if (!hotkey_mask_get() && !rc &&
1511 (hotkey_mask & ~hotkey_source_mask) !=
1512 (mask & ~hotkey_source_mask)) {
1513 printk(TPACPI_NOTICE
1514 "requested hot key mask 0x%08x, but "
1515 "firmware forced it to 0x%08x\n",
1516 mask, hotkey_mask);
1518 } else {
1519 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1520 HOTKEY_CONFIG_CRITICAL_START
1521 hotkey_mask = mask & hotkey_source_mask;
1522 HOTKEY_CONFIG_CRITICAL_END
1523 hotkey_mask_get();
1524 if (hotkey_mask != mask) {
1525 printk(TPACPI_NOTICE
1526 "requested hot key mask 0x%08x, "
1527 "forced to 0x%08x (NVRAM poll mask is "
1528 "0x%08x): no firmware mask support\n",
1529 mask, hotkey_mask, hotkey_source_mask);
1531 #else
1532 hotkey_mask_get();
1533 rc = -ENXIO;
1534 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1537 return rc;
1540 static int hotkey_status_get(int *status)
1542 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1543 return -EIO;
1545 return 0;
1548 static int hotkey_status_set(int status)
1550 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1551 return -EIO;
1553 return 0;
1556 static void tpacpi_input_send_tabletsw(void)
1558 int state;
1560 if (tp_features.hotkey_tablet &&
1561 !hotkey_get_tablet_mode(&state)) {
1562 mutex_lock(&tpacpi_inputdev_send_mutex);
1564 input_report_switch(tpacpi_inputdev,
1565 SW_TABLET_MODE, !!state);
1566 input_sync(tpacpi_inputdev);
1568 mutex_unlock(&tpacpi_inputdev_send_mutex);
1572 static void tpacpi_input_send_key(unsigned int scancode)
1574 unsigned int keycode;
1576 keycode = hotkey_keycode_map[scancode];
1578 if (keycode != KEY_RESERVED) {
1579 mutex_lock(&tpacpi_inputdev_send_mutex);
1581 input_report_key(tpacpi_inputdev, keycode, 1);
1582 if (keycode == KEY_UNKNOWN)
1583 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1584 scancode);
1585 input_sync(tpacpi_inputdev);
1587 input_report_key(tpacpi_inputdev, keycode, 0);
1588 if (keycode == KEY_UNKNOWN)
1589 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1590 scancode);
1591 input_sync(tpacpi_inputdev);
1593 mutex_unlock(&tpacpi_inputdev_send_mutex);
1597 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1598 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1600 static void tpacpi_hotkey_send_key(unsigned int scancode)
1602 tpacpi_input_send_key(scancode);
1603 if (hotkey_report_mode < 2) {
1604 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1605 0x80, 0x1001 + scancode);
1609 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1611 u8 d;
1613 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1614 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1615 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1616 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1617 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1618 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1620 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1621 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1622 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1624 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1625 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1626 n->displayexp_toggle =
1627 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1629 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1630 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1631 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1632 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1633 n->brightness_toggle =
1634 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1636 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1637 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1638 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1639 >> TP_NVRAM_POS_LEVEL_VOLUME;
1640 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1641 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1645 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1646 do { \
1647 if ((mask & (1 << __scancode)) && \
1648 oldn->__member != newn->__member) \
1649 tpacpi_hotkey_send_key(__scancode); \
1650 } while (0)
1652 #define TPACPI_MAY_SEND_KEY(__scancode) \
1653 do { if (mask & (1 << __scancode)) \
1654 tpacpi_hotkey_send_key(__scancode); } while (0)
1656 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1657 struct tp_nvram_state *newn,
1658 u32 mask)
1660 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1661 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1662 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1663 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1665 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1667 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1669 /* handle volume */
1670 if (oldn->volume_toggle != newn->volume_toggle) {
1671 if (oldn->mute != newn->mute) {
1672 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1674 if (oldn->volume_level > newn->volume_level) {
1675 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1676 } else if (oldn->volume_level < newn->volume_level) {
1677 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1678 } else if (oldn->mute == newn->mute) {
1679 /* repeated key presses that didn't change state */
1680 if (newn->mute) {
1681 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1682 } else if (newn->volume_level != 0) {
1683 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1684 } else {
1685 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1690 /* handle brightness */
1691 if (oldn->brightness_toggle != newn->brightness_toggle) {
1692 if (oldn->brightness_level < newn->brightness_level) {
1693 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1694 } else if (oldn->brightness_level > newn->brightness_level) {
1695 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1696 } else {
1697 /* repeated key presses that didn't change state */
1698 if (newn->brightness_level != 0) {
1699 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1700 } else {
1701 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1707 #undef TPACPI_COMPARE_KEY
1708 #undef TPACPI_MAY_SEND_KEY
1710 static int hotkey_kthread(void *data)
1712 struct tp_nvram_state s[2];
1713 u32 mask;
1714 unsigned int si, so;
1715 unsigned long t;
1716 unsigned int change_detector, must_reset;
1718 mutex_lock(&hotkey_thread_mutex);
1720 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1721 goto exit;
1723 set_freezable();
1725 so = 0;
1726 si = 1;
1727 t = 0;
1729 /* Initial state for compares */
1730 mutex_lock(&hotkey_thread_data_mutex);
1731 change_detector = hotkey_config_change;
1732 mask = hotkey_source_mask & hotkey_mask;
1733 mutex_unlock(&hotkey_thread_data_mutex);
1734 hotkey_read_nvram(&s[so], mask);
1736 while (!kthread_should_stop() && hotkey_poll_freq) {
1737 if (t == 0)
1738 t = 1000/hotkey_poll_freq;
1739 t = msleep_interruptible(t);
1740 if (unlikely(kthread_should_stop()))
1741 break;
1742 must_reset = try_to_freeze();
1743 if (t > 0 && !must_reset)
1744 continue;
1746 mutex_lock(&hotkey_thread_data_mutex);
1747 if (must_reset || hotkey_config_change != change_detector) {
1748 /* forget old state on thaw or config change */
1749 si = so;
1750 t = 0;
1751 change_detector = hotkey_config_change;
1753 mask = hotkey_source_mask & hotkey_mask;
1754 mutex_unlock(&hotkey_thread_data_mutex);
1756 if (likely(mask)) {
1757 hotkey_read_nvram(&s[si], mask);
1758 if (likely(si != so)) {
1759 hotkey_compare_and_issue_event(&s[so], &s[si],
1760 mask);
1764 so = si;
1765 si ^= 1;
1768 exit:
1769 mutex_unlock(&hotkey_thread_mutex);
1770 return 0;
1773 static void hotkey_poll_stop_sync(void)
1775 if (tpacpi_hotkey_task) {
1776 if (frozen(tpacpi_hotkey_task) ||
1777 freezing(tpacpi_hotkey_task))
1778 thaw_process(tpacpi_hotkey_task);
1780 kthread_stop(tpacpi_hotkey_task);
1781 tpacpi_hotkey_task = NULL;
1782 mutex_lock(&hotkey_thread_mutex);
1783 /* at this point, the thread did exit */
1784 mutex_unlock(&hotkey_thread_mutex);
1788 /* call with hotkey_mutex held */
1789 static void hotkey_poll_setup(int may_warn)
1791 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1792 hotkey_poll_freq > 0 &&
1793 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1794 if (!tpacpi_hotkey_task) {
1795 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1796 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1797 if (IS_ERR(tpacpi_hotkey_task)) {
1798 tpacpi_hotkey_task = NULL;
1799 printk(TPACPI_ERR
1800 "could not create kernel thread "
1801 "for hotkey polling\n");
1804 } else {
1805 hotkey_poll_stop_sync();
1806 if (may_warn &&
1807 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1808 printk(TPACPI_NOTICE
1809 "hot keys 0x%08x require polling, "
1810 "which is currently disabled\n",
1811 hotkey_source_mask);
1816 static void hotkey_poll_setup_safe(int may_warn)
1818 mutex_lock(&hotkey_mutex);
1819 hotkey_poll_setup(may_warn);
1820 mutex_unlock(&hotkey_mutex);
1823 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1825 static void hotkey_poll_setup_safe(int __unused)
1829 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1831 static int hotkey_inputdev_open(struct input_dev *dev)
1833 switch (tpacpi_lifecycle) {
1834 case TPACPI_LIFE_INIT:
1836 * hotkey_init will call hotkey_poll_setup_safe
1837 * at the appropriate moment
1839 return 0;
1840 case TPACPI_LIFE_EXITING:
1841 return -EBUSY;
1842 case TPACPI_LIFE_RUNNING:
1843 hotkey_poll_setup_safe(0);
1844 return 0;
1847 /* Should only happen if tpacpi_lifecycle is corrupt */
1848 BUG();
1849 return -EBUSY;
1852 static void hotkey_inputdev_close(struct input_dev *dev)
1854 /* disable hotkey polling when possible */
1855 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1856 hotkey_poll_setup_safe(0);
1859 /* sysfs hotkey enable ------------------------------------------------- */
1860 static ssize_t hotkey_enable_show(struct device *dev,
1861 struct device_attribute *attr,
1862 char *buf)
1864 int res, status;
1866 res = hotkey_status_get(&status);
1867 if (res)
1868 return res;
1870 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1873 static ssize_t hotkey_enable_store(struct device *dev,
1874 struct device_attribute *attr,
1875 const char *buf, size_t count)
1877 unsigned long t;
1878 int res;
1880 if (parse_strtoul(buf, 1, &t))
1881 return -EINVAL;
1883 res = hotkey_status_set(t);
1885 return (res) ? res : count;
1888 static struct device_attribute dev_attr_hotkey_enable =
1889 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1890 hotkey_enable_show, hotkey_enable_store);
1892 /* sysfs hotkey mask --------------------------------------------------- */
1893 static ssize_t hotkey_mask_show(struct device *dev,
1894 struct device_attribute *attr,
1895 char *buf)
1897 int res;
1899 if (mutex_lock_killable(&hotkey_mutex))
1900 return -ERESTARTSYS;
1901 res = hotkey_mask_get();
1902 mutex_unlock(&hotkey_mutex);
1904 return (res)?
1905 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1908 static ssize_t hotkey_mask_store(struct device *dev,
1909 struct device_attribute *attr,
1910 const char *buf, size_t count)
1912 unsigned long t;
1913 int res;
1915 if (parse_strtoul(buf, 0xffffffffUL, &t))
1916 return -EINVAL;
1918 if (mutex_lock_killable(&hotkey_mutex))
1919 return -ERESTARTSYS;
1921 res = hotkey_mask_set(t);
1923 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1924 hotkey_poll_setup(1);
1925 #endif
1927 mutex_unlock(&hotkey_mutex);
1929 return (res) ? res : count;
1932 static struct device_attribute dev_attr_hotkey_mask =
1933 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1934 hotkey_mask_show, hotkey_mask_store);
1936 /* sysfs hotkey bios_enabled ------------------------------------------- */
1937 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1938 struct device_attribute *attr,
1939 char *buf)
1941 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1944 static struct device_attribute dev_attr_hotkey_bios_enabled =
1945 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1947 /* sysfs hotkey bios_mask ---------------------------------------------- */
1948 static ssize_t hotkey_bios_mask_show(struct device *dev,
1949 struct device_attribute *attr,
1950 char *buf)
1952 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1955 static struct device_attribute dev_attr_hotkey_bios_mask =
1956 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1958 /* sysfs hotkey all_mask ----------------------------------------------- */
1959 static ssize_t hotkey_all_mask_show(struct device *dev,
1960 struct device_attribute *attr,
1961 char *buf)
1963 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1964 hotkey_all_mask | hotkey_source_mask);
1967 static struct device_attribute dev_attr_hotkey_all_mask =
1968 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1970 /* sysfs hotkey recommended_mask --------------------------------------- */
1971 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1972 struct device_attribute *attr,
1973 char *buf)
1975 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1976 (hotkey_all_mask | hotkey_source_mask)
1977 & ~hotkey_reserved_mask);
1980 static struct device_attribute dev_attr_hotkey_recommended_mask =
1981 __ATTR(hotkey_recommended_mask, S_IRUGO,
1982 hotkey_recommended_mask_show, NULL);
1984 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1986 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1987 static ssize_t hotkey_source_mask_show(struct device *dev,
1988 struct device_attribute *attr,
1989 char *buf)
1991 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1994 static ssize_t hotkey_source_mask_store(struct device *dev,
1995 struct device_attribute *attr,
1996 const char *buf, size_t count)
1998 unsigned long t;
2000 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2001 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2002 return -EINVAL;
2004 if (mutex_lock_killable(&hotkey_mutex))
2005 return -ERESTARTSYS;
2007 HOTKEY_CONFIG_CRITICAL_START
2008 hotkey_source_mask = t;
2009 HOTKEY_CONFIG_CRITICAL_END
2011 hotkey_poll_setup(1);
2013 mutex_unlock(&hotkey_mutex);
2015 return count;
2018 static struct device_attribute dev_attr_hotkey_source_mask =
2019 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2020 hotkey_source_mask_show, hotkey_source_mask_store);
2022 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2023 static ssize_t hotkey_poll_freq_show(struct device *dev,
2024 struct device_attribute *attr,
2025 char *buf)
2027 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2030 static ssize_t hotkey_poll_freq_store(struct device *dev,
2031 struct device_attribute *attr,
2032 const char *buf, size_t count)
2034 unsigned long t;
2036 if (parse_strtoul(buf, 25, &t))
2037 return -EINVAL;
2039 if (mutex_lock_killable(&hotkey_mutex))
2040 return -ERESTARTSYS;
2042 hotkey_poll_freq = t;
2044 hotkey_poll_setup(1);
2045 mutex_unlock(&hotkey_mutex);
2047 return count;
2050 static struct device_attribute dev_attr_hotkey_poll_freq =
2051 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2052 hotkey_poll_freq_show, hotkey_poll_freq_store);
2054 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2056 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2057 static ssize_t hotkey_radio_sw_show(struct device *dev,
2058 struct device_attribute *attr,
2059 char *buf)
2061 int res, s;
2062 res = hotkey_get_wlsw(&s);
2063 if (res < 0)
2064 return res;
2066 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2069 static struct device_attribute dev_attr_hotkey_radio_sw =
2070 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2072 static void hotkey_radio_sw_notify_change(void)
2074 if (tp_features.hotkey_wlsw)
2075 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2076 "hotkey_radio_sw");
2079 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2080 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2081 struct device_attribute *attr,
2082 char *buf)
2084 int res, s;
2085 res = hotkey_get_tablet_mode(&s);
2086 if (res < 0)
2087 return res;
2089 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2092 static struct device_attribute dev_attr_hotkey_tablet_mode =
2093 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2095 static void hotkey_tablet_mode_notify_change(void)
2097 if (tp_features.hotkey_tablet)
2098 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2099 "hotkey_tablet_mode");
2102 /* sysfs hotkey report_mode -------------------------------------------- */
2103 static ssize_t hotkey_report_mode_show(struct device *dev,
2104 struct device_attribute *attr,
2105 char *buf)
2107 return snprintf(buf, PAGE_SIZE, "%d\n",
2108 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2111 static struct device_attribute dev_attr_hotkey_report_mode =
2112 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2114 /* sysfs wakeup reason (pollable) -------------------------------------- */
2115 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2116 struct device_attribute *attr,
2117 char *buf)
2119 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2122 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2123 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2125 static void hotkey_wakeup_reason_notify_change(void)
2127 if (tp_features.hotkey_mask)
2128 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2129 "wakeup_reason");
2132 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2133 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2134 struct device_attribute *attr,
2135 char *buf)
2137 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2140 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2141 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2142 hotkey_wakeup_hotunplug_complete_show, NULL);
2144 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2146 if (tp_features.hotkey_mask)
2147 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2148 "wakeup_hotunplug_complete");
2151 /* --------------------------------------------------------------------- */
2153 static struct attribute *hotkey_attributes[] __initdata = {
2154 &dev_attr_hotkey_enable.attr,
2155 &dev_attr_hotkey_bios_enabled.attr,
2156 &dev_attr_hotkey_report_mode.attr,
2157 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2158 &dev_attr_hotkey_mask.attr,
2159 &dev_attr_hotkey_all_mask.attr,
2160 &dev_attr_hotkey_recommended_mask.attr,
2161 &dev_attr_hotkey_source_mask.attr,
2162 &dev_attr_hotkey_poll_freq.attr,
2163 #endif
2166 static struct attribute *hotkey_mask_attributes[] __initdata = {
2167 &dev_attr_hotkey_bios_mask.attr,
2168 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2169 &dev_attr_hotkey_mask.attr,
2170 &dev_attr_hotkey_all_mask.attr,
2171 &dev_attr_hotkey_recommended_mask.attr,
2172 #endif
2173 &dev_attr_hotkey_wakeup_reason.attr,
2174 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2177 static void bluetooth_update_rfk(void);
2178 static void wan_update_rfk(void);
2179 static void uwb_update_rfk(void);
2180 static void tpacpi_send_radiosw_update(void)
2182 int wlsw;
2184 /* Sync these BEFORE sending any rfkill events */
2185 if (tp_features.bluetooth)
2186 bluetooth_update_rfk();
2187 if (tp_features.wan)
2188 wan_update_rfk();
2189 if (tp_features.uwb)
2190 uwb_update_rfk();
2192 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2193 mutex_lock(&tpacpi_inputdev_send_mutex);
2195 input_report_switch(tpacpi_inputdev,
2196 SW_RFKILL_ALL, !!wlsw);
2197 input_sync(tpacpi_inputdev);
2199 mutex_unlock(&tpacpi_inputdev_send_mutex);
2201 hotkey_radio_sw_notify_change();
2204 static void hotkey_exit(void)
2206 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2207 hotkey_poll_stop_sync();
2208 #endif
2210 if (hotkey_dev_attributes)
2211 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2213 kfree(hotkey_keycode_map);
2215 if (tp_features.hotkey) {
2216 dbg_printk(TPACPI_DBG_EXIT,
2217 "restoring original hot key mask\n");
2218 /* no short-circuit boolean operator below! */
2219 if ((hotkey_mask_set(hotkey_orig_mask) |
2220 hotkey_status_set(hotkey_orig_status)) != 0)
2221 printk(TPACPI_ERR
2222 "failed to restore hot key mask "
2223 "to BIOS defaults\n");
2227 static int __init hotkey_init(struct ibm_init_struct *iibm)
2229 /* Requirements for changing the default keymaps:
2231 * 1. Many of the keys are mapped to KEY_RESERVED for very
2232 * good reasons. Do not change them unless you have deep
2233 * knowledge on the IBM and Lenovo ThinkPad firmware for
2234 * the various ThinkPad models. The driver behaves
2235 * differently for KEY_RESERVED: such keys have their
2236 * hot key mask *unset* in mask_recommended, and also
2237 * in the initial hot key mask programmed into the
2238 * firmware at driver load time, which means the firm-
2239 * ware may react very differently if you change them to
2240 * something else;
2242 * 2. You must be subscribed to the linux-thinkpad and
2243 * ibm-acpi-devel mailing lists, and you should read the
2244 * list archives since 2007 if you want to change the
2245 * keymaps. This requirement exists so that you will
2246 * know the past history of problems with the thinkpad-
2247 * acpi driver keymaps, and also that you will be
2248 * listening to any bug reports;
2250 * 3. Do not send thinkpad-acpi specific patches directly to
2251 * for merging, *ever*. Send them to the linux-acpi
2252 * mailinglist for comments. Merging is to be done only
2253 * through acpi-test and the ACPI maintainer.
2255 * If the above is too much to ask, don't change the keymap.
2256 * Ask the thinkpad-acpi maintainer to do it, instead.
2258 static u16 ibm_keycode_map[] __initdata = {
2259 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2260 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2261 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2262 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2264 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2265 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2266 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2267 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2269 /* brightness: firmware always reacts to them, unless
2270 * X.org did some tricks in the radeon BIOS scratch
2271 * registers of *some* models */
2272 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2273 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2275 /* Thinklight: firmware always react to it */
2276 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2278 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2279 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2281 /* Volume: firmware always react to it and reprograms
2282 * the built-in *extra* mixer. Never map it to control
2283 * another mixer by default. */
2284 KEY_RESERVED, /* 0x14: VOLUME UP */
2285 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2286 KEY_RESERVED, /* 0x16: MUTE */
2288 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2290 /* (assignments unknown, please report if found) */
2291 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2292 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2294 static u16 lenovo_keycode_map[] __initdata = {
2295 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2296 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2297 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2298 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2300 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2301 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2302 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2303 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2305 /* These either have to go through ACPI video, or
2306 * act like in the IBM ThinkPads, so don't ever
2307 * enable them by default */
2308 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2309 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2311 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2313 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2314 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2316 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2317 * react to it and reprograms the built-in *extra* mixer.
2318 * Never map it to control another mixer by default.
2320 * T60?, T61, R60?, R61: firmware and EC tries to send
2321 * these over the regular keyboard, so these are no-ops,
2322 * but there are still weird bugs re. MUTE, so do not
2323 * change unless you get test reports from all Lenovo
2324 * models. May cause the BIOS to interfere with the
2325 * HDA mixer.
2327 KEY_RESERVED, /* 0x14: VOLUME UP */
2328 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2329 KEY_RESERVED, /* 0x16: MUTE */
2331 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2333 /* (assignments unknown, please report if found) */
2334 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2335 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2338 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2339 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2340 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2342 int res, i;
2343 int status;
2344 int hkeyv;
2346 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2348 BUG_ON(!tpacpi_inputdev);
2349 BUG_ON(tpacpi_inputdev->open != NULL ||
2350 tpacpi_inputdev->close != NULL);
2352 TPACPI_ACPIHANDLE_INIT(hkey);
2353 mutex_init(&hotkey_mutex);
2355 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2356 mutex_init(&hotkey_thread_mutex);
2357 mutex_init(&hotkey_thread_data_mutex);
2358 #endif
2360 /* hotkey not supported on 570 */
2361 tp_features.hotkey = hkey_handle != NULL;
2363 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2364 str_supported(tp_features.hotkey));
2366 if (!tp_features.hotkey)
2367 return 1;
2369 tpacpi_disable_brightness_delay();
2371 hotkey_dev_attributes = create_attr_set(13, NULL);
2372 if (!hotkey_dev_attributes)
2373 return -ENOMEM;
2374 res = add_many_to_attr_set(hotkey_dev_attributes,
2375 hotkey_attributes,
2376 ARRAY_SIZE(hotkey_attributes));
2377 if (res)
2378 goto err_exit;
2380 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2381 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2382 for HKEY interface version 0x100 */
2383 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2384 if ((hkeyv >> 8) != 1) {
2385 printk(TPACPI_ERR "unknown version of the "
2386 "HKEY interface: 0x%x\n", hkeyv);
2387 printk(TPACPI_ERR "please report this to %s\n",
2388 TPACPI_MAIL);
2389 } else {
2391 * MHKV 0x100 in A31, R40, R40e,
2392 * T4x, X31, and later
2394 tp_features.hotkey_mask = 1;
2398 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2399 str_supported(tp_features.hotkey_mask));
2401 if (tp_features.hotkey_mask) {
2402 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2403 "MHKA", "qd")) {
2404 printk(TPACPI_ERR
2405 "missing MHKA handler, "
2406 "please report this to %s\n",
2407 TPACPI_MAIL);
2408 /* FN+F12, FN+F4, FN+F3 */
2409 hotkey_all_mask = 0x080cU;
2413 /* hotkey_source_mask *must* be zero for
2414 * the first hotkey_mask_get */
2415 res = hotkey_status_get(&hotkey_orig_status);
2416 if (res)
2417 goto err_exit;
2419 if (tp_features.hotkey_mask) {
2420 res = hotkey_mask_get();
2421 if (res)
2422 goto err_exit;
2424 hotkey_orig_mask = hotkey_mask;
2425 res = add_many_to_attr_set(
2426 hotkey_dev_attributes,
2427 hotkey_mask_attributes,
2428 ARRAY_SIZE(hotkey_mask_attributes));
2429 if (res)
2430 goto err_exit;
2433 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2434 if (tp_features.hotkey_mask) {
2435 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2436 & ~hotkey_all_mask;
2437 } else {
2438 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2441 vdbg_printk(TPACPI_DBG_INIT,
2442 "hotkey source mask 0x%08x, polling freq %d\n",
2443 hotkey_source_mask, hotkey_poll_freq);
2444 #endif
2446 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2447 if (dbg_wlswemul) {
2448 tp_features.hotkey_wlsw = 1;
2449 printk(TPACPI_INFO
2450 "radio switch emulation enabled\n");
2451 } else
2452 #endif
2453 /* Not all thinkpads have a hardware radio switch */
2454 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2455 tp_features.hotkey_wlsw = 1;
2456 printk(TPACPI_INFO
2457 "radio switch found; radios are %s\n",
2458 enabled(status, 0));
2460 if (tp_features.hotkey_wlsw)
2461 res = add_to_attr_set(hotkey_dev_attributes,
2462 &dev_attr_hotkey_radio_sw.attr);
2464 /* For X41t, X60t, X61t Tablets... */
2465 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2466 tp_features.hotkey_tablet = 1;
2467 printk(TPACPI_INFO
2468 "possible tablet mode switch found; "
2469 "ThinkPad in %s mode\n",
2470 (status & TP_HOTKEY_TABLET_MASK)?
2471 "tablet" : "laptop");
2472 res = add_to_attr_set(hotkey_dev_attributes,
2473 &dev_attr_hotkey_tablet_mode.attr);
2476 if (!res)
2477 res = register_attr_set_with_sysfs(
2478 hotkey_dev_attributes,
2479 &tpacpi_pdev->dev.kobj);
2480 if (res)
2481 goto err_exit;
2483 /* Set up key map */
2485 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2486 GFP_KERNEL);
2487 if (!hotkey_keycode_map) {
2488 printk(TPACPI_ERR
2489 "failed to allocate memory for key map\n");
2490 res = -ENOMEM;
2491 goto err_exit;
2494 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2495 dbg_printk(TPACPI_DBG_INIT,
2496 "using Lenovo default hot key map\n");
2497 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2498 TPACPI_HOTKEY_MAP_SIZE);
2499 } else {
2500 dbg_printk(TPACPI_DBG_INIT,
2501 "using IBM default hot key map\n");
2502 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2503 TPACPI_HOTKEY_MAP_SIZE);
2506 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2507 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2508 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2509 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2510 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2511 tpacpi_inputdev->keycode = hotkey_keycode_map;
2512 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2513 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2514 set_bit(hotkey_keycode_map[i],
2515 tpacpi_inputdev->keybit);
2516 } else {
2517 if (i < sizeof(hotkey_reserved_mask)*8)
2518 hotkey_reserved_mask |= 1 << i;
2522 if (tp_features.hotkey_wlsw) {
2523 set_bit(EV_SW, tpacpi_inputdev->evbit);
2524 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2526 if (tp_features.hotkey_tablet) {
2527 set_bit(EV_SW, tpacpi_inputdev->evbit);
2528 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2531 /* Do not issue duplicate brightness change events to
2532 * userspace */
2533 if (!tp_features.bright_acpimode)
2534 /* update bright_acpimode... */
2535 tpacpi_check_std_acpi_brightness_support();
2537 if (tp_features.bright_acpimode) {
2538 printk(TPACPI_INFO
2539 "This ThinkPad has standard ACPI backlight "
2540 "brightness control, supported by the ACPI "
2541 "video driver\n");
2542 printk(TPACPI_NOTICE
2543 "Disabling thinkpad-acpi brightness events "
2544 "by default...\n");
2546 /* The hotkey_reserved_mask change below is not
2547 * necessary while the keys are at KEY_RESERVED in the
2548 * default map, but better safe than sorry, leave it
2549 * here as a marker of what we have to do, especially
2550 * when we finally become able to set this at runtime
2551 * on response to X.org requests */
2552 hotkey_reserved_mask |=
2553 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2554 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2557 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2558 res = hotkey_status_set(1);
2559 if (res) {
2560 hotkey_exit();
2561 return res;
2563 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2564 & ~hotkey_reserved_mask)
2565 | hotkey_orig_mask);
2566 if (res < 0 && res != -ENXIO) {
2567 hotkey_exit();
2568 return res;
2571 dbg_printk(TPACPI_DBG_INIT,
2572 "legacy hot key reporting over procfs %s\n",
2573 (hotkey_report_mode < 2) ?
2574 "enabled" : "disabled");
2576 tpacpi_inputdev->open = &hotkey_inputdev_open;
2577 tpacpi_inputdev->close = &hotkey_inputdev_close;
2579 hotkey_poll_setup_safe(1);
2580 tpacpi_send_radiosw_update();
2581 tpacpi_input_send_tabletsw();
2583 return 0;
2585 err_exit:
2586 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2587 hotkey_dev_attributes = NULL;
2589 return (res < 0)? res : 1;
2592 static bool hotkey_notify_hotkey(const u32 hkey,
2593 bool *send_acpi_ev,
2594 bool *ignore_acpi_ev)
2596 /* 0x1000-0x1FFF: key presses */
2597 unsigned int scancode = hkey & 0xfff;
2598 *send_acpi_ev = true;
2599 *ignore_acpi_ev = false;
2601 if (scancode > 0 && scancode < 0x21) {
2602 scancode--;
2603 if (!(hotkey_source_mask & (1 << scancode))) {
2604 tpacpi_input_send_key(scancode);
2605 *send_acpi_ev = false;
2606 } else {
2607 *ignore_acpi_ev = true;
2609 return true;
2611 return false;
2614 static bool hotkey_notify_wakeup(const u32 hkey,
2615 bool *send_acpi_ev,
2616 bool *ignore_acpi_ev)
2618 /* 0x2000-0x2FFF: Wakeup reason */
2619 *send_acpi_ev = true;
2620 *ignore_acpi_ev = false;
2622 switch (hkey) {
2623 case 0x2304: /* suspend, undock */
2624 case 0x2404: /* hibernation, undock */
2625 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2626 *ignore_acpi_ev = true;
2627 break;
2629 case 0x2305: /* suspend, bay eject */
2630 case 0x2405: /* hibernation, bay eject */
2631 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2632 *ignore_acpi_ev = true;
2633 break;
2635 case 0x2313: /* Battery on critical low level (S3) */
2636 case 0x2413: /* Battery on critical low level (S4) */
2637 printk(TPACPI_ALERT
2638 "EMERGENCY WAKEUP: battery almost empty\n");
2639 /* how to auto-heal: */
2640 /* 2313: woke up from S3, go to S4/S5 */
2641 /* 2413: woke up from S4, go to S5 */
2642 break;
2644 default:
2645 return false;
2648 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2649 printk(TPACPI_INFO
2650 "woke up due to a hot-unplug "
2651 "request...\n");
2652 hotkey_wakeup_reason_notify_change();
2654 return true;
2657 static bool hotkey_notify_usrevent(const u32 hkey,
2658 bool *send_acpi_ev,
2659 bool *ignore_acpi_ev)
2661 /* 0x5000-0x5FFF: human interface helpers */
2662 *send_acpi_ev = true;
2663 *ignore_acpi_ev = false;
2665 switch (hkey) {
2666 case 0x5010: /* Lenovo new BIOS: brightness changed */
2667 case 0x500b: /* X61t: tablet pen inserted into bay */
2668 case 0x500c: /* X61t: tablet pen removed from bay */
2669 return true;
2671 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2672 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2673 tpacpi_input_send_tabletsw();
2674 hotkey_tablet_mode_notify_change();
2675 *send_acpi_ev = false;
2676 return true;
2678 case 0x5001:
2679 case 0x5002:
2680 /* LID switch events. Do not propagate */
2681 *ignore_acpi_ev = true;
2682 return true;
2684 default:
2685 return false;
2689 static bool hotkey_notify_thermal(const u32 hkey,
2690 bool *send_acpi_ev,
2691 bool *ignore_acpi_ev)
2693 /* 0x6000-0x6FFF: thermal alarms */
2694 *send_acpi_ev = true;
2695 *ignore_acpi_ev = false;
2697 switch (hkey) {
2698 case 0x6011:
2699 printk(TPACPI_CRIT
2700 "THERMAL ALARM: battery is too hot!\n");
2701 /* recommended action: warn user through gui */
2702 return true;
2703 case 0x6012:
2704 printk(TPACPI_ALERT
2705 "THERMAL EMERGENCY: battery is extremely hot!\n");
2706 /* recommended action: immediate sleep/hibernate */
2707 return true;
2708 case 0x6021:
2709 printk(TPACPI_CRIT
2710 "THERMAL ALARM: "
2711 "a sensor reports something is too hot!\n");
2712 /* recommended action: warn user through gui, that */
2713 /* some internal component is too hot */
2714 return true;
2715 case 0x6022:
2716 printk(TPACPI_ALERT
2717 "THERMAL EMERGENCY: "
2718 "a sensor reports something is extremely hot!\n");
2719 /* recommended action: immediate sleep/hibernate */
2720 return true;
2721 case 0x6030:
2722 printk(TPACPI_INFO
2723 "EC reports that Thermal Table has changed\n");
2724 /* recommended action: do nothing, we don't have
2725 * Lenovo ATM information */
2726 return true;
2727 default:
2728 printk(TPACPI_ALERT
2729 "THERMAL ALERT: unknown thermal alarm received\n");
2730 return false;
2734 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2736 u32 hkey;
2737 bool send_acpi_ev;
2738 bool ignore_acpi_ev;
2739 bool known_ev;
2741 if (event != 0x80) {
2742 printk(TPACPI_ERR
2743 "unknown HKEY notification event %d\n", event);
2744 /* forward it to userspace, maybe it knows how to handle it */
2745 acpi_bus_generate_netlink_event(
2746 ibm->acpi->device->pnp.device_class,
2747 dev_name(&ibm->acpi->device->dev),
2748 event, 0);
2749 return;
2752 while (1) {
2753 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2754 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2755 return;
2758 if (hkey == 0) {
2759 /* queue empty */
2760 return;
2763 send_acpi_ev = true;
2764 ignore_acpi_ev = false;
2766 switch (hkey >> 12) {
2767 case 1:
2768 /* 0x1000-0x1FFF: key presses */
2769 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2770 &ignore_acpi_ev);
2771 break;
2772 case 2:
2773 /* 0x2000-0x2FFF: Wakeup reason */
2774 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2775 &ignore_acpi_ev);
2776 break;
2777 case 3:
2778 /* 0x3000-0x3FFF: bay-related wakeups */
2779 if (hkey == 0x3003) {
2780 hotkey_autosleep_ack = 1;
2781 printk(TPACPI_INFO
2782 "bay ejected\n");
2783 hotkey_wakeup_hotunplug_complete_notify_change();
2784 known_ev = true;
2785 } else {
2786 known_ev = false;
2788 break;
2789 case 4:
2790 /* 0x4000-0x4FFF: dock-related wakeups */
2791 if (hkey == 0x4003) {
2792 hotkey_autosleep_ack = 1;
2793 printk(TPACPI_INFO
2794 "undocked\n");
2795 hotkey_wakeup_hotunplug_complete_notify_change();
2796 known_ev = true;
2797 } else {
2798 known_ev = false;
2800 break;
2801 case 5:
2802 /* 0x5000-0x5FFF: human interface helpers */
2803 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2804 &ignore_acpi_ev);
2805 break;
2806 case 6:
2807 /* 0x6000-0x6FFF: thermal alarms */
2808 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2809 &ignore_acpi_ev);
2810 break;
2811 case 7:
2812 /* 0x7000-0x7FFF: misc */
2813 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2814 tpacpi_send_radiosw_update();
2815 send_acpi_ev = 0;
2816 known_ev = true;
2817 break;
2819 /* fallthrough to default */
2820 default:
2821 known_ev = false;
2823 if (!known_ev) {
2824 printk(TPACPI_NOTICE
2825 "unhandled HKEY event 0x%04x\n", hkey);
2826 printk(TPACPI_NOTICE
2827 "please report the conditions when this "
2828 "event happened to %s\n", TPACPI_MAIL);
2831 /* Legacy events */
2832 if (!ignore_acpi_ev &&
2833 (send_acpi_ev || hotkey_report_mode < 2)) {
2834 acpi_bus_generate_proc_event(ibm->acpi->device,
2835 event, hkey);
2838 /* netlink events */
2839 if (!ignore_acpi_ev && send_acpi_ev) {
2840 acpi_bus_generate_netlink_event(
2841 ibm->acpi->device->pnp.device_class,
2842 dev_name(&ibm->acpi->device->dev),
2843 event, hkey);
2848 static void hotkey_suspend(pm_message_t state)
2850 /* Do these on suspend, we get the events on early resume! */
2851 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2852 hotkey_autosleep_ack = 0;
2855 static void hotkey_resume(void)
2857 tpacpi_disable_brightness_delay();
2859 if (hotkey_mask_get())
2860 printk(TPACPI_ERR
2861 "error while trying to read hot key mask "
2862 "from firmware\n");
2863 tpacpi_send_radiosw_update();
2864 hotkey_tablet_mode_notify_change();
2865 hotkey_wakeup_reason_notify_change();
2866 hotkey_wakeup_hotunplug_complete_notify_change();
2867 hotkey_poll_setup_safe(0);
2870 /* procfs -------------------------------------------------------------- */
2871 static int hotkey_read(char *p)
2873 int res, status;
2874 int len = 0;
2876 if (!tp_features.hotkey) {
2877 len += sprintf(p + len, "status:\t\tnot supported\n");
2878 return len;
2881 if (mutex_lock_killable(&hotkey_mutex))
2882 return -ERESTARTSYS;
2883 res = hotkey_status_get(&status);
2884 if (!res)
2885 res = hotkey_mask_get();
2886 mutex_unlock(&hotkey_mutex);
2887 if (res)
2888 return res;
2890 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2891 if (tp_features.hotkey_mask) {
2892 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2893 len += sprintf(p + len,
2894 "commands:\tenable, disable, reset, <mask>\n");
2895 } else {
2896 len += sprintf(p + len, "mask:\t\tnot supported\n");
2897 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2900 return len;
2903 static int hotkey_write(char *buf)
2905 int res, status;
2906 u32 mask;
2907 char *cmd;
2909 if (!tp_features.hotkey)
2910 return -ENODEV;
2912 if (mutex_lock_killable(&hotkey_mutex))
2913 return -ERESTARTSYS;
2915 status = -1;
2916 mask = hotkey_mask;
2918 res = 0;
2919 while ((cmd = next_cmd(&buf))) {
2920 if (strlencmp(cmd, "enable") == 0) {
2921 status = 1;
2922 } else if (strlencmp(cmd, "disable") == 0) {
2923 status = 0;
2924 } else if (strlencmp(cmd, "reset") == 0) {
2925 status = hotkey_orig_status;
2926 mask = hotkey_orig_mask;
2927 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2928 /* mask set */
2929 } else if (sscanf(cmd, "%x", &mask) == 1) {
2930 /* mask set */
2931 } else {
2932 res = -EINVAL;
2933 goto errexit;
2936 if (status != -1)
2937 res = hotkey_status_set(status);
2939 if (!res && mask != hotkey_mask)
2940 res = hotkey_mask_set(mask);
2942 errexit:
2943 mutex_unlock(&hotkey_mutex);
2944 return res;
2947 static const struct acpi_device_id ibm_htk_device_ids[] = {
2948 {TPACPI_ACPI_HKEY_HID, 0},
2949 {"", 0},
2952 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2953 .hid = ibm_htk_device_ids,
2954 .notify = hotkey_notify,
2955 .handle = &hkey_handle,
2956 .type = ACPI_DEVICE_NOTIFY,
2959 static struct ibm_struct hotkey_driver_data = {
2960 .name = "hotkey",
2961 .read = hotkey_read,
2962 .write = hotkey_write,
2963 .exit = hotkey_exit,
2964 .resume = hotkey_resume,
2965 .suspend = hotkey_suspend,
2966 .acpi = &ibm_hotkey_acpidriver,
2969 /*************************************************************************
2970 * Bluetooth subdriver
2973 enum {
2974 /* ACPI GBDC/SBDC bits */
2975 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2976 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2977 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2978 off / last state */
2981 enum {
2982 /* ACPI \BLTH commands */
2983 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2984 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2985 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2986 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2987 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2990 static struct rfkill *tpacpi_bluetooth_rfkill;
2992 static void bluetooth_suspend(pm_message_t state)
2994 /* Try to make sure radio will resume powered off */
2995 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2996 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2999 static int bluetooth_get_radiosw(void)
3001 int status;
3003 if (!tp_features.bluetooth)
3004 return -ENODEV;
3006 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3007 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3008 return RFKILL_STATE_HARD_BLOCKED;
3010 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3011 if (dbg_bluetoothemul)
3012 return (tpacpi_bluetooth_emulstate) ?
3013 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3014 #endif
3016 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3017 return -EIO;
3019 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3020 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3023 static void bluetooth_update_rfk(void)
3025 int status;
3027 if (!tpacpi_bluetooth_rfkill)
3028 return;
3030 status = bluetooth_get_radiosw();
3031 if (status < 0)
3032 return;
3033 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3036 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3038 int status;
3040 if (!tp_features.bluetooth)
3041 return -ENODEV;
3043 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3044 * reason to risk weird behaviour. */
3045 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3046 && radio_on)
3047 return -EPERM;
3049 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3050 if (dbg_bluetoothemul) {
3051 tpacpi_bluetooth_emulstate = !!radio_on;
3052 if (update_rfk)
3053 bluetooth_update_rfk();
3054 return 0;
3056 #endif
3058 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3059 if (radio_on)
3060 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3061 else
3062 status = 0;
3063 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3064 return -EIO;
3066 if (update_rfk)
3067 bluetooth_update_rfk();
3069 return 0;
3072 /* sysfs bluetooth enable ---------------------------------------------- */
3073 static ssize_t bluetooth_enable_show(struct device *dev,
3074 struct device_attribute *attr,
3075 char *buf)
3077 int status;
3079 status = bluetooth_get_radiosw();
3080 if (status < 0)
3081 return status;
3083 return snprintf(buf, PAGE_SIZE, "%d\n",
3084 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3087 static ssize_t bluetooth_enable_store(struct device *dev,
3088 struct device_attribute *attr,
3089 const char *buf, size_t count)
3091 unsigned long t;
3092 int res;
3094 if (parse_strtoul(buf, 1, &t))
3095 return -EINVAL;
3097 res = bluetooth_set_radiosw(t, 1);
3099 return (res) ? res : count;
3102 static struct device_attribute dev_attr_bluetooth_enable =
3103 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3104 bluetooth_enable_show, bluetooth_enable_store);
3106 /* --------------------------------------------------------------------- */
3108 static struct attribute *bluetooth_attributes[] = {
3109 &dev_attr_bluetooth_enable.attr,
3110 NULL
3113 static const struct attribute_group bluetooth_attr_group = {
3114 .attrs = bluetooth_attributes,
3117 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3119 int bts = bluetooth_get_radiosw();
3121 if (bts < 0)
3122 return bts;
3124 *state = bts;
3125 return 0;
3128 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3130 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3133 static void bluetooth_shutdown(void)
3135 /* Order firmware to save current state to NVRAM */
3136 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3137 TP_ACPI_BLTH_SAVE_STATE))
3138 printk(TPACPI_NOTICE
3139 "failed to save bluetooth state to NVRAM\n");
3142 static void bluetooth_exit(void)
3144 bluetooth_shutdown();
3146 if (tpacpi_bluetooth_rfkill)
3147 rfkill_unregister(tpacpi_bluetooth_rfkill);
3149 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3150 &bluetooth_attr_group);
3153 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3155 int res;
3156 int status = 0;
3158 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3160 TPACPI_ACPIHANDLE_INIT(hkey);
3162 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3163 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3164 tp_features.bluetooth = hkey_handle &&
3165 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3167 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3168 str_supported(tp_features.bluetooth),
3169 status);
3171 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3172 if (dbg_bluetoothemul) {
3173 tp_features.bluetooth = 1;
3174 printk(TPACPI_INFO
3175 "bluetooth switch emulation enabled\n");
3176 } else
3177 #endif
3178 if (tp_features.bluetooth &&
3179 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3180 /* no bluetooth hardware present in system */
3181 tp_features.bluetooth = 0;
3182 dbg_printk(TPACPI_DBG_INIT,
3183 "bluetooth hardware not installed\n");
3186 if (!tp_features.bluetooth)
3187 return 1;
3189 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3190 &bluetooth_attr_group);
3191 if (res)
3192 return res;
3194 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3195 &tpacpi_bluetooth_rfkill,
3196 RFKILL_TYPE_BLUETOOTH,
3197 "tpacpi_bluetooth_sw",
3198 true,
3199 tpacpi_bluetooth_rfk_set,
3200 tpacpi_bluetooth_rfk_get);
3201 if (res) {
3202 bluetooth_exit();
3203 return res;
3206 return 0;
3209 /* procfs -------------------------------------------------------------- */
3210 static int bluetooth_read(char *p)
3212 int len = 0;
3213 int status = bluetooth_get_radiosw();
3215 if (!tp_features.bluetooth)
3216 len += sprintf(p + len, "status:\t\tnot supported\n");
3217 else {
3218 len += sprintf(p + len, "status:\t\t%s\n",
3219 (status == RFKILL_STATE_UNBLOCKED) ?
3220 "enabled" : "disabled");
3221 len += sprintf(p + len, "commands:\tenable, disable\n");
3224 return len;
3227 static int bluetooth_write(char *buf)
3229 char *cmd;
3231 if (!tp_features.bluetooth)
3232 return -ENODEV;
3234 while ((cmd = next_cmd(&buf))) {
3235 if (strlencmp(cmd, "enable") == 0) {
3236 bluetooth_set_radiosw(1, 1);
3237 } else if (strlencmp(cmd, "disable") == 0) {
3238 bluetooth_set_radiosw(0, 1);
3239 } else
3240 return -EINVAL;
3243 return 0;
3246 static struct ibm_struct bluetooth_driver_data = {
3247 .name = "bluetooth",
3248 .read = bluetooth_read,
3249 .write = bluetooth_write,
3250 .exit = bluetooth_exit,
3251 .suspend = bluetooth_suspend,
3252 .shutdown = bluetooth_shutdown,
3255 /*************************************************************************
3256 * Wan subdriver
3259 enum {
3260 /* ACPI GWAN/SWAN bits */
3261 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3262 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3263 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3264 off / last state */
3267 static struct rfkill *tpacpi_wan_rfkill;
3269 static void wan_suspend(pm_message_t state)
3271 /* Try to make sure radio will resume powered off */
3272 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3273 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3276 static int wan_get_radiosw(void)
3278 int status;
3280 if (!tp_features.wan)
3281 return -ENODEV;
3283 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3284 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3285 return RFKILL_STATE_HARD_BLOCKED;
3287 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3288 if (dbg_wwanemul)
3289 return (tpacpi_wwan_emulstate) ?
3290 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3291 #endif
3293 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3294 return -EIO;
3296 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3297 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3300 static void wan_update_rfk(void)
3302 int status;
3304 if (!tpacpi_wan_rfkill)
3305 return;
3307 status = wan_get_radiosw();
3308 if (status < 0)
3309 return;
3310 rfkill_force_state(tpacpi_wan_rfkill, status);
3313 static int wan_set_radiosw(int radio_on, int update_rfk)
3315 int status;
3317 if (!tp_features.wan)
3318 return -ENODEV;
3320 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3321 * reason to risk weird behaviour. */
3322 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3323 && radio_on)
3324 return -EPERM;
3326 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3327 if (dbg_wwanemul) {
3328 tpacpi_wwan_emulstate = !!radio_on;
3329 if (update_rfk)
3330 wan_update_rfk();
3331 return 0;
3333 #endif
3335 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3336 if (radio_on)
3337 status = TP_ACPI_WANCARD_RADIOSSW;
3338 else
3339 status = 0;
3340 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3341 return -EIO;
3343 if (update_rfk)
3344 wan_update_rfk();
3346 return 0;
3349 /* sysfs wan enable ---------------------------------------------------- */
3350 static ssize_t wan_enable_show(struct device *dev,
3351 struct device_attribute *attr,
3352 char *buf)
3354 int status;
3356 status = wan_get_radiosw();
3357 if (status < 0)
3358 return status;
3360 return snprintf(buf, PAGE_SIZE, "%d\n",
3361 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3364 static ssize_t wan_enable_store(struct device *dev,
3365 struct device_attribute *attr,
3366 const char *buf, size_t count)
3368 unsigned long t;
3369 int res;
3371 if (parse_strtoul(buf, 1, &t))
3372 return -EINVAL;
3374 res = wan_set_radiosw(t, 1);
3376 return (res) ? res : count;
3379 static struct device_attribute dev_attr_wan_enable =
3380 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3381 wan_enable_show, wan_enable_store);
3383 /* --------------------------------------------------------------------- */
3385 static struct attribute *wan_attributes[] = {
3386 &dev_attr_wan_enable.attr,
3387 NULL
3390 static const struct attribute_group wan_attr_group = {
3391 .attrs = wan_attributes,
3394 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3396 int wans = wan_get_radiosw();
3398 if (wans < 0)
3399 return wans;
3401 *state = wans;
3402 return 0;
3405 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3407 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3410 static void wan_shutdown(void)
3412 /* Order firmware to save current state to NVRAM */
3413 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3414 TP_ACPI_WGSV_SAVE_STATE))
3415 printk(TPACPI_NOTICE
3416 "failed to save WWAN state to NVRAM\n");
3419 static void wan_exit(void)
3421 wan_shutdown();
3423 if (tpacpi_wan_rfkill)
3424 rfkill_unregister(tpacpi_wan_rfkill);
3426 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3427 &wan_attr_group);
3430 static int __init wan_init(struct ibm_init_struct *iibm)
3432 int res;
3433 int status = 0;
3435 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3437 TPACPI_ACPIHANDLE_INIT(hkey);
3439 tp_features.wan = hkey_handle &&
3440 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3442 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3443 str_supported(tp_features.wan),
3444 status);
3446 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3447 if (dbg_wwanemul) {
3448 tp_features.wan = 1;
3449 printk(TPACPI_INFO
3450 "wwan switch emulation enabled\n");
3451 } else
3452 #endif
3453 if (tp_features.wan &&
3454 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3455 /* no wan hardware present in system */
3456 tp_features.wan = 0;
3457 dbg_printk(TPACPI_DBG_INIT,
3458 "wan hardware not installed\n");
3461 if (!tp_features.wan)
3462 return 1;
3464 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3465 &wan_attr_group);
3466 if (res)
3467 return res;
3469 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3470 &tpacpi_wan_rfkill,
3471 RFKILL_TYPE_WWAN,
3472 "tpacpi_wwan_sw",
3473 true,
3474 tpacpi_wan_rfk_set,
3475 tpacpi_wan_rfk_get);
3476 if (res) {
3477 wan_exit();
3478 return res;
3481 return 0;
3484 /* procfs -------------------------------------------------------------- */
3485 static int wan_read(char *p)
3487 int len = 0;
3488 int status = wan_get_radiosw();
3490 if (!tp_features.wan)
3491 len += sprintf(p + len, "status:\t\tnot supported\n");
3492 else {
3493 len += sprintf(p + len, "status:\t\t%s\n",
3494 (status == RFKILL_STATE_UNBLOCKED) ?
3495 "enabled" : "disabled");
3496 len += sprintf(p + len, "commands:\tenable, disable\n");
3499 return len;
3502 static int wan_write(char *buf)
3504 char *cmd;
3506 if (!tp_features.wan)
3507 return -ENODEV;
3509 while ((cmd = next_cmd(&buf))) {
3510 if (strlencmp(cmd, "enable") == 0) {
3511 wan_set_radiosw(1, 1);
3512 } else if (strlencmp(cmd, "disable") == 0) {
3513 wan_set_radiosw(0, 1);
3514 } else
3515 return -EINVAL;
3518 return 0;
3521 static struct ibm_struct wan_driver_data = {
3522 .name = "wan",
3523 .read = wan_read,
3524 .write = wan_write,
3525 .exit = wan_exit,
3526 .suspend = wan_suspend,
3527 .shutdown = wan_shutdown,
3530 /*************************************************************************
3531 * UWB subdriver
3534 enum {
3535 /* ACPI GUWB/SUWB bits */
3536 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3537 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3540 static struct rfkill *tpacpi_uwb_rfkill;
3542 static int uwb_get_radiosw(void)
3544 int status;
3546 if (!tp_features.uwb)
3547 return -ENODEV;
3549 /* WLSW overrides UWB in firmware/hardware, reflect that */
3550 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3551 return RFKILL_STATE_HARD_BLOCKED;
3553 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3554 if (dbg_uwbemul)
3555 return (tpacpi_uwb_emulstate) ?
3556 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3557 #endif
3559 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3560 return -EIO;
3562 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3563 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3566 static void uwb_update_rfk(void)
3568 int status;
3570 if (!tpacpi_uwb_rfkill)
3571 return;
3573 status = uwb_get_radiosw();
3574 if (status < 0)
3575 return;
3576 rfkill_force_state(tpacpi_uwb_rfkill, status);
3579 static int uwb_set_radiosw(int radio_on, int update_rfk)
3581 int status;
3583 if (!tp_features.uwb)
3584 return -ENODEV;
3586 /* WLSW overrides UWB in firmware/hardware, but there is no
3587 * reason to risk weird behaviour. */
3588 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3589 && radio_on)
3590 return -EPERM;
3592 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3593 if (dbg_uwbemul) {
3594 tpacpi_uwb_emulstate = !!radio_on;
3595 if (update_rfk)
3596 uwb_update_rfk();
3597 return 0;
3599 #endif
3601 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3602 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3603 return -EIO;
3605 if (update_rfk)
3606 uwb_update_rfk();
3608 return 0;
3611 /* --------------------------------------------------------------------- */
3613 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3615 int uwbs = uwb_get_radiosw();
3617 if (uwbs < 0)
3618 return uwbs;
3620 *state = uwbs;
3621 return 0;
3624 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3626 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3629 static void uwb_exit(void)
3631 if (tpacpi_uwb_rfkill)
3632 rfkill_unregister(tpacpi_uwb_rfkill);
3635 static int __init uwb_init(struct ibm_init_struct *iibm)
3637 int res;
3638 int status = 0;
3640 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3642 TPACPI_ACPIHANDLE_INIT(hkey);
3644 tp_features.uwb = hkey_handle &&
3645 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3647 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3648 str_supported(tp_features.uwb),
3649 status);
3651 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3652 if (dbg_uwbemul) {
3653 tp_features.uwb = 1;
3654 printk(TPACPI_INFO
3655 "uwb switch emulation enabled\n");
3656 } else
3657 #endif
3658 if (tp_features.uwb &&
3659 !(status & TP_ACPI_UWB_HWPRESENT)) {
3660 /* no uwb hardware present in system */
3661 tp_features.uwb = 0;
3662 dbg_printk(TPACPI_DBG_INIT,
3663 "uwb hardware not installed\n");
3666 if (!tp_features.uwb)
3667 return 1;
3669 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3670 &tpacpi_uwb_rfkill,
3671 RFKILL_TYPE_UWB,
3672 "tpacpi_uwb_sw",
3673 false,
3674 tpacpi_uwb_rfk_set,
3675 tpacpi_uwb_rfk_get);
3677 return res;
3680 static struct ibm_struct uwb_driver_data = {
3681 .name = "uwb",
3682 .exit = uwb_exit,
3683 .flags.experimental = 1,
3686 /*************************************************************************
3687 * Video subdriver
3690 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3692 enum video_access_mode {
3693 TPACPI_VIDEO_NONE = 0,
3694 TPACPI_VIDEO_570, /* 570 */
3695 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3696 TPACPI_VIDEO_NEW, /* all others */
3699 enum { /* video status flags, based on VIDEO_570 */
3700 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3701 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3702 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3705 enum { /* TPACPI_VIDEO_570 constants */
3706 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3707 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3708 * video_status_flags */
3709 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3710 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3713 static enum video_access_mode video_supported;
3714 static int video_orig_autosw;
3716 static int video_autosw_get(void);
3717 static int video_autosw_set(int enable);
3719 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3721 static int __init video_init(struct ibm_init_struct *iibm)
3723 int ivga;
3725 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3727 TPACPI_ACPIHANDLE_INIT(vid);
3728 TPACPI_ACPIHANDLE_INIT(vid2);
3730 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3731 /* G41, assume IVGA doesn't change */
3732 vid_handle = vid2_handle;
3734 if (!vid_handle)
3735 /* video switching not supported on R30, R31 */
3736 video_supported = TPACPI_VIDEO_NONE;
3737 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3738 /* 570 */
3739 video_supported = TPACPI_VIDEO_570;
3740 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3741 /* 600e/x, 770e, 770x */
3742 video_supported = TPACPI_VIDEO_770;
3743 else
3744 /* all others */
3745 video_supported = TPACPI_VIDEO_NEW;
3747 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3748 str_supported(video_supported != TPACPI_VIDEO_NONE),
3749 video_supported);
3751 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3754 static void video_exit(void)
3756 dbg_printk(TPACPI_DBG_EXIT,
3757 "restoring original video autoswitch mode\n");
3758 if (video_autosw_set(video_orig_autosw))
3759 printk(TPACPI_ERR "error while trying to restore original "
3760 "video autoswitch mode\n");
3763 static int video_outputsw_get(void)
3765 int status = 0;
3766 int i;
3768 switch (video_supported) {
3769 case TPACPI_VIDEO_570:
3770 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3771 TP_ACPI_VIDEO_570_PHSCMD))
3772 return -EIO;
3773 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3774 break;
3775 case TPACPI_VIDEO_770:
3776 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3777 return -EIO;
3778 if (i)
3779 status |= TP_ACPI_VIDEO_S_LCD;
3780 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3781 return -EIO;
3782 if (i)
3783 status |= TP_ACPI_VIDEO_S_CRT;
3784 break;
3785 case TPACPI_VIDEO_NEW:
3786 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3787 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3788 return -EIO;
3789 if (i)
3790 status |= TP_ACPI_VIDEO_S_CRT;
3792 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3793 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3794 return -EIO;
3795 if (i)
3796 status |= TP_ACPI_VIDEO_S_LCD;
3797 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3798 return -EIO;
3799 if (i)
3800 status |= TP_ACPI_VIDEO_S_DVI;
3801 break;
3802 default:
3803 return -ENOSYS;
3806 return status;
3809 static int video_outputsw_set(int status)
3811 int autosw;
3812 int res = 0;
3814 switch (video_supported) {
3815 case TPACPI_VIDEO_570:
3816 res = acpi_evalf(NULL, NULL,
3817 "\\_SB.PHS2", "vdd",
3818 TP_ACPI_VIDEO_570_PHS2CMD,
3819 status | TP_ACPI_VIDEO_570_PHS2SET);
3820 break;
3821 case TPACPI_VIDEO_770:
3822 autosw = video_autosw_get();
3823 if (autosw < 0)
3824 return autosw;
3826 res = video_autosw_set(1);
3827 if (res)
3828 return res;
3829 res = acpi_evalf(vid_handle, NULL,
3830 "ASWT", "vdd", status * 0x100, 0);
3831 if (!autosw && video_autosw_set(autosw)) {
3832 printk(TPACPI_ERR
3833 "video auto-switch left enabled due to error\n");
3834 return -EIO;
3836 break;
3837 case TPACPI_VIDEO_NEW:
3838 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3839 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3840 break;
3841 default:
3842 return -ENOSYS;
3845 return (res)? 0 : -EIO;
3848 static int video_autosw_get(void)
3850 int autosw = 0;
3852 switch (video_supported) {
3853 case TPACPI_VIDEO_570:
3854 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3855 return -EIO;
3856 break;
3857 case TPACPI_VIDEO_770:
3858 case TPACPI_VIDEO_NEW:
3859 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3860 return -EIO;
3861 break;
3862 default:
3863 return -ENOSYS;
3866 return autosw & 1;
3869 static int video_autosw_set(int enable)
3871 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3872 return -EIO;
3873 return 0;
3876 static int video_outputsw_cycle(void)
3878 int autosw = video_autosw_get();
3879 int res;
3881 if (autosw < 0)
3882 return autosw;
3884 switch (video_supported) {
3885 case TPACPI_VIDEO_570:
3886 res = video_autosw_set(1);
3887 if (res)
3888 return res;
3889 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3890 break;
3891 case TPACPI_VIDEO_770:
3892 case TPACPI_VIDEO_NEW:
3893 res = video_autosw_set(1);
3894 if (res)
3895 return res;
3896 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3897 break;
3898 default:
3899 return -ENOSYS;
3901 if (!autosw && video_autosw_set(autosw)) {
3902 printk(TPACPI_ERR
3903 "video auto-switch left enabled due to error\n");
3904 return -EIO;
3907 return (res)? 0 : -EIO;
3910 static int video_expand_toggle(void)
3912 switch (video_supported) {
3913 case TPACPI_VIDEO_570:
3914 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3915 0 : -EIO;
3916 case TPACPI_VIDEO_770:
3917 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3918 0 : -EIO;
3919 case TPACPI_VIDEO_NEW:
3920 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3921 0 : -EIO;
3922 default:
3923 return -ENOSYS;
3925 /* not reached */
3928 static int video_read(char *p)
3930 int status, autosw;
3931 int len = 0;
3933 if (video_supported == TPACPI_VIDEO_NONE) {
3934 len += sprintf(p + len, "status:\t\tnot supported\n");
3935 return len;
3938 status = video_outputsw_get();
3939 if (status < 0)
3940 return status;
3942 autosw = video_autosw_get();
3943 if (autosw < 0)
3944 return autosw;
3946 len += sprintf(p + len, "status:\t\tsupported\n");
3947 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3948 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3949 if (video_supported == TPACPI_VIDEO_NEW)
3950 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3951 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3952 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3953 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3954 if (video_supported == TPACPI_VIDEO_NEW)
3955 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3956 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3957 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3959 return len;
3962 static int video_write(char *buf)
3964 char *cmd;
3965 int enable, disable, status;
3966 int res;
3968 if (video_supported == TPACPI_VIDEO_NONE)
3969 return -ENODEV;
3971 enable = 0;
3972 disable = 0;
3974 while ((cmd = next_cmd(&buf))) {
3975 if (strlencmp(cmd, "lcd_enable") == 0) {
3976 enable |= TP_ACPI_VIDEO_S_LCD;
3977 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3978 disable |= TP_ACPI_VIDEO_S_LCD;
3979 } else if (strlencmp(cmd, "crt_enable") == 0) {
3980 enable |= TP_ACPI_VIDEO_S_CRT;
3981 } else if (strlencmp(cmd, "crt_disable") == 0) {
3982 disable |= TP_ACPI_VIDEO_S_CRT;
3983 } else if (video_supported == TPACPI_VIDEO_NEW &&
3984 strlencmp(cmd, "dvi_enable") == 0) {
3985 enable |= TP_ACPI_VIDEO_S_DVI;
3986 } else if (video_supported == TPACPI_VIDEO_NEW &&
3987 strlencmp(cmd, "dvi_disable") == 0) {
3988 disable |= TP_ACPI_VIDEO_S_DVI;
3989 } else if (strlencmp(cmd, "auto_enable") == 0) {
3990 res = video_autosw_set(1);
3991 if (res)
3992 return res;
3993 } else if (strlencmp(cmd, "auto_disable") == 0) {
3994 res = video_autosw_set(0);
3995 if (res)
3996 return res;
3997 } else if (strlencmp(cmd, "video_switch") == 0) {
3998 res = video_outputsw_cycle();
3999 if (res)
4000 return res;
4001 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4002 res = video_expand_toggle();
4003 if (res)
4004 return res;
4005 } else
4006 return -EINVAL;
4009 if (enable || disable) {
4010 status = video_outputsw_get();
4011 if (status < 0)
4012 return status;
4013 res = video_outputsw_set((status & ~disable) | enable);
4014 if (res)
4015 return res;
4018 return 0;
4021 static struct ibm_struct video_driver_data = {
4022 .name = "video",
4023 .read = video_read,
4024 .write = video_write,
4025 .exit = video_exit,
4028 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4030 /*************************************************************************
4031 * Light (thinklight) subdriver
4034 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4035 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4037 static int light_get_status(void)
4039 int status = 0;
4041 if (tp_features.light_status) {
4042 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4043 return -EIO;
4044 return (!!status);
4047 return -ENXIO;
4050 static int light_set_status(int status)
4052 int rc;
4054 if (tp_features.light) {
4055 if (cmos_handle) {
4056 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4057 (status)?
4058 TP_CMOS_THINKLIGHT_ON :
4059 TP_CMOS_THINKLIGHT_OFF);
4060 } else {
4061 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4062 (status)? 1 : 0);
4064 return (rc)? 0 : -EIO;
4067 return -ENXIO;
4070 static void light_set_status_worker(struct work_struct *work)
4072 struct tpacpi_led_classdev *data =
4073 container_of(work, struct tpacpi_led_classdev, work);
4075 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4076 light_set_status((data->new_state != TPACPI_LED_OFF));
4079 static void light_sysfs_set(struct led_classdev *led_cdev,
4080 enum led_brightness brightness)
4082 struct tpacpi_led_classdev *data =
4083 container_of(led_cdev,
4084 struct tpacpi_led_classdev,
4085 led_classdev);
4086 data->new_state = (brightness != LED_OFF) ?
4087 TPACPI_LED_ON : TPACPI_LED_OFF;
4088 queue_work(tpacpi_wq, &data->work);
4091 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4093 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4096 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4097 .led_classdev = {
4098 .name = "tpacpi::thinklight",
4099 .brightness_set = &light_sysfs_set,
4100 .brightness_get = &light_sysfs_get,
4104 static int __init light_init(struct ibm_init_struct *iibm)
4106 int rc;
4108 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4110 TPACPI_ACPIHANDLE_INIT(ledb);
4111 TPACPI_ACPIHANDLE_INIT(lght);
4112 TPACPI_ACPIHANDLE_INIT(cmos);
4113 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4115 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4116 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4118 if (tp_features.light)
4119 /* light status not supported on
4120 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4121 tp_features.light_status =
4122 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4124 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4125 str_supported(tp_features.light),
4126 str_supported(tp_features.light_status));
4128 if (!tp_features.light)
4129 return 1;
4131 rc = led_classdev_register(&tpacpi_pdev->dev,
4132 &tpacpi_led_thinklight.led_classdev);
4134 if (rc < 0) {
4135 tp_features.light = 0;
4136 tp_features.light_status = 0;
4137 } else {
4138 rc = 0;
4141 return rc;
4144 static void light_exit(void)
4146 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4147 if (work_pending(&tpacpi_led_thinklight.work))
4148 flush_workqueue(tpacpi_wq);
4151 static int light_read(char *p)
4153 int len = 0;
4154 int status;
4156 if (!tp_features.light) {
4157 len += sprintf(p + len, "status:\t\tnot supported\n");
4158 } else if (!tp_features.light_status) {
4159 len += sprintf(p + len, "status:\t\tunknown\n");
4160 len += sprintf(p + len, "commands:\ton, off\n");
4161 } else {
4162 status = light_get_status();
4163 if (status < 0)
4164 return status;
4165 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4166 len += sprintf(p + len, "commands:\ton, off\n");
4169 return len;
4172 static int light_write(char *buf)
4174 char *cmd;
4175 int newstatus = 0;
4177 if (!tp_features.light)
4178 return -ENODEV;
4180 while ((cmd = next_cmd(&buf))) {
4181 if (strlencmp(cmd, "on") == 0) {
4182 newstatus = 1;
4183 } else if (strlencmp(cmd, "off") == 0) {
4184 newstatus = 0;
4185 } else
4186 return -EINVAL;
4189 return light_set_status(newstatus);
4192 static struct ibm_struct light_driver_data = {
4193 .name = "light",
4194 .read = light_read,
4195 .write = light_write,
4196 .exit = light_exit,
4199 /*************************************************************************
4200 * Dock subdriver
4203 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4205 static void dock_notify(struct ibm_struct *ibm, u32 event);
4206 static int dock_read(char *p);
4207 static int dock_write(char *buf);
4209 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4210 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4211 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4212 "\\_SB.PCI.ISA.SLCE", /* 570 */
4213 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4215 /* don't list other alternatives as we install a notify handler on the 570 */
4216 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4218 static const struct acpi_device_id ibm_pci_device_ids[] = {
4219 {PCI_ROOT_HID_STRING, 0},
4220 {"", 0},
4223 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4225 .notify = dock_notify,
4226 .handle = &dock_handle,
4227 .type = ACPI_SYSTEM_NOTIFY,
4230 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4231 * We just use it to get notifications of dock hotplug
4232 * in very old thinkpads */
4233 .hid = ibm_pci_device_ids,
4234 .notify = dock_notify,
4235 .handle = &pci_handle,
4236 .type = ACPI_SYSTEM_NOTIFY,
4240 static struct ibm_struct dock_driver_data[2] = {
4242 .name = "dock",
4243 .read = dock_read,
4244 .write = dock_write,
4245 .acpi = &ibm_dock_acpidriver[0],
4248 .name = "dock",
4249 .acpi = &ibm_dock_acpidriver[1],
4253 #define dock_docked() (_sta(dock_handle) & 1)
4255 static int __init dock_init(struct ibm_init_struct *iibm)
4257 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4259 TPACPI_ACPIHANDLE_INIT(dock);
4261 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4262 str_supported(dock_handle != NULL));
4264 return (dock_handle)? 0 : 1;
4267 static int __init dock_init2(struct ibm_init_struct *iibm)
4269 int dock2_needed;
4271 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4273 if (dock_driver_data[0].flags.acpi_driver_registered &&
4274 dock_driver_data[0].flags.acpi_notify_installed) {
4275 TPACPI_ACPIHANDLE_INIT(pci);
4276 dock2_needed = (pci_handle != NULL);
4277 vdbg_printk(TPACPI_DBG_INIT,
4278 "dock PCI handler for the TP 570 is %s\n",
4279 str_supported(dock2_needed));
4280 } else {
4281 vdbg_printk(TPACPI_DBG_INIT,
4282 "dock subdriver part 2 not required\n");
4283 dock2_needed = 0;
4286 return (dock2_needed)? 0 : 1;
4289 static void dock_notify(struct ibm_struct *ibm, u32 event)
4291 int docked = dock_docked();
4292 int pci = ibm->acpi->hid && ibm->acpi->device &&
4293 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4294 int data;
4296 if (event == 1 && !pci) /* 570 */
4297 data = 1; /* button */
4298 else if (event == 1 && pci) /* 570 */
4299 data = 3; /* dock */
4300 else if (event == 3 && docked)
4301 data = 1; /* button */
4302 else if (event == 3 && !docked)
4303 data = 2; /* undock */
4304 else if (event == 0 && docked)
4305 data = 3; /* dock */
4306 else {
4307 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4308 event, _sta(dock_handle));
4309 data = 0; /* unknown */
4311 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4312 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4313 dev_name(&ibm->acpi->device->dev),
4314 event, data);
4317 static int dock_read(char *p)
4319 int len = 0;
4320 int docked = dock_docked();
4322 if (!dock_handle)
4323 len += sprintf(p + len, "status:\t\tnot supported\n");
4324 else if (!docked)
4325 len += sprintf(p + len, "status:\t\tundocked\n");
4326 else {
4327 len += sprintf(p + len, "status:\t\tdocked\n");
4328 len += sprintf(p + len, "commands:\tdock, undock\n");
4331 return len;
4334 static int dock_write(char *buf)
4336 char *cmd;
4338 if (!dock_docked())
4339 return -ENODEV;
4341 while ((cmd = next_cmd(&buf))) {
4342 if (strlencmp(cmd, "undock") == 0) {
4343 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4344 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4345 return -EIO;
4346 } else if (strlencmp(cmd, "dock") == 0) {
4347 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4348 return -EIO;
4349 } else
4350 return -EINVAL;
4353 return 0;
4356 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4358 /*************************************************************************
4359 * Bay subdriver
4362 #ifdef CONFIG_THINKPAD_ACPI_BAY
4364 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4365 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4366 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4367 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4368 ); /* A21e, R30, R31 */
4369 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4370 "_EJ0", /* all others */
4371 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4372 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4373 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4374 ); /* all others */
4375 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4376 "_EJ0", /* 770x */
4377 ); /* all others */
4379 static int __init bay_init(struct ibm_init_struct *iibm)
4381 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4383 TPACPI_ACPIHANDLE_INIT(bay);
4384 if (bay_handle)
4385 TPACPI_ACPIHANDLE_INIT(bay_ej);
4386 TPACPI_ACPIHANDLE_INIT(bay2);
4387 if (bay2_handle)
4388 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4390 tp_features.bay_status = bay_handle &&
4391 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4392 tp_features.bay_status2 = bay2_handle &&
4393 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4395 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4396 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4397 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4398 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4400 vdbg_printk(TPACPI_DBG_INIT,
4401 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4402 str_supported(tp_features.bay_status),
4403 str_supported(tp_features.bay_eject),
4404 str_supported(tp_features.bay_status2),
4405 str_supported(tp_features.bay_eject2));
4407 return (tp_features.bay_status || tp_features.bay_eject ||
4408 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4411 static void bay_notify(struct ibm_struct *ibm, u32 event)
4413 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4414 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4415 dev_name(&ibm->acpi->device->dev),
4416 event, 0);
4419 #define bay_occupied(b) (_sta(b##_handle) & 1)
4421 static int bay_read(char *p)
4423 int len = 0;
4424 int occupied = bay_occupied(bay);
4425 int occupied2 = bay_occupied(bay2);
4426 int eject, eject2;
4428 len += sprintf(p + len, "status:\t\t%s\n",
4429 tp_features.bay_status ?
4430 (occupied ? "occupied" : "unoccupied") :
4431 "not supported");
4432 if (tp_features.bay_status2)
4433 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4434 "occupied" : "unoccupied");
4436 eject = tp_features.bay_eject && occupied;
4437 eject2 = tp_features.bay_eject2 && occupied2;
4439 if (eject && eject2)
4440 len += sprintf(p + len, "commands:\teject, eject2\n");
4441 else if (eject)
4442 len += sprintf(p + len, "commands:\teject\n");
4443 else if (eject2)
4444 len += sprintf(p + len, "commands:\teject2\n");
4446 return len;
4449 static int bay_write(char *buf)
4451 char *cmd;
4453 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4454 return -ENODEV;
4456 while ((cmd = next_cmd(&buf))) {
4457 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4458 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4459 return -EIO;
4460 } else if (tp_features.bay_eject2 &&
4461 strlencmp(cmd, "eject2") == 0) {
4462 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4463 return -EIO;
4464 } else
4465 return -EINVAL;
4468 return 0;
4471 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4472 .notify = bay_notify,
4473 .handle = &bay_handle,
4474 .type = ACPI_SYSTEM_NOTIFY,
4477 static struct ibm_struct bay_driver_data = {
4478 .name = "bay",
4479 .read = bay_read,
4480 .write = bay_write,
4481 .acpi = &ibm_bay_acpidriver,
4484 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4486 /*************************************************************************
4487 * CMOS subdriver
4490 /* sysfs cmos_command -------------------------------------------------- */
4491 static ssize_t cmos_command_store(struct device *dev,
4492 struct device_attribute *attr,
4493 const char *buf, size_t count)
4495 unsigned long cmos_cmd;
4496 int res;
4498 if (parse_strtoul(buf, 21, &cmos_cmd))
4499 return -EINVAL;
4501 res = issue_thinkpad_cmos_command(cmos_cmd);
4502 return (res)? res : count;
4505 static struct device_attribute dev_attr_cmos_command =
4506 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4508 /* --------------------------------------------------------------------- */
4510 static int __init cmos_init(struct ibm_init_struct *iibm)
4512 int res;
4514 vdbg_printk(TPACPI_DBG_INIT,
4515 "initializing cmos commands subdriver\n");
4517 TPACPI_ACPIHANDLE_INIT(cmos);
4519 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4520 str_supported(cmos_handle != NULL));
4522 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4523 if (res)
4524 return res;
4526 return (cmos_handle)? 0 : 1;
4529 static void cmos_exit(void)
4531 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4534 static int cmos_read(char *p)
4536 int len = 0;
4538 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4539 R30, R31, T20-22, X20-21 */
4540 if (!cmos_handle)
4541 len += sprintf(p + len, "status:\t\tnot supported\n");
4542 else {
4543 len += sprintf(p + len, "status:\t\tsupported\n");
4544 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4547 return len;
4550 static int cmos_write(char *buf)
4552 char *cmd;
4553 int cmos_cmd, res;
4555 while ((cmd = next_cmd(&buf))) {
4556 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4557 cmos_cmd >= 0 && cmos_cmd <= 21) {
4558 /* cmos_cmd set */
4559 } else
4560 return -EINVAL;
4562 res = issue_thinkpad_cmos_command(cmos_cmd);
4563 if (res)
4564 return res;
4567 return 0;
4570 static struct ibm_struct cmos_driver_data = {
4571 .name = "cmos",
4572 .read = cmos_read,
4573 .write = cmos_write,
4574 .exit = cmos_exit,
4577 /*************************************************************************
4578 * LED subdriver
4581 enum led_access_mode {
4582 TPACPI_LED_NONE = 0,
4583 TPACPI_LED_570, /* 570 */
4584 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4585 TPACPI_LED_NEW, /* all others */
4588 enum { /* For TPACPI_LED_OLD */
4589 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4590 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4591 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4594 static enum led_access_mode led_supported;
4596 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4597 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4598 /* T20-22, X20-21 */
4599 "LED", /* all others */
4600 ); /* R30, R31 */
4602 #define TPACPI_LED_NUMLEDS 8
4603 static struct tpacpi_led_classdev *tpacpi_leds;
4604 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4605 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4606 /* there's a limit of 19 chars + NULL before 2.6.26 */
4607 "tpacpi::power",
4608 "tpacpi:orange:batt",
4609 "tpacpi:green:batt",
4610 "tpacpi::dock_active",
4611 "tpacpi::bay_active",
4612 "tpacpi::dock_batt",
4613 "tpacpi::unknown_led",
4614 "tpacpi::standby",
4617 static int led_get_status(const unsigned int led)
4619 int status;
4620 enum led_status_t led_s;
4622 switch (led_supported) {
4623 case TPACPI_LED_570:
4624 if (!acpi_evalf(ec_handle,
4625 &status, "GLED", "dd", 1 << led))
4626 return -EIO;
4627 led_s = (status == 0)?
4628 TPACPI_LED_OFF :
4629 ((status == 1)?
4630 TPACPI_LED_ON :
4631 TPACPI_LED_BLINK);
4632 tpacpi_led_state_cache[led] = led_s;
4633 return led_s;
4634 default:
4635 return -ENXIO;
4638 /* not reached */
4641 static int led_set_status(const unsigned int led,
4642 const enum led_status_t ledstatus)
4644 /* off, on, blink. Index is led_status_t */
4645 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4646 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4648 int rc = 0;
4650 switch (led_supported) {
4651 case TPACPI_LED_570:
4652 /* 570 */
4653 if (led > 7)
4654 return -EINVAL;
4655 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4656 (1 << led), led_sled_arg1[ledstatus]))
4657 rc = -EIO;
4658 break;
4659 case TPACPI_LED_OLD:
4660 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4661 if (led > 7)
4662 return -EINVAL;
4663 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4664 if (rc >= 0)
4665 rc = ec_write(TPACPI_LED_EC_HLBL,
4666 (ledstatus == TPACPI_LED_BLINK) << led);
4667 if (rc >= 0)
4668 rc = ec_write(TPACPI_LED_EC_HLCL,
4669 (ledstatus != TPACPI_LED_OFF) << led);
4670 break;
4671 case TPACPI_LED_NEW:
4672 /* all others */
4673 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4674 led, led_led_arg1[ledstatus]))
4675 rc = -EIO;
4676 break;
4677 default:
4678 rc = -ENXIO;
4681 if (!rc)
4682 tpacpi_led_state_cache[led] = ledstatus;
4684 return rc;
4687 static void led_set_status_worker(struct work_struct *work)
4689 struct tpacpi_led_classdev *data =
4690 container_of(work, struct tpacpi_led_classdev, work);
4692 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4693 led_set_status(data->led, data->new_state);
4696 static void led_sysfs_set(struct led_classdev *led_cdev,
4697 enum led_brightness brightness)
4699 struct tpacpi_led_classdev *data = container_of(led_cdev,
4700 struct tpacpi_led_classdev, led_classdev);
4702 if (brightness == LED_OFF)
4703 data->new_state = TPACPI_LED_OFF;
4704 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4705 data->new_state = TPACPI_LED_ON;
4706 else
4707 data->new_state = TPACPI_LED_BLINK;
4709 queue_work(tpacpi_wq, &data->work);
4712 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4713 unsigned long *delay_on, unsigned long *delay_off)
4715 struct tpacpi_led_classdev *data = container_of(led_cdev,
4716 struct tpacpi_led_classdev, led_classdev);
4718 /* Can we choose the flash rate? */
4719 if (*delay_on == 0 && *delay_off == 0) {
4720 /* yes. set them to the hardware blink rate (1 Hz) */
4721 *delay_on = 500; /* ms */
4722 *delay_off = 500; /* ms */
4723 } else if ((*delay_on != 500) || (*delay_off != 500))
4724 return -EINVAL;
4726 data->new_state = TPACPI_LED_BLINK;
4727 queue_work(tpacpi_wq, &data->work);
4729 return 0;
4732 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4734 int rc;
4736 struct tpacpi_led_classdev *data = container_of(led_cdev,
4737 struct tpacpi_led_classdev, led_classdev);
4739 rc = led_get_status(data->led);
4741 if (rc == TPACPI_LED_OFF || rc < 0)
4742 rc = LED_OFF; /* no error handling in led class :( */
4743 else
4744 rc = LED_FULL;
4746 return rc;
4749 static void led_exit(void)
4751 unsigned int i;
4753 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4754 if (tpacpi_leds[i].led_classdev.name)
4755 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4758 kfree(tpacpi_leds);
4761 static int __init led_init(struct ibm_init_struct *iibm)
4763 unsigned int i;
4764 int rc;
4766 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4768 TPACPI_ACPIHANDLE_INIT(led);
4770 if (!led_handle)
4771 /* led not supported on R30, R31 */
4772 led_supported = TPACPI_LED_NONE;
4773 else if (strlencmp(led_path, "SLED") == 0)
4774 /* 570 */
4775 led_supported = TPACPI_LED_570;
4776 else if (strlencmp(led_path, "SYSL") == 0)
4777 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4778 led_supported = TPACPI_LED_OLD;
4779 else
4780 /* all others */
4781 led_supported = TPACPI_LED_NEW;
4783 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4784 str_supported(led_supported), led_supported);
4786 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4787 GFP_KERNEL);
4788 if (!tpacpi_leds) {
4789 printk(TPACPI_ERR "Out of memory for LED data\n");
4790 return -ENOMEM;
4793 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4794 tpacpi_leds[i].led = i;
4796 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4797 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4798 if (led_supported == TPACPI_LED_570)
4799 tpacpi_leds[i].led_classdev.brightness_get =
4800 &led_sysfs_get;
4802 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4804 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4806 rc = led_classdev_register(&tpacpi_pdev->dev,
4807 &tpacpi_leds[i].led_classdev);
4808 if (rc < 0) {
4809 tpacpi_leds[i].led_classdev.name = NULL;
4810 led_exit();
4811 return rc;
4815 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4818 #define str_led_status(s) \
4819 ((s) == TPACPI_LED_OFF ? "off" : \
4820 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4822 static int led_read(char *p)
4824 int len = 0;
4826 if (!led_supported) {
4827 len += sprintf(p + len, "status:\t\tnot supported\n");
4828 return len;
4830 len += sprintf(p + len, "status:\t\tsupported\n");
4832 if (led_supported == TPACPI_LED_570) {
4833 /* 570 */
4834 int i, status;
4835 for (i = 0; i < 8; i++) {
4836 status = led_get_status(i);
4837 if (status < 0)
4838 return -EIO;
4839 len += sprintf(p + len, "%d:\t\t%s\n",
4840 i, str_led_status(status));
4844 len += sprintf(p + len, "commands:\t"
4845 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4847 return len;
4850 static int led_write(char *buf)
4852 char *cmd;
4853 int led, rc;
4854 enum led_status_t s;
4856 if (!led_supported)
4857 return -ENODEV;
4859 while ((cmd = next_cmd(&buf))) {
4860 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4861 return -EINVAL;
4863 if (strstr(cmd, "off")) {
4864 s = TPACPI_LED_OFF;
4865 } else if (strstr(cmd, "on")) {
4866 s = TPACPI_LED_ON;
4867 } else if (strstr(cmd, "blink")) {
4868 s = TPACPI_LED_BLINK;
4869 } else {
4870 return -EINVAL;
4873 rc = led_set_status(led, s);
4874 if (rc < 0)
4875 return rc;
4878 return 0;
4881 static struct ibm_struct led_driver_data = {
4882 .name = "led",
4883 .read = led_read,
4884 .write = led_write,
4885 .exit = led_exit,
4888 /*************************************************************************
4889 * Beep subdriver
4892 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4894 static int __init beep_init(struct ibm_init_struct *iibm)
4896 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4898 TPACPI_ACPIHANDLE_INIT(beep);
4900 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4901 str_supported(beep_handle != NULL));
4903 return (beep_handle)? 0 : 1;
4906 static int beep_read(char *p)
4908 int len = 0;
4910 if (!beep_handle)
4911 len += sprintf(p + len, "status:\t\tnot supported\n");
4912 else {
4913 len += sprintf(p + len, "status:\t\tsupported\n");
4914 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4917 return len;
4920 static int beep_write(char *buf)
4922 char *cmd;
4923 int beep_cmd;
4925 if (!beep_handle)
4926 return -ENODEV;
4928 while ((cmd = next_cmd(&buf))) {
4929 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4930 beep_cmd >= 0 && beep_cmd <= 17) {
4931 /* beep_cmd set */
4932 } else
4933 return -EINVAL;
4934 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4935 return -EIO;
4938 return 0;
4941 static struct ibm_struct beep_driver_data = {
4942 .name = "beep",
4943 .read = beep_read,
4944 .write = beep_write,
4947 /*************************************************************************
4948 * Thermal subdriver
4951 enum thermal_access_mode {
4952 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4953 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4954 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4955 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4956 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4959 enum { /* TPACPI_THERMAL_TPEC_* */
4960 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4961 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4962 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4965 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4966 struct ibm_thermal_sensors_struct {
4967 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4970 static enum thermal_access_mode thermal_read_mode;
4972 /* idx is zero-based */
4973 static int thermal_get_sensor(int idx, s32 *value)
4975 int t;
4976 s8 tmp;
4977 char tmpi[5];
4979 t = TP_EC_THERMAL_TMP0;
4981 switch (thermal_read_mode) {
4982 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4983 case TPACPI_THERMAL_TPEC_16:
4984 if (idx >= 8 && idx <= 15) {
4985 t = TP_EC_THERMAL_TMP8;
4986 idx -= 8;
4988 /* fallthrough */
4989 #endif
4990 case TPACPI_THERMAL_TPEC_8:
4991 if (idx <= 7) {
4992 if (!acpi_ec_read(t + idx, &tmp))
4993 return -EIO;
4994 *value = tmp * 1000;
4995 return 0;
4997 break;
4999 case TPACPI_THERMAL_ACPI_UPDT:
5000 if (idx <= 7) {
5001 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5002 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5003 return -EIO;
5004 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5005 return -EIO;
5006 *value = (t - 2732) * 100;
5007 return 0;
5009 break;
5011 case TPACPI_THERMAL_ACPI_TMP07:
5012 if (idx <= 7) {
5013 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5014 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5015 return -EIO;
5016 if (t > 127 || t < -127)
5017 t = TP_EC_THERMAL_TMP_NA;
5018 *value = t * 1000;
5019 return 0;
5021 break;
5023 case TPACPI_THERMAL_NONE:
5024 default:
5025 return -ENOSYS;
5028 return -EINVAL;
5031 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5033 int res, i;
5034 int n;
5036 n = 8;
5037 i = 0;
5039 if (!s)
5040 return -EINVAL;
5042 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5043 n = 16;
5045 for (i = 0 ; i < n; i++) {
5046 res = thermal_get_sensor(i, &s->temp[i]);
5047 if (res)
5048 return res;
5051 return n;
5054 /* sysfs temp##_input -------------------------------------------------- */
5056 static ssize_t thermal_temp_input_show(struct device *dev,
5057 struct device_attribute *attr,
5058 char *buf)
5060 struct sensor_device_attribute *sensor_attr =
5061 to_sensor_dev_attr(attr);
5062 int idx = sensor_attr->index;
5063 s32 value;
5064 int res;
5066 res = thermal_get_sensor(idx, &value);
5067 if (res)
5068 return res;
5069 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5070 return -ENXIO;
5072 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5075 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5076 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5077 thermal_temp_input_show, NULL, _idxB)
5079 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5080 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5081 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5082 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5083 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5084 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5085 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5086 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5087 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5088 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5089 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5090 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5091 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5092 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5093 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5094 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5095 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5098 #define THERMAL_ATTRS(X) \
5099 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5101 static struct attribute *thermal_temp_input_attr[] = {
5102 THERMAL_ATTRS(8),
5103 THERMAL_ATTRS(9),
5104 THERMAL_ATTRS(10),
5105 THERMAL_ATTRS(11),
5106 THERMAL_ATTRS(12),
5107 THERMAL_ATTRS(13),
5108 THERMAL_ATTRS(14),
5109 THERMAL_ATTRS(15),
5110 THERMAL_ATTRS(0),
5111 THERMAL_ATTRS(1),
5112 THERMAL_ATTRS(2),
5113 THERMAL_ATTRS(3),
5114 THERMAL_ATTRS(4),
5115 THERMAL_ATTRS(5),
5116 THERMAL_ATTRS(6),
5117 THERMAL_ATTRS(7),
5118 NULL
5121 static const struct attribute_group thermal_temp_input16_group = {
5122 .attrs = thermal_temp_input_attr
5125 static const struct attribute_group thermal_temp_input8_group = {
5126 .attrs = &thermal_temp_input_attr[8]
5129 #undef THERMAL_SENSOR_ATTR_TEMP
5130 #undef THERMAL_ATTRS
5132 /* --------------------------------------------------------------------- */
5134 static int __init thermal_init(struct ibm_init_struct *iibm)
5136 u8 t, ta1, ta2;
5137 int i;
5138 int acpi_tmp7;
5139 int res;
5141 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5143 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5145 if (thinkpad_id.ec_model) {
5147 * Direct EC access mode: sensors at registers
5148 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5149 * non-implemented, thermal sensors return 0x80 when
5150 * not available
5153 ta1 = ta2 = 0;
5154 for (i = 0; i < 8; i++) {
5155 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5156 ta1 |= t;
5157 } else {
5158 ta1 = 0;
5159 break;
5161 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5162 ta2 |= t;
5163 } else {
5164 ta1 = 0;
5165 break;
5168 if (ta1 == 0) {
5169 /* This is sheer paranoia, but we handle it anyway */
5170 if (acpi_tmp7) {
5171 printk(TPACPI_ERR
5172 "ThinkPad ACPI EC access misbehaving, "
5173 "falling back to ACPI TMPx access "
5174 "mode\n");
5175 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5176 } else {
5177 printk(TPACPI_ERR
5178 "ThinkPad ACPI EC access misbehaving, "
5179 "disabling thermal sensors access\n");
5180 thermal_read_mode = TPACPI_THERMAL_NONE;
5182 } else {
5183 thermal_read_mode =
5184 (ta2 != 0) ?
5185 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5187 } else if (acpi_tmp7) {
5188 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5189 /* 600e/x, 770e, 770x */
5190 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5191 } else {
5192 /* Standard ACPI TMPx access, max 8 sensors */
5193 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5195 } else {
5196 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5197 thermal_read_mode = TPACPI_THERMAL_NONE;
5200 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5201 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5202 thermal_read_mode);
5204 switch (thermal_read_mode) {
5205 case TPACPI_THERMAL_TPEC_16:
5206 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5207 &thermal_temp_input16_group);
5208 if (res)
5209 return res;
5210 break;
5211 case TPACPI_THERMAL_TPEC_8:
5212 case TPACPI_THERMAL_ACPI_TMP07:
5213 case TPACPI_THERMAL_ACPI_UPDT:
5214 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5215 &thermal_temp_input8_group);
5216 if (res)
5217 return res;
5218 break;
5219 case TPACPI_THERMAL_NONE:
5220 default:
5221 return 1;
5224 return 0;
5227 static void thermal_exit(void)
5229 switch (thermal_read_mode) {
5230 case TPACPI_THERMAL_TPEC_16:
5231 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5232 &thermal_temp_input16_group);
5233 break;
5234 case TPACPI_THERMAL_TPEC_8:
5235 case TPACPI_THERMAL_ACPI_TMP07:
5236 case TPACPI_THERMAL_ACPI_UPDT:
5237 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5238 &thermal_temp_input16_group);
5239 break;
5240 case TPACPI_THERMAL_NONE:
5241 default:
5242 break;
5246 static int thermal_read(char *p)
5248 int len = 0;
5249 int n, i;
5250 struct ibm_thermal_sensors_struct t;
5252 n = thermal_get_sensors(&t);
5253 if (unlikely(n < 0))
5254 return n;
5256 len += sprintf(p + len, "temperatures:\t");
5258 if (n > 0) {
5259 for (i = 0; i < (n - 1); i++)
5260 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5261 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5262 } else
5263 len += sprintf(p + len, "not supported\n");
5265 return len;
5268 static struct ibm_struct thermal_driver_data = {
5269 .name = "thermal",
5270 .read = thermal_read,
5271 .exit = thermal_exit,
5274 /*************************************************************************
5275 * EC Dump subdriver
5278 static u8 ecdump_regs[256];
5280 static int ecdump_read(char *p)
5282 int len = 0;
5283 int i, j;
5284 u8 v;
5286 len += sprintf(p + len, "EC "
5287 " +00 +01 +02 +03 +04 +05 +06 +07"
5288 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5289 for (i = 0; i < 256; i += 16) {
5290 len += sprintf(p + len, "EC 0x%02x:", i);
5291 for (j = 0; j < 16; j++) {
5292 if (!acpi_ec_read(i + j, &v))
5293 break;
5294 if (v != ecdump_regs[i + j])
5295 len += sprintf(p + len, " *%02x", v);
5296 else
5297 len += sprintf(p + len, " %02x", v);
5298 ecdump_regs[i + j] = v;
5300 len += sprintf(p + len, "\n");
5301 if (j != 16)
5302 break;
5305 /* These are way too dangerous to advertise openly... */
5306 #if 0
5307 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5308 " (<offset> is 00-ff, <value> is 00-ff)\n");
5309 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5310 " (<offset> is 00-ff, <value> is 0-255)\n");
5311 #endif
5312 return len;
5315 static int ecdump_write(char *buf)
5317 char *cmd;
5318 int i, v;
5320 while ((cmd = next_cmd(&buf))) {
5321 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5322 /* i and v set */
5323 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5324 /* i and v set */
5325 } else
5326 return -EINVAL;
5327 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5328 if (!acpi_ec_write(i, v))
5329 return -EIO;
5330 } else
5331 return -EINVAL;
5334 return 0;
5337 static struct ibm_struct ecdump_driver_data = {
5338 .name = "ecdump",
5339 .read = ecdump_read,
5340 .write = ecdump_write,
5341 .flags.experimental = 1,
5344 /*************************************************************************
5345 * Backlight/brightness subdriver
5348 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5350 enum {
5351 TP_EC_BACKLIGHT = 0x31,
5353 /* TP_EC_BACKLIGHT bitmasks */
5354 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5355 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5356 TP_EC_BACKLIGHT_MAPSW = 0x20,
5359 static struct backlight_device *ibm_backlight_device;
5360 static int brightness_mode;
5361 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5363 static struct mutex brightness_mutex;
5366 * ThinkPads can read brightness from two places: EC 0x31, or
5367 * CMOS NVRAM byte 0x5E, bits 0-3.
5369 * EC 0x31 has the following layout
5370 * Bit 7: unknown function
5371 * Bit 6: unknown function
5372 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5373 * Bit 4: must be set to zero to avoid problems
5374 * Bit 3-0: backlight brightness level
5376 * brightness_get_raw returns status data in the EC 0x31 layout
5378 static int brightness_get_raw(int *status)
5380 u8 lec = 0, lcmos = 0, level = 0;
5382 if (brightness_mode & 1) {
5383 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5384 return -EIO;
5385 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5387 if (brightness_mode & 2) {
5388 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5389 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5390 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5391 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5392 level = lcmos;
5395 if (brightness_mode == 3) {
5396 *status = lec; /* Prefer EC, CMOS is just a backing store */
5397 lec &= TP_EC_BACKLIGHT_LVLMSK;
5398 if (lec == lcmos)
5399 tp_warned.bright_cmos_ec_unsync = 0;
5400 else {
5401 if (!tp_warned.bright_cmos_ec_unsync) {
5402 printk(TPACPI_ERR
5403 "CMOS NVRAM (%u) and EC (%u) do not "
5404 "agree on display brightness level\n",
5405 (unsigned int) lcmos,
5406 (unsigned int) lec);
5407 tp_warned.bright_cmos_ec_unsync = 1;
5409 return -EIO;
5411 } else {
5412 *status = level;
5415 return 0;
5418 /* May return EINTR which can always be mapped to ERESTARTSYS */
5419 static int brightness_set(int value)
5421 int cmos_cmd, inc, i, res;
5422 int current_value;
5423 int command_bits;
5425 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5426 value < 0)
5427 return -EINVAL;
5429 res = mutex_lock_killable(&brightness_mutex);
5430 if (res < 0)
5431 return res;
5433 res = brightness_get_raw(&current_value);
5434 if (res < 0)
5435 goto errout;
5437 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5438 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5440 cmos_cmd = value > current_value ?
5441 TP_CMOS_BRIGHTNESS_UP :
5442 TP_CMOS_BRIGHTNESS_DOWN;
5443 inc = (value > current_value)? 1 : -1;
5445 res = 0;
5446 for (i = current_value; i != value; i += inc) {
5447 if ((brightness_mode & 2) &&
5448 issue_thinkpad_cmos_command(cmos_cmd)) {
5449 res = -EIO;
5450 goto errout;
5452 if ((brightness_mode & 1) &&
5453 !acpi_ec_write(TP_EC_BACKLIGHT,
5454 (i + inc) | command_bits)) {
5455 res = -EIO;
5456 goto errout;;
5460 errout:
5461 mutex_unlock(&brightness_mutex);
5462 return res;
5465 /* sysfs backlight class ----------------------------------------------- */
5467 static int brightness_update_status(struct backlight_device *bd)
5469 /* it is the backlight class's job (caller) to handle
5470 * EINTR and other errors properly */
5471 return brightness_set(
5472 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5473 bd->props.power == FB_BLANK_UNBLANK) ?
5474 bd->props.brightness : 0);
5477 static int brightness_get(struct backlight_device *bd)
5479 int status, res;
5481 res = brightness_get_raw(&status);
5482 if (res < 0)
5483 return 0; /* FIXME: teach backlight about error handling */
5485 return status & TP_EC_BACKLIGHT_LVLMSK;
5488 static struct backlight_ops ibm_backlight_data = {
5489 .get_brightness = brightness_get,
5490 .update_status = brightness_update_status,
5493 /* --------------------------------------------------------------------- */
5495 static int __init brightness_init(struct ibm_init_struct *iibm)
5497 int b;
5499 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5501 mutex_init(&brightness_mutex);
5504 * We always attempt to detect acpi support, so as to switch
5505 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5506 * going to publish a backlight interface
5508 b = tpacpi_check_std_acpi_brightness_support();
5509 if (b > 0) {
5510 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5511 printk(TPACPI_NOTICE
5512 "Lenovo BIOS switched to ACPI backlight "
5513 "control mode\n");
5515 if (brightness_enable > 1) {
5516 printk(TPACPI_NOTICE
5517 "standard ACPI backlight interface "
5518 "available, not loading native one...\n");
5519 return 1;
5523 if (!brightness_enable) {
5524 dbg_printk(TPACPI_DBG_INIT,
5525 "brightness support disabled by "
5526 "module parameter\n");
5527 return 1;
5530 if (b > 16) {
5531 printk(TPACPI_ERR
5532 "Unsupported brightness interface, "
5533 "please contact %s\n", TPACPI_MAIL);
5534 return 1;
5536 if (b == 16)
5537 tp_features.bright_16levels = 1;
5539 if (!brightness_mode) {
5540 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5541 brightness_mode = 2;
5542 else
5543 brightness_mode = 3;
5545 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5546 brightness_mode);
5549 if (brightness_mode > 3)
5550 return -EINVAL;
5552 if (brightness_get_raw(&b) < 0)
5553 return 1;
5555 if (tp_features.bright_16levels)
5556 printk(TPACPI_INFO
5557 "detected a 16-level brightness capable ThinkPad\n");
5559 ibm_backlight_device = backlight_device_register(
5560 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5561 &ibm_backlight_data);
5562 if (IS_ERR(ibm_backlight_device)) {
5563 printk(TPACPI_ERR "Could not register backlight device\n");
5564 return PTR_ERR(ibm_backlight_device);
5566 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5568 ibm_backlight_device->props.max_brightness =
5569 (tp_features.bright_16levels)? 15 : 7;
5570 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5571 backlight_update_status(ibm_backlight_device);
5573 return 0;
5576 static void brightness_exit(void)
5578 if (ibm_backlight_device) {
5579 vdbg_printk(TPACPI_DBG_EXIT,
5580 "calling backlight_device_unregister()\n");
5581 backlight_device_unregister(ibm_backlight_device);
5585 static int brightness_read(char *p)
5587 int len = 0;
5588 int level;
5590 level = brightness_get(NULL);
5591 if (level < 0) {
5592 len += sprintf(p + len, "level:\t\tunreadable\n");
5593 } else {
5594 len += sprintf(p + len, "level:\t\t%d\n", level);
5595 len += sprintf(p + len, "commands:\tup, down\n");
5596 len += sprintf(p + len, "commands:\tlevel <level>"
5597 " (<level> is 0-%d)\n",
5598 (tp_features.bright_16levels) ? 15 : 7);
5601 return len;
5604 static int brightness_write(char *buf)
5606 int level;
5607 int rc;
5608 char *cmd;
5609 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5611 level = brightness_get(NULL);
5612 if (level < 0)
5613 return level;
5615 while ((cmd = next_cmd(&buf))) {
5616 if (strlencmp(cmd, "up") == 0) {
5617 if (level < max_level)
5618 level++;
5619 } else if (strlencmp(cmd, "down") == 0) {
5620 if (level > 0)
5621 level--;
5622 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5623 level >= 0 && level <= max_level) {
5624 /* new level set */
5625 } else
5626 return -EINVAL;
5630 * Now we know what the final level should be, so we try to set it.
5631 * Doing it this way makes the syscall restartable in case of EINTR
5633 rc = brightness_set(level);
5634 return (rc == -EINTR)? -ERESTARTSYS : rc;
5637 static struct ibm_struct brightness_driver_data = {
5638 .name = "brightness",
5639 .read = brightness_read,
5640 .write = brightness_write,
5641 .exit = brightness_exit,
5644 /*************************************************************************
5645 * Volume subdriver
5648 static int volume_offset = 0x30;
5650 static int volume_read(char *p)
5652 int len = 0;
5653 u8 level;
5655 if (!acpi_ec_read(volume_offset, &level)) {
5656 len += sprintf(p + len, "level:\t\tunreadable\n");
5657 } else {
5658 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5659 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5660 len += sprintf(p + len, "commands:\tup, down, mute\n");
5661 len += sprintf(p + len, "commands:\tlevel <level>"
5662 " (<level> is 0-15)\n");
5665 return len;
5668 static int volume_write(char *buf)
5670 int cmos_cmd, inc, i;
5671 u8 level, mute;
5672 int new_level, new_mute;
5673 char *cmd;
5675 while ((cmd = next_cmd(&buf))) {
5676 if (!acpi_ec_read(volume_offset, &level))
5677 return -EIO;
5678 new_mute = mute = level & 0x40;
5679 new_level = level = level & 0xf;
5681 if (strlencmp(cmd, "up") == 0) {
5682 if (mute)
5683 new_mute = 0;
5684 else
5685 new_level = level == 15 ? 15 : level + 1;
5686 } else if (strlencmp(cmd, "down") == 0) {
5687 if (mute)
5688 new_mute = 0;
5689 else
5690 new_level = level == 0 ? 0 : level - 1;
5691 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5692 new_level >= 0 && new_level <= 15) {
5693 /* new_level set */
5694 } else if (strlencmp(cmd, "mute") == 0) {
5695 new_mute = 0x40;
5696 } else
5697 return -EINVAL;
5699 if (new_level != level) {
5700 /* mute doesn't change */
5702 cmos_cmd = (new_level > level) ?
5703 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5704 inc = new_level > level ? 1 : -1;
5706 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5707 !acpi_ec_write(volume_offset, level)))
5708 return -EIO;
5710 for (i = level; i != new_level; i += inc)
5711 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5712 !acpi_ec_write(volume_offset, i + inc))
5713 return -EIO;
5715 if (mute &&
5716 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5717 !acpi_ec_write(volume_offset, new_level + mute))) {
5718 return -EIO;
5722 if (new_mute != mute) {
5723 /* level doesn't change */
5725 cmos_cmd = (new_mute) ?
5726 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5728 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5729 !acpi_ec_write(volume_offset, level + new_mute))
5730 return -EIO;
5734 return 0;
5737 static struct ibm_struct volume_driver_data = {
5738 .name = "volume",
5739 .read = volume_read,
5740 .write = volume_write,
5743 /*************************************************************************
5744 * Fan subdriver
5748 * FAN ACCESS MODES
5750 * TPACPI_FAN_RD_ACPI_GFAN:
5751 * ACPI GFAN method: returns fan level
5753 * see TPACPI_FAN_WR_ACPI_SFAN
5754 * EC 0x2f (HFSP) not available if GFAN exists
5756 * TPACPI_FAN_WR_ACPI_SFAN:
5757 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5759 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5760 * it for writing.
5762 * TPACPI_FAN_WR_TPEC:
5763 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5764 * Supported on almost all ThinkPads
5766 * Fan speed changes of any sort (including those caused by the
5767 * disengaged mode) are usually done slowly by the firmware as the
5768 * maximum ammount of fan duty cycle change per second seems to be
5769 * limited.
5771 * Reading is not available if GFAN exists.
5772 * Writing is not available if SFAN exists.
5774 * Bits
5775 * 7 automatic mode engaged;
5776 * (default operation mode of the ThinkPad)
5777 * fan level is ignored in this mode.
5778 * 6 full speed mode (takes precedence over bit 7);
5779 * not available on all thinkpads. May disable
5780 * the tachometer while the fan controller ramps up
5781 * the speed (which can take up to a few *minutes*).
5782 * Speeds up fan to 100% duty-cycle, which is far above
5783 * the standard RPM levels. It is not impossible that
5784 * it could cause hardware damage.
5785 * 5-3 unused in some models. Extra bits for fan level
5786 * in others, but still useless as all values above
5787 * 7 map to the same speed as level 7 in these models.
5788 * 2-0 fan level (0..7 usually)
5789 * 0x00 = stop
5790 * 0x07 = max (set when temperatures critical)
5791 * Some ThinkPads may have other levels, see
5792 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5794 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5795 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5796 * does so, its initial value is meaningless (0x07).
5798 * For firmware bugs, refer to:
5799 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5801 * ----
5803 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5804 * Main fan tachometer reading (in RPM)
5806 * This register is present on all ThinkPads with a new-style EC, and
5807 * it is known not to be present on the A21m/e, and T22, as there is
5808 * something else in offset 0x84 according to the ACPI DSDT. Other
5809 * ThinkPads from this same time period (and earlier) probably lack the
5810 * tachometer as well.
5812 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5813 * was never fixed by IBM to report the EC firmware version string
5814 * probably support the tachometer (like the early X models), so
5815 * detecting it is quite hard. We need more data to know for sure.
5817 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5818 * might result.
5820 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5821 * mode.
5823 * For firmware bugs, refer to:
5824 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5826 * TPACPI_FAN_WR_ACPI_FANS:
5827 * ThinkPad X31, X40, X41. Not available in the X60.
5829 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5830 * high speed. ACPI DSDT seems to map these three speeds to levels
5831 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5832 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5834 * The speeds are stored on handles
5835 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5837 * There are three default speed sets, acessible as handles:
5838 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5840 * ACPI DSDT switches which set is in use depending on various
5841 * factors.
5843 * TPACPI_FAN_WR_TPEC is also available and should be used to
5844 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5845 * but the ACPI tables just mention level 7.
5848 enum { /* Fan control constants */
5849 fan_status_offset = 0x2f, /* EC register 0x2f */
5850 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5851 * 0x84 must be read before 0x85 */
5853 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5854 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5856 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5859 enum fan_status_access_mode {
5860 TPACPI_FAN_NONE = 0, /* No fan status or control */
5861 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5862 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5865 enum fan_control_access_mode {
5866 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5867 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5868 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5869 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5872 enum fan_control_commands {
5873 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5874 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5875 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5876 * and also watchdog cmd */
5879 static int fan_control_allowed;
5881 static enum fan_status_access_mode fan_status_access_mode;
5882 static enum fan_control_access_mode fan_control_access_mode;
5883 static enum fan_control_commands fan_control_commands;
5885 static u8 fan_control_initial_status;
5886 static u8 fan_control_desired_level;
5887 static u8 fan_control_resume_level;
5888 static int fan_watchdog_maxinterval;
5890 static struct mutex fan_mutex;
5892 static void fan_watchdog_fire(struct work_struct *ignored);
5893 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5895 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5896 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5897 "\\FSPD", /* 600e/x, 770e, 770x */
5898 ); /* all others */
5899 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5900 "JFNS", /* 770x-JL */
5901 ); /* all others */
5904 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5905 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5906 * be in auto mode (0x80).
5908 * This is corrected by any write to HFSP either by the driver, or
5909 * by the firmware.
5911 * We assume 0x07 really means auto mode while this quirk is active,
5912 * as this is far more likely than the ThinkPad being in level 7,
5913 * which is only used by the firmware during thermal emergencies.
5916 static void fan_quirk1_detect(void)
5918 /* In some ThinkPads, neither the EC nor the ACPI
5919 * DSDT initialize the HFSP register, and it ends up
5920 * being initially set to 0x07 when it *could* be
5921 * either 0x07 or 0x80.
5923 * Enable for TP-1Y (T43), TP-78 (R51e),
5924 * TP-76 (R52), TP-70 (T43, R52), which are known
5925 * to be buggy. */
5926 if (fan_control_initial_status == 0x07) {
5927 switch (thinkpad_id.ec_model) {
5928 case 0x5931: /* TP-1Y */
5929 case 0x3837: /* TP-78 */
5930 case 0x3637: /* TP-76 */
5931 case 0x3037: /* TP-70 */
5932 printk(TPACPI_NOTICE
5933 "fan_init: initial fan status is unknown, "
5934 "assuming it is in auto mode\n");
5935 tp_features.fan_ctrl_status_undef = 1;
5941 static void fan_quirk1_handle(u8 *fan_status)
5943 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5944 if (*fan_status != fan_control_initial_status) {
5945 /* something changed the HFSP regisnter since
5946 * driver init time, so it is not undefined
5947 * anymore */
5948 tp_features.fan_ctrl_status_undef = 0;
5949 } else {
5950 /* Return most likely status. In fact, it
5951 * might be the only possible status */
5952 *fan_status = TP_EC_FAN_AUTO;
5958 * Call with fan_mutex held
5960 static void fan_update_desired_level(u8 status)
5962 if ((status &
5963 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5964 if (status > 7)
5965 fan_control_desired_level = 7;
5966 else
5967 fan_control_desired_level = status;
5971 static int fan_get_status(u8 *status)
5973 u8 s;
5975 /* TODO:
5976 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5978 switch (fan_status_access_mode) {
5979 case TPACPI_FAN_RD_ACPI_GFAN:
5980 /* 570, 600e/x, 770e, 770x */
5982 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5983 return -EIO;
5985 if (likely(status))
5986 *status = s & 0x07;
5988 break;
5990 case TPACPI_FAN_RD_TPEC:
5991 /* all except 570, 600e/x, 770e, 770x */
5992 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5993 return -EIO;
5995 if (likely(status)) {
5996 *status = s;
5997 fan_quirk1_handle(status);
6000 break;
6002 default:
6003 return -ENXIO;
6006 return 0;
6009 static int fan_get_status_safe(u8 *status)
6011 int rc;
6012 u8 s;
6014 if (mutex_lock_killable(&fan_mutex))
6015 return -ERESTARTSYS;
6016 rc = fan_get_status(&s);
6017 if (!rc)
6018 fan_update_desired_level(s);
6019 mutex_unlock(&fan_mutex);
6021 if (status)
6022 *status = s;
6024 return rc;
6027 static int fan_get_speed(unsigned int *speed)
6029 u8 hi, lo;
6031 switch (fan_status_access_mode) {
6032 case TPACPI_FAN_RD_TPEC:
6033 /* all except 570, 600e/x, 770e, 770x */
6034 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6035 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6036 return -EIO;
6038 if (likely(speed))
6039 *speed = (hi << 8) | lo;
6041 break;
6043 default:
6044 return -ENXIO;
6047 return 0;
6050 static int fan_set_level(int level)
6052 if (!fan_control_allowed)
6053 return -EPERM;
6055 switch (fan_control_access_mode) {
6056 case TPACPI_FAN_WR_ACPI_SFAN:
6057 if (level >= 0 && level <= 7) {
6058 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6059 return -EIO;
6060 } else
6061 return -EINVAL;
6062 break;
6064 case TPACPI_FAN_WR_ACPI_FANS:
6065 case TPACPI_FAN_WR_TPEC:
6066 if (!(level & TP_EC_FAN_AUTO) &&
6067 !(level & TP_EC_FAN_FULLSPEED) &&
6068 ((level < 0) || (level > 7)))
6069 return -EINVAL;
6071 /* safety net should the EC not support AUTO
6072 * or FULLSPEED mode bits and just ignore them */
6073 if (level & TP_EC_FAN_FULLSPEED)
6074 level |= 7; /* safety min speed 7 */
6075 else if (level & TP_EC_FAN_AUTO)
6076 level |= 4; /* safety min speed 4 */
6078 if (!acpi_ec_write(fan_status_offset, level))
6079 return -EIO;
6080 else
6081 tp_features.fan_ctrl_status_undef = 0;
6082 break;
6084 default:
6085 return -ENXIO;
6087 return 0;
6090 static int fan_set_level_safe(int level)
6092 int rc;
6094 if (!fan_control_allowed)
6095 return -EPERM;
6097 if (mutex_lock_killable(&fan_mutex))
6098 return -ERESTARTSYS;
6100 if (level == TPACPI_FAN_LAST_LEVEL)
6101 level = fan_control_desired_level;
6103 rc = fan_set_level(level);
6104 if (!rc)
6105 fan_update_desired_level(level);
6107 mutex_unlock(&fan_mutex);
6108 return rc;
6111 static int fan_set_enable(void)
6113 u8 s;
6114 int rc;
6116 if (!fan_control_allowed)
6117 return -EPERM;
6119 if (mutex_lock_killable(&fan_mutex))
6120 return -ERESTARTSYS;
6122 switch (fan_control_access_mode) {
6123 case TPACPI_FAN_WR_ACPI_FANS:
6124 case TPACPI_FAN_WR_TPEC:
6125 rc = fan_get_status(&s);
6126 if (rc < 0)
6127 break;
6129 /* Don't go out of emergency fan mode */
6130 if (s != 7) {
6131 s &= 0x07;
6132 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6135 if (!acpi_ec_write(fan_status_offset, s))
6136 rc = -EIO;
6137 else {
6138 tp_features.fan_ctrl_status_undef = 0;
6139 rc = 0;
6141 break;
6143 case TPACPI_FAN_WR_ACPI_SFAN:
6144 rc = fan_get_status(&s);
6145 if (rc < 0)
6146 break;
6148 s &= 0x07;
6150 /* Set fan to at least level 4 */
6151 s |= 4;
6153 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6154 rc = -EIO;
6155 else
6156 rc = 0;
6157 break;
6159 default:
6160 rc = -ENXIO;
6163 mutex_unlock(&fan_mutex);
6164 return rc;
6167 static int fan_set_disable(void)
6169 int rc;
6171 if (!fan_control_allowed)
6172 return -EPERM;
6174 if (mutex_lock_killable(&fan_mutex))
6175 return -ERESTARTSYS;
6177 rc = 0;
6178 switch (fan_control_access_mode) {
6179 case TPACPI_FAN_WR_ACPI_FANS:
6180 case TPACPI_FAN_WR_TPEC:
6181 if (!acpi_ec_write(fan_status_offset, 0x00))
6182 rc = -EIO;
6183 else {
6184 fan_control_desired_level = 0;
6185 tp_features.fan_ctrl_status_undef = 0;
6187 break;
6189 case TPACPI_FAN_WR_ACPI_SFAN:
6190 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6191 rc = -EIO;
6192 else
6193 fan_control_desired_level = 0;
6194 break;
6196 default:
6197 rc = -ENXIO;
6201 mutex_unlock(&fan_mutex);
6202 return rc;
6205 static int fan_set_speed(int speed)
6207 int rc;
6209 if (!fan_control_allowed)
6210 return -EPERM;
6212 if (mutex_lock_killable(&fan_mutex))
6213 return -ERESTARTSYS;
6215 rc = 0;
6216 switch (fan_control_access_mode) {
6217 case TPACPI_FAN_WR_ACPI_FANS:
6218 if (speed >= 0 && speed <= 65535) {
6219 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6220 speed, speed, speed))
6221 rc = -EIO;
6222 } else
6223 rc = -EINVAL;
6224 break;
6226 default:
6227 rc = -ENXIO;
6230 mutex_unlock(&fan_mutex);
6231 return rc;
6234 static void fan_watchdog_reset(void)
6236 static int fan_watchdog_active;
6238 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6239 return;
6241 if (fan_watchdog_active)
6242 cancel_delayed_work(&fan_watchdog_task);
6244 if (fan_watchdog_maxinterval > 0 &&
6245 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6246 fan_watchdog_active = 1;
6247 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6248 msecs_to_jiffies(fan_watchdog_maxinterval
6249 * 1000))) {
6250 printk(TPACPI_ERR
6251 "failed to queue the fan watchdog, "
6252 "watchdog will not trigger\n");
6254 } else
6255 fan_watchdog_active = 0;
6258 static void fan_watchdog_fire(struct work_struct *ignored)
6260 int rc;
6262 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6263 return;
6265 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6266 rc = fan_set_enable();
6267 if (rc < 0) {
6268 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6269 "will try again later...\n", -rc);
6270 /* reschedule for later */
6271 fan_watchdog_reset();
6276 * SYSFS fan layout: hwmon compatible (device)
6278 * pwm*_enable:
6279 * 0: "disengaged" mode
6280 * 1: manual mode
6281 * 2: native EC "auto" mode (recommended, hardware default)
6283 * pwm*: set speed in manual mode, ignored otherwise.
6284 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6285 * interpolation.
6287 * fan*_input: tachometer reading, RPM
6290 * SYSFS fan layout: extensions
6292 * fan_watchdog (driver):
6293 * fan watchdog interval in seconds, 0 disables (default), max 120
6296 /* sysfs fan pwm1_enable ----------------------------------------------- */
6297 static ssize_t fan_pwm1_enable_show(struct device *dev,
6298 struct device_attribute *attr,
6299 char *buf)
6301 int res, mode;
6302 u8 status;
6304 res = fan_get_status_safe(&status);
6305 if (res)
6306 return res;
6308 if (status & TP_EC_FAN_FULLSPEED) {
6309 mode = 0;
6310 } else if (status & TP_EC_FAN_AUTO) {
6311 mode = 2;
6312 } else
6313 mode = 1;
6315 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6318 static ssize_t fan_pwm1_enable_store(struct device *dev,
6319 struct device_attribute *attr,
6320 const char *buf, size_t count)
6322 unsigned long t;
6323 int res, level;
6325 if (parse_strtoul(buf, 2, &t))
6326 return -EINVAL;
6328 switch (t) {
6329 case 0:
6330 level = TP_EC_FAN_FULLSPEED;
6331 break;
6332 case 1:
6333 level = TPACPI_FAN_LAST_LEVEL;
6334 break;
6335 case 2:
6336 level = TP_EC_FAN_AUTO;
6337 break;
6338 case 3:
6339 /* reserved for software-controlled auto mode */
6340 return -ENOSYS;
6341 default:
6342 return -EINVAL;
6345 res = fan_set_level_safe(level);
6346 if (res == -ENXIO)
6347 return -EINVAL;
6348 else if (res < 0)
6349 return res;
6351 fan_watchdog_reset();
6353 return count;
6356 static struct device_attribute dev_attr_fan_pwm1_enable =
6357 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6358 fan_pwm1_enable_show, fan_pwm1_enable_store);
6360 /* sysfs fan pwm1 ------------------------------------------------------ */
6361 static ssize_t fan_pwm1_show(struct device *dev,
6362 struct device_attribute *attr,
6363 char *buf)
6365 int res;
6366 u8 status;
6368 res = fan_get_status_safe(&status);
6369 if (res)
6370 return res;
6372 if ((status &
6373 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6374 status = fan_control_desired_level;
6376 if (status > 7)
6377 status = 7;
6379 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6382 static ssize_t fan_pwm1_store(struct device *dev,
6383 struct device_attribute *attr,
6384 const char *buf, size_t count)
6386 unsigned long s;
6387 int rc;
6388 u8 status, newlevel;
6390 if (parse_strtoul(buf, 255, &s))
6391 return -EINVAL;
6393 /* scale down from 0-255 to 0-7 */
6394 newlevel = (s >> 5) & 0x07;
6396 if (mutex_lock_killable(&fan_mutex))
6397 return -ERESTARTSYS;
6399 rc = fan_get_status(&status);
6400 if (!rc && (status &
6401 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6402 rc = fan_set_level(newlevel);
6403 if (rc == -ENXIO)
6404 rc = -EINVAL;
6405 else if (!rc) {
6406 fan_update_desired_level(newlevel);
6407 fan_watchdog_reset();
6411 mutex_unlock(&fan_mutex);
6412 return (rc)? rc : count;
6415 static struct device_attribute dev_attr_fan_pwm1 =
6416 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6417 fan_pwm1_show, fan_pwm1_store);
6419 /* sysfs fan fan1_input ------------------------------------------------ */
6420 static ssize_t fan_fan1_input_show(struct device *dev,
6421 struct device_attribute *attr,
6422 char *buf)
6424 int res;
6425 unsigned int speed;
6427 res = fan_get_speed(&speed);
6428 if (res < 0)
6429 return res;
6431 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6434 static struct device_attribute dev_attr_fan_fan1_input =
6435 __ATTR(fan1_input, S_IRUGO,
6436 fan_fan1_input_show, NULL);
6438 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6439 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6440 char *buf)
6442 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6445 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6446 const char *buf, size_t count)
6448 unsigned long t;
6450 if (parse_strtoul(buf, 120, &t))
6451 return -EINVAL;
6453 if (!fan_control_allowed)
6454 return -EPERM;
6456 fan_watchdog_maxinterval = t;
6457 fan_watchdog_reset();
6459 return count;
6462 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6463 fan_fan_watchdog_show, fan_fan_watchdog_store);
6465 /* --------------------------------------------------------------------- */
6466 static struct attribute *fan_attributes[] = {
6467 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6468 &dev_attr_fan_fan1_input.attr,
6469 NULL
6472 static const struct attribute_group fan_attr_group = {
6473 .attrs = fan_attributes,
6476 static int __init fan_init(struct ibm_init_struct *iibm)
6478 int rc;
6480 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6482 mutex_init(&fan_mutex);
6483 fan_status_access_mode = TPACPI_FAN_NONE;
6484 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6485 fan_control_commands = 0;
6486 fan_watchdog_maxinterval = 0;
6487 tp_features.fan_ctrl_status_undef = 0;
6488 fan_control_desired_level = 7;
6490 TPACPI_ACPIHANDLE_INIT(fans);
6491 TPACPI_ACPIHANDLE_INIT(gfan);
6492 TPACPI_ACPIHANDLE_INIT(sfan);
6494 if (gfan_handle) {
6495 /* 570, 600e/x, 770e, 770x */
6496 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6497 } else {
6498 /* all other ThinkPads: note that even old-style
6499 * ThinkPad ECs supports the fan control register */
6500 if (likely(acpi_ec_read(fan_status_offset,
6501 &fan_control_initial_status))) {
6502 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6503 fan_quirk1_detect();
6504 } else {
6505 printk(TPACPI_ERR
6506 "ThinkPad ACPI EC access misbehaving, "
6507 "fan status and control unavailable\n");
6508 return 1;
6512 if (sfan_handle) {
6513 /* 570, 770x-JL */
6514 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6515 fan_control_commands |=
6516 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6517 } else {
6518 if (!gfan_handle) {
6519 /* gfan without sfan means no fan control */
6520 /* all other models implement TP EC 0x2f control */
6522 if (fans_handle) {
6523 /* X31, X40, X41 */
6524 fan_control_access_mode =
6525 TPACPI_FAN_WR_ACPI_FANS;
6526 fan_control_commands |=
6527 TPACPI_FAN_CMD_SPEED |
6528 TPACPI_FAN_CMD_LEVEL |
6529 TPACPI_FAN_CMD_ENABLE;
6530 } else {
6531 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6532 fan_control_commands |=
6533 TPACPI_FAN_CMD_LEVEL |
6534 TPACPI_FAN_CMD_ENABLE;
6539 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6540 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6541 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6542 fan_status_access_mode, fan_control_access_mode);
6544 /* fan control master switch */
6545 if (!fan_control_allowed) {
6546 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6547 fan_control_commands = 0;
6548 dbg_printk(TPACPI_DBG_INIT,
6549 "fan control features disabled by parameter\n");
6552 /* update fan_control_desired_level */
6553 if (fan_status_access_mode != TPACPI_FAN_NONE)
6554 fan_get_status_safe(NULL);
6556 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6557 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6558 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6559 &fan_attr_group);
6560 if (rc < 0)
6561 return rc;
6563 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6564 &driver_attr_fan_watchdog);
6565 if (rc < 0) {
6566 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6567 &fan_attr_group);
6568 return rc;
6570 return 0;
6571 } else
6572 return 1;
6575 static void fan_exit(void)
6577 vdbg_printk(TPACPI_DBG_EXIT,
6578 "cancelling any pending fan watchdog tasks\n");
6580 /* FIXME: can we really do this unconditionally? */
6581 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6582 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6583 &driver_attr_fan_watchdog);
6585 cancel_delayed_work(&fan_watchdog_task);
6586 flush_workqueue(tpacpi_wq);
6589 static void fan_suspend(pm_message_t state)
6591 int rc;
6593 if (!fan_control_allowed)
6594 return;
6596 /* Store fan status in cache */
6597 fan_control_resume_level = 0;
6598 rc = fan_get_status_safe(&fan_control_resume_level);
6599 if (rc < 0)
6600 printk(TPACPI_NOTICE
6601 "failed to read fan level for later "
6602 "restore during resume: %d\n", rc);
6604 /* if it is undefined, don't attempt to restore it.
6605 * KEEP THIS LAST */
6606 if (tp_features.fan_ctrl_status_undef)
6607 fan_control_resume_level = 0;
6610 static void fan_resume(void)
6612 u8 current_level = 7;
6613 bool do_set = false;
6614 int rc;
6616 /* DSDT *always* updates status on resume */
6617 tp_features.fan_ctrl_status_undef = 0;
6619 if (!fan_control_allowed ||
6620 !fan_control_resume_level ||
6621 (fan_get_status_safe(&current_level) < 0))
6622 return;
6624 switch (fan_control_access_mode) {
6625 case TPACPI_FAN_WR_ACPI_SFAN:
6626 /* never decrease fan level */
6627 do_set = (fan_control_resume_level > current_level);
6628 break;
6629 case TPACPI_FAN_WR_ACPI_FANS:
6630 case TPACPI_FAN_WR_TPEC:
6631 /* never decrease fan level, scale is:
6632 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6634 * We expect the firmware to set either 7 or AUTO, but we
6635 * handle FULLSPEED out of paranoia.
6637 * So, we can safely only restore FULLSPEED or 7, anything
6638 * else could slow the fan. Restoring AUTO is useless, at
6639 * best that's exactly what the DSDT already set (it is the
6640 * slower it uses).
6642 * Always keep in mind that the DSDT *will* have set the
6643 * fans to what the vendor supposes is the best level. We
6644 * muck with it only to speed the fan up.
6646 if (fan_control_resume_level != 7 &&
6647 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6648 return;
6649 else
6650 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6651 (current_level != fan_control_resume_level);
6652 break;
6653 default:
6654 return;
6656 if (do_set) {
6657 printk(TPACPI_NOTICE
6658 "restoring fan level to 0x%02x\n",
6659 fan_control_resume_level);
6660 rc = fan_set_level_safe(fan_control_resume_level);
6661 if (rc < 0)
6662 printk(TPACPI_NOTICE
6663 "failed to restore fan level: %d\n", rc);
6667 static int fan_read(char *p)
6669 int len = 0;
6670 int rc;
6671 u8 status;
6672 unsigned int speed = 0;
6674 switch (fan_status_access_mode) {
6675 case TPACPI_FAN_RD_ACPI_GFAN:
6676 /* 570, 600e/x, 770e, 770x */
6677 rc = fan_get_status_safe(&status);
6678 if (rc < 0)
6679 return rc;
6681 len += sprintf(p + len, "status:\t\t%s\n"
6682 "level:\t\t%d\n",
6683 (status != 0) ? "enabled" : "disabled", status);
6684 break;
6686 case TPACPI_FAN_RD_TPEC:
6687 /* all except 570, 600e/x, 770e, 770x */
6688 rc = fan_get_status_safe(&status);
6689 if (rc < 0)
6690 return rc;
6692 len += sprintf(p + len, "status:\t\t%s\n",
6693 (status != 0) ? "enabled" : "disabled");
6695 rc = fan_get_speed(&speed);
6696 if (rc < 0)
6697 return rc;
6699 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6701 if (status & TP_EC_FAN_FULLSPEED)
6702 /* Disengaged mode takes precedence */
6703 len += sprintf(p + len, "level:\t\tdisengaged\n");
6704 else if (status & TP_EC_FAN_AUTO)
6705 len += sprintf(p + len, "level:\t\tauto\n");
6706 else
6707 len += sprintf(p + len, "level:\t\t%d\n", status);
6708 break;
6710 case TPACPI_FAN_NONE:
6711 default:
6712 len += sprintf(p + len, "status:\t\tnot supported\n");
6715 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6716 len += sprintf(p + len, "commands:\tlevel <level>");
6718 switch (fan_control_access_mode) {
6719 case TPACPI_FAN_WR_ACPI_SFAN:
6720 len += sprintf(p + len, " (<level> is 0-7)\n");
6721 break;
6723 default:
6724 len += sprintf(p + len, " (<level> is 0-7, "
6725 "auto, disengaged, full-speed)\n");
6726 break;
6730 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6731 len += sprintf(p + len, "commands:\tenable, disable\n"
6732 "commands:\twatchdog <timeout> (<timeout> "
6733 "is 0 (off), 1-120 (seconds))\n");
6735 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6736 len += sprintf(p + len, "commands:\tspeed <speed>"
6737 " (<speed> is 0-65535)\n");
6739 return len;
6742 static int fan_write_cmd_level(const char *cmd, int *rc)
6744 int level;
6746 if (strlencmp(cmd, "level auto") == 0)
6747 level = TP_EC_FAN_AUTO;
6748 else if ((strlencmp(cmd, "level disengaged") == 0) |
6749 (strlencmp(cmd, "level full-speed") == 0))
6750 level = TP_EC_FAN_FULLSPEED;
6751 else if (sscanf(cmd, "level %d", &level) != 1)
6752 return 0;
6754 *rc = fan_set_level_safe(level);
6755 if (*rc == -ENXIO)
6756 printk(TPACPI_ERR "level command accepted for unsupported "
6757 "access mode %d", fan_control_access_mode);
6759 return 1;
6762 static int fan_write_cmd_enable(const char *cmd, int *rc)
6764 if (strlencmp(cmd, "enable") != 0)
6765 return 0;
6767 *rc = fan_set_enable();
6768 if (*rc == -ENXIO)
6769 printk(TPACPI_ERR "enable command accepted for unsupported "
6770 "access mode %d", fan_control_access_mode);
6772 return 1;
6775 static int fan_write_cmd_disable(const char *cmd, int *rc)
6777 if (strlencmp(cmd, "disable") != 0)
6778 return 0;
6780 *rc = fan_set_disable();
6781 if (*rc == -ENXIO)
6782 printk(TPACPI_ERR "disable command accepted for unsupported "
6783 "access mode %d", fan_control_access_mode);
6785 return 1;
6788 static int fan_write_cmd_speed(const char *cmd, int *rc)
6790 int speed;
6792 /* TODO:
6793 * Support speed <low> <medium> <high> ? */
6795 if (sscanf(cmd, "speed %d", &speed) != 1)
6796 return 0;
6798 *rc = fan_set_speed(speed);
6799 if (*rc == -ENXIO)
6800 printk(TPACPI_ERR "speed command accepted for unsupported "
6801 "access mode %d", fan_control_access_mode);
6803 return 1;
6806 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6808 int interval;
6810 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6811 return 0;
6813 if (interval < 0 || interval > 120)
6814 *rc = -EINVAL;
6815 else
6816 fan_watchdog_maxinterval = interval;
6818 return 1;
6821 static int fan_write(char *buf)
6823 char *cmd;
6824 int rc = 0;
6826 while (!rc && (cmd = next_cmd(&buf))) {
6827 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6828 fan_write_cmd_level(cmd, &rc)) &&
6829 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6830 (fan_write_cmd_enable(cmd, &rc) ||
6831 fan_write_cmd_disable(cmd, &rc) ||
6832 fan_write_cmd_watchdog(cmd, &rc))) &&
6833 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6834 fan_write_cmd_speed(cmd, &rc))
6836 rc = -EINVAL;
6837 else if (!rc)
6838 fan_watchdog_reset();
6841 return rc;
6844 static struct ibm_struct fan_driver_data = {
6845 .name = "fan",
6846 .read = fan_read,
6847 .write = fan_write,
6848 .exit = fan_exit,
6849 .suspend = fan_suspend,
6850 .resume = fan_resume,
6853 /****************************************************************************
6854 ****************************************************************************
6856 * Infrastructure
6858 ****************************************************************************
6859 ****************************************************************************/
6861 /* sysfs name ---------------------------------------------------------- */
6862 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6863 struct device_attribute *attr,
6864 char *buf)
6866 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6869 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6870 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6872 /* --------------------------------------------------------------------- */
6874 /* /proc support */
6875 static struct proc_dir_entry *proc_dir;
6878 * Module and infrastructure proble, init and exit handling
6881 static int force_load;
6883 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6884 static const char * __init str_supported(int is_supported)
6886 static char text_unsupported[] __initdata = "not supported";
6888 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6890 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6892 static void ibm_exit(struct ibm_struct *ibm)
6894 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6896 list_del_init(&ibm->all_drivers);
6898 if (ibm->flags.acpi_notify_installed) {
6899 dbg_printk(TPACPI_DBG_EXIT,
6900 "%s: acpi_remove_notify_handler\n", ibm->name);
6901 BUG_ON(!ibm->acpi);
6902 acpi_remove_notify_handler(*ibm->acpi->handle,
6903 ibm->acpi->type,
6904 dispatch_acpi_notify);
6905 ibm->flags.acpi_notify_installed = 0;
6906 ibm->flags.acpi_notify_installed = 0;
6909 if (ibm->flags.proc_created) {
6910 dbg_printk(TPACPI_DBG_EXIT,
6911 "%s: remove_proc_entry\n", ibm->name);
6912 remove_proc_entry(ibm->name, proc_dir);
6913 ibm->flags.proc_created = 0;
6916 if (ibm->flags.acpi_driver_registered) {
6917 dbg_printk(TPACPI_DBG_EXIT,
6918 "%s: acpi_bus_unregister_driver\n", ibm->name);
6919 BUG_ON(!ibm->acpi);
6920 acpi_bus_unregister_driver(ibm->acpi->driver);
6921 kfree(ibm->acpi->driver);
6922 ibm->acpi->driver = NULL;
6923 ibm->flags.acpi_driver_registered = 0;
6926 if (ibm->flags.init_called && ibm->exit) {
6927 ibm->exit();
6928 ibm->flags.init_called = 0;
6931 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6934 static int __init ibm_init(struct ibm_init_struct *iibm)
6936 int ret;
6937 struct ibm_struct *ibm = iibm->data;
6938 struct proc_dir_entry *entry;
6940 BUG_ON(ibm == NULL);
6942 INIT_LIST_HEAD(&ibm->all_drivers);
6944 if (ibm->flags.experimental && !experimental)
6945 return 0;
6947 dbg_printk(TPACPI_DBG_INIT,
6948 "probing for %s\n", ibm->name);
6950 if (iibm->init) {
6951 ret = iibm->init(iibm);
6952 if (ret > 0)
6953 return 0; /* probe failed */
6954 if (ret)
6955 return ret;
6957 ibm->flags.init_called = 1;
6960 if (ibm->acpi) {
6961 if (ibm->acpi->hid) {
6962 ret = register_tpacpi_subdriver(ibm);
6963 if (ret)
6964 goto err_out;
6967 if (ibm->acpi->notify) {
6968 ret = setup_acpi_notify(ibm);
6969 if (ret == -ENODEV) {
6970 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6971 ibm->name);
6972 ret = 0;
6973 goto err_out;
6975 if (ret < 0)
6976 goto err_out;
6980 dbg_printk(TPACPI_DBG_INIT,
6981 "%s installed\n", ibm->name);
6983 if (ibm->read) {
6984 entry = create_proc_entry(ibm->name,
6985 S_IFREG | S_IRUGO | S_IWUSR,
6986 proc_dir);
6987 if (!entry) {
6988 printk(TPACPI_ERR "unable to create proc entry %s\n",
6989 ibm->name);
6990 ret = -ENODEV;
6991 goto err_out;
6993 entry->owner = THIS_MODULE;
6994 entry->data = ibm;
6995 entry->read_proc = &dispatch_procfs_read;
6996 if (ibm->write)
6997 entry->write_proc = &dispatch_procfs_write;
6998 ibm->flags.proc_created = 1;
7001 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7003 return 0;
7005 err_out:
7006 dbg_printk(TPACPI_DBG_INIT,
7007 "%s: at error exit path with result %d\n",
7008 ibm->name, ret);
7010 ibm_exit(ibm);
7011 return (ret < 0)? ret : 0;
7014 /* Probing */
7016 /* returns 0 - probe ok, or < 0 - probe error.
7017 * Probe ok doesn't mean thinkpad found.
7018 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7019 static int __must_check __init get_thinkpad_model_data(
7020 struct thinkpad_id_data *tp)
7022 const struct dmi_device *dev = NULL;
7023 char ec_fw_string[18];
7024 char const *s;
7026 if (!tp)
7027 return -EINVAL;
7029 memset(tp, 0, sizeof(*tp));
7031 if (dmi_name_in_vendors("IBM"))
7032 tp->vendor = PCI_VENDOR_ID_IBM;
7033 else if (dmi_name_in_vendors("LENOVO"))
7034 tp->vendor = PCI_VENDOR_ID_LENOVO;
7035 else
7036 return 0;
7038 s = dmi_get_system_info(DMI_BIOS_VERSION);
7039 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7040 if (s && !tp->bios_version_str)
7041 return -ENOMEM;
7042 if (!tp->bios_version_str)
7043 return 0;
7044 tp->bios_model = tp->bios_version_str[0]
7045 | (tp->bios_version_str[1] << 8);
7048 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7049 * X32 or newer, all Z series; Some models must have an
7050 * up-to-date BIOS or they will not be detected.
7052 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7054 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7055 if (sscanf(dev->name,
7056 "IBM ThinkPad Embedded Controller -[%17c",
7057 ec_fw_string) == 1) {
7058 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7059 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7061 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7062 if (!tp->ec_version_str)
7063 return -ENOMEM;
7064 tp->ec_model = ec_fw_string[0]
7065 | (ec_fw_string[1] << 8);
7066 break;
7070 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7071 if (s && !strnicmp(s, "ThinkPad", 8)) {
7072 tp->model_str = kstrdup(s, GFP_KERNEL);
7073 if (!tp->model_str)
7074 return -ENOMEM;
7077 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7078 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7079 if (s && !tp->nummodel_str)
7080 return -ENOMEM;
7082 return 0;
7085 static int __init probe_for_thinkpad(void)
7087 int is_thinkpad;
7089 if (acpi_disabled)
7090 return -ENODEV;
7093 * Non-ancient models have better DMI tagging, but very old models
7094 * don't.
7096 is_thinkpad = (thinkpad_id.model_str != NULL);
7098 /* ec is required because many other handles are relative to it */
7099 TPACPI_ACPIHANDLE_INIT(ec);
7100 if (!ec_handle) {
7101 if (is_thinkpad)
7102 printk(TPACPI_ERR
7103 "Not yet supported ThinkPad detected!\n");
7104 return -ENODEV;
7108 * Risks a regression on very old machines, but reduces potential
7109 * false positives a damn great deal
7111 if (!is_thinkpad)
7112 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7114 if (!is_thinkpad && !force_load)
7115 return -ENODEV;
7117 return 0;
7121 /* Module init, exit, parameters */
7123 static struct ibm_init_struct ibms_init[] __initdata = {
7125 .init = thinkpad_acpi_driver_init,
7126 .data = &thinkpad_acpi_driver_data,
7129 .init = hotkey_init,
7130 .data = &hotkey_driver_data,
7133 .init = bluetooth_init,
7134 .data = &bluetooth_driver_data,
7137 .init = wan_init,
7138 .data = &wan_driver_data,
7141 .init = uwb_init,
7142 .data = &uwb_driver_data,
7144 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7146 .init = video_init,
7147 .data = &video_driver_data,
7149 #endif
7151 .init = light_init,
7152 .data = &light_driver_data,
7154 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7156 .init = dock_init,
7157 .data = &dock_driver_data[0],
7160 .init = dock_init2,
7161 .data = &dock_driver_data[1],
7163 #endif
7164 #ifdef CONFIG_THINKPAD_ACPI_BAY
7166 .init = bay_init,
7167 .data = &bay_driver_data,
7169 #endif
7171 .init = cmos_init,
7172 .data = &cmos_driver_data,
7175 .init = led_init,
7176 .data = &led_driver_data,
7179 .init = beep_init,
7180 .data = &beep_driver_data,
7183 .init = thermal_init,
7184 .data = &thermal_driver_data,
7187 .data = &ecdump_driver_data,
7190 .init = brightness_init,
7191 .data = &brightness_driver_data,
7194 .data = &volume_driver_data,
7197 .init = fan_init,
7198 .data = &fan_driver_data,
7202 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7204 unsigned int i;
7205 struct ibm_struct *ibm;
7207 if (!kp || !kp->name || !val)
7208 return -EINVAL;
7210 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7211 ibm = ibms_init[i].data;
7212 WARN_ON(ibm == NULL);
7214 if (!ibm || !ibm->name)
7215 continue;
7217 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7218 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7219 return -ENOSPC;
7220 strcpy(ibms_init[i].param, val);
7221 strcat(ibms_init[i].param, ",");
7222 return 0;
7226 return -EINVAL;
7229 module_param(experimental, int, 0);
7230 MODULE_PARM_DESC(experimental,
7231 "Enables experimental features when non-zero");
7233 module_param_named(debug, dbg_level, uint, 0);
7234 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7236 module_param(force_load, bool, 0);
7237 MODULE_PARM_DESC(force_load,
7238 "Attempts to load the driver even on a "
7239 "mis-identified ThinkPad when true");
7241 module_param_named(fan_control, fan_control_allowed, bool, 0);
7242 MODULE_PARM_DESC(fan_control,
7243 "Enables setting fan parameters features when true");
7245 module_param_named(brightness_mode, brightness_mode, int, 0);
7246 MODULE_PARM_DESC(brightness_mode,
7247 "Selects brightness control strategy: "
7248 "0=auto, 1=EC, 2=CMOS, 3=both");
7250 module_param(brightness_enable, uint, 0);
7251 MODULE_PARM_DESC(brightness_enable,
7252 "Enables backlight control when 1, disables when 0");
7254 module_param(hotkey_report_mode, uint, 0);
7255 MODULE_PARM_DESC(hotkey_report_mode,
7256 "used for backwards compatibility with userspace, "
7257 "see documentation");
7259 #define TPACPI_PARAM(feature) \
7260 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7261 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7262 "at module load, see documentation")
7264 TPACPI_PARAM(hotkey);
7265 TPACPI_PARAM(bluetooth);
7266 TPACPI_PARAM(video);
7267 TPACPI_PARAM(light);
7268 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7269 TPACPI_PARAM(dock);
7270 #endif
7271 #ifdef CONFIG_THINKPAD_ACPI_BAY
7272 TPACPI_PARAM(bay);
7273 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7274 TPACPI_PARAM(cmos);
7275 TPACPI_PARAM(led);
7276 TPACPI_PARAM(beep);
7277 TPACPI_PARAM(ecdump);
7278 TPACPI_PARAM(brightness);
7279 TPACPI_PARAM(volume);
7280 TPACPI_PARAM(fan);
7282 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7283 module_param(dbg_wlswemul, uint, 0);
7284 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7285 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7286 MODULE_PARM_DESC(wlsw_state,
7287 "Initial state of the emulated WLSW switch");
7289 module_param(dbg_bluetoothemul, uint, 0);
7290 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7291 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7292 MODULE_PARM_DESC(bluetooth_state,
7293 "Initial state of the emulated bluetooth switch");
7295 module_param(dbg_wwanemul, uint, 0);
7296 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7297 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7298 MODULE_PARM_DESC(wwan_state,
7299 "Initial state of the emulated WWAN switch");
7301 module_param(dbg_uwbemul, uint, 0);
7302 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7303 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7304 MODULE_PARM_DESC(uwb_state,
7305 "Initial state of the emulated UWB switch");
7306 #endif
7308 static void thinkpad_acpi_module_exit(void)
7310 struct ibm_struct *ibm, *itmp;
7312 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7314 list_for_each_entry_safe_reverse(ibm, itmp,
7315 &tpacpi_all_drivers,
7316 all_drivers) {
7317 ibm_exit(ibm);
7320 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7322 if (tpacpi_inputdev) {
7323 if (tp_features.input_device_registered)
7324 input_unregister_device(tpacpi_inputdev);
7325 else
7326 input_free_device(tpacpi_inputdev);
7329 if (tpacpi_hwmon)
7330 hwmon_device_unregister(tpacpi_hwmon);
7332 if (tp_features.sensors_pdev_attrs_registered)
7333 device_remove_file(&tpacpi_sensors_pdev->dev,
7334 &dev_attr_thinkpad_acpi_pdev_name);
7335 if (tpacpi_sensors_pdev)
7336 platform_device_unregister(tpacpi_sensors_pdev);
7337 if (tpacpi_pdev)
7338 platform_device_unregister(tpacpi_pdev);
7340 if (tp_features.sensors_pdrv_attrs_registered)
7341 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7342 if (tp_features.platform_drv_attrs_registered)
7343 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7345 if (tp_features.sensors_pdrv_registered)
7346 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7348 if (tp_features.platform_drv_registered)
7349 platform_driver_unregister(&tpacpi_pdriver);
7351 if (proc_dir)
7352 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7354 if (tpacpi_wq)
7355 destroy_workqueue(tpacpi_wq);
7357 kfree(thinkpad_id.bios_version_str);
7358 kfree(thinkpad_id.ec_version_str);
7359 kfree(thinkpad_id.model_str);
7363 static int __init thinkpad_acpi_module_init(void)
7365 int ret, i;
7367 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7369 /* Parameter checking */
7370 if (hotkey_report_mode > 2)
7371 return -EINVAL;
7373 /* Driver-level probe */
7375 ret = get_thinkpad_model_data(&thinkpad_id);
7376 if (ret) {
7377 printk(TPACPI_ERR
7378 "unable to get DMI data: %d\n", ret);
7379 thinkpad_acpi_module_exit();
7380 return ret;
7382 ret = probe_for_thinkpad();
7383 if (ret) {
7384 thinkpad_acpi_module_exit();
7385 return ret;
7388 /* Driver initialization */
7390 TPACPI_ACPIHANDLE_INIT(ecrd);
7391 TPACPI_ACPIHANDLE_INIT(ecwr);
7393 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7394 if (!tpacpi_wq) {
7395 thinkpad_acpi_module_exit();
7396 return -ENOMEM;
7399 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7400 if (!proc_dir) {
7401 printk(TPACPI_ERR
7402 "unable to create proc dir " TPACPI_PROC_DIR);
7403 thinkpad_acpi_module_exit();
7404 return -ENODEV;
7406 proc_dir->owner = THIS_MODULE;
7408 ret = platform_driver_register(&tpacpi_pdriver);
7409 if (ret) {
7410 printk(TPACPI_ERR
7411 "unable to register main platform driver\n");
7412 thinkpad_acpi_module_exit();
7413 return ret;
7415 tp_features.platform_drv_registered = 1;
7417 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7418 if (ret) {
7419 printk(TPACPI_ERR
7420 "unable to register hwmon platform driver\n");
7421 thinkpad_acpi_module_exit();
7422 return ret;
7424 tp_features.sensors_pdrv_registered = 1;
7426 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7427 if (!ret) {
7428 tp_features.platform_drv_attrs_registered = 1;
7429 ret = tpacpi_create_driver_attributes(
7430 &tpacpi_hwmon_pdriver.driver);
7432 if (ret) {
7433 printk(TPACPI_ERR
7434 "unable to create sysfs driver attributes\n");
7435 thinkpad_acpi_module_exit();
7436 return ret;
7438 tp_features.sensors_pdrv_attrs_registered = 1;
7441 /* Device initialization */
7442 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7443 NULL, 0);
7444 if (IS_ERR(tpacpi_pdev)) {
7445 ret = PTR_ERR(tpacpi_pdev);
7446 tpacpi_pdev = NULL;
7447 printk(TPACPI_ERR "unable to register platform device\n");
7448 thinkpad_acpi_module_exit();
7449 return ret;
7451 tpacpi_sensors_pdev = platform_device_register_simple(
7452 TPACPI_HWMON_DRVR_NAME,
7453 -1, NULL, 0);
7454 if (IS_ERR(tpacpi_sensors_pdev)) {
7455 ret = PTR_ERR(tpacpi_sensors_pdev);
7456 tpacpi_sensors_pdev = NULL;
7457 printk(TPACPI_ERR
7458 "unable to register hwmon platform device\n");
7459 thinkpad_acpi_module_exit();
7460 return ret;
7462 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7463 &dev_attr_thinkpad_acpi_pdev_name);
7464 if (ret) {
7465 printk(TPACPI_ERR
7466 "unable to create sysfs hwmon device attributes\n");
7467 thinkpad_acpi_module_exit();
7468 return ret;
7470 tp_features.sensors_pdev_attrs_registered = 1;
7471 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7472 if (IS_ERR(tpacpi_hwmon)) {
7473 ret = PTR_ERR(tpacpi_hwmon);
7474 tpacpi_hwmon = NULL;
7475 printk(TPACPI_ERR "unable to register hwmon device\n");
7476 thinkpad_acpi_module_exit();
7477 return ret;
7479 mutex_init(&tpacpi_inputdev_send_mutex);
7480 tpacpi_inputdev = input_allocate_device();
7481 if (!tpacpi_inputdev) {
7482 printk(TPACPI_ERR "unable to allocate input device\n");
7483 thinkpad_acpi_module_exit();
7484 return -ENOMEM;
7485 } else {
7486 /* Prepare input device, but don't register */
7487 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7488 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7489 tpacpi_inputdev->id.bustype = BUS_HOST;
7490 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7491 thinkpad_id.vendor :
7492 PCI_VENDOR_ID_IBM;
7493 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7494 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7496 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7497 ret = ibm_init(&ibms_init[i]);
7498 if (ret >= 0 && *ibms_init[i].param)
7499 ret = ibms_init[i].data->write(ibms_init[i].param);
7500 if (ret < 0) {
7501 thinkpad_acpi_module_exit();
7502 return ret;
7505 ret = input_register_device(tpacpi_inputdev);
7506 if (ret < 0) {
7507 printk(TPACPI_ERR "unable to register input device\n");
7508 thinkpad_acpi_module_exit();
7509 return ret;
7510 } else {
7511 tp_features.input_device_registered = 1;
7514 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7515 return 0;
7518 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7521 * DMI matching for module autoloading
7523 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7524 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7526 * Only models listed in thinkwiki will be supported, so add yours
7527 * if it is not there yet.
7529 #define IBM_BIOS_MODULE_ALIAS(__type) \
7530 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7532 /* Non-ancient thinkpads */
7533 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7534 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7536 /* Ancient thinkpad BIOSes have to be identified by
7537 * BIOS type or model number, and there are far less
7538 * BIOS types than model numbers... */
7539 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7540 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7541 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7543 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7544 MODULE_DESCRIPTION(TPACPI_DESC);
7545 MODULE_VERSION(TPACPI_VERSION);
7546 MODULE_LICENSE("GPL");
7548 module_init(thinkpad_acpi_module_init);
7549 module_exit(thinkpad_acpi_module_exit);