ACPI: thinkpad-acpi: handle HKEY thermal and battery alarms
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob275be87dcfb906e9f6e60caa0c98b2820ec49c36
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 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.21"
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>
79 #include <acpi/acnamesp.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* Debugging */
177 #define TPACPI_LOG TPACPI_FILE ": "
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_NOTICE KERN_NOTICE TPACPI_LOG
182 #define TPACPI_INFO KERN_INFO TPACPI_LOG
183 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
185 #define TPACPI_DBG_ALL 0xffff
186 #define TPACPI_DBG_INIT 0x0001
187 #define TPACPI_DBG_EXIT 0x0002
188 #define dbg_printk(a_dbg_level, format, arg...) \
189 do { if (dbg_level & a_dbg_level) \
190 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
191 } while (0)
192 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
193 #define vdbg_printk(a_dbg_level, format, arg...) \
194 dbg_printk(a_dbg_level, format, ## arg)
195 static const char *str_supported(int is_supported);
196 #else
197 #define vdbg_printk(a_dbg_level, format, arg...)
198 #endif
200 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
201 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
202 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
205 /****************************************************************************
206 * Driver-wide structs and misc. variables
209 struct ibm_struct;
211 struct tp_acpi_drv_struct {
212 const struct acpi_device_id *hid;
213 struct acpi_driver *driver;
215 void (*notify) (struct ibm_struct *, u32);
216 acpi_handle *handle;
217 u32 type;
218 struct acpi_device *device;
221 struct ibm_struct {
222 char *name;
224 int (*read) (char *);
225 int (*write) (char *);
226 void (*exit) (void);
227 void (*resume) (void);
228 void (*suspend) (pm_message_t state);
229 void (*shutdown) (void);
231 struct list_head all_drivers;
233 struct tp_acpi_drv_struct *acpi;
235 struct {
236 u8 acpi_driver_registered:1;
237 u8 acpi_notify_installed:1;
238 u8 proc_created:1;
239 u8 init_called:1;
240 u8 experimental:1;
241 } flags;
244 struct ibm_init_struct {
245 char param[32];
247 int (*init) (struct ibm_init_struct *);
248 struct ibm_struct *data;
251 static struct {
252 #ifdef CONFIG_THINKPAD_ACPI_BAY
253 u32 bay_status:1;
254 u32 bay_eject:1;
255 u32 bay_status2:1;
256 u32 bay_eject2:1;
257 #endif
258 u32 bluetooth:1;
259 u32 hotkey:1;
260 u32 hotkey_mask:1;
261 u32 hotkey_wlsw:1;
262 u32 hotkey_tablet:1;
263 u32 light:1;
264 u32 light_status:1;
265 u32 bright_16levels:1;
266 u32 bright_acpimode:1;
267 u32 wan:1;
268 u32 uwb:1;
269 u32 fan_ctrl_status_undef:1;
270 u32 input_device_registered:1;
271 u32 platform_drv_registered:1;
272 u32 platform_drv_attrs_registered:1;
273 u32 sensors_pdrv_registered:1;
274 u32 sensors_pdrv_attrs_registered:1;
275 u32 sensors_pdev_attrs_registered:1;
276 u32 hotkey_poll_active:1;
277 } tp_features;
279 static struct {
280 u16 hotkey_mask_ff:1;
281 u16 bright_cmos_ec_unsync:1;
282 } tp_warned;
284 struct thinkpad_id_data {
285 unsigned int vendor; /* ThinkPad vendor:
286 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
288 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
289 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
291 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
292 u16 ec_model;
294 char *model_str; /* ThinkPad T43 */
295 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
297 static struct thinkpad_id_data thinkpad_id;
299 static enum {
300 TPACPI_LIFE_INIT = 0,
301 TPACPI_LIFE_RUNNING,
302 TPACPI_LIFE_EXITING,
303 } tpacpi_lifecycle;
305 static int experimental;
306 static u32 dbg_level;
308 static struct workqueue_struct *tpacpi_wq;
310 enum led_status_t {
311 TPACPI_LED_OFF = 0,
312 TPACPI_LED_ON,
313 TPACPI_LED_BLINK,
316 /* Special LED class that can defer work */
317 struct tpacpi_led_classdev {
318 struct led_classdev led_classdev;
319 struct work_struct work;
320 enum led_status_t new_state;
321 unsigned int led;
324 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
325 static int dbg_wlswemul;
326 static int tpacpi_wlsw_emulstate;
327 static int dbg_bluetoothemul;
328 static int tpacpi_bluetooth_emulstate;
329 static int dbg_wwanemul;
330 static int tpacpi_wwan_emulstate;
331 static int dbg_uwbemul;
332 static int tpacpi_uwb_emulstate;
333 #endif
336 /****************************************************************************
337 ****************************************************************************
339 * ACPI Helpers and device model
341 ****************************************************************************
342 ****************************************************************************/
344 /*************************************************************************
345 * ACPI basic handles
348 static acpi_handle root_handle;
350 #define TPACPI_HANDLE(object, parent, paths...) \
351 static acpi_handle object##_handle; \
352 static acpi_handle *object##_parent = &parent##_handle; \
353 static char *object##_path; \
354 static char *object##_paths[] = { paths }
356 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
357 "\\_SB.PCI.ISA.EC", /* 570 */
358 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
359 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
360 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
361 "\\_SB.PCI0.ICH3.EC0", /* R31 */
362 "\\_SB.PCI0.LPC.EC", /* all others */
365 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
366 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
368 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
369 /* T4x, X31, X40 */
370 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
371 "\\CMS", /* R40, R40e */
372 ); /* all others */
374 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
375 "^HKEY", /* R30, R31 */
376 "HKEY", /* all others */
377 ); /* 570 */
379 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
380 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
381 "\\_SB.PCI0.VID0", /* 770e */
382 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
383 "\\_SB.PCI0.AGP.VID", /* all others */
384 ); /* R30, R31 */
387 /*************************************************************************
388 * ACPI helpers
391 static int acpi_evalf(acpi_handle handle,
392 void *res, char *method, char *fmt, ...)
394 char *fmt0 = fmt;
395 struct acpi_object_list params;
396 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
397 struct acpi_buffer result, *resultp;
398 union acpi_object out_obj;
399 acpi_status status;
400 va_list ap;
401 char res_type;
402 int success;
403 int quiet;
405 if (!*fmt) {
406 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
407 return 0;
410 if (*fmt == 'q') {
411 quiet = 1;
412 fmt++;
413 } else
414 quiet = 0;
416 res_type = *(fmt++);
418 params.count = 0;
419 params.pointer = &in_objs[0];
421 va_start(ap, fmt);
422 while (*fmt) {
423 char c = *(fmt++);
424 switch (c) {
425 case 'd': /* int */
426 in_objs[params.count].integer.value = va_arg(ap, int);
427 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
428 break;
429 /* add more types as needed */
430 default:
431 printk(TPACPI_ERR "acpi_evalf() called "
432 "with invalid format character '%c'\n", c);
433 return 0;
436 va_end(ap);
438 if (res_type != 'v') {
439 result.length = sizeof(out_obj);
440 result.pointer = &out_obj;
441 resultp = &result;
442 } else
443 resultp = NULL;
445 status = acpi_evaluate_object(handle, method, &params, resultp);
447 switch (res_type) {
448 case 'd': /* int */
449 if (res)
450 *(int *)res = out_obj.integer.value;
451 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
452 break;
453 case 'v': /* void */
454 success = status == AE_OK;
455 break;
456 /* add more types as needed */
457 default:
458 printk(TPACPI_ERR "acpi_evalf() called "
459 "with invalid format character '%c'\n", res_type);
460 return 0;
463 if (!success && !quiet)
464 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
465 method, fmt0, status);
467 return success;
470 static int acpi_ec_read(int i, u8 *p)
472 int v;
474 if (ecrd_handle) {
475 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
476 return 0;
477 *p = v;
478 } else {
479 if (ec_read(i, p) < 0)
480 return 0;
483 return 1;
486 static int acpi_ec_write(int i, u8 v)
488 if (ecwr_handle) {
489 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
490 return 0;
491 } else {
492 if (ec_write(i, v) < 0)
493 return 0;
496 return 1;
499 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
500 static int _sta(acpi_handle handle)
502 int status;
504 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
505 status = 0;
507 return status;
509 #endif
511 static int issue_thinkpad_cmos_command(int cmos_cmd)
513 if (!cmos_handle)
514 return -ENXIO;
516 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
517 return -EIO;
519 return 0;
522 /*************************************************************************
523 * ACPI device model
526 #define TPACPI_ACPIHANDLE_INIT(object) \
527 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
528 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
530 static void drv_acpi_handle_init(char *name,
531 acpi_handle *handle, acpi_handle parent,
532 char **paths, int num_paths, char **path)
534 int i;
535 acpi_status status;
537 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
538 name);
540 for (i = 0; i < num_paths; i++) {
541 status = acpi_get_handle(parent, paths[i], handle);
542 if (ACPI_SUCCESS(status)) {
543 *path = paths[i];
544 dbg_printk(TPACPI_DBG_INIT,
545 "Found ACPI handle %s for %s\n",
546 *path, name);
547 return;
551 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
552 name);
553 *handle = NULL;
556 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
558 struct ibm_struct *ibm = data;
560 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
561 return;
563 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
564 return;
566 ibm->acpi->notify(ibm, event);
569 static int __init setup_acpi_notify(struct ibm_struct *ibm)
571 acpi_status status;
572 int rc;
574 BUG_ON(!ibm->acpi);
576 if (!*ibm->acpi->handle)
577 return 0;
579 vdbg_printk(TPACPI_DBG_INIT,
580 "setting up ACPI notify for %s\n", ibm->name);
582 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
583 if (rc < 0) {
584 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
585 ibm->name, rc);
586 return -ENODEV;
589 ibm->acpi->device->driver_data = ibm;
590 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
591 TPACPI_ACPI_EVENT_PREFIX,
592 ibm->name);
594 status = acpi_install_notify_handler(*ibm->acpi->handle,
595 ibm->acpi->type, dispatch_acpi_notify, ibm);
596 if (ACPI_FAILURE(status)) {
597 if (status == AE_ALREADY_EXISTS) {
598 printk(TPACPI_NOTICE
599 "another device driver is already "
600 "handling %s events\n", ibm->name);
601 } else {
602 printk(TPACPI_ERR
603 "acpi_install_notify_handler(%s) failed: %d\n",
604 ibm->name, status);
606 return -ENODEV;
608 ibm->flags.acpi_notify_installed = 1;
609 return 0;
612 static int __init tpacpi_device_add(struct acpi_device *device)
614 return 0;
617 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
619 int rc;
621 dbg_printk(TPACPI_DBG_INIT,
622 "registering %s as an ACPI driver\n", ibm->name);
624 BUG_ON(!ibm->acpi);
626 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
627 if (!ibm->acpi->driver) {
628 printk(TPACPI_ERR
629 "failed to allocate memory for ibm->acpi->driver\n");
630 return -ENOMEM;
633 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
634 ibm->acpi->driver->ids = ibm->acpi->hid;
636 ibm->acpi->driver->ops.add = &tpacpi_device_add;
638 rc = acpi_bus_register_driver(ibm->acpi->driver);
639 if (rc < 0) {
640 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
641 ibm->name, rc);
642 kfree(ibm->acpi->driver);
643 ibm->acpi->driver = NULL;
644 } else if (!rc)
645 ibm->flags.acpi_driver_registered = 1;
647 return rc;
651 /****************************************************************************
652 ****************************************************************************
654 * Procfs Helpers
656 ****************************************************************************
657 ****************************************************************************/
659 static int dispatch_procfs_read(char *page, char **start, off_t off,
660 int count, int *eof, void *data)
662 struct ibm_struct *ibm = data;
663 int len;
665 if (!ibm || !ibm->read)
666 return -EINVAL;
668 len = ibm->read(page);
669 if (len < 0)
670 return len;
672 if (len <= off + count)
673 *eof = 1;
674 *start = page + off;
675 len -= off;
676 if (len > count)
677 len = count;
678 if (len < 0)
679 len = 0;
681 return len;
684 static int dispatch_procfs_write(struct file *file,
685 const char __user *userbuf,
686 unsigned long count, void *data)
688 struct ibm_struct *ibm = data;
689 char *kernbuf;
690 int ret;
692 if (!ibm || !ibm->write)
693 return -EINVAL;
695 kernbuf = kmalloc(count + 2, GFP_KERNEL);
696 if (!kernbuf)
697 return -ENOMEM;
699 if (copy_from_user(kernbuf, userbuf, count)) {
700 kfree(kernbuf);
701 return -EFAULT;
704 kernbuf[count] = 0;
705 strcat(kernbuf, ",");
706 ret = ibm->write(kernbuf);
707 if (ret == 0)
708 ret = count;
710 kfree(kernbuf);
712 return ret;
715 static char *next_cmd(char **cmds)
717 char *start = *cmds;
718 char *end;
720 while ((end = strchr(start, ',')) && end == start)
721 start = end + 1;
723 if (!end)
724 return NULL;
726 *end = 0;
727 *cmds = end + 1;
728 return start;
732 /****************************************************************************
733 ****************************************************************************
735 * Device model: input, hwmon and platform
737 ****************************************************************************
738 ****************************************************************************/
740 static struct platform_device *tpacpi_pdev;
741 static struct platform_device *tpacpi_sensors_pdev;
742 static struct device *tpacpi_hwmon;
743 static struct input_dev *tpacpi_inputdev;
744 static struct mutex tpacpi_inputdev_send_mutex;
745 static LIST_HEAD(tpacpi_all_drivers);
747 static int tpacpi_suspend_handler(struct platform_device *pdev,
748 pm_message_t state)
750 struct ibm_struct *ibm, *itmp;
752 list_for_each_entry_safe(ibm, itmp,
753 &tpacpi_all_drivers,
754 all_drivers) {
755 if (ibm->suspend)
756 (ibm->suspend)(state);
759 return 0;
762 static int tpacpi_resume_handler(struct platform_device *pdev)
764 struct ibm_struct *ibm, *itmp;
766 list_for_each_entry_safe(ibm, itmp,
767 &tpacpi_all_drivers,
768 all_drivers) {
769 if (ibm->resume)
770 (ibm->resume)();
773 return 0;
776 static void tpacpi_shutdown_handler(struct platform_device *pdev)
778 struct ibm_struct *ibm, *itmp;
780 list_for_each_entry_safe(ibm, itmp,
781 &tpacpi_all_drivers,
782 all_drivers) {
783 if (ibm->shutdown)
784 (ibm->shutdown)();
788 static struct platform_driver tpacpi_pdriver = {
789 .driver = {
790 .name = TPACPI_DRVR_NAME,
791 .owner = THIS_MODULE,
793 .suspend = tpacpi_suspend_handler,
794 .resume = tpacpi_resume_handler,
795 .shutdown = tpacpi_shutdown_handler,
798 static struct platform_driver tpacpi_hwmon_pdriver = {
799 .driver = {
800 .name = TPACPI_HWMON_DRVR_NAME,
801 .owner = THIS_MODULE,
805 /*************************************************************************
806 * sysfs support helpers
809 struct attribute_set {
810 unsigned int members, max_members;
811 struct attribute_group group;
814 struct attribute_set_obj {
815 struct attribute_set s;
816 struct attribute *a;
817 } __attribute__((packed));
819 static struct attribute_set *create_attr_set(unsigned int max_members,
820 const char *name)
822 struct attribute_set_obj *sobj;
824 if (max_members == 0)
825 return NULL;
827 /* Allocates space for implicit NULL at the end too */
828 sobj = kzalloc(sizeof(struct attribute_set_obj) +
829 max_members * sizeof(struct attribute *),
830 GFP_KERNEL);
831 if (!sobj)
832 return NULL;
833 sobj->s.max_members = max_members;
834 sobj->s.group.attrs = &sobj->a;
835 sobj->s.group.name = name;
837 return &sobj->s;
840 #define destroy_attr_set(_set) \
841 kfree(_set);
843 /* not multi-threaded safe, use it in a single thread per set */
844 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
846 if (!s || !attr)
847 return -EINVAL;
849 if (s->members >= s->max_members)
850 return -ENOMEM;
852 s->group.attrs[s->members] = attr;
853 s->members++;
855 return 0;
858 static int add_many_to_attr_set(struct attribute_set *s,
859 struct attribute **attr,
860 unsigned int count)
862 int i, res;
864 for (i = 0; i < count; i++) {
865 res = add_to_attr_set(s, attr[i]);
866 if (res)
867 return res;
870 return 0;
873 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
875 sysfs_remove_group(kobj, &s->group);
876 destroy_attr_set(s);
879 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
880 sysfs_create_group(_kobj, &_attr_set->group)
882 static int parse_strtoul(const char *buf,
883 unsigned long max, unsigned long *value)
885 char *endp;
887 while (*buf && isspace(*buf))
888 buf++;
889 *value = simple_strtoul(buf, &endp, 0);
890 while (*endp && isspace(*endp))
891 endp++;
892 if (*endp || *value > max)
893 return -EINVAL;
895 return 0;
898 static void tpacpi_disable_brightness_delay(void)
900 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
901 printk(TPACPI_NOTICE
902 "ACPI backlight control delay disabled\n");
905 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
907 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
908 union acpi_object *obj;
909 int rc;
911 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
912 obj = (union acpi_object *)buffer.pointer;
913 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
914 printk(TPACPI_ERR "Unknown _BCL data, "
915 "please report this to %s\n", TPACPI_MAIL);
916 rc = 0;
917 } else {
918 rc = obj->package.count;
920 } else {
921 return 0;
924 kfree(buffer.pointer);
925 return rc;
928 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
929 u32 lvl, void *context, void **rv)
931 char name[ACPI_PATH_SEGMENT_LENGTH];
932 struct acpi_buffer buffer = { sizeof(name), &name };
934 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
935 !strncmp("_BCL", name, sizeof(name) - 1)) {
936 BUG_ON(!rv || !*rv);
937 **(int **)rv = tpacpi_query_bcl_levels(handle);
938 return AE_CTRL_TERMINATE;
939 } else {
940 return AE_OK;
945 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
947 static int __init tpacpi_check_std_acpi_brightness_support(void)
949 int status;
950 int bcl_levels = 0;
951 void *bcl_ptr = &bcl_levels;
953 if (!vid_handle) {
954 TPACPI_ACPIHANDLE_INIT(vid);
956 if (!vid_handle)
957 return 0;
960 * Search for a _BCL method, and execute it. This is safe on all
961 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
962 * BIOS in ACPI backlight control mode. We do NOT have to care
963 * about calling the _BCL method in an enabled video device, any
964 * will do for our purposes.
967 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
968 tpacpi_acpi_walk_find_bcl, NULL,
969 &bcl_ptr);
971 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
972 tp_features.bright_acpimode = 1;
973 return (bcl_levels - 2);
976 return 0;
979 static int __init tpacpi_new_rfkill(const unsigned int id,
980 struct rfkill **rfk,
981 const enum rfkill_type rfktype,
982 const char *name,
983 const bool set_default,
984 int (*toggle_radio)(void *, enum rfkill_state),
985 int (*get_state)(void *, enum rfkill_state *))
987 int res;
988 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
990 res = get_state(NULL, &initial_state);
991 if (res < 0) {
992 printk(TPACPI_ERR
993 "failed to read initial state for %s, error %d; "
994 "will turn radio off\n", name, res);
995 } else if (set_default) {
996 /* try to set the initial state as the default for the rfkill
997 * type, since we ask the firmware to preserve it across S5 in
998 * NVRAM */
999 rfkill_set_default(rfktype,
1000 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1001 RFKILL_STATE_UNBLOCKED :
1002 RFKILL_STATE_SOFT_BLOCKED);
1005 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1006 if (!*rfk) {
1007 printk(TPACPI_ERR
1008 "failed to allocate memory for rfkill class\n");
1009 return -ENOMEM;
1012 (*rfk)->name = name;
1013 (*rfk)->get_state = get_state;
1014 (*rfk)->toggle_radio = toggle_radio;
1015 (*rfk)->state = initial_state;
1017 res = rfkill_register(*rfk);
1018 if (res < 0) {
1019 printk(TPACPI_ERR
1020 "failed to register %s rfkill switch: %d\n",
1021 name, res);
1022 rfkill_free(*rfk);
1023 *rfk = NULL;
1024 return res;
1027 return 0;
1030 /*************************************************************************
1031 * thinkpad-acpi driver attributes
1034 /* interface_version --------------------------------------------------- */
1035 static ssize_t tpacpi_driver_interface_version_show(
1036 struct device_driver *drv,
1037 char *buf)
1039 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1042 static DRIVER_ATTR(interface_version, S_IRUGO,
1043 tpacpi_driver_interface_version_show, NULL);
1045 /* debug_level --------------------------------------------------------- */
1046 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1047 char *buf)
1049 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1052 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1053 const char *buf, size_t count)
1055 unsigned long t;
1057 if (parse_strtoul(buf, 0xffff, &t))
1058 return -EINVAL;
1060 dbg_level = t;
1062 return count;
1065 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1066 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1068 /* version ------------------------------------------------------------- */
1069 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1070 char *buf)
1072 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1073 TPACPI_DESC, TPACPI_VERSION);
1076 static DRIVER_ATTR(version, S_IRUGO,
1077 tpacpi_driver_version_show, NULL);
1079 /* --------------------------------------------------------------------- */
1081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1083 static void tpacpi_send_radiosw_update(void);
1085 /* wlsw_emulstate ------------------------------------------------------ */
1086 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1087 char *buf)
1089 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1092 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1093 const char *buf, size_t count)
1095 unsigned long t;
1097 if (parse_strtoul(buf, 1, &t))
1098 return -EINVAL;
1100 if (tpacpi_wlsw_emulstate != t) {
1101 tpacpi_wlsw_emulstate = !!t;
1102 tpacpi_send_radiosw_update();
1103 } else
1104 tpacpi_wlsw_emulstate = !!t;
1106 return count;
1109 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1110 tpacpi_driver_wlsw_emulstate_show,
1111 tpacpi_driver_wlsw_emulstate_store);
1113 /* bluetooth_emulstate ------------------------------------------------- */
1114 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1115 struct device_driver *drv,
1116 char *buf)
1118 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1121 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1122 struct device_driver *drv,
1123 const char *buf, size_t count)
1125 unsigned long t;
1127 if (parse_strtoul(buf, 1, &t))
1128 return -EINVAL;
1130 tpacpi_bluetooth_emulstate = !!t;
1132 return count;
1135 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1136 tpacpi_driver_bluetooth_emulstate_show,
1137 tpacpi_driver_bluetooth_emulstate_store);
1139 /* wwan_emulstate ------------------------------------------------- */
1140 static ssize_t tpacpi_driver_wwan_emulstate_show(
1141 struct device_driver *drv,
1142 char *buf)
1144 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1147 static ssize_t tpacpi_driver_wwan_emulstate_store(
1148 struct device_driver *drv,
1149 const char *buf, size_t count)
1151 unsigned long t;
1153 if (parse_strtoul(buf, 1, &t))
1154 return -EINVAL;
1156 tpacpi_wwan_emulstate = !!t;
1158 return count;
1161 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1162 tpacpi_driver_wwan_emulstate_show,
1163 tpacpi_driver_wwan_emulstate_store);
1165 /* uwb_emulstate ------------------------------------------------- */
1166 static ssize_t tpacpi_driver_uwb_emulstate_show(
1167 struct device_driver *drv,
1168 char *buf)
1170 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1173 static ssize_t tpacpi_driver_uwb_emulstate_store(
1174 struct device_driver *drv,
1175 const char *buf, size_t count)
1177 unsigned long t;
1179 if (parse_strtoul(buf, 1, &t))
1180 return -EINVAL;
1182 tpacpi_uwb_emulstate = !!t;
1184 return count;
1187 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1188 tpacpi_driver_uwb_emulstate_show,
1189 tpacpi_driver_uwb_emulstate_store);
1190 #endif
1192 /* --------------------------------------------------------------------- */
1194 static struct driver_attribute *tpacpi_driver_attributes[] = {
1195 &driver_attr_debug_level, &driver_attr_version,
1196 &driver_attr_interface_version,
1199 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1201 int i, res;
1203 i = 0;
1204 res = 0;
1205 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1206 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1207 i++;
1210 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1211 if (!res && dbg_wlswemul)
1212 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1213 if (!res && dbg_bluetoothemul)
1214 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1215 if (!res && dbg_wwanemul)
1216 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1217 if (!res && dbg_uwbemul)
1218 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1219 #endif
1221 return res;
1224 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1226 int i;
1228 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1229 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1231 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1232 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1233 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1234 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1235 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1236 #endif
1239 /****************************************************************************
1240 ****************************************************************************
1242 * Subdrivers
1244 ****************************************************************************
1245 ****************************************************************************/
1247 /*************************************************************************
1248 * thinkpad-acpi init subdriver
1251 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1253 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1254 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1256 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1257 (thinkpad_id.bios_version_str) ?
1258 thinkpad_id.bios_version_str : "unknown",
1259 (thinkpad_id.ec_version_str) ?
1260 thinkpad_id.ec_version_str : "unknown");
1262 if (thinkpad_id.vendor && thinkpad_id.model_str)
1263 printk(TPACPI_INFO "%s %s, model %s\n",
1264 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1265 "IBM" : ((thinkpad_id.vendor ==
1266 PCI_VENDOR_ID_LENOVO) ?
1267 "Lenovo" : "Unknown vendor"),
1268 thinkpad_id.model_str,
1269 (thinkpad_id.nummodel_str) ?
1270 thinkpad_id.nummodel_str : "unknown");
1272 return 0;
1275 static int thinkpad_acpi_driver_read(char *p)
1277 int len = 0;
1279 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1280 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1282 return len;
1285 static struct ibm_struct thinkpad_acpi_driver_data = {
1286 .name = "driver",
1287 .read = thinkpad_acpi_driver_read,
1290 /*************************************************************************
1291 * Hotkey subdriver
1294 enum { /* hot key scan codes (derived from ACPI DSDT) */
1295 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1296 TP_ACPI_HOTKEYSCAN_FNF2,
1297 TP_ACPI_HOTKEYSCAN_FNF3,
1298 TP_ACPI_HOTKEYSCAN_FNF4,
1299 TP_ACPI_HOTKEYSCAN_FNF5,
1300 TP_ACPI_HOTKEYSCAN_FNF6,
1301 TP_ACPI_HOTKEYSCAN_FNF7,
1302 TP_ACPI_HOTKEYSCAN_FNF8,
1303 TP_ACPI_HOTKEYSCAN_FNF9,
1304 TP_ACPI_HOTKEYSCAN_FNF10,
1305 TP_ACPI_HOTKEYSCAN_FNF11,
1306 TP_ACPI_HOTKEYSCAN_FNF12,
1307 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1308 TP_ACPI_HOTKEYSCAN_FNINSERT,
1309 TP_ACPI_HOTKEYSCAN_FNDELETE,
1310 TP_ACPI_HOTKEYSCAN_FNHOME,
1311 TP_ACPI_HOTKEYSCAN_FNEND,
1312 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1313 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1314 TP_ACPI_HOTKEYSCAN_FNSPACE,
1315 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1316 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1317 TP_ACPI_HOTKEYSCAN_MUTE,
1318 TP_ACPI_HOTKEYSCAN_THINKPAD,
1321 enum { /* Keys available through NVRAM polling */
1322 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1323 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1326 enum { /* Positions of some of the keys in hotkey masks */
1327 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1328 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1329 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1330 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1331 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1332 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1333 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1334 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1335 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1336 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1337 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1340 enum { /* NVRAM to ACPI HKEY group map */
1341 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1342 TP_ACPI_HKEY_ZOOM_MASK |
1343 TP_ACPI_HKEY_DISPSWTCH_MASK |
1344 TP_ACPI_HKEY_HIBERNATE_MASK,
1345 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1346 TP_ACPI_HKEY_BRGHTDWN_MASK,
1347 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1348 TP_ACPI_HKEY_VOLDWN_MASK |
1349 TP_ACPI_HKEY_MUTE_MASK,
1352 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1353 struct tp_nvram_state {
1354 u16 thinkpad_toggle:1;
1355 u16 zoom_toggle:1;
1356 u16 display_toggle:1;
1357 u16 thinklight_toggle:1;
1358 u16 hibernate_toggle:1;
1359 u16 displayexp_toggle:1;
1360 u16 display_state:1;
1361 u16 brightness_toggle:1;
1362 u16 volume_toggle:1;
1363 u16 mute:1;
1365 u8 brightness_level;
1366 u8 volume_level;
1369 static struct task_struct *tpacpi_hotkey_task;
1370 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1371 static int hotkey_poll_freq = 10; /* Hz */
1372 static struct mutex hotkey_thread_mutex;
1373 static struct mutex hotkey_thread_data_mutex;
1374 static unsigned int hotkey_config_change;
1376 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1378 #define hotkey_source_mask 0U
1380 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1382 static struct mutex hotkey_mutex;
1384 static enum { /* Reasons for waking up */
1385 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1386 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1387 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1388 } hotkey_wakeup_reason;
1390 static int hotkey_autosleep_ack;
1392 static int hotkey_orig_status;
1393 static u32 hotkey_orig_mask;
1394 static u32 hotkey_all_mask;
1395 static u32 hotkey_reserved_mask;
1396 static u32 hotkey_mask;
1398 static unsigned int hotkey_report_mode;
1400 static u16 *hotkey_keycode_map;
1402 static struct attribute_set *hotkey_dev_attributes;
1404 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1405 #define HOTKEY_CONFIG_CRITICAL_START \
1406 do { \
1407 mutex_lock(&hotkey_thread_data_mutex); \
1408 hotkey_config_change++; \
1409 } while (0);
1410 #define HOTKEY_CONFIG_CRITICAL_END \
1411 mutex_unlock(&hotkey_thread_data_mutex);
1412 #else
1413 #define HOTKEY_CONFIG_CRITICAL_START
1414 #define HOTKEY_CONFIG_CRITICAL_END
1415 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1417 /* HKEY.MHKG() return bits */
1418 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1420 static int hotkey_get_wlsw(int *status)
1422 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1423 if (dbg_wlswemul) {
1424 *status = !!tpacpi_wlsw_emulstate;
1425 return 0;
1427 #endif
1428 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1429 return -EIO;
1430 return 0;
1433 static int hotkey_get_tablet_mode(int *status)
1435 int s;
1437 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1438 return -EIO;
1440 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1441 return 0;
1445 * Call with hotkey_mutex held
1447 static int hotkey_mask_get(void)
1449 u32 m = 0;
1451 if (tp_features.hotkey_mask) {
1452 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1453 return -EIO;
1455 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1457 return 0;
1461 * Call with hotkey_mutex held
1463 static int hotkey_mask_set(u32 mask)
1465 int i;
1466 int rc = 0;
1468 if (tp_features.hotkey_mask) {
1469 if (!tp_warned.hotkey_mask_ff &&
1470 (mask == 0xffff || mask == 0xffffff ||
1471 mask == 0xffffffff)) {
1472 tp_warned.hotkey_mask_ff = 1;
1473 printk(TPACPI_NOTICE
1474 "setting the hotkey mask to 0x%08x is likely "
1475 "not the best way to go about it\n", mask);
1476 printk(TPACPI_NOTICE
1477 "please consider using the driver defaults, "
1478 "and refer to up-to-date thinkpad-acpi "
1479 "documentation\n");
1482 HOTKEY_CONFIG_CRITICAL_START
1483 for (i = 0; i < 32; i++) {
1484 u32 m = 1 << i;
1485 /* enable in firmware mask only keys not in NVRAM
1486 * mode, but enable the key in the cached hotkey_mask
1487 * regardless of mode, or the key will end up
1488 * disabled by hotkey_mask_get() */
1489 if (!acpi_evalf(hkey_handle,
1490 NULL, "MHKM", "vdd", i + 1,
1491 !!((mask & ~hotkey_source_mask) & m))) {
1492 rc = -EIO;
1493 break;
1494 } else {
1495 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1498 HOTKEY_CONFIG_CRITICAL_END
1500 /* hotkey_mask_get must be called unconditionally below */
1501 if (!hotkey_mask_get() && !rc &&
1502 (hotkey_mask & ~hotkey_source_mask) !=
1503 (mask & ~hotkey_source_mask)) {
1504 printk(TPACPI_NOTICE
1505 "requested hot key mask 0x%08x, but "
1506 "firmware forced it to 0x%08x\n",
1507 mask, hotkey_mask);
1509 } else {
1510 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1511 HOTKEY_CONFIG_CRITICAL_START
1512 hotkey_mask = mask & hotkey_source_mask;
1513 HOTKEY_CONFIG_CRITICAL_END
1514 hotkey_mask_get();
1515 if (hotkey_mask != mask) {
1516 printk(TPACPI_NOTICE
1517 "requested hot key mask 0x%08x, "
1518 "forced to 0x%08x (NVRAM poll mask is "
1519 "0x%08x): no firmware mask support\n",
1520 mask, hotkey_mask, hotkey_source_mask);
1522 #else
1523 hotkey_mask_get();
1524 rc = -ENXIO;
1525 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1528 return rc;
1531 static int hotkey_status_get(int *status)
1533 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1534 return -EIO;
1536 return 0;
1539 static int hotkey_status_set(int status)
1541 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1542 return -EIO;
1544 return 0;
1547 static void tpacpi_input_send_tabletsw(void)
1549 int state;
1551 if (tp_features.hotkey_tablet &&
1552 !hotkey_get_tablet_mode(&state)) {
1553 mutex_lock(&tpacpi_inputdev_send_mutex);
1555 input_report_switch(tpacpi_inputdev,
1556 SW_TABLET_MODE, !!state);
1557 input_sync(tpacpi_inputdev);
1559 mutex_unlock(&tpacpi_inputdev_send_mutex);
1563 static void tpacpi_input_send_key(unsigned int scancode)
1565 unsigned int keycode;
1567 keycode = hotkey_keycode_map[scancode];
1569 if (keycode != KEY_RESERVED) {
1570 mutex_lock(&tpacpi_inputdev_send_mutex);
1572 input_report_key(tpacpi_inputdev, keycode, 1);
1573 if (keycode == KEY_UNKNOWN)
1574 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1575 scancode);
1576 input_sync(tpacpi_inputdev);
1578 input_report_key(tpacpi_inputdev, keycode, 0);
1579 if (keycode == KEY_UNKNOWN)
1580 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1581 scancode);
1582 input_sync(tpacpi_inputdev);
1584 mutex_unlock(&tpacpi_inputdev_send_mutex);
1588 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1589 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1591 static void tpacpi_hotkey_send_key(unsigned int scancode)
1593 tpacpi_input_send_key(scancode);
1594 if (hotkey_report_mode < 2) {
1595 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1596 0x80, 0x1001 + scancode);
1600 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1602 u8 d;
1604 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1605 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1606 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1607 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1608 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1609 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1611 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1612 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1613 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1615 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1616 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1617 n->displayexp_toggle =
1618 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1620 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1621 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1622 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1623 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1624 n->brightness_toggle =
1625 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1627 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1628 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1629 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1630 >> TP_NVRAM_POS_LEVEL_VOLUME;
1631 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1632 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1636 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1637 do { \
1638 if ((mask & (1 << __scancode)) && \
1639 oldn->__member != newn->__member) \
1640 tpacpi_hotkey_send_key(__scancode); \
1641 } while (0)
1643 #define TPACPI_MAY_SEND_KEY(__scancode) \
1644 do { if (mask & (1 << __scancode)) \
1645 tpacpi_hotkey_send_key(__scancode); } while (0)
1647 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1648 struct tp_nvram_state *newn,
1649 u32 mask)
1651 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1652 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1653 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1654 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1656 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1658 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1660 /* handle volume */
1661 if (oldn->volume_toggle != newn->volume_toggle) {
1662 if (oldn->mute != newn->mute) {
1663 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1665 if (oldn->volume_level > newn->volume_level) {
1666 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1667 } else if (oldn->volume_level < newn->volume_level) {
1668 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1669 } else if (oldn->mute == newn->mute) {
1670 /* repeated key presses that didn't change state */
1671 if (newn->mute) {
1672 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1673 } else if (newn->volume_level != 0) {
1674 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1675 } else {
1676 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1681 /* handle brightness */
1682 if (oldn->brightness_toggle != newn->brightness_toggle) {
1683 if (oldn->brightness_level < newn->brightness_level) {
1684 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1685 } else if (oldn->brightness_level > newn->brightness_level) {
1686 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1687 } else {
1688 /* repeated key presses that didn't change state */
1689 if (newn->brightness_level != 0) {
1690 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1691 } else {
1692 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1698 #undef TPACPI_COMPARE_KEY
1699 #undef TPACPI_MAY_SEND_KEY
1701 static int hotkey_kthread(void *data)
1703 struct tp_nvram_state s[2];
1704 u32 mask;
1705 unsigned int si, so;
1706 unsigned long t;
1707 unsigned int change_detector, must_reset;
1709 mutex_lock(&hotkey_thread_mutex);
1711 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1712 goto exit;
1714 set_freezable();
1716 so = 0;
1717 si = 1;
1718 t = 0;
1720 /* Initial state for compares */
1721 mutex_lock(&hotkey_thread_data_mutex);
1722 change_detector = hotkey_config_change;
1723 mask = hotkey_source_mask & hotkey_mask;
1724 mutex_unlock(&hotkey_thread_data_mutex);
1725 hotkey_read_nvram(&s[so], mask);
1727 while (!kthread_should_stop() && hotkey_poll_freq) {
1728 if (t == 0)
1729 t = 1000/hotkey_poll_freq;
1730 t = msleep_interruptible(t);
1731 if (unlikely(kthread_should_stop()))
1732 break;
1733 must_reset = try_to_freeze();
1734 if (t > 0 && !must_reset)
1735 continue;
1737 mutex_lock(&hotkey_thread_data_mutex);
1738 if (must_reset || hotkey_config_change != change_detector) {
1739 /* forget old state on thaw or config change */
1740 si = so;
1741 t = 0;
1742 change_detector = hotkey_config_change;
1744 mask = hotkey_source_mask & hotkey_mask;
1745 mutex_unlock(&hotkey_thread_data_mutex);
1747 if (likely(mask)) {
1748 hotkey_read_nvram(&s[si], mask);
1749 if (likely(si != so)) {
1750 hotkey_compare_and_issue_event(&s[so], &s[si],
1751 mask);
1755 so = si;
1756 si ^= 1;
1759 exit:
1760 mutex_unlock(&hotkey_thread_mutex);
1761 return 0;
1764 static void hotkey_poll_stop_sync(void)
1766 if (tpacpi_hotkey_task) {
1767 if (frozen(tpacpi_hotkey_task) ||
1768 freezing(tpacpi_hotkey_task))
1769 thaw_process(tpacpi_hotkey_task);
1771 kthread_stop(tpacpi_hotkey_task);
1772 tpacpi_hotkey_task = NULL;
1773 mutex_lock(&hotkey_thread_mutex);
1774 /* at this point, the thread did exit */
1775 mutex_unlock(&hotkey_thread_mutex);
1779 /* call with hotkey_mutex held */
1780 static void hotkey_poll_setup(int may_warn)
1782 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1783 hotkey_poll_freq > 0 &&
1784 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1785 if (!tpacpi_hotkey_task) {
1786 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1787 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1788 if (IS_ERR(tpacpi_hotkey_task)) {
1789 tpacpi_hotkey_task = NULL;
1790 printk(TPACPI_ERR
1791 "could not create kernel thread "
1792 "for hotkey polling\n");
1795 } else {
1796 hotkey_poll_stop_sync();
1797 if (may_warn &&
1798 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1799 printk(TPACPI_NOTICE
1800 "hot keys 0x%08x require polling, "
1801 "which is currently disabled\n",
1802 hotkey_source_mask);
1807 static void hotkey_poll_setup_safe(int may_warn)
1809 mutex_lock(&hotkey_mutex);
1810 hotkey_poll_setup(may_warn);
1811 mutex_unlock(&hotkey_mutex);
1814 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1816 static void hotkey_poll_setup_safe(int __unused)
1820 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1822 static int hotkey_inputdev_open(struct input_dev *dev)
1824 switch (tpacpi_lifecycle) {
1825 case TPACPI_LIFE_INIT:
1827 * hotkey_init will call hotkey_poll_setup_safe
1828 * at the appropriate moment
1830 return 0;
1831 case TPACPI_LIFE_EXITING:
1832 return -EBUSY;
1833 case TPACPI_LIFE_RUNNING:
1834 hotkey_poll_setup_safe(0);
1835 return 0;
1838 /* Should only happen if tpacpi_lifecycle is corrupt */
1839 BUG();
1840 return -EBUSY;
1843 static void hotkey_inputdev_close(struct input_dev *dev)
1845 /* disable hotkey polling when possible */
1846 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1847 hotkey_poll_setup_safe(0);
1850 /* sysfs hotkey enable ------------------------------------------------- */
1851 static ssize_t hotkey_enable_show(struct device *dev,
1852 struct device_attribute *attr,
1853 char *buf)
1855 int res, status;
1857 res = hotkey_status_get(&status);
1858 if (res)
1859 return res;
1861 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1864 static ssize_t hotkey_enable_store(struct device *dev,
1865 struct device_attribute *attr,
1866 const char *buf, size_t count)
1868 unsigned long t;
1869 int res;
1871 if (parse_strtoul(buf, 1, &t))
1872 return -EINVAL;
1874 res = hotkey_status_set(t);
1876 return (res) ? res : count;
1879 static struct device_attribute dev_attr_hotkey_enable =
1880 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1881 hotkey_enable_show, hotkey_enable_store);
1883 /* sysfs hotkey mask --------------------------------------------------- */
1884 static ssize_t hotkey_mask_show(struct device *dev,
1885 struct device_attribute *attr,
1886 char *buf)
1888 int res;
1890 if (mutex_lock_killable(&hotkey_mutex))
1891 return -ERESTARTSYS;
1892 res = hotkey_mask_get();
1893 mutex_unlock(&hotkey_mutex);
1895 return (res)?
1896 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1899 static ssize_t hotkey_mask_store(struct device *dev,
1900 struct device_attribute *attr,
1901 const char *buf, size_t count)
1903 unsigned long t;
1904 int res;
1906 if (parse_strtoul(buf, 0xffffffffUL, &t))
1907 return -EINVAL;
1909 if (mutex_lock_killable(&hotkey_mutex))
1910 return -ERESTARTSYS;
1912 res = hotkey_mask_set(t);
1914 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 hotkey_poll_setup(1);
1916 #endif
1918 mutex_unlock(&hotkey_mutex);
1920 return (res) ? res : count;
1923 static struct device_attribute dev_attr_hotkey_mask =
1924 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1925 hotkey_mask_show, hotkey_mask_store);
1927 /* sysfs hotkey bios_enabled ------------------------------------------- */
1928 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1929 struct device_attribute *attr,
1930 char *buf)
1932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1935 static struct device_attribute dev_attr_hotkey_bios_enabled =
1936 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1938 /* sysfs hotkey bios_mask ---------------------------------------------- */
1939 static ssize_t hotkey_bios_mask_show(struct device *dev,
1940 struct device_attribute *attr,
1941 char *buf)
1943 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1946 static struct device_attribute dev_attr_hotkey_bios_mask =
1947 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1949 /* sysfs hotkey all_mask ----------------------------------------------- */
1950 static ssize_t hotkey_all_mask_show(struct device *dev,
1951 struct device_attribute *attr,
1952 char *buf)
1954 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1955 hotkey_all_mask | hotkey_source_mask);
1958 static struct device_attribute dev_attr_hotkey_all_mask =
1959 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1961 /* sysfs hotkey recommended_mask --------------------------------------- */
1962 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1963 struct device_attribute *attr,
1964 char *buf)
1966 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1967 (hotkey_all_mask | hotkey_source_mask)
1968 & ~hotkey_reserved_mask);
1971 static struct device_attribute dev_attr_hotkey_recommended_mask =
1972 __ATTR(hotkey_recommended_mask, S_IRUGO,
1973 hotkey_recommended_mask_show, NULL);
1975 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1977 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1978 static ssize_t hotkey_source_mask_show(struct device *dev,
1979 struct device_attribute *attr,
1980 char *buf)
1982 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1985 static ssize_t hotkey_source_mask_store(struct device *dev,
1986 struct device_attribute *attr,
1987 const char *buf, size_t count)
1989 unsigned long t;
1991 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1992 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1993 return -EINVAL;
1995 if (mutex_lock_killable(&hotkey_mutex))
1996 return -ERESTARTSYS;
1998 HOTKEY_CONFIG_CRITICAL_START
1999 hotkey_source_mask = t;
2000 HOTKEY_CONFIG_CRITICAL_END
2002 hotkey_poll_setup(1);
2004 mutex_unlock(&hotkey_mutex);
2006 return count;
2009 static struct device_attribute dev_attr_hotkey_source_mask =
2010 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2011 hotkey_source_mask_show, hotkey_source_mask_store);
2013 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2014 static ssize_t hotkey_poll_freq_show(struct device *dev,
2015 struct device_attribute *attr,
2016 char *buf)
2018 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2021 static ssize_t hotkey_poll_freq_store(struct device *dev,
2022 struct device_attribute *attr,
2023 const char *buf, size_t count)
2025 unsigned long t;
2027 if (parse_strtoul(buf, 25, &t))
2028 return -EINVAL;
2030 if (mutex_lock_killable(&hotkey_mutex))
2031 return -ERESTARTSYS;
2033 hotkey_poll_freq = t;
2035 hotkey_poll_setup(1);
2036 mutex_unlock(&hotkey_mutex);
2038 return count;
2041 static struct device_attribute dev_attr_hotkey_poll_freq =
2042 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2043 hotkey_poll_freq_show, hotkey_poll_freq_store);
2045 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2047 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2048 static ssize_t hotkey_radio_sw_show(struct device *dev,
2049 struct device_attribute *attr,
2050 char *buf)
2052 int res, s;
2053 res = hotkey_get_wlsw(&s);
2054 if (res < 0)
2055 return res;
2057 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2060 static struct device_attribute dev_attr_hotkey_radio_sw =
2061 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2063 static void hotkey_radio_sw_notify_change(void)
2065 if (tp_features.hotkey_wlsw)
2066 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2067 "hotkey_radio_sw");
2070 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2071 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2072 struct device_attribute *attr,
2073 char *buf)
2075 int res, s;
2076 res = hotkey_get_tablet_mode(&s);
2077 if (res < 0)
2078 return res;
2080 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2083 static struct device_attribute dev_attr_hotkey_tablet_mode =
2084 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2086 static void hotkey_tablet_mode_notify_change(void)
2088 if (tp_features.hotkey_tablet)
2089 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2090 "hotkey_tablet_mode");
2093 /* sysfs hotkey report_mode -------------------------------------------- */
2094 static ssize_t hotkey_report_mode_show(struct device *dev,
2095 struct device_attribute *attr,
2096 char *buf)
2098 return snprintf(buf, PAGE_SIZE, "%d\n",
2099 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2102 static struct device_attribute dev_attr_hotkey_report_mode =
2103 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2105 /* sysfs wakeup reason (pollable) -------------------------------------- */
2106 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2107 struct device_attribute *attr,
2108 char *buf)
2110 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2113 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2114 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2116 static void hotkey_wakeup_reason_notify_change(void)
2118 if (tp_features.hotkey_mask)
2119 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2120 "wakeup_reason");
2123 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2124 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2125 struct device_attribute *attr,
2126 char *buf)
2128 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2131 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2132 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2133 hotkey_wakeup_hotunplug_complete_show, NULL);
2135 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2137 if (tp_features.hotkey_mask)
2138 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2139 "wakeup_hotunplug_complete");
2142 /* --------------------------------------------------------------------- */
2144 static struct attribute *hotkey_attributes[] __initdata = {
2145 &dev_attr_hotkey_enable.attr,
2146 &dev_attr_hotkey_bios_enabled.attr,
2147 &dev_attr_hotkey_report_mode.attr,
2148 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2149 &dev_attr_hotkey_mask.attr,
2150 &dev_attr_hotkey_all_mask.attr,
2151 &dev_attr_hotkey_recommended_mask.attr,
2152 &dev_attr_hotkey_source_mask.attr,
2153 &dev_attr_hotkey_poll_freq.attr,
2154 #endif
2157 static struct attribute *hotkey_mask_attributes[] __initdata = {
2158 &dev_attr_hotkey_bios_mask.attr,
2159 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2160 &dev_attr_hotkey_mask.attr,
2161 &dev_attr_hotkey_all_mask.attr,
2162 &dev_attr_hotkey_recommended_mask.attr,
2163 #endif
2164 &dev_attr_hotkey_wakeup_reason.attr,
2165 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2168 static void bluetooth_update_rfk(void);
2169 static void wan_update_rfk(void);
2170 static void uwb_update_rfk(void);
2171 static void tpacpi_send_radiosw_update(void)
2173 int wlsw;
2175 /* Sync these BEFORE sending any rfkill events */
2176 if (tp_features.bluetooth)
2177 bluetooth_update_rfk();
2178 if (tp_features.wan)
2179 wan_update_rfk();
2180 if (tp_features.uwb)
2181 uwb_update_rfk();
2183 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2184 mutex_lock(&tpacpi_inputdev_send_mutex);
2186 input_report_switch(tpacpi_inputdev,
2187 SW_RFKILL_ALL, !!wlsw);
2188 input_sync(tpacpi_inputdev);
2190 mutex_unlock(&tpacpi_inputdev_send_mutex);
2192 hotkey_radio_sw_notify_change();
2195 static void hotkey_exit(void)
2197 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2198 hotkey_poll_stop_sync();
2199 #endif
2201 if (hotkey_dev_attributes)
2202 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2204 kfree(hotkey_keycode_map);
2206 if (tp_features.hotkey) {
2207 dbg_printk(TPACPI_DBG_EXIT,
2208 "restoring original hot key mask\n");
2209 /* no short-circuit boolean operator below! */
2210 if ((hotkey_mask_set(hotkey_orig_mask) |
2211 hotkey_status_set(hotkey_orig_status)) != 0)
2212 printk(TPACPI_ERR
2213 "failed to restore hot key mask "
2214 "to BIOS defaults\n");
2218 static int __init hotkey_init(struct ibm_init_struct *iibm)
2220 /* Requirements for changing the default keymaps:
2222 * 1. Many of the keys are mapped to KEY_RESERVED for very
2223 * good reasons. Do not change them unless you have deep
2224 * knowledge on the IBM and Lenovo ThinkPad firmware for
2225 * the various ThinkPad models. The driver behaves
2226 * differently for KEY_RESERVED: such keys have their
2227 * hot key mask *unset* in mask_recommended, and also
2228 * in the initial hot key mask programmed into the
2229 * firmware at driver load time, which means the firm-
2230 * ware may react very differently if you change them to
2231 * something else;
2233 * 2. You must be subscribed to the linux-thinkpad and
2234 * ibm-acpi-devel mailing lists, and you should read the
2235 * list archives since 2007 if you want to change the
2236 * keymaps. This requirement exists so that you will
2237 * know the past history of problems with the thinkpad-
2238 * acpi driver keymaps, and also that you will be
2239 * listening to any bug reports;
2241 * 3. Do not send thinkpad-acpi specific patches directly to
2242 * for merging, *ever*. Send them to the linux-acpi
2243 * mailinglist for comments. Merging is to be done only
2244 * through acpi-test and the ACPI maintainer.
2246 * If the above is too much to ask, don't change the keymap.
2247 * Ask the thinkpad-acpi maintainer to do it, instead.
2249 static u16 ibm_keycode_map[] __initdata = {
2250 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2251 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2252 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2253 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2255 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2256 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2257 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2258 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2260 /* brightness: firmware always reacts to them, unless
2261 * X.org did some tricks in the radeon BIOS scratch
2262 * registers of *some* models */
2263 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2264 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2266 /* Thinklight: firmware always react to it */
2267 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2269 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2270 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2272 /* Volume: firmware always react to it and reprograms
2273 * the built-in *extra* mixer. Never map it to control
2274 * another mixer by default. */
2275 KEY_RESERVED, /* 0x14: VOLUME UP */
2276 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2277 KEY_RESERVED, /* 0x16: MUTE */
2279 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2281 /* (assignments unknown, please report if found) */
2282 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2283 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2285 static u16 lenovo_keycode_map[] __initdata = {
2286 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2287 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2288 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2289 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2291 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2292 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2293 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2294 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2296 /* These either have to go through ACPI video, or
2297 * act like in the IBM ThinkPads, so don't ever
2298 * enable them by default */
2299 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2300 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2302 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2304 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2305 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2307 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2308 * react to it and reprograms the built-in *extra* mixer.
2309 * Never map it to control another mixer by default.
2311 * T60?, T61, R60?, R61: firmware and EC tries to send
2312 * these over the regular keyboard, so these are no-ops,
2313 * but there are still weird bugs re. MUTE, so do not
2314 * change unless you get test reports from all Lenovo
2315 * models. May cause the BIOS to interfere with the
2316 * HDA mixer.
2318 KEY_RESERVED, /* 0x14: VOLUME UP */
2319 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2320 KEY_RESERVED, /* 0x16: MUTE */
2322 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2324 /* (assignments unknown, please report if found) */
2325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2329 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2330 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2331 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2333 int res, i;
2334 int status;
2335 int hkeyv;
2337 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2339 BUG_ON(!tpacpi_inputdev);
2340 BUG_ON(tpacpi_inputdev->open != NULL ||
2341 tpacpi_inputdev->close != NULL);
2343 TPACPI_ACPIHANDLE_INIT(hkey);
2344 mutex_init(&hotkey_mutex);
2346 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2347 mutex_init(&hotkey_thread_mutex);
2348 mutex_init(&hotkey_thread_data_mutex);
2349 #endif
2351 /* hotkey not supported on 570 */
2352 tp_features.hotkey = hkey_handle != NULL;
2354 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2355 str_supported(tp_features.hotkey));
2357 if (!tp_features.hotkey)
2358 return 1;
2360 tpacpi_disable_brightness_delay();
2362 hotkey_dev_attributes = create_attr_set(13, NULL);
2363 if (!hotkey_dev_attributes)
2364 return -ENOMEM;
2365 res = add_many_to_attr_set(hotkey_dev_attributes,
2366 hotkey_attributes,
2367 ARRAY_SIZE(hotkey_attributes));
2368 if (res)
2369 goto err_exit;
2371 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2372 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2373 for HKEY interface version 0x100 */
2374 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2375 if ((hkeyv >> 8) != 1) {
2376 printk(TPACPI_ERR "unknown version of the "
2377 "HKEY interface: 0x%x\n", hkeyv);
2378 printk(TPACPI_ERR "please report this to %s\n",
2379 TPACPI_MAIL);
2380 } else {
2382 * MHKV 0x100 in A31, R40, R40e,
2383 * T4x, X31, and later
2385 tp_features.hotkey_mask = 1;
2389 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2390 str_supported(tp_features.hotkey_mask));
2392 if (tp_features.hotkey_mask) {
2393 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2394 "MHKA", "qd")) {
2395 printk(TPACPI_ERR
2396 "missing MHKA handler, "
2397 "please report this to %s\n",
2398 TPACPI_MAIL);
2399 /* FN+F12, FN+F4, FN+F3 */
2400 hotkey_all_mask = 0x080cU;
2404 /* hotkey_source_mask *must* be zero for
2405 * the first hotkey_mask_get */
2406 res = hotkey_status_get(&hotkey_orig_status);
2407 if (res)
2408 goto err_exit;
2410 if (tp_features.hotkey_mask) {
2411 res = hotkey_mask_get();
2412 if (res)
2413 goto err_exit;
2415 hotkey_orig_mask = hotkey_mask;
2416 res = add_many_to_attr_set(
2417 hotkey_dev_attributes,
2418 hotkey_mask_attributes,
2419 ARRAY_SIZE(hotkey_mask_attributes));
2420 if (res)
2421 goto err_exit;
2424 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2425 if (tp_features.hotkey_mask) {
2426 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2427 & ~hotkey_all_mask;
2428 } else {
2429 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2432 vdbg_printk(TPACPI_DBG_INIT,
2433 "hotkey source mask 0x%08x, polling freq %d\n",
2434 hotkey_source_mask, hotkey_poll_freq);
2435 #endif
2437 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2438 if (dbg_wlswemul) {
2439 tp_features.hotkey_wlsw = 1;
2440 printk(TPACPI_INFO
2441 "radio switch emulation enabled\n");
2442 } else
2443 #endif
2444 /* Not all thinkpads have a hardware radio switch */
2445 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2446 tp_features.hotkey_wlsw = 1;
2447 printk(TPACPI_INFO
2448 "radio switch found; radios are %s\n",
2449 enabled(status, 0));
2451 if (tp_features.hotkey_wlsw)
2452 res = add_to_attr_set(hotkey_dev_attributes,
2453 &dev_attr_hotkey_radio_sw.attr);
2455 /* For X41t, X60t, X61t Tablets... */
2456 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2457 tp_features.hotkey_tablet = 1;
2458 printk(TPACPI_INFO
2459 "possible tablet mode switch found; "
2460 "ThinkPad in %s mode\n",
2461 (status & TP_HOTKEY_TABLET_MASK)?
2462 "tablet" : "laptop");
2463 res = add_to_attr_set(hotkey_dev_attributes,
2464 &dev_attr_hotkey_tablet_mode.attr);
2467 if (!res)
2468 res = register_attr_set_with_sysfs(
2469 hotkey_dev_attributes,
2470 &tpacpi_pdev->dev.kobj);
2471 if (res)
2472 goto err_exit;
2474 /* Set up key map */
2476 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2477 GFP_KERNEL);
2478 if (!hotkey_keycode_map) {
2479 printk(TPACPI_ERR
2480 "failed to allocate memory for key map\n");
2481 res = -ENOMEM;
2482 goto err_exit;
2485 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2486 dbg_printk(TPACPI_DBG_INIT,
2487 "using Lenovo default hot key map\n");
2488 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2489 TPACPI_HOTKEY_MAP_SIZE);
2490 } else {
2491 dbg_printk(TPACPI_DBG_INIT,
2492 "using IBM default hot key map\n");
2493 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2494 TPACPI_HOTKEY_MAP_SIZE);
2497 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2498 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2499 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2500 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2501 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2502 tpacpi_inputdev->keycode = hotkey_keycode_map;
2503 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2504 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2505 set_bit(hotkey_keycode_map[i],
2506 tpacpi_inputdev->keybit);
2507 } else {
2508 if (i < sizeof(hotkey_reserved_mask)*8)
2509 hotkey_reserved_mask |= 1 << i;
2513 if (tp_features.hotkey_wlsw) {
2514 set_bit(EV_SW, tpacpi_inputdev->evbit);
2515 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2517 if (tp_features.hotkey_tablet) {
2518 set_bit(EV_SW, tpacpi_inputdev->evbit);
2519 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2522 /* Do not issue duplicate brightness change events to
2523 * userspace */
2524 if (!tp_features.bright_acpimode)
2525 /* update bright_acpimode... */
2526 tpacpi_check_std_acpi_brightness_support();
2528 if (tp_features.bright_acpimode) {
2529 printk(TPACPI_INFO
2530 "This ThinkPad has standard ACPI backlight "
2531 "brightness control, supported by the ACPI "
2532 "video driver\n");
2533 printk(TPACPI_NOTICE
2534 "Disabling thinkpad-acpi brightness events "
2535 "by default...\n");
2537 /* The hotkey_reserved_mask change below is not
2538 * necessary while the keys are at KEY_RESERVED in the
2539 * default map, but better safe than sorry, leave it
2540 * here as a marker of what we have to do, especially
2541 * when we finally become able to set this at runtime
2542 * on response to X.org requests */
2543 hotkey_reserved_mask |=
2544 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2545 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2548 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2549 res = hotkey_status_set(1);
2550 if (res) {
2551 hotkey_exit();
2552 return res;
2554 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2555 & ~hotkey_reserved_mask)
2556 | hotkey_orig_mask);
2557 if (res < 0 && res != -ENXIO) {
2558 hotkey_exit();
2559 return res;
2562 dbg_printk(TPACPI_DBG_INIT,
2563 "legacy hot key reporting over procfs %s\n",
2564 (hotkey_report_mode < 2) ?
2565 "enabled" : "disabled");
2567 tpacpi_inputdev->open = &hotkey_inputdev_open;
2568 tpacpi_inputdev->close = &hotkey_inputdev_close;
2570 hotkey_poll_setup_safe(1);
2571 tpacpi_send_radiosw_update();
2572 tpacpi_input_send_tabletsw();
2574 return 0;
2576 err_exit:
2577 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2578 hotkey_dev_attributes = NULL;
2580 return (res < 0)? res : 1;
2583 static bool hotkey_notify_hotkey(const u32 hkey,
2584 bool *send_acpi_ev,
2585 bool *ignore_acpi_ev)
2587 /* 0x1000-0x1FFF: key presses */
2588 unsigned int scancode = hkey & 0xfff;
2589 *send_acpi_ev = true;
2590 *ignore_acpi_ev = false;
2592 if (scancode > 0 && scancode < 0x21) {
2593 scancode--;
2594 if (!(hotkey_source_mask & (1 << scancode))) {
2595 tpacpi_input_send_key(scancode);
2596 *send_acpi_ev = false;
2597 } else {
2598 *ignore_acpi_ev = true;
2600 return true;
2602 return false;
2605 static bool hotkey_notify_wakeup(const u32 hkey,
2606 bool *send_acpi_ev,
2607 bool *ignore_acpi_ev)
2609 /* 0x2000-0x2FFF: Wakeup reason */
2610 *send_acpi_ev = true;
2611 *ignore_acpi_ev = false;
2613 switch (hkey) {
2614 case 0x2304: /* suspend, undock */
2615 case 0x2404: /* hibernation, undock */
2616 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2617 *ignore_acpi_ev = true;
2618 break;
2620 case 0x2305: /* suspend, bay eject */
2621 case 0x2405: /* hibernation, bay eject */
2622 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2623 *ignore_acpi_ev = true;
2624 break;
2626 case 0x2313: /* Battery on critical low level (S3) */
2627 case 0x2413: /* Battery on critical low level (S4) */
2628 printk(TPACPI_ALERT
2629 "EMERGENCY WAKEUP: battery almost empty\n");
2630 /* how to auto-heal: */
2631 /* 2313: woke up from S3, go to S4/S5 */
2632 /* 2413: woke up from S4, go to S5 */
2633 break;
2635 default:
2636 return false;
2639 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2640 printk(TPACPI_INFO
2641 "woke up due to a hot-unplug "
2642 "request...\n");
2643 hotkey_wakeup_reason_notify_change();
2645 return true;
2648 static bool hotkey_notify_usrevent(const u32 hkey,
2649 bool *send_acpi_ev,
2650 bool *ignore_acpi_ev)
2652 /* 0x5000-0x5FFF: human interface helpers */
2653 *send_acpi_ev = true;
2654 *ignore_acpi_ev = false;
2656 switch (hkey) {
2657 case 0x5010: /* Lenovo new BIOS: brightness changed */
2658 case 0x500b: /* X61t: tablet pen inserted into bay */
2659 case 0x500c: /* X61t: tablet pen removed from bay */
2660 return true;
2662 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2663 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2664 tpacpi_input_send_tabletsw();
2665 hotkey_tablet_mode_notify_change();
2666 *send_acpi_ev = false;
2667 return true;
2669 case 0x5001:
2670 case 0x5002:
2671 /* LID switch events. Do not propagate */
2672 *ignore_acpi_ev = true;
2673 return true;
2675 default:
2676 return false;
2680 static bool hotkey_notify_thermal(const u32 hkey,
2681 bool *send_acpi_ev,
2682 bool *ignore_acpi_ev)
2684 /* 0x6000-0x6FFF: thermal alarms */
2685 *send_acpi_ev = true;
2686 *ignore_acpi_ev = false;
2688 switch (hkey) {
2689 case 0x6011:
2690 printk(TPACPI_CRIT
2691 "THERMAL ALARM: battery is too hot!\n");
2692 /* recommended action: warn user through gui */
2693 return true;
2694 case 0x6012:
2695 printk(TPACPI_ALERT
2696 "THERMAL EMERGENCY: battery is extremely hot!\n");
2697 /* recommended action: immediate sleep/hibernate */
2698 return true;
2699 case 0x6021:
2700 printk(TPACPI_CRIT
2701 "THERMAL ALARM: "
2702 "a sensor reports something is too hot!\n");
2703 /* recommended action: warn user through gui, that */
2704 /* some internal component is too hot */
2705 return true;
2706 case 0x6022:
2707 printk(TPACPI_ALERT
2708 "THERMAL EMERGENCY: "
2709 "a sensor reports something is extremely hot!\n");
2710 /* recommended action: immediate sleep/hibernate */
2711 return true;
2712 default:
2713 printk(TPACPI_ALERT
2714 "THERMAL ALERT: unknown thermal alarm received\n");
2715 return false;
2719 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2721 u32 hkey;
2722 bool send_acpi_ev;
2723 bool ignore_acpi_ev;
2724 bool known_ev;
2726 if (event != 0x80) {
2727 printk(TPACPI_ERR
2728 "unknown HKEY notification event %d\n", event);
2729 /* forward it to userspace, maybe it knows how to handle it */
2730 acpi_bus_generate_netlink_event(
2731 ibm->acpi->device->pnp.device_class,
2732 dev_name(&ibm->acpi->device->dev),
2733 event, 0);
2734 return;
2737 while (1) {
2738 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2739 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2740 return;
2743 if (hkey == 0) {
2744 /* queue empty */
2745 return;
2748 send_acpi_ev = true;
2749 ignore_acpi_ev = false;
2751 switch (hkey >> 12) {
2752 case 1:
2753 /* 0x1000-0x1FFF: key presses */
2754 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2755 &ignore_acpi_ev);
2756 break;
2757 case 2:
2758 /* 0x2000-0x2FFF: Wakeup reason */
2759 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2760 &ignore_acpi_ev);
2761 break;
2762 case 3:
2763 /* 0x3000-0x3FFF: bay-related wakeups */
2764 if (hkey == 0x3003) {
2765 hotkey_autosleep_ack = 1;
2766 printk(TPACPI_INFO
2767 "bay ejected\n");
2768 hotkey_wakeup_hotunplug_complete_notify_change();
2769 known_ev = true;
2770 } else {
2771 known_ev = false;
2773 break;
2774 case 4:
2775 /* 0x4000-0x4FFF: dock-related wakeups */
2776 if (hkey == 0x4003) {
2777 hotkey_autosleep_ack = 1;
2778 printk(TPACPI_INFO
2779 "undocked\n");
2780 hotkey_wakeup_hotunplug_complete_notify_change();
2781 known_ev = true;
2782 } else {
2783 known_ev = false;
2785 break;
2786 case 5:
2787 /* 0x5000-0x5FFF: human interface helpers */
2788 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2789 &ignore_acpi_ev);
2790 break;
2791 case 6:
2792 /* 0x6000-0x6FFF: thermal alarms */
2793 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2794 &ignore_acpi_ev);
2795 break;
2796 case 7:
2797 /* 0x7000-0x7FFF: misc */
2798 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2799 tpacpi_send_radiosw_update();
2800 send_acpi_ev = 0;
2801 known_ev = true;
2802 break;
2804 /* fallthrough to default */
2805 default:
2806 known_ev = false;
2808 if (!known_ev) {
2809 printk(TPACPI_NOTICE
2810 "unhandled HKEY event 0x%04x\n", hkey);
2813 /* Legacy events */
2814 if (!ignore_acpi_ev &&
2815 (send_acpi_ev || hotkey_report_mode < 2)) {
2816 acpi_bus_generate_proc_event(ibm->acpi->device,
2817 event, hkey);
2820 /* netlink events */
2821 if (!ignore_acpi_ev && send_acpi_ev) {
2822 acpi_bus_generate_netlink_event(
2823 ibm->acpi->device->pnp.device_class,
2824 dev_name(&ibm->acpi->device->dev),
2825 event, hkey);
2830 static void hotkey_suspend(pm_message_t state)
2832 /* Do these on suspend, we get the events on early resume! */
2833 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2834 hotkey_autosleep_ack = 0;
2837 static void hotkey_resume(void)
2839 tpacpi_disable_brightness_delay();
2841 if (hotkey_mask_get())
2842 printk(TPACPI_ERR
2843 "error while trying to read hot key mask "
2844 "from firmware\n");
2845 tpacpi_send_radiosw_update();
2846 hotkey_tablet_mode_notify_change();
2847 hotkey_wakeup_reason_notify_change();
2848 hotkey_wakeup_hotunplug_complete_notify_change();
2849 hotkey_poll_setup_safe(0);
2852 /* procfs -------------------------------------------------------------- */
2853 static int hotkey_read(char *p)
2855 int res, status;
2856 int len = 0;
2858 if (!tp_features.hotkey) {
2859 len += sprintf(p + len, "status:\t\tnot supported\n");
2860 return len;
2863 if (mutex_lock_killable(&hotkey_mutex))
2864 return -ERESTARTSYS;
2865 res = hotkey_status_get(&status);
2866 if (!res)
2867 res = hotkey_mask_get();
2868 mutex_unlock(&hotkey_mutex);
2869 if (res)
2870 return res;
2872 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2873 if (tp_features.hotkey_mask) {
2874 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2875 len += sprintf(p + len,
2876 "commands:\tenable, disable, reset, <mask>\n");
2877 } else {
2878 len += sprintf(p + len, "mask:\t\tnot supported\n");
2879 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2882 return len;
2885 static int hotkey_write(char *buf)
2887 int res, status;
2888 u32 mask;
2889 char *cmd;
2891 if (!tp_features.hotkey)
2892 return -ENODEV;
2894 if (mutex_lock_killable(&hotkey_mutex))
2895 return -ERESTARTSYS;
2897 status = -1;
2898 mask = hotkey_mask;
2900 res = 0;
2901 while ((cmd = next_cmd(&buf))) {
2902 if (strlencmp(cmd, "enable") == 0) {
2903 status = 1;
2904 } else if (strlencmp(cmd, "disable") == 0) {
2905 status = 0;
2906 } else if (strlencmp(cmd, "reset") == 0) {
2907 status = hotkey_orig_status;
2908 mask = hotkey_orig_mask;
2909 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2910 /* mask set */
2911 } else if (sscanf(cmd, "%x", &mask) == 1) {
2912 /* mask set */
2913 } else {
2914 res = -EINVAL;
2915 goto errexit;
2918 if (status != -1)
2919 res = hotkey_status_set(status);
2921 if (!res && mask != hotkey_mask)
2922 res = hotkey_mask_set(mask);
2924 errexit:
2925 mutex_unlock(&hotkey_mutex);
2926 return res;
2929 static const struct acpi_device_id ibm_htk_device_ids[] = {
2930 {TPACPI_ACPI_HKEY_HID, 0},
2931 {"", 0},
2934 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2935 .hid = ibm_htk_device_ids,
2936 .notify = hotkey_notify,
2937 .handle = &hkey_handle,
2938 .type = ACPI_DEVICE_NOTIFY,
2941 static struct ibm_struct hotkey_driver_data = {
2942 .name = "hotkey",
2943 .read = hotkey_read,
2944 .write = hotkey_write,
2945 .exit = hotkey_exit,
2946 .resume = hotkey_resume,
2947 .suspend = hotkey_suspend,
2948 .acpi = &ibm_hotkey_acpidriver,
2951 /*************************************************************************
2952 * Bluetooth subdriver
2955 enum {
2956 /* ACPI GBDC/SBDC bits */
2957 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2958 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2959 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2960 off / last state */
2963 enum {
2964 /* ACPI \BLTH commands */
2965 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2966 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2967 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2968 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2969 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2972 static struct rfkill *tpacpi_bluetooth_rfkill;
2974 static void bluetooth_suspend(pm_message_t state)
2976 /* Try to make sure radio will resume powered off */
2977 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2978 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2981 static int bluetooth_get_radiosw(void)
2983 int status;
2985 if (!tp_features.bluetooth)
2986 return -ENODEV;
2988 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2989 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2990 return RFKILL_STATE_HARD_BLOCKED;
2992 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2993 if (dbg_bluetoothemul)
2994 return (tpacpi_bluetooth_emulstate) ?
2995 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2996 #endif
2998 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2999 return -EIO;
3001 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3002 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3005 static void bluetooth_update_rfk(void)
3007 int status;
3009 if (!tpacpi_bluetooth_rfkill)
3010 return;
3012 status = bluetooth_get_radiosw();
3013 if (status < 0)
3014 return;
3015 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3018 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3020 int status;
3022 if (!tp_features.bluetooth)
3023 return -ENODEV;
3025 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3026 * reason to risk weird behaviour. */
3027 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3028 && radio_on)
3029 return -EPERM;
3031 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3032 if (dbg_bluetoothemul) {
3033 tpacpi_bluetooth_emulstate = !!radio_on;
3034 if (update_rfk)
3035 bluetooth_update_rfk();
3036 return 0;
3038 #endif
3040 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3041 if (radio_on)
3042 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3043 else
3044 status = 0;
3045 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3046 return -EIO;
3048 if (update_rfk)
3049 bluetooth_update_rfk();
3051 return 0;
3054 /* sysfs bluetooth enable ---------------------------------------------- */
3055 static ssize_t bluetooth_enable_show(struct device *dev,
3056 struct device_attribute *attr,
3057 char *buf)
3059 int status;
3061 status = bluetooth_get_radiosw();
3062 if (status < 0)
3063 return status;
3065 return snprintf(buf, PAGE_SIZE, "%d\n",
3066 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3069 static ssize_t bluetooth_enable_store(struct device *dev,
3070 struct device_attribute *attr,
3071 const char *buf, size_t count)
3073 unsigned long t;
3074 int res;
3076 if (parse_strtoul(buf, 1, &t))
3077 return -EINVAL;
3079 res = bluetooth_set_radiosw(t, 1);
3081 return (res) ? res : count;
3084 static struct device_attribute dev_attr_bluetooth_enable =
3085 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3086 bluetooth_enable_show, bluetooth_enable_store);
3088 /* --------------------------------------------------------------------- */
3090 static struct attribute *bluetooth_attributes[] = {
3091 &dev_attr_bluetooth_enable.attr,
3092 NULL
3095 static const struct attribute_group bluetooth_attr_group = {
3096 .attrs = bluetooth_attributes,
3099 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3101 int bts = bluetooth_get_radiosw();
3103 if (bts < 0)
3104 return bts;
3106 *state = bts;
3107 return 0;
3110 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3112 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3115 static void bluetooth_shutdown(void)
3117 /* Order firmware to save current state to NVRAM */
3118 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3119 TP_ACPI_BLTH_SAVE_STATE))
3120 printk(TPACPI_NOTICE
3121 "failed to save bluetooth state to NVRAM\n");
3124 static void bluetooth_exit(void)
3126 bluetooth_shutdown();
3128 if (tpacpi_bluetooth_rfkill)
3129 rfkill_unregister(tpacpi_bluetooth_rfkill);
3131 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3132 &bluetooth_attr_group);
3135 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3137 int res;
3138 int status = 0;
3140 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3142 TPACPI_ACPIHANDLE_INIT(hkey);
3144 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3145 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3146 tp_features.bluetooth = hkey_handle &&
3147 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3149 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3150 str_supported(tp_features.bluetooth),
3151 status);
3153 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3154 if (dbg_bluetoothemul) {
3155 tp_features.bluetooth = 1;
3156 printk(TPACPI_INFO
3157 "bluetooth switch emulation enabled\n");
3158 } else
3159 #endif
3160 if (tp_features.bluetooth &&
3161 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3162 /* no bluetooth hardware present in system */
3163 tp_features.bluetooth = 0;
3164 dbg_printk(TPACPI_DBG_INIT,
3165 "bluetooth hardware not installed\n");
3168 if (!tp_features.bluetooth)
3169 return 1;
3171 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3172 &bluetooth_attr_group);
3173 if (res)
3174 return res;
3176 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3177 &tpacpi_bluetooth_rfkill,
3178 RFKILL_TYPE_BLUETOOTH,
3179 "tpacpi_bluetooth_sw",
3180 true,
3181 tpacpi_bluetooth_rfk_set,
3182 tpacpi_bluetooth_rfk_get);
3183 if (res) {
3184 bluetooth_exit();
3185 return res;
3188 return 0;
3191 /* procfs -------------------------------------------------------------- */
3192 static int bluetooth_read(char *p)
3194 int len = 0;
3195 int status = bluetooth_get_radiosw();
3197 if (!tp_features.bluetooth)
3198 len += sprintf(p + len, "status:\t\tnot supported\n");
3199 else {
3200 len += sprintf(p + len, "status:\t\t%s\n",
3201 (status == RFKILL_STATE_UNBLOCKED) ?
3202 "enabled" : "disabled");
3203 len += sprintf(p + len, "commands:\tenable, disable\n");
3206 return len;
3209 static int bluetooth_write(char *buf)
3211 char *cmd;
3213 if (!tp_features.bluetooth)
3214 return -ENODEV;
3216 while ((cmd = next_cmd(&buf))) {
3217 if (strlencmp(cmd, "enable") == 0) {
3218 bluetooth_set_radiosw(1, 1);
3219 } else if (strlencmp(cmd, "disable") == 0) {
3220 bluetooth_set_radiosw(0, 1);
3221 } else
3222 return -EINVAL;
3225 return 0;
3228 static struct ibm_struct bluetooth_driver_data = {
3229 .name = "bluetooth",
3230 .read = bluetooth_read,
3231 .write = bluetooth_write,
3232 .exit = bluetooth_exit,
3233 .suspend = bluetooth_suspend,
3234 .shutdown = bluetooth_shutdown,
3237 /*************************************************************************
3238 * Wan subdriver
3241 enum {
3242 /* ACPI GWAN/SWAN bits */
3243 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3244 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3245 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3246 off / last state */
3249 static struct rfkill *tpacpi_wan_rfkill;
3251 static void wan_suspend(pm_message_t state)
3253 /* Try to make sure radio will resume powered off */
3254 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3255 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3258 static int wan_get_radiosw(void)
3260 int status;
3262 if (!tp_features.wan)
3263 return -ENODEV;
3265 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3266 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3267 return RFKILL_STATE_HARD_BLOCKED;
3269 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3270 if (dbg_wwanemul)
3271 return (tpacpi_wwan_emulstate) ?
3272 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3273 #endif
3275 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3276 return -EIO;
3278 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3279 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3282 static void wan_update_rfk(void)
3284 int status;
3286 if (!tpacpi_wan_rfkill)
3287 return;
3289 status = wan_get_radiosw();
3290 if (status < 0)
3291 return;
3292 rfkill_force_state(tpacpi_wan_rfkill, status);
3295 static int wan_set_radiosw(int radio_on, int update_rfk)
3297 int status;
3299 if (!tp_features.wan)
3300 return -ENODEV;
3302 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3303 * reason to risk weird behaviour. */
3304 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3305 && radio_on)
3306 return -EPERM;
3308 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3309 if (dbg_wwanemul) {
3310 tpacpi_wwan_emulstate = !!radio_on;
3311 if (update_rfk)
3312 wan_update_rfk();
3313 return 0;
3315 #endif
3317 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3318 if (radio_on)
3319 status = TP_ACPI_WANCARD_RADIOSSW;
3320 else
3321 status = 0;
3322 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3323 return -EIO;
3325 if (update_rfk)
3326 wan_update_rfk();
3328 return 0;
3331 /* sysfs wan enable ---------------------------------------------------- */
3332 static ssize_t wan_enable_show(struct device *dev,
3333 struct device_attribute *attr,
3334 char *buf)
3336 int status;
3338 status = wan_get_radiosw();
3339 if (status < 0)
3340 return status;
3342 return snprintf(buf, PAGE_SIZE, "%d\n",
3343 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3346 static ssize_t wan_enable_store(struct device *dev,
3347 struct device_attribute *attr,
3348 const char *buf, size_t count)
3350 unsigned long t;
3351 int res;
3353 if (parse_strtoul(buf, 1, &t))
3354 return -EINVAL;
3356 res = wan_set_radiosw(t, 1);
3358 return (res) ? res : count;
3361 static struct device_attribute dev_attr_wan_enable =
3362 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3363 wan_enable_show, wan_enable_store);
3365 /* --------------------------------------------------------------------- */
3367 static struct attribute *wan_attributes[] = {
3368 &dev_attr_wan_enable.attr,
3369 NULL
3372 static const struct attribute_group wan_attr_group = {
3373 .attrs = wan_attributes,
3376 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3378 int wans = wan_get_radiosw();
3380 if (wans < 0)
3381 return wans;
3383 *state = wans;
3384 return 0;
3387 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3389 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3392 static void wan_shutdown(void)
3394 /* Order firmware to save current state to NVRAM */
3395 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3396 TP_ACPI_WGSV_SAVE_STATE))
3397 printk(TPACPI_NOTICE
3398 "failed to save WWAN state to NVRAM\n");
3401 static void wan_exit(void)
3403 wan_shutdown();
3405 if (tpacpi_wan_rfkill)
3406 rfkill_unregister(tpacpi_wan_rfkill);
3408 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3409 &wan_attr_group);
3412 static int __init wan_init(struct ibm_init_struct *iibm)
3414 int res;
3415 int status = 0;
3417 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3419 TPACPI_ACPIHANDLE_INIT(hkey);
3421 tp_features.wan = hkey_handle &&
3422 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3424 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3425 str_supported(tp_features.wan),
3426 status);
3428 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3429 if (dbg_wwanemul) {
3430 tp_features.wan = 1;
3431 printk(TPACPI_INFO
3432 "wwan switch emulation enabled\n");
3433 } else
3434 #endif
3435 if (tp_features.wan &&
3436 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3437 /* no wan hardware present in system */
3438 tp_features.wan = 0;
3439 dbg_printk(TPACPI_DBG_INIT,
3440 "wan hardware not installed\n");
3443 if (!tp_features.wan)
3444 return 1;
3446 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3447 &wan_attr_group);
3448 if (res)
3449 return res;
3451 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3452 &tpacpi_wan_rfkill,
3453 RFKILL_TYPE_WWAN,
3454 "tpacpi_wwan_sw",
3455 true,
3456 tpacpi_wan_rfk_set,
3457 tpacpi_wan_rfk_get);
3458 if (res) {
3459 wan_exit();
3460 return res;
3463 return 0;
3466 /* procfs -------------------------------------------------------------- */
3467 static int wan_read(char *p)
3469 int len = 0;
3470 int status = wan_get_radiosw();
3472 if (!tp_features.wan)
3473 len += sprintf(p + len, "status:\t\tnot supported\n");
3474 else {
3475 len += sprintf(p + len, "status:\t\t%s\n",
3476 (status == RFKILL_STATE_UNBLOCKED) ?
3477 "enabled" : "disabled");
3478 len += sprintf(p + len, "commands:\tenable, disable\n");
3481 return len;
3484 static int wan_write(char *buf)
3486 char *cmd;
3488 if (!tp_features.wan)
3489 return -ENODEV;
3491 while ((cmd = next_cmd(&buf))) {
3492 if (strlencmp(cmd, "enable") == 0) {
3493 wan_set_radiosw(1, 1);
3494 } else if (strlencmp(cmd, "disable") == 0) {
3495 wan_set_radiosw(0, 1);
3496 } else
3497 return -EINVAL;
3500 return 0;
3503 static struct ibm_struct wan_driver_data = {
3504 .name = "wan",
3505 .read = wan_read,
3506 .write = wan_write,
3507 .exit = wan_exit,
3508 .suspend = wan_suspend,
3509 .shutdown = wan_shutdown,
3512 /*************************************************************************
3513 * UWB subdriver
3516 enum {
3517 /* ACPI GUWB/SUWB bits */
3518 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3519 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3522 static struct rfkill *tpacpi_uwb_rfkill;
3524 static int uwb_get_radiosw(void)
3526 int status;
3528 if (!tp_features.uwb)
3529 return -ENODEV;
3531 /* WLSW overrides UWB in firmware/hardware, reflect that */
3532 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3533 return RFKILL_STATE_HARD_BLOCKED;
3535 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3536 if (dbg_uwbemul)
3537 return (tpacpi_uwb_emulstate) ?
3538 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3539 #endif
3541 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3542 return -EIO;
3544 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3545 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3548 static void uwb_update_rfk(void)
3550 int status;
3552 if (!tpacpi_uwb_rfkill)
3553 return;
3555 status = uwb_get_radiosw();
3556 if (status < 0)
3557 return;
3558 rfkill_force_state(tpacpi_uwb_rfkill, status);
3561 static int uwb_set_radiosw(int radio_on, int update_rfk)
3563 int status;
3565 if (!tp_features.uwb)
3566 return -ENODEV;
3568 /* WLSW overrides UWB in firmware/hardware, but there is no
3569 * reason to risk weird behaviour. */
3570 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3571 && radio_on)
3572 return -EPERM;
3574 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3575 if (dbg_uwbemul) {
3576 tpacpi_uwb_emulstate = !!radio_on;
3577 if (update_rfk)
3578 uwb_update_rfk();
3579 return 0;
3581 #endif
3583 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3584 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3585 return -EIO;
3587 if (update_rfk)
3588 uwb_update_rfk();
3590 return 0;
3593 /* --------------------------------------------------------------------- */
3595 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3597 int uwbs = uwb_get_radiosw();
3599 if (uwbs < 0)
3600 return uwbs;
3602 *state = uwbs;
3603 return 0;
3606 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3608 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3611 static void uwb_exit(void)
3613 if (tpacpi_uwb_rfkill)
3614 rfkill_unregister(tpacpi_uwb_rfkill);
3617 static int __init uwb_init(struct ibm_init_struct *iibm)
3619 int res;
3620 int status = 0;
3622 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3624 TPACPI_ACPIHANDLE_INIT(hkey);
3626 tp_features.uwb = hkey_handle &&
3627 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3629 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3630 str_supported(tp_features.uwb),
3631 status);
3633 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3634 if (dbg_uwbemul) {
3635 tp_features.uwb = 1;
3636 printk(TPACPI_INFO
3637 "uwb switch emulation enabled\n");
3638 } else
3639 #endif
3640 if (tp_features.uwb &&
3641 !(status & TP_ACPI_UWB_HWPRESENT)) {
3642 /* no uwb hardware present in system */
3643 tp_features.uwb = 0;
3644 dbg_printk(TPACPI_DBG_INIT,
3645 "uwb hardware not installed\n");
3648 if (!tp_features.uwb)
3649 return 1;
3651 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3652 &tpacpi_uwb_rfkill,
3653 RFKILL_TYPE_UWB,
3654 "tpacpi_uwb_sw",
3655 false,
3656 tpacpi_uwb_rfk_set,
3657 tpacpi_uwb_rfk_get);
3659 return res;
3662 static struct ibm_struct uwb_driver_data = {
3663 .name = "uwb",
3664 .exit = uwb_exit,
3665 .flags.experimental = 1,
3668 /*************************************************************************
3669 * Video subdriver
3672 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3674 enum video_access_mode {
3675 TPACPI_VIDEO_NONE = 0,
3676 TPACPI_VIDEO_570, /* 570 */
3677 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3678 TPACPI_VIDEO_NEW, /* all others */
3681 enum { /* video status flags, based on VIDEO_570 */
3682 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3683 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3684 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3687 enum { /* TPACPI_VIDEO_570 constants */
3688 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3689 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3690 * video_status_flags */
3691 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3692 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3695 static enum video_access_mode video_supported;
3696 static int video_orig_autosw;
3698 static int video_autosw_get(void);
3699 static int video_autosw_set(int enable);
3701 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3703 static int __init video_init(struct ibm_init_struct *iibm)
3705 int ivga;
3707 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3709 TPACPI_ACPIHANDLE_INIT(vid);
3710 TPACPI_ACPIHANDLE_INIT(vid2);
3712 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3713 /* G41, assume IVGA doesn't change */
3714 vid_handle = vid2_handle;
3716 if (!vid_handle)
3717 /* video switching not supported on R30, R31 */
3718 video_supported = TPACPI_VIDEO_NONE;
3719 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3720 /* 570 */
3721 video_supported = TPACPI_VIDEO_570;
3722 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3723 /* 600e/x, 770e, 770x */
3724 video_supported = TPACPI_VIDEO_770;
3725 else
3726 /* all others */
3727 video_supported = TPACPI_VIDEO_NEW;
3729 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3730 str_supported(video_supported != TPACPI_VIDEO_NONE),
3731 video_supported);
3733 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3736 static void video_exit(void)
3738 dbg_printk(TPACPI_DBG_EXIT,
3739 "restoring original video autoswitch mode\n");
3740 if (video_autosw_set(video_orig_autosw))
3741 printk(TPACPI_ERR "error while trying to restore original "
3742 "video autoswitch mode\n");
3745 static int video_outputsw_get(void)
3747 int status = 0;
3748 int i;
3750 switch (video_supported) {
3751 case TPACPI_VIDEO_570:
3752 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3753 TP_ACPI_VIDEO_570_PHSCMD))
3754 return -EIO;
3755 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3756 break;
3757 case TPACPI_VIDEO_770:
3758 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3759 return -EIO;
3760 if (i)
3761 status |= TP_ACPI_VIDEO_S_LCD;
3762 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3763 return -EIO;
3764 if (i)
3765 status |= TP_ACPI_VIDEO_S_CRT;
3766 break;
3767 case TPACPI_VIDEO_NEW:
3768 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3769 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3770 return -EIO;
3771 if (i)
3772 status |= TP_ACPI_VIDEO_S_CRT;
3774 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3775 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3776 return -EIO;
3777 if (i)
3778 status |= TP_ACPI_VIDEO_S_LCD;
3779 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3780 return -EIO;
3781 if (i)
3782 status |= TP_ACPI_VIDEO_S_DVI;
3783 break;
3784 default:
3785 return -ENOSYS;
3788 return status;
3791 static int video_outputsw_set(int status)
3793 int autosw;
3794 int res = 0;
3796 switch (video_supported) {
3797 case TPACPI_VIDEO_570:
3798 res = acpi_evalf(NULL, NULL,
3799 "\\_SB.PHS2", "vdd",
3800 TP_ACPI_VIDEO_570_PHS2CMD,
3801 status | TP_ACPI_VIDEO_570_PHS2SET);
3802 break;
3803 case TPACPI_VIDEO_770:
3804 autosw = video_autosw_get();
3805 if (autosw < 0)
3806 return autosw;
3808 res = video_autosw_set(1);
3809 if (res)
3810 return res;
3811 res = acpi_evalf(vid_handle, NULL,
3812 "ASWT", "vdd", status * 0x100, 0);
3813 if (!autosw && video_autosw_set(autosw)) {
3814 printk(TPACPI_ERR
3815 "video auto-switch left enabled due to error\n");
3816 return -EIO;
3818 break;
3819 case TPACPI_VIDEO_NEW:
3820 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3821 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3822 break;
3823 default:
3824 return -ENOSYS;
3827 return (res)? 0 : -EIO;
3830 static int video_autosw_get(void)
3832 int autosw = 0;
3834 switch (video_supported) {
3835 case TPACPI_VIDEO_570:
3836 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3837 return -EIO;
3838 break;
3839 case TPACPI_VIDEO_770:
3840 case TPACPI_VIDEO_NEW:
3841 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3842 return -EIO;
3843 break;
3844 default:
3845 return -ENOSYS;
3848 return autosw & 1;
3851 static int video_autosw_set(int enable)
3853 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3854 return -EIO;
3855 return 0;
3858 static int video_outputsw_cycle(void)
3860 int autosw = video_autosw_get();
3861 int res;
3863 if (autosw < 0)
3864 return autosw;
3866 switch (video_supported) {
3867 case TPACPI_VIDEO_570:
3868 res = video_autosw_set(1);
3869 if (res)
3870 return res;
3871 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3872 break;
3873 case TPACPI_VIDEO_770:
3874 case TPACPI_VIDEO_NEW:
3875 res = video_autosw_set(1);
3876 if (res)
3877 return res;
3878 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3879 break;
3880 default:
3881 return -ENOSYS;
3883 if (!autosw && video_autosw_set(autosw)) {
3884 printk(TPACPI_ERR
3885 "video auto-switch left enabled due to error\n");
3886 return -EIO;
3889 return (res)? 0 : -EIO;
3892 static int video_expand_toggle(void)
3894 switch (video_supported) {
3895 case TPACPI_VIDEO_570:
3896 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3897 0 : -EIO;
3898 case TPACPI_VIDEO_770:
3899 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3900 0 : -EIO;
3901 case TPACPI_VIDEO_NEW:
3902 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3903 0 : -EIO;
3904 default:
3905 return -ENOSYS;
3907 /* not reached */
3910 static int video_read(char *p)
3912 int status, autosw;
3913 int len = 0;
3915 if (video_supported == TPACPI_VIDEO_NONE) {
3916 len += sprintf(p + len, "status:\t\tnot supported\n");
3917 return len;
3920 status = video_outputsw_get();
3921 if (status < 0)
3922 return status;
3924 autosw = video_autosw_get();
3925 if (autosw < 0)
3926 return autosw;
3928 len += sprintf(p + len, "status:\t\tsupported\n");
3929 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3930 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3931 if (video_supported == TPACPI_VIDEO_NEW)
3932 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3933 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3934 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3935 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3936 if (video_supported == TPACPI_VIDEO_NEW)
3937 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3938 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3939 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3941 return len;
3944 static int video_write(char *buf)
3946 char *cmd;
3947 int enable, disable, status;
3948 int res;
3950 if (video_supported == TPACPI_VIDEO_NONE)
3951 return -ENODEV;
3953 enable = 0;
3954 disable = 0;
3956 while ((cmd = next_cmd(&buf))) {
3957 if (strlencmp(cmd, "lcd_enable") == 0) {
3958 enable |= TP_ACPI_VIDEO_S_LCD;
3959 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3960 disable |= TP_ACPI_VIDEO_S_LCD;
3961 } else if (strlencmp(cmd, "crt_enable") == 0) {
3962 enable |= TP_ACPI_VIDEO_S_CRT;
3963 } else if (strlencmp(cmd, "crt_disable") == 0) {
3964 disable |= TP_ACPI_VIDEO_S_CRT;
3965 } else if (video_supported == TPACPI_VIDEO_NEW &&
3966 strlencmp(cmd, "dvi_enable") == 0) {
3967 enable |= TP_ACPI_VIDEO_S_DVI;
3968 } else if (video_supported == TPACPI_VIDEO_NEW &&
3969 strlencmp(cmd, "dvi_disable") == 0) {
3970 disable |= TP_ACPI_VIDEO_S_DVI;
3971 } else if (strlencmp(cmd, "auto_enable") == 0) {
3972 res = video_autosw_set(1);
3973 if (res)
3974 return res;
3975 } else if (strlencmp(cmd, "auto_disable") == 0) {
3976 res = video_autosw_set(0);
3977 if (res)
3978 return res;
3979 } else if (strlencmp(cmd, "video_switch") == 0) {
3980 res = video_outputsw_cycle();
3981 if (res)
3982 return res;
3983 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3984 res = video_expand_toggle();
3985 if (res)
3986 return res;
3987 } else
3988 return -EINVAL;
3991 if (enable || disable) {
3992 status = video_outputsw_get();
3993 if (status < 0)
3994 return status;
3995 res = video_outputsw_set((status & ~disable) | enable);
3996 if (res)
3997 return res;
4000 return 0;
4003 static struct ibm_struct video_driver_data = {
4004 .name = "video",
4005 .read = video_read,
4006 .write = video_write,
4007 .exit = video_exit,
4010 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4012 /*************************************************************************
4013 * Light (thinklight) subdriver
4016 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4017 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4019 static int light_get_status(void)
4021 int status = 0;
4023 if (tp_features.light_status) {
4024 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4025 return -EIO;
4026 return (!!status);
4029 return -ENXIO;
4032 static int light_set_status(int status)
4034 int rc;
4036 if (tp_features.light) {
4037 if (cmos_handle) {
4038 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4039 (status)?
4040 TP_CMOS_THINKLIGHT_ON :
4041 TP_CMOS_THINKLIGHT_OFF);
4042 } else {
4043 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4044 (status)? 1 : 0);
4046 return (rc)? 0 : -EIO;
4049 return -ENXIO;
4052 static void light_set_status_worker(struct work_struct *work)
4054 struct tpacpi_led_classdev *data =
4055 container_of(work, struct tpacpi_led_classdev, work);
4057 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4058 light_set_status((data->new_state != TPACPI_LED_OFF));
4061 static void light_sysfs_set(struct led_classdev *led_cdev,
4062 enum led_brightness brightness)
4064 struct tpacpi_led_classdev *data =
4065 container_of(led_cdev,
4066 struct tpacpi_led_classdev,
4067 led_classdev);
4068 data->new_state = (brightness != LED_OFF) ?
4069 TPACPI_LED_ON : TPACPI_LED_OFF;
4070 queue_work(tpacpi_wq, &data->work);
4073 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4075 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4078 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4079 .led_classdev = {
4080 .name = "tpacpi::thinklight",
4081 .brightness_set = &light_sysfs_set,
4082 .brightness_get = &light_sysfs_get,
4086 static int __init light_init(struct ibm_init_struct *iibm)
4088 int rc;
4090 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4092 TPACPI_ACPIHANDLE_INIT(ledb);
4093 TPACPI_ACPIHANDLE_INIT(lght);
4094 TPACPI_ACPIHANDLE_INIT(cmos);
4095 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4097 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4098 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4100 if (tp_features.light)
4101 /* light status not supported on
4102 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4103 tp_features.light_status =
4104 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4106 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4107 str_supported(tp_features.light),
4108 str_supported(tp_features.light_status));
4110 if (!tp_features.light)
4111 return 1;
4113 rc = led_classdev_register(&tpacpi_pdev->dev,
4114 &tpacpi_led_thinklight.led_classdev);
4116 if (rc < 0) {
4117 tp_features.light = 0;
4118 tp_features.light_status = 0;
4119 } else {
4120 rc = 0;
4123 return rc;
4126 static void light_exit(void)
4128 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4129 if (work_pending(&tpacpi_led_thinklight.work))
4130 flush_workqueue(tpacpi_wq);
4133 static int light_read(char *p)
4135 int len = 0;
4136 int status;
4138 if (!tp_features.light) {
4139 len += sprintf(p + len, "status:\t\tnot supported\n");
4140 } else if (!tp_features.light_status) {
4141 len += sprintf(p + len, "status:\t\tunknown\n");
4142 len += sprintf(p + len, "commands:\ton, off\n");
4143 } else {
4144 status = light_get_status();
4145 if (status < 0)
4146 return status;
4147 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4148 len += sprintf(p + len, "commands:\ton, off\n");
4151 return len;
4154 static int light_write(char *buf)
4156 char *cmd;
4157 int newstatus = 0;
4159 if (!tp_features.light)
4160 return -ENODEV;
4162 while ((cmd = next_cmd(&buf))) {
4163 if (strlencmp(cmd, "on") == 0) {
4164 newstatus = 1;
4165 } else if (strlencmp(cmd, "off") == 0) {
4166 newstatus = 0;
4167 } else
4168 return -EINVAL;
4171 return light_set_status(newstatus);
4174 static struct ibm_struct light_driver_data = {
4175 .name = "light",
4176 .read = light_read,
4177 .write = light_write,
4178 .exit = light_exit,
4181 /*************************************************************************
4182 * Dock subdriver
4185 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4187 static void dock_notify(struct ibm_struct *ibm, u32 event);
4188 static int dock_read(char *p);
4189 static int dock_write(char *buf);
4191 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4192 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4193 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4194 "\\_SB.PCI.ISA.SLCE", /* 570 */
4195 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4197 /* don't list other alternatives as we install a notify handler on the 570 */
4198 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4200 static const struct acpi_device_id ibm_pci_device_ids[] = {
4201 {PCI_ROOT_HID_STRING, 0},
4202 {"", 0},
4205 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4207 .notify = dock_notify,
4208 .handle = &dock_handle,
4209 .type = ACPI_SYSTEM_NOTIFY,
4212 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4213 * We just use it to get notifications of dock hotplug
4214 * in very old thinkpads */
4215 .hid = ibm_pci_device_ids,
4216 .notify = dock_notify,
4217 .handle = &pci_handle,
4218 .type = ACPI_SYSTEM_NOTIFY,
4222 static struct ibm_struct dock_driver_data[2] = {
4224 .name = "dock",
4225 .read = dock_read,
4226 .write = dock_write,
4227 .acpi = &ibm_dock_acpidriver[0],
4230 .name = "dock",
4231 .acpi = &ibm_dock_acpidriver[1],
4235 #define dock_docked() (_sta(dock_handle) & 1)
4237 static int __init dock_init(struct ibm_init_struct *iibm)
4239 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4241 TPACPI_ACPIHANDLE_INIT(dock);
4243 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4244 str_supported(dock_handle != NULL));
4246 return (dock_handle)? 0 : 1;
4249 static int __init dock_init2(struct ibm_init_struct *iibm)
4251 int dock2_needed;
4253 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4255 if (dock_driver_data[0].flags.acpi_driver_registered &&
4256 dock_driver_data[0].flags.acpi_notify_installed) {
4257 TPACPI_ACPIHANDLE_INIT(pci);
4258 dock2_needed = (pci_handle != NULL);
4259 vdbg_printk(TPACPI_DBG_INIT,
4260 "dock PCI handler for the TP 570 is %s\n",
4261 str_supported(dock2_needed));
4262 } else {
4263 vdbg_printk(TPACPI_DBG_INIT,
4264 "dock subdriver part 2 not required\n");
4265 dock2_needed = 0;
4268 return (dock2_needed)? 0 : 1;
4271 static void dock_notify(struct ibm_struct *ibm, u32 event)
4273 int docked = dock_docked();
4274 int pci = ibm->acpi->hid && ibm->acpi->device &&
4275 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4276 int data;
4278 if (event == 1 && !pci) /* 570 */
4279 data = 1; /* button */
4280 else if (event == 1 && pci) /* 570 */
4281 data = 3; /* dock */
4282 else if (event == 3 && docked)
4283 data = 1; /* button */
4284 else if (event == 3 && !docked)
4285 data = 2; /* undock */
4286 else if (event == 0 && docked)
4287 data = 3; /* dock */
4288 else {
4289 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4290 event, _sta(dock_handle));
4291 data = 0; /* unknown */
4293 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4294 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4295 dev_name(&ibm->acpi->device->dev),
4296 event, data);
4299 static int dock_read(char *p)
4301 int len = 0;
4302 int docked = dock_docked();
4304 if (!dock_handle)
4305 len += sprintf(p + len, "status:\t\tnot supported\n");
4306 else if (!docked)
4307 len += sprintf(p + len, "status:\t\tundocked\n");
4308 else {
4309 len += sprintf(p + len, "status:\t\tdocked\n");
4310 len += sprintf(p + len, "commands:\tdock, undock\n");
4313 return len;
4316 static int dock_write(char *buf)
4318 char *cmd;
4320 if (!dock_docked())
4321 return -ENODEV;
4323 while ((cmd = next_cmd(&buf))) {
4324 if (strlencmp(cmd, "undock") == 0) {
4325 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4326 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4327 return -EIO;
4328 } else if (strlencmp(cmd, "dock") == 0) {
4329 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4330 return -EIO;
4331 } else
4332 return -EINVAL;
4335 return 0;
4338 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4340 /*************************************************************************
4341 * Bay subdriver
4344 #ifdef CONFIG_THINKPAD_ACPI_BAY
4346 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4347 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4348 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4349 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4350 ); /* A21e, R30, R31 */
4351 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4352 "_EJ0", /* all others */
4353 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4354 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4355 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4356 ); /* all others */
4357 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4358 "_EJ0", /* 770x */
4359 ); /* all others */
4361 static int __init bay_init(struct ibm_init_struct *iibm)
4363 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4365 TPACPI_ACPIHANDLE_INIT(bay);
4366 if (bay_handle)
4367 TPACPI_ACPIHANDLE_INIT(bay_ej);
4368 TPACPI_ACPIHANDLE_INIT(bay2);
4369 if (bay2_handle)
4370 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4372 tp_features.bay_status = bay_handle &&
4373 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4374 tp_features.bay_status2 = bay2_handle &&
4375 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4377 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4378 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4379 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4380 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4382 vdbg_printk(TPACPI_DBG_INIT,
4383 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4384 str_supported(tp_features.bay_status),
4385 str_supported(tp_features.bay_eject),
4386 str_supported(tp_features.bay_status2),
4387 str_supported(tp_features.bay_eject2));
4389 return (tp_features.bay_status || tp_features.bay_eject ||
4390 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4393 static void bay_notify(struct ibm_struct *ibm, u32 event)
4395 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4396 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4397 dev_name(&ibm->acpi->device->dev),
4398 event, 0);
4401 #define bay_occupied(b) (_sta(b##_handle) & 1)
4403 static int bay_read(char *p)
4405 int len = 0;
4406 int occupied = bay_occupied(bay);
4407 int occupied2 = bay_occupied(bay2);
4408 int eject, eject2;
4410 len += sprintf(p + len, "status:\t\t%s\n",
4411 tp_features.bay_status ?
4412 (occupied ? "occupied" : "unoccupied") :
4413 "not supported");
4414 if (tp_features.bay_status2)
4415 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4416 "occupied" : "unoccupied");
4418 eject = tp_features.bay_eject && occupied;
4419 eject2 = tp_features.bay_eject2 && occupied2;
4421 if (eject && eject2)
4422 len += sprintf(p + len, "commands:\teject, eject2\n");
4423 else if (eject)
4424 len += sprintf(p + len, "commands:\teject\n");
4425 else if (eject2)
4426 len += sprintf(p + len, "commands:\teject2\n");
4428 return len;
4431 static int bay_write(char *buf)
4433 char *cmd;
4435 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4436 return -ENODEV;
4438 while ((cmd = next_cmd(&buf))) {
4439 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4440 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4441 return -EIO;
4442 } else if (tp_features.bay_eject2 &&
4443 strlencmp(cmd, "eject2") == 0) {
4444 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4445 return -EIO;
4446 } else
4447 return -EINVAL;
4450 return 0;
4453 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4454 .notify = bay_notify,
4455 .handle = &bay_handle,
4456 .type = ACPI_SYSTEM_NOTIFY,
4459 static struct ibm_struct bay_driver_data = {
4460 .name = "bay",
4461 .read = bay_read,
4462 .write = bay_write,
4463 .acpi = &ibm_bay_acpidriver,
4466 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4468 /*************************************************************************
4469 * CMOS subdriver
4472 /* sysfs cmos_command -------------------------------------------------- */
4473 static ssize_t cmos_command_store(struct device *dev,
4474 struct device_attribute *attr,
4475 const char *buf, size_t count)
4477 unsigned long cmos_cmd;
4478 int res;
4480 if (parse_strtoul(buf, 21, &cmos_cmd))
4481 return -EINVAL;
4483 res = issue_thinkpad_cmos_command(cmos_cmd);
4484 return (res)? res : count;
4487 static struct device_attribute dev_attr_cmos_command =
4488 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4490 /* --------------------------------------------------------------------- */
4492 static int __init cmos_init(struct ibm_init_struct *iibm)
4494 int res;
4496 vdbg_printk(TPACPI_DBG_INIT,
4497 "initializing cmos commands subdriver\n");
4499 TPACPI_ACPIHANDLE_INIT(cmos);
4501 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4502 str_supported(cmos_handle != NULL));
4504 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4505 if (res)
4506 return res;
4508 return (cmos_handle)? 0 : 1;
4511 static void cmos_exit(void)
4513 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4516 static int cmos_read(char *p)
4518 int len = 0;
4520 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4521 R30, R31, T20-22, X20-21 */
4522 if (!cmos_handle)
4523 len += sprintf(p + len, "status:\t\tnot supported\n");
4524 else {
4525 len += sprintf(p + len, "status:\t\tsupported\n");
4526 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4529 return len;
4532 static int cmos_write(char *buf)
4534 char *cmd;
4535 int cmos_cmd, res;
4537 while ((cmd = next_cmd(&buf))) {
4538 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4539 cmos_cmd >= 0 && cmos_cmd <= 21) {
4540 /* cmos_cmd set */
4541 } else
4542 return -EINVAL;
4544 res = issue_thinkpad_cmos_command(cmos_cmd);
4545 if (res)
4546 return res;
4549 return 0;
4552 static struct ibm_struct cmos_driver_data = {
4553 .name = "cmos",
4554 .read = cmos_read,
4555 .write = cmos_write,
4556 .exit = cmos_exit,
4559 /*************************************************************************
4560 * LED subdriver
4563 enum led_access_mode {
4564 TPACPI_LED_NONE = 0,
4565 TPACPI_LED_570, /* 570 */
4566 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4567 TPACPI_LED_NEW, /* all others */
4570 enum { /* For TPACPI_LED_OLD */
4571 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4572 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4573 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4576 static enum led_access_mode led_supported;
4578 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4579 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4580 /* T20-22, X20-21 */
4581 "LED", /* all others */
4582 ); /* R30, R31 */
4584 #define TPACPI_LED_NUMLEDS 8
4585 static struct tpacpi_led_classdev *tpacpi_leds;
4586 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4587 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4588 /* there's a limit of 19 chars + NULL before 2.6.26 */
4589 "tpacpi::power",
4590 "tpacpi:orange:batt",
4591 "tpacpi:green:batt",
4592 "tpacpi::dock_active",
4593 "tpacpi::bay_active",
4594 "tpacpi::dock_batt",
4595 "tpacpi::unknown_led",
4596 "tpacpi::standby",
4599 static int led_get_status(const unsigned int led)
4601 int status;
4602 enum led_status_t led_s;
4604 switch (led_supported) {
4605 case TPACPI_LED_570:
4606 if (!acpi_evalf(ec_handle,
4607 &status, "GLED", "dd", 1 << led))
4608 return -EIO;
4609 led_s = (status == 0)?
4610 TPACPI_LED_OFF :
4611 ((status == 1)?
4612 TPACPI_LED_ON :
4613 TPACPI_LED_BLINK);
4614 tpacpi_led_state_cache[led] = led_s;
4615 return led_s;
4616 default:
4617 return -ENXIO;
4620 /* not reached */
4623 static int led_set_status(const unsigned int led,
4624 const enum led_status_t ledstatus)
4626 /* off, on, blink. Index is led_status_t */
4627 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4628 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4630 int rc = 0;
4632 switch (led_supported) {
4633 case TPACPI_LED_570:
4634 /* 570 */
4635 if (led > 7)
4636 return -EINVAL;
4637 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4638 (1 << led), led_sled_arg1[ledstatus]))
4639 rc = -EIO;
4640 break;
4641 case TPACPI_LED_OLD:
4642 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4643 if (led > 7)
4644 return -EINVAL;
4645 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4646 if (rc >= 0)
4647 rc = ec_write(TPACPI_LED_EC_HLBL,
4648 (ledstatus == TPACPI_LED_BLINK) << led);
4649 if (rc >= 0)
4650 rc = ec_write(TPACPI_LED_EC_HLCL,
4651 (ledstatus != TPACPI_LED_OFF) << led);
4652 break;
4653 case TPACPI_LED_NEW:
4654 /* all others */
4655 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4656 led, led_led_arg1[ledstatus]))
4657 rc = -EIO;
4658 break;
4659 default:
4660 rc = -ENXIO;
4663 if (!rc)
4664 tpacpi_led_state_cache[led] = ledstatus;
4666 return rc;
4669 static void led_set_status_worker(struct work_struct *work)
4671 struct tpacpi_led_classdev *data =
4672 container_of(work, struct tpacpi_led_classdev, work);
4674 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4675 led_set_status(data->led, data->new_state);
4678 static void led_sysfs_set(struct led_classdev *led_cdev,
4679 enum led_brightness brightness)
4681 struct tpacpi_led_classdev *data = container_of(led_cdev,
4682 struct tpacpi_led_classdev, led_classdev);
4684 if (brightness == LED_OFF)
4685 data->new_state = TPACPI_LED_OFF;
4686 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4687 data->new_state = TPACPI_LED_ON;
4688 else
4689 data->new_state = TPACPI_LED_BLINK;
4691 queue_work(tpacpi_wq, &data->work);
4694 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4695 unsigned long *delay_on, unsigned long *delay_off)
4697 struct tpacpi_led_classdev *data = container_of(led_cdev,
4698 struct tpacpi_led_classdev, led_classdev);
4700 /* Can we choose the flash rate? */
4701 if (*delay_on == 0 && *delay_off == 0) {
4702 /* yes. set them to the hardware blink rate (1 Hz) */
4703 *delay_on = 500; /* ms */
4704 *delay_off = 500; /* ms */
4705 } else if ((*delay_on != 500) || (*delay_off != 500))
4706 return -EINVAL;
4708 data->new_state = TPACPI_LED_BLINK;
4709 queue_work(tpacpi_wq, &data->work);
4711 return 0;
4714 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4716 int rc;
4718 struct tpacpi_led_classdev *data = container_of(led_cdev,
4719 struct tpacpi_led_classdev, led_classdev);
4721 rc = led_get_status(data->led);
4723 if (rc == TPACPI_LED_OFF || rc < 0)
4724 rc = LED_OFF; /* no error handling in led class :( */
4725 else
4726 rc = LED_FULL;
4728 return rc;
4731 static void led_exit(void)
4733 unsigned int i;
4735 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4736 if (tpacpi_leds[i].led_classdev.name)
4737 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4740 kfree(tpacpi_leds);
4743 static int __init led_init(struct ibm_init_struct *iibm)
4745 unsigned int i;
4746 int rc;
4748 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4750 TPACPI_ACPIHANDLE_INIT(led);
4752 if (!led_handle)
4753 /* led not supported on R30, R31 */
4754 led_supported = TPACPI_LED_NONE;
4755 else if (strlencmp(led_path, "SLED") == 0)
4756 /* 570 */
4757 led_supported = TPACPI_LED_570;
4758 else if (strlencmp(led_path, "SYSL") == 0)
4759 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4760 led_supported = TPACPI_LED_OLD;
4761 else
4762 /* all others */
4763 led_supported = TPACPI_LED_NEW;
4765 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4766 str_supported(led_supported), led_supported);
4768 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4769 GFP_KERNEL);
4770 if (!tpacpi_leds) {
4771 printk(TPACPI_ERR "Out of memory for LED data\n");
4772 return -ENOMEM;
4775 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4776 tpacpi_leds[i].led = i;
4778 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4779 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4780 if (led_supported == TPACPI_LED_570)
4781 tpacpi_leds[i].led_classdev.brightness_get =
4782 &led_sysfs_get;
4784 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4786 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4788 rc = led_classdev_register(&tpacpi_pdev->dev,
4789 &tpacpi_leds[i].led_classdev);
4790 if (rc < 0) {
4791 tpacpi_leds[i].led_classdev.name = NULL;
4792 led_exit();
4793 return rc;
4797 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4800 #define str_led_status(s) \
4801 ((s) == TPACPI_LED_OFF ? "off" : \
4802 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4804 static int led_read(char *p)
4806 int len = 0;
4808 if (!led_supported) {
4809 len += sprintf(p + len, "status:\t\tnot supported\n");
4810 return len;
4812 len += sprintf(p + len, "status:\t\tsupported\n");
4814 if (led_supported == TPACPI_LED_570) {
4815 /* 570 */
4816 int i, status;
4817 for (i = 0; i < 8; i++) {
4818 status = led_get_status(i);
4819 if (status < 0)
4820 return -EIO;
4821 len += sprintf(p + len, "%d:\t\t%s\n",
4822 i, str_led_status(status));
4826 len += sprintf(p + len, "commands:\t"
4827 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4829 return len;
4832 static int led_write(char *buf)
4834 char *cmd;
4835 int led, rc;
4836 enum led_status_t s;
4838 if (!led_supported)
4839 return -ENODEV;
4841 while ((cmd = next_cmd(&buf))) {
4842 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4843 return -EINVAL;
4845 if (strstr(cmd, "off")) {
4846 s = TPACPI_LED_OFF;
4847 } else if (strstr(cmd, "on")) {
4848 s = TPACPI_LED_ON;
4849 } else if (strstr(cmd, "blink")) {
4850 s = TPACPI_LED_BLINK;
4851 } else {
4852 return -EINVAL;
4855 rc = led_set_status(led, s);
4856 if (rc < 0)
4857 return rc;
4860 return 0;
4863 static struct ibm_struct led_driver_data = {
4864 .name = "led",
4865 .read = led_read,
4866 .write = led_write,
4867 .exit = led_exit,
4870 /*************************************************************************
4871 * Beep subdriver
4874 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4876 static int __init beep_init(struct ibm_init_struct *iibm)
4878 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4880 TPACPI_ACPIHANDLE_INIT(beep);
4882 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4883 str_supported(beep_handle != NULL));
4885 return (beep_handle)? 0 : 1;
4888 static int beep_read(char *p)
4890 int len = 0;
4892 if (!beep_handle)
4893 len += sprintf(p + len, "status:\t\tnot supported\n");
4894 else {
4895 len += sprintf(p + len, "status:\t\tsupported\n");
4896 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4899 return len;
4902 static int beep_write(char *buf)
4904 char *cmd;
4905 int beep_cmd;
4907 if (!beep_handle)
4908 return -ENODEV;
4910 while ((cmd = next_cmd(&buf))) {
4911 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4912 beep_cmd >= 0 && beep_cmd <= 17) {
4913 /* beep_cmd set */
4914 } else
4915 return -EINVAL;
4916 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4917 return -EIO;
4920 return 0;
4923 static struct ibm_struct beep_driver_data = {
4924 .name = "beep",
4925 .read = beep_read,
4926 .write = beep_write,
4929 /*************************************************************************
4930 * Thermal subdriver
4933 enum thermal_access_mode {
4934 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4935 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4936 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4937 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4938 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4941 enum { /* TPACPI_THERMAL_TPEC_* */
4942 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4943 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4944 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4947 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4948 struct ibm_thermal_sensors_struct {
4949 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4952 static enum thermal_access_mode thermal_read_mode;
4954 /* idx is zero-based */
4955 static int thermal_get_sensor(int idx, s32 *value)
4957 int t;
4958 s8 tmp;
4959 char tmpi[5];
4961 t = TP_EC_THERMAL_TMP0;
4963 switch (thermal_read_mode) {
4964 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4965 case TPACPI_THERMAL_TPEC_16:
4966 if (idx >= 8 && idx <= 15) {
4967 t = TP_EC_THERMAL_TMP8;
4968 idx -= 8;
4970 /* fallthrough */
4971 #endif
4972 case TPACPI_THERMAL_TPEC_8:
4973 if (idx <= 7) {
4974 if (!acpi_ec_read(t + idx, &tmp))
4975 return -EIO;
4976 *value = tmp * 1000;
4977 return 0;
4979 break;
4981 case TPACPI_THERMAL_ACPI_UPDT:
4982 if (idx <= 7) {
4983 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4984 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4985 return -EIO;
4986 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4987 return -EIO;
4988 *value = (t - 2732) * 100;
4989 return 0;
4991 break;
4993 case TPACPI_THERMAL_ACPI_TMP07:
4994 if (idx <= 7) {
4995 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4996 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4997 return -EIO;
4998 if (t > 127 || t < -127)
4999 t = TP_EC_THERMAL_TMP_NA;
5000 *value = t * 1000;
5001 return 0;
5003 break;
5005 case TPACPI_THERMAL_NONE:
5006 default:
5007 return -ENOSYS;
5010 return -EINVAL;
5013 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5015 int res, i;
5016 int n;
5018 n = 8;
5019 i = 0;
5021 if (!s)
5022 return -EINVAL;
5024 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5025 n = 16;
5027 for (i = 0 ; i < n; i++) {
5028 res = thermal_get_sensor(i, &s->temp[i]);
5029 if (res)
5030 return res;
5033 return n;
5036 /* sysfs temp##_input -------------------------------------------------- */
5038 static ssize_t thermal_temp_input_show(struct device *dev,
5039 struct device_attribute *attr,
5040 char *buf)
5042 struct sensor_device_attribute *sensor_attr =
5043 to_sensor_dev_attr(attr);
5044 int idx = sensor_attr->index;
5045 s32 value;
5046 int res;
5048 res = thermal_get_sensor(idx, &value);
5049 if (res)
5050 return res;
5051 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5052 return -ENXIO;
5054 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5057 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5058 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5059 thermal_temp_input_show, NULL, _idxB)
5061 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5062 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5063 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5064 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5065 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5066 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5067 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5068 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5069 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5070 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5071 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5072 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5073 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5074 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5075 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5076 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5077 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5080 #define THERMAL_ATTRS(X) \
5081 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5083 static struct attribute *thermal_temp_input_attr[] = {
5084 THERMAL_ATTRS(8),
5085 THERMAL_ATTRS(9),
5086 THERMAL_ATTRS(10),
5087 THERMAL_ATTRS(11),
5088 THERMAL_ATTRS(12),
5089 THERMAL_ATTRS(13),
5090 THERMAL_ATTRS(14),
5091 THERMAL_ATTRS(15),
5092 THERMAL_ATTRS(0),
5093 THERMAL_ATTRS(1),
5094 THERMAL_ATTRS(2),
5095 THERMAL_ATTRS(3),
5096 THERMAL_ATTRS(4),
5097 THERMAL_ATTRS(5),
5098 THERMAL_ATTRS(6),
5099 THERMAL_ATTRS(7),
5100 NULL
5103 static const struct attribute_group thermal_temp_input16_group = {
5104 .attrs = thermal_temp_input_attr
5107 static const struct attribute_group thermal_temp_input8_group = {
5108 .attrs = &thermal_temp_input_attr[8]
5111 #undef THERMAL_SENSOR_ATTR_TEMP
5112 #undef THERMAL_ATTRS
5114 /* --------------------------------------------------------------------- */
5116 static int __init thermal_init(struct ibm_init_struct *iibm)
5118 u8 t, ta1, ta2;
5119 int i;
5120 int acpi_tmp7;
5121 int res;
5123 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5125 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5127 if (thinkpad_id.ec_model) {
5129 * Direct EC access mode: sensors at registers
5130 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5131 * non-implemented, thermal sensors return 0x80 when
5132 * not available
5135 ta1 = ta2 = 0;
5136 for (i = 0; i < 8; i++) {
5137 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5138 ta1 |= t;
5139 } else {
5140 ta1 = 0;
5141 break;
5143 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5144 ta2 |= t;
5145 } else {
5146 ta1 = 0;
5147 break;
5150 if (ta1 == 0) {
5151 /* This is sheer paranoia, but we handle it anyway */
5152 if (acpi_tmp7) {
5153 printk(TPACPI_ERR
5154 "ThinkPad ACPI EC access misbehaving, "
5155 "falling back to ACPI TMPx access "
5156 "mode\n");
5157 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5158 } else {
5159 printk(TPACPI_ERR
5160 "ThinkPad ACPI EC access misbehaving, "
5161 "disabling thermal sensors access\n");
5162 thermal_read_mode = TPACPI_THERMAL_NONE;
5164 } else {
5165 thermal_read_mode =
5166 (ta2 != 0) ?
5167 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5169 } else if (acpi_tmp7) {
5170 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5171 /* 600e/x, 770e, 770x */
5172 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5173 } else {
5174 /* Standard ACPI TMPx access, max 8 sensors */
5175 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5177 } else {
5178 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5179 thermal_read_mode = TPACPI_THERMAL_NONE;
5182 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5183 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5184 thermal_read_mode);
5186 switch (thermal_read_mode) {
5187 case TPACPI_THERMAL_TPEC_16:
5188 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5189 &thermal_temp_input16_group);
5190 if (res)
5191 return res;
5192 break;
5193 case TPACPI_THERMAL_TPEC_8:
5194 case TPACPI_THERMAL_ACPI_TMP07:
5195 case TPACPI_THERMAL_ACPI_UPDT:
5196 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5197 &thermal_temp_input8_group);
5198 if (res)
5199 return res;
5200 break;
5201 case TPACPI_THERMAL_NONE:
5202 default:
5203 return 1;
5206 return 0;
5209 static void thermal_exit(void)
5211 switch (thermal_read_mode) {
5212 case TPACPI_THERMAL_TPEC_16:
5213 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5214 &thermal_temp_input16_group);
5215 break;
5216 case TPACPI_THERMAL_TPEC_8:
5217 case TPACPI_THERMAL_ACPI_TMP07:
5218 case TPACPI_THERMAL_ACPI_UPDT:
5219 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5220 &thermal_temp_input16_group);
5221 break;
5222 case TPACPI_THERMAL_NONE:
5223 default:
5224 break;
5228 static int thermal_read(char *p)
5230 int len = 0;
5231 int n, i;
5232 struct ibm_thermal_sensors_struct t;
5234 n = thermal_get_sensors(&t);
5235 if (unlikely(n < 0))
5236 return n;
5238 len += sprintf(p + len, "temperatures:\t");
5240 if (n > 0) {
5241 for (i = 0; i < (n - 1); i++)
5242 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5243 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5244 } else
5245 len += sprintf(p + len, "not supported\n");
5247 return len;
5250 static struct ibm_struct thermal_driver_data = {
5251 .name = "thermal",
5252 .read = thermal_read,
5253 .exit = thermal_exit,
5256 /*************************************************************************
5257 * EC Dump subdriver
5260 static u8 ecdump_regs[256];
5262 static int ecdump_read(char *p)
5264 int len = 0;
5265 int i, j;
5266 u8 v;
5268 len += sprintf(p + len, "EC "
5269 " +00 +01 +02 +03 +04 +05 +06 +07"
5270 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5271 for (i = 0; i < 256; i += 16) {
5272 len += sprintf(p + len, "EC 0x%02x:", i);
5273 for (j = 0; j < 16; j++) {
5274 if (!acpi_ec_read(i + j, &v))
5275 break;
5276 if (v != ecdump_regs[i + j])
5277 len += sprintf(p + len, " *%02x", v);
5278 else
5279 len += sprintf(p + len, " %02x", v);
5280 ecdump_regs[i + j] = v;
5282 len += sprintf(p + len, "\n");
5283 if (j != 16)
5284 break;
5287 /* These are way too dangerous to advertise openly... */
5288 #if 0
5289 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5290 " (<offset> is 00-ff, <value> is 00-ff)\n");
5291 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5292 " (<offset> is 00-ff, <value> is 0-255)\n");
5293 #endif
5294 return len;
5297 static int ecdump_write(char *buf)
5299 char *cmd;
5300 int i, v;
5302 while ((cmd = next_cmd(&buf))) {
5303 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5304 /* i and v set */
5305 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5306 /* i and v set */
5307 } else
5308 return -EINVAL;
5309 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5310 if (!acpi_ec_write(i, v))
5311 return -EIO;
5312 } else
5313 return -EINVAL;
5316 return 0;
5319 static struct ibm_struct ecdump_driver_data = {
5320 .name = "ecdump",
5321 .read = ecdump_read,
5322 .write = ecdump_write,
5323 .flags.experimental = 1,
5326 /*************************************************************************
5327 * Backlight/brightness subdriver
5330 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5332 enum {
5333 TP_EC_BACKLIGHT = 0x31,
5335 /* TP_EC_BACKLIGHT bitmasks */
5336 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5337 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5338 TP_EC_BACKLIGHT_MAPSW = 0x20,
5341 static struct backlight_device *ibm_backlight_device;
5342 static int brightness_mode;
5343 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5345 static struct mutex brightness_mutex;
5348 * ThinkPads can read brightness from two places: EC 0x31, or
5349 * CMOS NVRAM byte 0x5E, bits 0-3.
5351 * EC 0x31 has the following layout
5352 * Bit 7: unknown function
5353 * Bit 6: unknown function
5354 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5355 * Bit 4: must be set to zero to avoid problems
5356 * Bit 3-0: backlight brightness level
5358 * brightness_get_raw returns status data in the EC 0x31 layout
5360 static int brightness_get_raw(int *status)
5362 u8 lec = 0, lcmos = 0, level = 0;
5364 if (brightness_mode & 1) {
5365 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5366 return -EIO;
5367 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5369 if (brightness_mode & 2) {
5370 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5371 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5372 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5373 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5374 level = lcmos;
5377 if (brightness_mode == 3) {
5378 *status = lec; /* Prefer EC, CMOS is just a backing store */
5379 lec &= TP_EC_BACKLIGHT_LVLMSK;
5380 if (lec == lcmos)
5381 tp_warned.bright_cmos_ec_unsync = 0;
5382 else {
5383 if (!tp_warned.bright_cmos_ec_unsync) {
5384 printk(TPACPI_ERR
5385 "CMOS NVRAM (%u) and EC (%u) do not "
5386 "agree on display brightness level\n",
5387 (unsigned int) lcmos,
5388 (unsigned int) lec);
5389 tp_warned.bright_cmos_ec_unsync = 1;
5391 return -EIO;
5393 } else {
5394 *status = level;
5397 return 0;
5400 /* May return EINTR which can always be mapped to ERESTARTSYS */
5401 static int brightness_set(int value)
5403 int cmos_cmd, inc, i, res;
5404 int current_value;
5405 int command_bits;
5407 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5408 value < 0)
5409 return -EINVAL;
5411 res = mutex_lock_killable(&brightness_mutex);
5412 if (res < 0)
5413 return res;
5415 res = brightness_get_raw(&current_value);
5416 if (res < 0)
5417 goto errout;
5419 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5420 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5422 cmos_cmd = value > current_value ?
5423 TP_CMOS_BRIGHTNESS_UP :
5424 TP_CMOS_BRIGHTNESS_DOWN;
5425 inc = (value > current_value)? 1 : -1;
5427 res = 0;
5428 for (i = current_value; i != value; i += inc) {
5429 if ((brightness_mode & 2) &&
5430 issue_thinkpad_cmos_command(cmos_cmd)) {
5431 res = -EIO;
5432 goto errout;
5434 if ((brightness_mode & 1) &&
5435 !acpi_ec_write(TP_EC_BACKLIGHT,
5436 (i + inc) | command_bits)) {
5437 res = -EIO;
5438 goto errout;;
5442 errout:
5443 mutex_unlock(&brightness_mutex);
5444 return res;
5447 /* sysfs backlight class ----------------------------------------------- */
5449 static int brightness_update_status(struct backlight_device *bd)
5451 /* it is the backlight class's job (caller) to handle
5452 * EINTR and other errors properly */
5453 return brightness_set(
5454 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5455 bd->props.power == FB_BLANK_UNBLANK) ?
5456 bd->props.brightness : 0);
5459 static int brightness_get(struct backlight_device *bd)
5461 int status, res;
5463 res = brightness_get_raw(&status);
5464 if (res < 0)
5465 return 0; /* FIXME: teach backlight about error handling */
5467 return status & TP_EC_BACKLIGHT_LVLMSK;
5470 static struct backlight_ops ibm_backlight_data = {
5471 .get_brightness = brightness_get,
5472 .update_status = brightness_update_status,
5475 /* --------------------------------------------------------------------- */
5477 static int __init brightness_init(struct ibm_init_struct *iibm)
5479 int b;
5481 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5483 mutex_init(&brightness_mutex);
5486 * We always attempt to detect acpi support, so as to switch
5487 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5488 * going to publish a backlight interface
5490 b = tpacpi_check_std_acpi_brightness_support();
5491 if (b > 0) {
5493 if (acpi_video_backlight_support()) {
5494 if (brightness_enable > 1) {
5495 printk(TPACPI_NOTICE
5496 "Standard ACPI backlight interface "
5497 "available, not loading native one.\n");
5498 return 1;
5499 } else if (brightness_enable == 1) {
5500 printk(TPACPI_NOTICE
5501 "Backlight control force enabled, even if standard "
5502 "ACPI backlight interface is available\n");
5504 } else {
5505 if (brightness_enable > 1) {
5506 printk(TPACPI_NOTICE
5507 "Standard ACPI backlight interface not "
5508 "available, thinkpad_acpi native "
5509 "brightness control enabled\n");
5514 if (!brightness_enable) {
5515 dbg_printk(TPACPI_DBG_INIT,
5516 "brightness support disabled by "
5517 "module parameter\n");
5518 return 1;
5521 if (b > 16) {
5522 printk(TPACPI_ERR
5523 "Unsupported brightness interface, "
5524 "please contact %s\n", TPACPI_MAIL);
5525 return 1;
5527 if (b == 16)
5528 tp_features.bright_16levels = 1;
5530 if (!brightness_mode) {
5531 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5532 brightness_mode = 2;
5533 else
5534 brightness_mode = 3;
5536 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5537 brightness_mode);
5540 if (brightness_mode > 3)
5541 return -EINVAL;
5543 if (brightness_get_raw(&b) < 0)
5544 return 1;
5546 if (tp_features.bright_16levels)
5547 printk(TPACPI_INFO
5548 "detected a 16-level brightness capable ThinkPad\n");
5550 ibm_backlight_device = backlight_device_register(
5551 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5552 &ibm_backlight_data);
5553 if (IS_ERR(ibm_backlight_device)) {
5554 printk(TPACPI_ERR "Could not register backlight device\n");
5555 return PTR_ERR(ibm_backlight_device);
5557 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5559 ibm_backlight_device->props.max_brightness =
5560 (tp_features.bright_16levels)? 15 : 7;
5561 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5562 backlight_update_status(ibm_backlight_device);
5564 return 0;
5567 static void brightness_exit(void)
5569 if (ibm_backlight_device) {
5570 vdbg_printk(TPACPI_DBG_EXIT,
5571 "calling backlight_device_unregister()\n");
5572 backlight_device_unregister(ibm_backlight_device);
5576 static int brightness_read(char *p)
5578 int len = 0;
5579 int level;
5581 level = brightness_get(NULL);
5582 if (level < 0) {
5583 len += sprintf(p + len, "level:\t\tunreadable\n");
5584 } else {
5585 len += sprintf(p + len, "level:\t\t%d\n", level);
5586 len += sprintf(p + len, "commands:\tup, down\n");
5587 len += sprintf(p + len, "commands:\tlevel <level>"
5588 " (<level> is 0-%d)\n",
5589 (tp_features.bright_16levels) ? 15 : 7);
5592 return len;
5595 static int brightness_write(char *buf)
5597 int level;
5598 int rc;
5599 char *cmd;
5600 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5602 level = brightness_get(NULL);
5603 if (level < 0)
5604 return level;
5606 while ((cmd = next_cmd(&buf))) {
5607 if (strlencmp(cmd, "up") == 0) {
5608 if (level < max_level)
5609 level++;
5610 } else if (strlencmp(cmd, "down") == 0) {
5611 if (level > 0)
5612 level--;
5613 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5614 level >= 0 && level <= max_level) {
5615 /* new level set */
5616 } else
5617 return -EINVAL;
5621 * Now we know what the final level should be, so we try to set it.
5622 * Doing it this way makes the syscall restartable in case of EINTR
5624 rc = brightness_set(level);
5625 return (rc == -EINTR)? ERESTARTSYS : rc;
5628 static struct ibm_struct brightness_driver_data = {
5629 .name = "brightness",
5630 .read = brightness_read,
5631 .write = brightness_write,
5632 .exit = brightness_exit,
5635 /*************************************************************************
5636 * Volume subdriver
5639 static int volume_offset = 0x30;
5641 static int volume_read(char *p)
5643 int len = 0;
5644 u8 level;
5646 if (!acpi_ec_read(volume_offset, &level)) {
5647 len += sprintf(p + len, "level:\t\tunreadable\n");
5648 } else {
5649 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5650 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5651 len += sprintf(p + len, "commands:\tup, down, mute\n");
5652 len += sprintf(p + len, "commands:\tlevel <level>"
5653 " (<level> is 0-15)\n");
5656 return len;
5659 static int volume_write(char *buf)
5661 int cmos_cmd, inc, i;
5662 u8 level, mute;
5663 int new_level, new_mute;
5664 char *cmd;
5666 while ((cmd = next_cmd(&buf))) {
5667 if (!acpi_ec_read(volume_offset, &level))
5668 return -EIO;
5669 new_mute = mute = level & 0x40;
5670 new_level = level = level & 0xf;
5672 if (strlencmp(cmd, "up") == 0) {
5673 if (mute)
5674 new_mute = 0;
5675 else
5676 new_level = level == 15 ? 15 : level + 1;
5677 } else if (strlencmp(cmd, "down") == 0) {
5678 if (mute)
5679 new_mute = 0;
5680 else
5681 new_level = level == 0 ? 0 : level - 1;
5682 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5683 new_level >= 0 && new_level <= 15) {
5684 /* new_level set */
5685 } else if (strlencmp(cmd, "mute") == 0) {
5686 new_mute = 0x40;
5687 } else
5688 return -EINVAL;
5690 if (new_level != level) {
5691 /* mute doesn't change */
5693 cmos_cmd = (new_level > level) ?
5694 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5695 inc = new_level > level ? 1 : -1;
5697 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5698 !acpi_ec_write(volume_offset, level)))
5699 return -EIO;
5701 for (i = level; i != new_level; i += inc)
5702 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5703 !acpi_ec_write(volume_offset, i + inc))
5704 return -EIO;
5706 if (mute &&
5707 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5708 !acpi_ec_write(volume_offset, new_level + mute))) {
5709 return -EIO;
5713 if (new_mute != mute) {
5714 /* level doesn't change */
5716 cmos_cmd = (new_mute) ?
5717 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5719 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5720 !acpi_ec_write(volume_offset, level + new_mute))
5721 return -EIO;
5725 return 0;
5728 static struct ibm_struct volume_driver_data = {
5729 .name = "volume",
5730 .read = volume_read,
5731 .write = volume_write,
5734 /*************************************************************************
5735 * Fan subdriver
5739 * FAN ACCESS MODES
5741 * TPACPI_FAN_RD_ACPI_GFAN:
5742 * ACPI GFAN method: returns fan level
5744 * see TPACPI_FAN_WR_ACPI_SFAN
5745 * EC 0x2f (HFSP) not available if GFAN exists
5747 * TPACPI_FAN_WR_ACPI_SFAN:
5748 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5750 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5751 * it for writing.
5753 * TPACPI_FAN_WR_TPEC:
5754 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5755 * Supported on almost all ThinkPads
5757 * Fan speed changes of any sort (including those caused by the
5758 * disengaged mode) are usually done slowly by the firmware as the
5759 * maximum ammount of fan duty cycle change per second seems to be
5760 * limited.
5762 * Reading is not available if GFAN exists.
5763 * Writing is not available if SFAN exists.
5765 * Bits
5766 * 7 automatic mode engaged;
5767 * (default operation mode of the ThinkPad)
5768 * fan level is ignored in this mode.
5769 * 6 full speed mode (takes precedence over bit 7);
5770 * not available on all thinkpads. May disable
5771 * the tachometer while the fan controller ramps up
5772 * the speed (which can take up to a few *minutes*).
5773 * Speeds up fan to 100% duty-cycle, which is far above
5774 * the standard RPM levels. It is not impossible that
5775 * it could cause hardware damage.
5776 * 5-3 unused in some models. Extra bits for fan level
5777 * in others, but still useless as all values above
5778 * 7 map to the same speed as level 7 in these models.
5779 * 2-0 fan level (0..7 usually)
5780 * 0x00 = stop
5781 * 0x07 = max (set when temperatures critical)
5782 * Some ThinkPads may have other levels, see
5783 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5785 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5786 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5787 * does so, its initial value is meaningless (0x07).
5789 * For firmware bugs, refer to:
5790 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5792 * ----
5794 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5795 * Main fan tachometer reading (in RPM)
5797 * This register is present on all ThinkPads with a new-style EC, and
5798 * it is known not to be present on the A21m/e, and T22, as there is
5799 * something else in offset 0x84 according to the ACPI DSDT. Other
5800 * ThinkPads from this same time period (and earlier) probably lack the
5801 * tachometer as well.
5803 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5804 * was never fixed by IBM to report the EC firmware version string
5805 * probably support the tachometer (like the early X models), so
5806 * detecting it is quite hard. We need more data to know for sure.
5808 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5809 * might result.
5811 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5812 * mode.
5814 * For firmware bugs, refer to:
5815 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5817 * TPACPI_FAN_WR_ACPI_FANS:
5818 * ThinkPad X31, X40, X41. Not available in the X60.
5820 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5821 * high speed. ACPI DSDT seems to map these three speeds to levels
5822 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5823 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5825 * The speeds are stored on handles
5826 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5828 * There are three default speed sets, acessible as handles:
5829 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5831 * ACPI DSDT switches which set is in use depending on various
5832 * factors.
5834 * TPACPI_FAN_WR_TPEC is also available and should be used to
5835 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5836 * but the ACPI tables just mention level 7.
5839 enum { /* Fan control constants */
5840 fan_status_offset = 0x2f, /* EC register 0x2f */
5841 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5842 * 0x84 must be read before 0x85 */
5844 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5845 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5847 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5850 enum fan_status_access_mode {
5851 TPACPI_FAN_NONE = 0, /* No fan status or control */
5852 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5853 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5856 enum fan_control_access_mode {
5857 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5858 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5859 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5860 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5863 enum fan_control_commands {
5864 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5865 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5866 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5867 * and also watchdog cmd */
5870 static int fan_control_allowed;
5872 static enum fan_status_access_mode fan_status_access_mode;
5873 static enum fan_control_access_mode fan_control_access_mode;
5874 static enum fan_control_commands fan_control_commands;
5876 static u8 fan_control_initial_status;
5877 static u8 fan_control_desired_level;
5878 static u8 fan_control_resume_level;
5879 static int fan_watchdog_maxinterval;
5881 static struct mutex fan_mutex;
5883 static void fan_watchdog_fire(struct work_struct *ignored);
5884 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5886 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5887 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5888 "\\FSPD", /* 600e/x, 770e, 770x */
5889 ); /* all others */
5890 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5891 "JFNS", /* 770x-JL */
5892 ); /* all others */
5895 * Call with fan_mutex held
5897 static void fan_update_desired_level(u8 status)
5899 if ((status &
5900 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5901 if (status > 7)
5902 fan_control_desired_level = 7;
5903 else
5904 fan_control_desired_level = status;
5908 static int fan_get_status(u8 *status)
5910 u8 s;
5912 /* TODO:
5913 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5915 switch (fan_status_access_mode) {
5916 case TPACPI_FAN_RD_ACPI_GFAN:
5917 /* 570, 600e/x, 770e, 770x */
5919 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5920 return -EIO;
5922 if (likely(status))
5923 *status = s & 0x07;
5925 break;
5927 case TPACPI_FAN_RD_TPEC:
5928 /* all except 570, 600e/x, 770e, 770x */
5929 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5930 return -EIO;
5932 if (likely(status))
5933 *status = s;
5935 break;
5937 default:
5938 return -ENXIO;
5941 return 0;
5944 static int fan_get_status_safe(u8 *status)
5946 int rc;
5947 u8 s;
5949 if (mutex_lock_killable(&fan_mutex))
5950 return -ERESTARTSYS;
5951 rc = fan_get_status(&s);
5952 if (!rc)
5953 fan_update_desired_level(s);
5954 mutex_unlock(&fan_mutex);
5956 if (status)
5957 *status = s;
5959 return rc;
5962 static int fan_get_speed(unsigned int *speed)
5964 u8 hi, lo;
5966 switch (fan_status_access_mode) {
5967 case TPACPI_FAN_RD_TPEC:
5968 /* all except 570, 600e/x, 770e, 770x */
5969 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5970 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5971 return -EIO;
5973 if (likely(speed))
5974 *speed = (hi << 8) | lo;
5976 break;
5978 default:
5979 return -ENXIO;
5982 return 0;
5985 static int fan_set_level(int level)
5987 if (!fan_control_allowed)
5988 return -EPERM;
5990 switch (fan_control_access_mode) {
5991 case TPACPI_FAN_WR_ACPI_SFAN:
5992 if (level >= 0 && level <= 7) {
5993 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5994 return -EIO;
5995 } else
5996 return -EINVAL;
5997 break;
5999 case TPACPI_FAN_WR_ACPI_FANS:
6000 case TPACPI_FAN_WR_TPEC:
6001 if (!(level & TP_EC_FAN_AUTO) &&
6002 !(level & TP_EC_FAN_FULLSPEED) &&
6003 ((level < 0) || (level > 7)))
6004 return -EINVAL;
6006 /* safety net should the EC not support AUTO
6007 * or FULLSPEED mode bits and just ignore them */
6008 if (level & TP_EC_FAN_FULLSPEED)
6009 level |= 7; /* safety min speed 7 */
6010 else if (level & TP_EC_FAN_AUTO)
6011 level |= 4; /* safety min speed 4 */
6013 if (!acpi_ec_write(fan_status_offset, level))
6014 return -EIO;
6015 else
6016 tp_features.fan_ctrl_status_undef = 0;
6017 break;
6019 default:
6020 return -ENXIO;
6022 return 0;
6025 static int fan_set_level_safe(int level)
6027 int rc;
6029 if (!fan_control_allowed)
6030 return -EPERM;
6032 if (mutex_lock_killable(&fan_mutex))
6033 return -ERESTARTSYS;
6035 if (level == TPACPI_FAN_LAST_LEVEL)
6036 level = fan_control_desired_level;
6038 rc = fan_set_level(level);
6039 if (!rc)
6040 fan_update_desired_level(level);
6042 mutex_unlock(&fan_mutex);
6043 return rc;
6046 static int fan_set_enable(void)
6048 u8 s;
6049 int rc;
6051 if (!fan_control_allowed)
6052 return -EPERM;
6054 if (mutex_lock_killable(&fan_mutex))
6055 return -ERESTARTSYS;
6057 switch (fan_control_access_mode) {
6058 case TPACPI_FAN_WR_ACPI_FANS:
6059 case TPACPI_FAN_WR_TPEC:
6060 rc = fan_get_status(&s);
6061 if (rc < 0)
6062 break;
6064 /* Don't go out of emergency fan mode */
6065 if (s != 7) {
6066 s &= 0x07;
6067 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6070 if (!acpi_ec_write(fan_status_offset, s))
6071 rc = -EIO;
6072 else {
6073 tp_features.fan_ctrl_status_undef = 0;
6074 rc = 0;
6076 break;
6078 case TPACPI_FAN_WR_ACPI_SFAN:
6079 rc = fan_get_status(&s);
6080 if (rc < 0)
6081 break;
6083 s &= 0x07;
6085 /* Set fan to at least level 4 */
6086 s |= 4;
6088 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6089 rc = -EIO;
6090 else
6091 rc = 0;
6092 break;
6094 default:
6095 rc = -ENXIO;
6098 mutex_unlock(&fan_mutex);
6099 return rc;
6102 static int fan_set_disable(void)
6104 int rc;
6106 if (!fan_control_allowed)
6107 return -EPERM;
6109 if (mutex_lock_killable(&fan_mutex))
6110 return -ERESTARTSYS;
6112 rc = 0;
6113 switch (fan_control_access_mode) {
6114 case TPACPI_FAN_WR_ACPI_FANS:
6115 case TPACPI_FAN_WR_TPEC:
6116 if (!acpi_ec_write(fan_status_offset, 0x00))
6117 rc = -EIO;
6118 else {
6119 fan_control_desired_level = 0;
6120 tp_features.fan_ctrl_status_undef = 0;
6122 break;
6124 case TPACPI_FAN_WR_ACPI_SFAN:
6125 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6126 rc = -EIO;
6127 else
6128 fan_control_desired_level = 0;
6129 break;
6131 default:
6132 rc = -ENXIO;
6136 mutex_unlock(&fan_mutex);
6137 return rc;
6140 static int fan_set_speed(int speed)
6142 int rc;
6144 if (!fan_control_allowed)
6145 return -EPERM;
6147 if (mutex_lock_killable(&fan_mutex))
6148 return -ERESTARTSYS;
6150 rc = 0;
6151 switch (fan_control_access_mode) {
6152 case TPACPI_FAN_WR_ACPI_FANS:
6153 if (speed >= 0 && speed <= 65535) {
6154 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6155 speed, speed, speed))
6156 rc = -EIO;
6157 } else
6158 rc = -EINVAL;
6159 break;
6161 default:
6162 rc = -ENXIO;
6165 mutex_unlock(&fan_mutex);
6166 return rc;
6169 static void fan_watchdog_reset(void)
6171 static int fan_watchdog_active;
6173 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6174 return;
6176 if (fan_watchdog_active)
6177 cancel_delayed_work(&fan_watchdog_task);
6179 if (fan_watchdog_maxinterval > 0 &&
6180 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6181 fan_watchdog_active = 1;
6182 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6183 msecs_to_jiffies(fan_watchdog_maxinterval
6184 * 1000))) {
6185 printk(TPACPI_ERR
6186 "failed to queue the fan watchdog, "
6187 "watchdog will not trigger\n");
6189 } else
6190 fan_watchdog_active = 0;
6193 static void fan_watchdog_fire(struct work_struct *ignored)
6195 int rc;
6197 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6198 return;
6200 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6201 rc = fan_set_enable();
6202 if (rc < 0) {
6203 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6204 "will try again later...\n", -rc);
6205 /* reschedule for later */
6206 fan_watchdog_reset();
6211 * SYSFS fan layout: hwmon compatible (device)
6213 * pwm*_enable:
6214 * 0: "disengaged" mode
6215 * 1: manual mode
6216 * 2: native EC "auto" mode (recommended, hardware default)
6218 * pwm*: set speed in manual mode, ignored otherwise.
6219 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6220 * interpolation.
6222 * fan*_input: tachometer reading, RPM
6225 * SYSFS fan layout: extensions
6227 * fan_watchdog (driver):
6228 * fan watchdog interval in seconds, 0 disables (default), max 120
6231 /* sysfs fan pwm1_enable ----------------------------------------------- */
6232 static ssize_t fan_pwm1_enable_show(struct device *dev,
6233 struct device_attribute *attr,
6234 char *buf)
6236 int res, mode;
6237 u8 status;
6239 res = fan_get_status_safe(&status);
6240 if (res)
6241 return res;
6243 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6244 if (status != fan_control_initial_status) {
6245 tp_features.fan_ctrl_status_undef = 0;
6246 } else {
6247 /* Return most likely status. In fact, it
6248 * might be the only possible status */
6249 status = TP_EC_FAN_AUTO;
6253 if (status & TP_EC_FAN_FULLSPEED) {
6254 mode = 0;
6255 } else if (status & TP_EC_FAN_AUTO) {
6256 mode = 2;
6257 } else
6258 mode = 1;
6260 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6263 static ssize_t fan_pwm1_enable_store(struct device *dev,
6264 struct device_attribute *attr,
6265 const char *buf, size_t count)
6267 unsigned long t;
6268 int res, level;
6270 if (parse_strtoul(buf, 2, &t))
6271 return -EINVAL;
6273 switch (t) {
6274 case 0:
6275 level = TP_EC_FAN_FULLSPEED;
6276 break;
6277 case 1:
6278 level = TPACPI_FAN_LAST_LEVEL;
6279 break;
6280 case 2:
6281 level = TP_EC_FAN_AUTO;
6282 break;
6283 case 3:
6284 /* reserved for software-controlled auto mode */
6285 return -ENOSYS;
6286 default:
6287 return -EINVAL;
6290 res = fan_set_level_safe(level);
6291 if (res == -ENXIO)
6292 return -EINVAL;
6293 else if (res < 0)
6294 return res;
6296 fan_watchdog_reset();
6298 return count;
6301 static struct device_attribute dev_attr_fan_pwm1_enable =
6302 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6303 fan_pwm1_enable_show, fan_pwm1_enable_store);
6305 /* sysfs fan pwm1 ------------------------------------------------------ */
6306 static ssize_t fan_pwm1_show(struct device *dev,
6307 struct device_attribute *attr,
6308 char *buf)
6310 int res;
6311 u8 status;
6313 res = fan_get_status_safe(&status);
6314 if (res)
6315 return res;
6317 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6318 if (status != fan_control_initial_status) {
6319 tp_features.fan_ctrl_status_undef = 0;
6320 } else {
6321 status = TP_EC_FAN_AUTO;
6325 if ((status &
6326 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6327 status = fan_control_desired_level;
6329 if (status > 7)
6330 status = 7;
6332 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6335 static ssize_t fan_pwm1_store(struct device *dev,
6336 struct device_attribute *attr,
6337 const char *buf, size_t count)
6339 unsigned long s;
6340 int rc;
6341 u8 status, newlevel;
6343 if (parse_strtoul(buf, 255, &s))
6344 return -EINVAL;
6346 /* scale down from 0-255 to 0-7 */
6347 newlevel = (s >> 5) & 0x07;
6349 if (mutex_lock_killable(&fan_mutex))
6350 return -ERESTARTSYS;
6352 rc = fan_get_status(&status);
6353 if (!rc && (status &
6354 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6355 rc = fan_set_level(newlevel);
6356 if (rc == -ENXIO)
6357 rc = -EINVAL;
6358 else if (!rc) {
6359 fan_update_desired_level(newlevel);
6360 fan_watchdog_reset();
6364 mutex_unlock(&fan_mutex);
6365 return (rc)? rc : count;
6368 static struct device_attribute dev_attr_fan_pwm1 =
6369 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6370 fan_pwm1_show, fan_pwm1_store);
6372 /* sysfs fan fan1_input ------------------------------------------------ */
6373 static ssize_t fan_fan1_input_show(struct device *dev,
6374 struct device_attribute *attr,
6375 char *buf)
6377 int res;
6378 unsigned int speed;
6380 res = fan_get_speed(&speed);
6381 if (res < 0)
6382 return res;
6384 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6387 static struct device_attribute dev_attr_fan_fan1_input =
6388 __ATTR(fan1_input, S_IRUGO,
6389 fan_fan1_input_show, NULL);
6391 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6392 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6393 char *buf)
6395 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6398 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6399 const char *buf, size_t count)
6401 unsigned long t;
6403 if (parse_strtoul(buf, 120, &t))
6404 return -EINVAL;
6406 if (!fan_control_allowed)
6407 return -EPERM;
6409 fan_watchdog_maxinterval = t;
6410 fan_watchdog_reset();
6412 return count;
6415 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6416 fan_fan_watchdog_show, fan_fan_watchdog_store);
6418 /* --------------------------------------------------------------------- */
6419 static struct attribute *fan_attributes[] = {
6420 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6421 &dev_attr_fan_fan1_input.attr,
6422 NULL
6425 static const struct attribute_group fan_attr_group = {
6426 .attrs = fan_attributes,
6429 static int __init fan_init(struct ibm_init_struct *iibm)
6431 int rc;
6433 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6435 mutex_init(&fan_mutex);
6436 fan_status_access_mode = TPACPI_FAN_NONE;
6437 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6438 fan_control_commands = 0;
6439 fan_watchdog_maxinterval = 0;
6440 tp_features.fan_ctrl_status_undef = 0;
6441 fan_control_desired_level = 7;
6443 TPACPI_ACPIHANDLE_INIT(fans);
6444 TPACPI_ACPIHANDLE_INIT(gfan);
6445 TPACPI_ACPIHANDLE_INIT(sfan);
6447 if (gfan_handle) {
6448 /* 570, 600e/x, 770e, 770x */
6449 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6450 } else {
6451 /* all other ThinkPads: note that even old-style
6452 * ThinkPad ECs supports the fan control register */
6453 if (likely(acpi_ec_read(fan_status_offset,
6454 &fan_control_initial_status))) {
6455 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6457 /* In some ThinkPads, neither the EC nor the ACPI
6458 * DSDT initialize the fan status, and it ends up
6459 * being set to 0x07 when it *could* be either
6460 * 0x07 or 0x80.
6462 * Enable for TP-1Y (T43), TP-78 (R51e),
6463 * TP-76 (R52), TP-70 (T43, R52), which are known
6464 * to be buggy. */
6465 if (fan_control_initial_status == 0x07) {
6466 switch (thinkpad_id.ec_model) {
6467 case 0x5931: /* TP-1Y */
6468 case 0x3837: /* TP-78 */
6469 case 0x3637: /* TP-76 */
6470 case 0x3037: /* TP-70 */
6471 printk(TPACPI_NOTICE
6472 "fan_init: initial fan status "
6473 "is unknown, assuming it is "
6474 "in auto mode\n");
6475 tp_features.fan_ctrl_status_undef = 1;
6479 } else {
6480 printk(TPACPI_ERR
6481 "ThinkPad ACPI EC access misbehaving, "
6482 "fan status and control unavailable\n");
6483 return 1;
6487 if (sfan_handle) {
6488 /* 570, 770x-JL */
6489 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6490 fan_control_commands |=
6491 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6492 } else {
6493 if (!gfan_handle) {
6494 /* gfan without sfan means no fan control */
6495 /* all other models implement TP EC 0x2f control */
6497 if (fans_handle) {
6498 /* X31, X40, X41 */
6499 fan_control_access_mode =
6500 TPACPI_FAN_WR_ACPI_FANS;
6501 fan_control_commands |=
6502 TPACPI_FAN_CMD_SPEED |
6503 TPACPI_FAN_CMD_LEVEL |
6504 TPACPI_FAN_CMD_ENABLE;
6505 } else {
6506 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6507 fan_control_commands |=
6508 TPACPI_FAN_CMD_LEVEL |
6509 TPACPI_FAN_CMD_ENABLE;
6514 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6515 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6516 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6517 fan_status_access_mode, fan_control_access_mode);
6519 /* fan control master switch */
6520 if (!fan_control_allowed) {
6521 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6522 fan_control_commands = 0;
6523 dbg_printk(TPACPI_DBG_INIT,
6524 "fan control features disabled by parameter\n");
6527 /* update fan_control_desired_level */
6528 if (fan_status_access_mode != TPACPI_FAN_NONE)
6529 fan_get_status_safe(NULL);
6531 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6532 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6533 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6534 &fan_attr_group);
6535 if (rc < 0)
6536 return rc;
6538 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6539 &driver_attr_fan_watchdog);
6540 if (rc < 0) {
6541 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6542 &fan_attr_group);
6543 return rc;
6545 return 0;
6546 } else
6547 return 1;
6550 static void fan_exit(void)
6552 vdbg_printk(TPACPI_DBG_EXIT,
6553 "cancelling any pending fan watchdog tasks\n");
6555 /* FIXME: can we really do this unconditionally? */
6556 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6557 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6558 &driver_attr_fan_watchdog);
6560 cancel_delayed_work(&fan_watchdog_task);
6561 flush_workqueue(tpacpi_wq);
6564 static void fan_suspend(pm_message_t state)
6566 int rc;
6568 if (!fan_control_allowed)
6569 return;
6571 /* Store fan status in cache */
6572 fan_control_resume_level = 0;
6573 rc = fan_get_status_safe(&fan_control_resume_level);
6574 if (rc < 0)
6575 printk(TPACPI_NOTICE
6576 "failed to read fan level for later "
6577 "restore during resume: %d\n", rc);
6579 /* if it is undefined, don't attempt to restore it.
6580 * KEEP THIS LAST */
6581 if (tp_features.fan_ctrl_status_undef)
6582 fan_control_resume_level = 0;
6585 static void fan_resume(void)
6587 u8 current_level = 7;
6588 bool do_set = false;
6589 int rc;
6591 /* DSDT *always* updates status on resume */
6592 tp_features.fan_ctrl_status_undef = 0;
6594 if (!fan_control_allowed ||
6595 !fan_control_resume_level ||
6596 (fan_get_status_safe(&current_level) < 0))
6597 return;
6599 switch (fan_control_access_mode) {
6600 case TPACPI_FAN_WR_ACPI_SFAN:
6601 /* never decrease fan level */
6602 do_set = (fan_control_resume_level > current_level);
6603 break;
6604 case TPACPI_FAN_WR_ACPI_FANS:
6605 case TPACPI_FAN_WR_TPEC:
6606 /* never decrease fan level, scale is:
6607 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6609 * We expect the firmware to set either 7 or AUTO, but we
6610 * handle FULLSPEED out of paranoia.
6612 * So, we can safely only restore FULLSPEED or 7, anything
6613 * else could slow the fan. Restoring AUTO is useless, at
6614 * best that's exactly what the DSDT already set (it is the
6615 * slower it uses).
6617 * Always keep in mind that the DSDT *will* have set the
6618 * fans to what the vendor supposes is the best level. We
6619 * muck with it only to speed the fan up.
6621 if (fan_control_resume_level != 7 &&
6622 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6623 return;
6624 else
6625 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6626 (current_level != fan_control_resume_level);
6627 break;
6628 default:
6629 return;
6631 if (do_set) {
6632 printk(TPACPI_NOTICE
6633 "restoring fan level to 0x%02x\n",
6634 fan_control_resume_level);
6635 rc = fan_set_level_safe(fan_control_resume_level);
6636 if (rc < 0)
6637 printk(TPACPI_NOTICE
6638 "failed to restore fan level: %d\n", rc);
6642 static int fan_read(char *p)
6644 int len = 0;
6645 int rc;
6646 u8 status;
6647 unsigned int speed = 0;
6649 switch (fan_status_access_mode) {
6650 case TPACPI_FAN_RD_ACPI_GFAN:
6651 /* 570, 600e/x, 770e, 770x */
6652 rc = fan_get_status_safe(&status);
6653 if (rc < 0)
6654 return rc;
6656 len += sprintf(p + len, "status:\t\t%s\n"
6657 "level:\t\t%d\n",
6658 (status != 0) ? "enabled" : "disabled", status);
6659 break;
6661 case TPACPI_FAN_RD_TPEC:
6662 /* all except 570, 600e/x, 770e, 770x */
6663 rc = fan_get_status_safe(&status);
6664 if (rc < 0)
6665 return rc;
6667 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6668 if (status != fan_control_initial_status)
6669 tp_features.fan_ctrl_status_undef = 0;
6670 else
6671 /* Return most likely status. In fact, it
6672 * might be the only possible status */
6673 status = TP_EC_FAN_AUTO;
6676 len += sprintf(p + len, "status:\t\t%s\n",
6677 (status != 0) ? "enabled" : "disabled");
6679 rc = fan_get_speed(&speed);
6680 if (rc < 0)
6681 return rc;
6683 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6685 if (status & TP_EC_FAN_FULLSPEED)
6686 /* Disengaged mode takes precedence */
6687 len += sprintf(p + len, "level:\t\tdisengaged\n");
6688 else if (status & TP_EC_FAN_AUTO)
6689 len += sprintf(p + len, "level:\t\tauto\n");
6690 else
6691 len += sprintf(p + len, "level:\t\t%d\n", status);
6692 break;
6694 case TPACPI_FAN_NONE:
6695 default:
6696 len += sprintf(p + len, "status:\t\tnot supported\n");
6699 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6700 len += sprintf(p + len, "commands:\tlevel <level>");
6702 switch (fan_control_access_mode) {
6703 case TPACPI_FAN_WR_ACPI_SFAN:
6704 len += sprintf(p + len, " (<level> is 0-7)\n");
6705 break;
6707 default:
6708 len += sprintf(p + len, " (<level> is 0-7, "
6709 "auto, disengaged, full-speed)\n");
6710 break;
6714 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6715 len += sprintf(p + len, "commands:\tenable, disable\n"
6716 "commands:\twatchdog <timeout> (<timeout> "
6717 "is 0 (off), 1-120 (seconds))\n");
6719 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6720 len += sprintf(p + len, "commands:\tspeed <speed>"
6721 " (<speed> is 0-65535)\n");
6723 return len;
6726 static int fan_write_cmd_level(const char *cmd, int *rc)
6728 int level;
6730 if (strlencmp(cmd, "level auto") == 0)
6731 level = TP_EC_FAN_AUTO;
6732 else if ((strlencmp(cmd, "level disengaged") == 0) |
6733 (strlencmp(cmd, "level full-speed") == 0))
6734 level = TP_EC_FAN_FULLSPEED;
6735 else if (sscanf(cmd, "level %d", &level) != 1)
6736 return 0;
6738 *rc = fan_set_level_safe(level);
6739 if (*rc == -ENXIO)
6740 printk(TPACPI_ERR "level command accepted for unsupported "
6741 "access mode %d", fan_control_access_mode);
6743 return 1;
6746 static int fan_write_cmd_enable(const char *cmd, int *rc)
6748 if (strlencmp(cmd, "enable") != 0)
6749 return 0;
6751 *rc = fan_set_enable();
6752 if (*rc == -ENXIO)
6753 printk(TPACPI_ERR "enable command accepted for unsupported "
6754 "access mode %d", fan_control_access_mode);
6756 return 1;
6759 static int fan_write_cmd_disable(const char *cmd, int *rc)
6761 if (strlencmp(cmd, "disable") != 0)
6762 return 0;
6764 *rc = fan_set_disable();
6765 if (*rc == -ENXIO)
6766 printk(TPACPI_ERR "disable command accepted for unsupported "
6767 "access mode %d", fan_control_access_mode);
6769 return 1;
6772 static int fan_write_cmd_speed(const char *cmd, int *rc)
6774 int speed;
6776 /* TODO:
6777 * Support speed <low> <medium> <high> ? */
6779 if (sscanf(cmd, "speed %d", &speed) != 1)
6780 return 0;
6782 *rc = fan_set_speed(speed);
6783 if (*rc == -ENXIO)
6784 printk(TPACPI_ERR "speed command accepted for unsupported "
6785 "access mode %d", fan_control_access_mode);
6787 return 1;
6790 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6792 int interval;
6794 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6795 return 0;
6797 if (interval < 0 || interval > 120)
6798 *rc = -EINVAL;
6799 else
6800 fan_watchdog_maxinterval = interval;
6802 return 1;
6805 static int fan_write(char *buf)
6807 char *cmd;
6808 int rc = 0;
6810 while (!rc && (cmd = next_cmd(&buf))) {
6811 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6812 fan_write_cmd_level(cmd, &rc)) &&
6813 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6814 (fan_write_cmd_enable(cmd, &rc) ||
6815 fan_write_cmd_disable(cmd, &rc) ||
6816 fan_write_cmd_watchdog(cmd, &rc))) &&
6817 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6818 fan_write_cmd_speed(cmd, &rc))
6820 rc = -EINVAL;
6821 else if (!rc)
6822 fan_watchdog_reset();
6825 return rc;
6828 static struct ibm_struct fan_driver_data = {
6829 .name = "fan",
6830 .read = fan_read,
6831 .write = fan_write,
6832 .exit = fan_exit,
6833 .suspend = fan_suspend,
6834 .resume = fan_resume,
6837 /****************************************************************************
6838 ****************************************************************************
6840 * Infrastructure
6842 ****************************************************************************
6843 ****************************************************************************/
6845 /* sysfs name ---------------------------------------------------------- */
6846 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6847 struct device_attribute *attr,
6848 char *buf)
6850 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6853 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6854 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6856 /* --------------------------------------------------------------------- */
6858 /* /proc support */
6859 static struct proc_dir_entry *proc_dir;
6862 * Module and infrastructure proble, init and exit handling
6865 static int force_load;
6867 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6868 static const char * __init str_supported(int is_supported)
6870 static char text_unsupported[] __initdata = "not supported";
6872 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6874 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6876 static void ibm_exit(struct ibm_struct *ibm)
6878 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6880 list_del_init(&ibm->all_drivers);
6882 if (ibm->flags.acpi_notify_installed) {
6883 dbg_printk(TPACPI_DBG_EXIT,
6884 "%s: acpi_remove_notify_handler\n", ibm->name);
6885 BUG_ON(!ibm->acpi);
6886 acpi_remove_notify_handler(*ibm->acpi->handle,
6887 ibm->acpi->type,
6888 dispatch_acpi_notify);
6889 ibm->flags.acpi_notify_installed = 0;
6890 ibm->flags.acpi_notify_installed = 0;
6893 if (ibm->flags.proc_created) {
6894 dbg_printk(TPACPI_DBG_EXIT,
6895 "%s: remove_proc_entry\n", ibm->name);
6896 remove_proc_entry(ibm->name, proc_dir);
6897 ibm->flags.proc_created = 0;
6900 if (ibm->flags.acpi_driver_registered) {
6901 dbg_printk(TPACPI_DBG_EXIT,
6902 "%s: acpi_bus_unregister_driver\n", ibm->name);
6903 BUG_ON(!ibm->acpi);
6904 acpi_bus_unregister_driver(ibm->acpi->driver);
6905 kfree(ibm->acpi->driver);
6906 ibm->acpi->driver = NULL;
6907 ibm->flags.acpi_driver_registered = 0;
6910 if (ibm->flags.init_called && ibm->exit) {
6911 ibm->exit();
6912 ibm->flags.init_called = 0;
6915 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6918 static int __init ibm_init(struct ibm_init_struct *iibm)
6920 int ret;
6921 struct ibm_struct *ibm = iibm->data;
6922 struct proc_dir_entry *entry;
6924 BUG_ON(ibm == NULL);
6926 INIT_LIST_HEAD(&ibm->all_drivers);
6928 if (ibm->flags.experimental && !experimental)
6929 return 0;
6931 dbg_printk(TPACPI_DBG_INIT,
6932 "probing for %s\n", ibm->name);
6934 if (iibm->init) {
6935 ret = iibm->init(iibm);
6936 if (ret > 0)
6937 return 0; /* probe failed */
6938 if (ret)
6939 return ret;
6941 ibm->flags.init_called = 1;
6944 if (ibm->acpi) {
6945 if (ibm->acpi->hid) {
6946 ret = register_tpacpi_subdriver(ibm);
6947 if (ret)
6948 goto err_out;
6951 if (ibm->acpi->notify) {
6952 ret = setup_acpi_notify(ibm);
6953 if (ret == -ENODEV) {
6954 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6955 ibm->name);
6956 ret = 0;
6957 goto err_out;
6959 if (ret < 0)
6960 goto err_out;
6964 dbg_printk(TPACPI_DBG_INIT,
6965 "%s installed\n", ibm->name);
6967 if (ibm->read) {
6968 entry = create_proc_entry(ibm->name,
6969 S_IFREG | S_IRUGO | S_IWUSR,
6970 proc_dir);
6971 if (!entry) {
6972 printk(TPACPI_ERR "unable to create proc entry %s\n",
6973 ibm->name);
6974 ret = -ENODEV;
6975 goto err_out;
6977 entry->owner = THIS_MODULE;
6978 entry->data = ibm;
6979 entry->read_proc = &dispatch_procfs_read;
6980 if (ibm->write)
6981 entry->write_proc = &dispatch_procfs_write;
6982 ibm->flags.proc_created = 1;
6985 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6987 return 0;
6989 err_out:
6990 dbg_printk(TPACPI_DBG_INIT,
6991 "%s: at error exit path with result %d\n",
6992 ibm->name, ret);
6994 ibm_exit(ibm);
6995 return (ret < 0)? ret : 0;
6998 /* Probing */
7000 /* returns 0 - probe ok, or < 0 - probe error.
7001 * Probe ok doesn't mean thinkpad found.
7002 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7003 static int __must_check __init get_thinkpad_model_data(
7004 struct thinkpad_id_data *tp)
7006 const struct dmi_device *dev = NULL;
7007 char ec_fw_string[18];
7008 char const *s;
7010 if (!tp)
7011 return -EINVAL;
7013 memset(tp, 0, sizeof(*tp));
7015 if (dmi_name_in_vendors("IBM"))
7016 tp->vendor = PCI_VENDOR_ID_IBM;
7017 else if (dmi_name_in_vendors("LENOVO"))
7018 tp->vendor = PCI_VENDOR_ID_LENOVO;
7019 else
7020 return 0;
7022 s = dmi_get_system_info(DMI_BIOS_VERSION);
7023 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7024 if (s && !tp->bios_version_str)
7025 return -ENOMEM;
7026 if (!tp->bios_version_str)
7027 return 0;
7028 tp->bios_model = tp->bios_version_str[0]
7029 | (tp->bios_version_str[1] << 8);
7032 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7033 * X32 or newer, all Z series; Some models must have an
7034 * up-to-date BIOS or they will not be detected.
7036 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7038 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7039 if (sscanf(dev->name,
7040 "IBM ThinkPad Embedded Controller -[%17c",
7041 ec_fw_string) == 1) {
7042 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7043 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7045 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7046 if (!tp->ec_version_str)
7047 return -ENOMEM;
7048 tp->ec_model = ec_fw_string[0]
7049 | (ec_fw_string[1] << 8);
7050 break;
7054 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7055 if (s && !strnicmp(s, "ThinkPad", 8)) {
7056 tp->model_str = kstrdup(s, GFP_KERNEL);
7057 if (!tp->model_str)
7058 return -ENOMEM;
7061 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7062 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7063 if (s && !tp->nummodel_str)
7064 return -ENOMEM;
7066 return 0;
7069 static int __init probe_for_thinkpad(void)
7071 int is_thinkpad;
7073 if (acpi_disabled)
7074 return -ENODEV;
7077 * Non-ancient models have better DMI tagging, but very old models
7078 * don't.
7080 is_thinkpad = (thinkpad_id.model_str != NULL);
7082 /* ec is required because many other handles are relative to it */
7083 TPACPI_ACPIHANDLE_INIT(ec);
7084 if (!ec_handle) {
7085 if (is_thinkpad)
7086 printk(TPACPI_ERR
7087 "Not yet supported ThinkPad detected!\n");
7088 return -ENODEV;
7092 * Risks a regression on very old machines, but reduces potential
7093 * false positives a damn great deal
7095 if (!is_thinkpad)
7096 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7098 if (!is_thinkpad && !force_load)
7099 return -ENODEV;
7101 return 0;
7105 /* Module init, exit, parameters */
7107 static struct ibm_init_struct ibms_init[] __initdata = {
7109 .init = thinkpad_acpi_driver_init,
7110 .data = &thinkpad_acpi_driver_data,
7113 .init = hotkey_init,
7114 .data = &hotkey_driver_data,
7117 .init = bluetooth_init,
7118 .data = &bluetooth_driver_data,
7121 .init = wan_init,
7122 .data = &wan_driver_data,
7125 .init = uwb_init,
7126 .data = &uwb_driver_data,
7128 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7130 .init = video_init,
7131 .data = &video_driver_data,
7133 #endif
7135 .init = light_init,
7136 .data = &light_driver_data,
7138 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7140 .init = dock_init,
7141 .data = &dock_driver_data[0],
7144 .init = dock_init2,
7145 .data = &dock_driver_data[1],
7147 #endif
7148 #ifdef CONFIG_THINKPAD_ACPI_BAY
7150 .init = bay_init,
7151 .data = &bay_driver_data,
7153 #endif
7155 .init = cmos_init,
7156 .data = &cmos_driver_data,
7159 .init = led_init,
7160 .data = &led_driver_data,
7163 .init = beep_init,
7164 .data = &beep_driver_data,
7167 .init = thermal_init,
7168 .data = &thermal_driver_data,
7171 .data = &ecdump_driver_data,
7174 .init = brightness_init,
7175 .data = &brightness_driver_data,
7178 .data = &volume_driver_data,
7181 .init = fan_init,
7182 .data = &fan_driver_data,
7186 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7188 unsigned int i;
7189 struct ibm_struct *ibm;
7191 if (!kp || !kp->name || !val)
7192 return -EINVAL;
7194 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7195 ibm = ibms_init[i].data;
7196 WARN_ON(ibm == NULL);
7198 if (!ibm || !ibm->name)
7199 continue;
7201 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7202 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7203 return -ENOSPC;
7204 strcpy(ibms_init[i].param, val);
7205 strcat(ibms_init[i].param, ",");
7206 return 0;
7210 return -EINVAL;
7213 module_param(experimental, int, 0);
7214 MODULE_PARM_DESC(experimental,
7215 "Enables experimental features when non-zero");
7217 module_param_named(debug, dbg_level, uint, 0);
7218 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7220 module_param(force_load, bool, 0);
7221 MODULE_PARM_DESC(force_load,
7222 "Attempts to load the driver even on a "
7223 "mis-identified ThinkPad when true");
7225 module_param_named(fan_control, fan_control_allowed, bool, 0);
7226 MODULE_PARM_DESC(fan_control,
7227 "Enables setting fan parameters features when true");
7229 module_param_named(brightness_mode, brightness_mode, int, 0);
7230 MODULE_PARM_DESC(brightness_mode,
7231 "Selects brightness control strategy: "
7232 "0=auto, 1=EC, 2=CMOS, 3=both");
7234 module_param(brightness_enable, uint, 0);
7235 MODULE_PARM_DESC(brightness_enable,
7236 "Enables backlight control when 1, disables when 0");
7238 module_param(hotkey_report_mode, uint, 0);
7239 MODULE_PARM_DESC(hotkey_report_mode,
7240 "used for backwards compatibility with userspace, "
7241 "see documentation");
7243 #define TPACPI_PARAM(feature) \
7244 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7245 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7246 "at module load, see documentation")
7248 TPACPI_PARAM(hotkey);
7249 TPACPI_PARAM(bluetooth);
7250 TPACPI_PARAM(video);
7251 TPACPI_PARAM(light);
7252 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7253 TPACPI_PARAM(dock);
7254 #endif
7255 #ifdef CONFIG_THINKPAD_ACPI_BAY
7256 TPACPI_PARAM(bay);
7257 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7258 TPACPI_PARAM(cmos);
7259 TPACPI_PARAM(led);
7260 TPACPI_PARAM(beep);
7261 TPACPI_PARAM(ecdump);
7262 TPACPI_PARAM(brightness);
7263 TPACPI_PARAM(volume);
7264 TPACPI_PARAM(fan);
7266 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7267 module_param(dbg_wlswemul, uint, 0);
7268 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7269 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7270 MODULE_PARM_DESC(wlsw_state,
7271 "Initial state of the emulated WLSW switch");
7273 module_param(dbg_bluetoothemul, uint, 0);
7274 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7275 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7276 MODULE_PARM_DESC(bluetooth_state,
7277 "Initial state of the emulated bluetooth switch");
7279 module_param(dbg_wwanemul, uint, 0);
7280 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7281 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7282 MODULE_PARM_DESC(wwan_state,
7283 "Initial state of the emulated WWAN switch");
7285 module_param(dbg_uwbemul, uint, 0);
7286 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7287 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7288 MODULE_PARM_DESC(uwb_state,
7289 "Initial state of the emulated UWB switch");
7290 #endif
7292 static void thinkpad_acpi_module_exit(void)
7294 struct ibm_struct *ibm, *itmp;
7296 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7298 list_for_each_entry_safe_reverse(ibm, itmp,
7299 &tpacpi_all_drivers,
7300 all_drivers) {
7301 ibm_exit(ibm);
7304 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7306 if (tpacpi_inputdev) {
7307 if (tp_features.input_device_registered)
7308 input_unregister_device(tpacpi_inputdev);
7309 else
7310 input_free_device(tpacpi_inputdev);
7313 if (tpacpi_hwmon)
7314 hwmon_device_unregister(tpacpi_hwmon);
7316 if (tp_features.sensors_pdev_attrs_registered)
7317 device_remove_file(&tpacpi_sensors_pdev->dev,
7318 &dev_attr_thinkpad_acpi_pdev_name);
7319 if (tpacpi_sensors_pdev)
7320 platform_device_unregister(tpacpi_sensors_pdev);
7321 if (tpacpi_pdev)
7322 platform_device_unregister(tpacpi_pdev);
7324 if (tp_features.sensors_pdrv_attrs_registered)
7325 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7326 if (tp_features.platform_drv_attrs_registered)
7327 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7329 if (tp_features.sensors_pdrv_registered)
7330 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7332 if (tp_features.platform_drv_registered)
7333 platform_driver_unregister(&tpacpi_pdriver);
7335 if (proc_dir)
7336 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7338 if (tpacpi_wq)
7339 destroy_workqueue(tpacpi_wq);
7341 kfree(thinkpad_id.bios_version_str);
7342 kfree(thinkpad_id.ec_version_str);
7343 kfree(thinkpad_id.model_str);
7347 static int __init thinkpad_acpi_module_init(void)
7349 int ret, i;
7351 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7353 /* Parameter checking */
7354 if (hotkey_report_mode > 2)
7355 return -EINVAL;
7357 /* Driver-level probe */
7359 ret = get_thinkpad_model_data(&thinkpad_id);
7360 if (ret) {
7361 printk(TPACPI_ERR
7362 "unable to get DMI data: %d\n", ret);
7363 thinkpad_acpi_module_exit();
7364 return ret;
7366 ret = probe_for_thinkpad();
7367 if (ret) {
7368 thinkpad_acpi_module_exit();
7369 return ret;
7372 /* Driver initialization */
7374 TPACPI_ACPIHANDLE_INIT(ecrd);
7375 TPACPI_ACPIHANDLE_INIT(ecwr);
7377 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7378 if (!tpacpi_wq) {
7379 thinkpad_acpi_module_exit();
7380 return -ENOMEM;
7383 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7384 if (!proc_dir) {
7385 printk(TPACPI_ERR
7386 "unable to create proc dir " TPACPI_PROC_DIR);
7387 thinkpad_acpi_module_exit();
7388 return -ENODEV;
7390 proc_dir->owner = THIS_MODULE;
7392 ret = platform_driver_register(&tpacpi_pdriver);
7393 if (ret) {
7394 printk(TPACPI_ERR
7395 "unable to register main platform driver\n");
7396 thinkpad_acpi_module_exit();
7397 return ret;
7399 tp_features.platform_drv_registered = 1;
7401 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7402 if (ret) {
7403 printk(TPACPI_ERR
7404 "unable to register hwmon platform driver\n");
7405 thinkpad_acpi_module_exit();
7406 return ret;
7408 tp_features.sensors_pdrv_registered = 1;
7410 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7411 if (!ret) {
7412 tp_features.platform_drv_attrs_registered = 1;
7413 ret = tpacpi_create_driver_attributes(
7414 &tpacpi_hwmon_pdriver.driver);
7416 if (ret) {
7417 printk(TPACPI_ERR
7418 "unable to create sysfs driver attributes\n");
7419 thinkpad_acpi_module_exit();
7420 return ret;
7422 tp_features.sensors_pdrv_attrs_registered = 1;
7425 /* Device initialization */
7426 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7427 NULL, 0);
7428 if (IS_ERR(tpacpi_pdev)) {
7429 ret = PTR_ERR(tpacpi_pdev);
7430 tpacpi_pdev = NULL;
7431 printk(TPACPI_ERR "unable to register platform device\n");
7432 thinkpad_acpi_module_exit();
7433 return ret;
7435 tpacpi_sensors_pdev = platform_device_register_simple(
7436 TPACPI_HWMON_DRVR_NAME,
7437 -1, NULL, 0);
7438 if (IS_ERR(tpacpi_sensors_pdev)) {
7439 ret = PTR_ERR(tpacpi_sensors_pdev);
7440 tpacpi_sensors_pdev = NULL;
7441 printk(TPACPI_ERR
7442 "unable to register hwmon platform device\n");
7443 thinkpad_acpi_module_exit();
7444 return ret;
7446 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7447 &dev_attr_thinkpad_acpi_pdev_name);
7448 if (ret) {
7449 printk(TPACPI_ERR
7450 "unable to create sysfs hwmon device attributes\n");
7451 thinkpad_acpi_module_exit();
7452 return ret;
7454 tp_features.sensors_pdev_attrs_registered = 1;
7455 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7456 if (IS_ERR(tpacpi_hwmon)) {
7457 ret = PTR_ERR(tpacpi_hwmon);
7458 tpacpi_hwmon = NULL;
7459 printk(TPACPI_ERR "unable to register hwmon device\n");
7460 thinkpad_acpi_module_exit();
7461 return ret;
7463 mutex_init(&tpacpi_inputdev_send_mutex);
7464 tpacpi_inputdev = input_allocate_device();
7465 if (!tpacpi_inputdev) {
7466 printk(TPACPI_ERR "unable to allocate input device\n");
7467 thinkpad_acpi_module_exit();
7468 return -ENOMEM;
7469 } else {
7470 /* Prepare input device, but don't register */
7471 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7472 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7473 tpacpi_inputdev->id.bustype = BUS_HOST;
7474 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7475 thinkpad_id.vendor :
7476 PCI_VENDOR_ID_IBM;
7477 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7478 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7480 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7481 ret = ibm_init(&ibms_init[i]);
7482 if (ret >= 0 && *ibms_init[i].param)
7483 ret = ibms_init[i].data->write(ibms_init[i].param);
7484 if (ret < 0) {
7485 thinkpad_acpi_module_exit();
7486 return ret;
7489 ret = input_register_device(tpacpi_inputdev);
7490 if (ret < 0) {
7491 printk(TPACPI_ERR "unable to register input device\n");
7492 thinkpad_acpi_module_exit();
7493 return ret;
7494 } else {
7495 tp_features.input_device_registered = 1;
7498 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7499 return 0;
7502 /* Please remove this in year 2009 */
7503 MODULE_ALIAS("ibm_acpi");
7505 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7508 * DMI matching for module autoloading
7510 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7511 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7513 * Only models listed in thinkwiki will be supported, so add yours
7514 * if it is not there yet.
7516 #define IBM_BIOS_MODULE_ALIAS(__type) \
7517 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7519 /* Non-ancient thinkpads */
7520 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7521 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7523 /* Ancient thinkpad BIOSes have to be identified by
7524 * BIOS type or model number, and there are far less
7525 * BIOS types than model numbers... */
7526 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7527 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7528 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7530 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7531 MODULE_DESCRIPTION(TPACPI_DESC);
7532 MODULE_VERSION(TPACPI_VERSION);
7533 MODULE_LICENSE("GPL");
7535 module_init(thinkpad_acpi_module_init);
7536 module_exit(thinkpad_acpi_module_exit);