ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob99d9bd8e816069667487f27f344d8b5329caf680
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-20081111"
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 /* Special LED class that can defer work */
311 struct tpacpi_led_classdev {
312 struct led_classdev led_classdev;
313 struct work_struct work;
314 enum led_brightness new_brightness;
315 unsigned int led;
318 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
319 static int dbg_wlswemul;
320 static int tpacpi_wlsw_emulstate;
321 static int dbg_bluetoothemul;
322 static int tpacpi_bluetooth_emulstate;
323 static int dbg_wwanemul;
324 static int tpacpi_wwan_emulstate;
325 static int dbg_uwbemul;
326 static int tpacpi_uwb_emulstate;
327 #endif
330 /****************************************************************************
331 ****************************************************************************
333 * ACPI Helpers and device model
335 ****************************************************************************
336 ****************************************************************************/
338 /*************************************************************************
339 * ACPI basic handles
342 static acpi_handle root_handle;
344 #define TPACPI_HANDLE(object, parent, paths...) \
345 static acpi_handle object##_handle; \
346 static acpi_handle *object##_parent = &parent##_handle; \
347 static char *object##_path; \
348 static char *object##_paths[] = { paths }
350 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
351 "\\_SB.PCI.ISA.EC", /* 570 */
352 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
353 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
354 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
355 "\\_SB.PCI0.ICH3.EC0", /* R31 */
356 "\\_SB.PCI0.LPC.EC", /* all others */
359 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
360 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
362 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
363 /* T4x, X31, X40 */
364 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
365 "\\CMS", /* R40, R40e */
366 ); /* all others */
368 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
369 "^HKEY", /* R30, R31 */
370 "HKEY", /* all others */
371 ); /* 570 */
373 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
374 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
375 "\\_SB.PCI0.VID0", /* 770e */
376 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
377 "\\_SB.PCI0.AGP.VID", /* all others */
378 ); /* R30, R31 */
381 /*************************************************************************
382 * ACPI helpers
385 static int acpi_evalf(acpi_handle handle,
386 void *res, char *method, char *fmt, ...)
388 char *fmt0 = fmt;
389 struct acpi_object_list params;
390 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
391 struct acpi_buffer result, *resultp;
392 union acpi_object out_obj;
393 acpi_status status;
394 va_list ap;
395 char res_type;
396 int success;
397 int quiet;
399 if (!*fmt) {
400 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
401 return 0;
404 if (*fmt == 'q') {
405 quiet = 1;
406 fmt++;
407 } else
408 quiet = 0;
410 res_type = *(fmt++);
412 params.count = 0;
413 params.pointer = &in_objs[0];
415 va_start(ap, fmt);
416 while (*fmt) {
417 char c = *(fmt++);
418 switch (c) {
419 case 'd': /* int */
420 in_objs[params.count].integer.value = va_arg(ap, int);
421 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
422 break;
423 /* add more types as needed */
424 default:
425 printk(TPACPI_ERR "acpi_evalf() called "
426 "with invalid format character '%c'\n", c);
427 return 0;
430 va_end(ap);
432 if (res_type != 'v') {
433 result.length = sizeof(out_obj);
434 result.pointer = &out_obj;
435 resultp = &result;
436 } else
437 resultp = NULL;
439 status = acpi_evaluate_object(handle, method, &params, resultp);
441 switch (res_type) {
442 case 'd': /* int */
443 if (res)
444 *(int *)res = out_obj.integer.value;
445 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
446 break;
447 case 'v': /* void */
448 success = status == AE_OK;
449 break;
450 /* add more types as needed */
451 default:
452 printk(TPACPI_ERR "acpi_evalf() called "
453 "with invalid format character '%c'\n", res_type);
454 return 0;
457 if (!success && !quiet)
458 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
459 method, fmt0, status);
461 return success;
464 static int acpi_ec_read(int i, u8 *p)
466 int v;
468 if (ecrd_handle) {
469 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
470 return 0;
471 *p = v;
472 } else {
473 if (ec_read(i, p) < 0)
474 return 0;
477 return 1;
480 static int acpi_ec_write(int i, u8 v)
482 if (ecwr_handle) {
483 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
484 return 0;
485 } else {
486 if (ec_write(i, v) < 0)
487 return 0;
490 return 1;
493 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
494 static int _sta(acpi_handle handle)
496 int status;
498 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
499 status = 0;
501 return status;
503 #endif
505 static int issue_thinkpad_cmos_command(int cmos_cmd)
507 if (!cmos_handle)
508 return -ENXIO;
510 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
511 return -EIO;
513 return 0;
516 /*************************************************************************
517 * ACPI device model
520 #define TPACPI_ACPIHANDLE_INIT(object) \
521 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
522 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
524 static void drv_acpi_handle_init(char *name,
525 acpi_handle *handle, acpi_handle parent,
526 char **paths, int num_paths, char **path)
528 int i;
529 acpi_status status;
531 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
532 name);
534 for (i = 0; i < num_paths; i++) {
535 status = acpi_get_handle(parent, paths[i], handle);
536 if (ACPI_SUCCESS(status)) {
537 *path = paths[i];
538 dbg_printk(TPACPI_DBG_INIT,
539 "Found ACPI handle %s for %s\n",
540 *path, name);
541 return;
545 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
546 name);
547 *handle = NULL;
550 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
552 struct ibm_struct *ibm = data;
554 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
555 return;
557 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
558 return;
560 ibm->acpi->notify(ibm, event);
563 static int __init setup_acpi_notify(struct ibm_struct *ibm)
565 acpi_status status;
566 int rc;
568 BUG_ON(!ibm->acpi);
570 if (!*ibm->acpi->handle)
571 return 0;
573 vdbg_printk(TPACPI_DBG_INIT,
574 "setting up ACPI notify for %s\n", ibm->name);
576 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
577 if (rc < 0) {
578 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
579 ibm->name, rc);
580 return -ENODEV;
583 ibm->acpi->device->driver_data = ibm;
584 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
585 TPACPI_ACPI_EVENT_PREFIX,
586 ibm->name);
588 status = acpi_install_notify_handler(*ibm->acpi->handle,
589 ibm->acpi->type, dispatch_acpi_notify, ibm);
590 if (ACPI_FAILURE(status)) {
591 if (status == AE_ALREADY_EXISTS) {
592 printk(TPACPI_NOTICE
593 "another device driver is already "
594 "handling %s events\n", ibm->name);
595 } else {
596 printk(TPACPI_ERR
597 "acpi_install_notify_handler(%s) failed: %d\n",
598 ibm->name, status);
600 return -ENODEV;
602 ibm->flags.acpi_notify_installed = 1;
603 return 0;
606 static int __init tpacpi_device_add(struct acpi_device *device)
608 return 0;
611 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
613 int rc;
615 dbg_printk(TPACPI_DBG_INIT,
616 "registering %s as an ACPI driver\n", ibm->name);
618 BUG_ON(!ibm->acpi);
620 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
621 if (!ibm->acpi->driver) {
622 printk(TPACPI_ERR
623 "failed to allocate memory for ibm->acpi->driver\n");
624 return -ENOMEM;
627 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
628 ibm->acpi->driver->ids = ibm->acpi->hid;
630 ibm->acpi->driver->ops.add = &tpacpi_device_add;
632 rc = acpi_bus_register_driver(ibm->acpi->driver);
633 if (rc < 0) {
634 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
635 ibm->name, rc);
636 kfree(ibm->acpi->driver);
637 ibm->acpi->driver = NULL;
638 } else if (!rc)
639 ibm->flags.acpi_driver_registered = 1;
641 return rc;
645 /****************************************************************************
646 ****************************************************************************
648 * Procfs Helpers
650 ****************************************************************************
651 ****************************************************************************/
653 static int dispatch_procfs_read(char *page, char **start, off_t off,
654 int count, int *eof, void *data)
656 struct ibm_struct *ibm = data;
657 int len;
659 if (!ibm || !ibm->read)
660 return -EINVAL;
662 len = ibm->read(page);
663 if (len < 0)
664 return len;
666 if (len <= off + count)
667 *eof = 1;
668 *start = page + off;
669 len -= off;
670 if (len > count)
671 len = count;
672 if (len < 0)
673 len = 0;
675 return len;
678 static int dispatch_procfs_write(struct file *file,
679 const char __user *userbuf,
680 unsigned long count, void *data)
682 struct ibm_struct *ibm = data;
683 char *kernbuf;
684 int ret;
686 if (!ibm || !ibm->write)
687 return -EINVAL;
689 kernbuf = kmalloc(count + 2, GFP_KERNEL);
690 if (!kernbuf)
691 return -ENOMEM;
693 if (copy_from_user(kernbuf, userbuf, count)) {
694 kfree(kernbuf);
695 return -EFAULT;
698 kernbuf[count] = 0;
699 strcat(kernbuf, ",");
700 ret = ibm->write(kernbuf);
701 if (ret == 0)
702 ret = count;
704 kfree(kernbuf);
706 return ret;
709 static char *next_cmd(char **cmds)
711 char *start = *cmds;
712 char *end;
714 while ((end = strchr(start, ',')) && end == start)
715 start = end + 1;
717 if (!end)
718 return NULL;
720 *end = 0;
721 *cmds = end + 1;
722 return start;
726 /****************************************************************************
727 ****************************************************************************
729 * Device model: input, hwmon and platform
731 ****************************************************************************
732 ****************************************************************************/
734 static struct platform_device *tpacpi_pdev;
735 static struct platform_device *tpacpi_sensors_pdev;
736 static struct device *tpacpi_hwmon;
737 static struct input_dev *tpacpi_inputdev;
738 static struct mutex tpacpi_inputdev_send_mutex;
739 static LIST_HEAD(tpacpi_all_drivers);
741 static int tpacpi_suspend_handler(struct platform_device *pdev,
742 pm_message_t state)
744 struct ibm_struct *ibm, *itmp;
746 list_for_each_entry_safe(ibm, itmp,
747 &tpacpi_all_drivers,
748 all_drivers) {
749 if (ibm->suspend)
750 (ibm->suspend)(state);
753 return 0;
756 static int tpacpi_resume_handler(struct platform_device *pdev)
758 struct ibm_struct *ibm, *itmp;
760 list_for_each_entry_safe(ibm, itmp,
761 &tpacpi_all_drivers,
762 all_drivers) {
763 if (ibm->resume)
764 (ibm->resume)();
767 return 0;
770 static void tpacpi_shutdown_handler(struct platform_device *pdev)
772 struct ibm_struct *ibm, *itmp;
774 list_for_each_entry_safe(ibm, itmp,
775 &tpacpi_all_drivers,
776 all_drivers) {
777 if (ibm->shutdown)
778 (ibm->shutdown)();
782 static struct platform_driver tpacpi_pdriver = {
783 .driver = {
784 .name = TPACPI_DRVR_NAME,
785 .owner = THIS_MODULE,
787 .suspend = tpacpi_suspend_handler,
788 .resume = tpacpi_resume_handler,
789 .shutdown = tpacpi_shutdown_handler,
792 static struct platform_driver tpacpi_hwmon_pdriver = {
793 .driver = {
794 .name = TPACPI_HWMON_DRVR_NAME,
795 .owner = THIS_MODULE,
799 /*************************************************************************
800 * sysfs support helpers
803 struct attribute_set {
804 unsigned int members, max_members;
805 struct attribute_group group;
808 struct attribute_set_obj {
809 struct attribute_set s;
810 struct attribute *a;
811 } __attribute__((packed));
813 static struct attribute_set *create_attr_set(unsigned int max_members,
814 const char *name)
816 struct attribute_set_obj *sobj;
818 if (max_members == 0)
819 return NULL;
821 /* Allocates space for implicit NULL at the end too */
822 sobj = kzalloc(sizeof(struct attribute_set_obj) +
823 max_members * sizeof(struct attribute *),
824 GFP_KERNEL);
825 if (!sobj)
826 return NULL;
827 sobj->s.max_members = max_members;
828 sobj->s.group.attrs = &sobj->a;
829 sobj->s.group.name = name;
831 return &sobj->s;
834 #define destroy_attr_set(_set) \
835 kfree(_set);
837 /* not multi-threaded safe, use it in a single thread per set */
838 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
840 if (!s || !attr)
841 return -EINVAL;
843 if (s->members >= s->max_members)
844 return -ENOMEM;
846 s->group.attrs[s->members] = attr;
847 s->members++;
849 return 0;
852 static int add_many_to_attr_set(struct attribute_set *s,
853 struct attribute **attr,
854 unsigned int count)
856 int i, res;
858 for (i = 0; i < count; i++) {
859 res = add_to_attr_set(s, attr[i]);
860 if (res)
861 return res;
864 return 0;
867 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
869 sysfs_remove_group(kobj, &s->group);
870 destroy_attr_set(s);
873 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
874 sysfs_create_group(_kobj, &_attr_set->group)
876 static int parse_strtoul(const char *buf,
877 unsigned long max, unsigned long *value)
879 char *endp;
881 while (*buf && isspace(*buf))
882 buf++;
883 *value = simple_strtoul(buf, &endp, 0);
884 while (*endp && isspace(*endp))
885 endp++;
886 if (*endp || *value > max)
887 return -EINVAL;
889 return 0;
892 static void tpacpi_disable_brightness_delay(void)
894 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
895 printk(TPACPI_NOTICE
896 "ACPI backlight control delay disabled\n");
899 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
901 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
902 union acpi_object *obj;
903 int rc;
905 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
906 obj = (union acpi_object *)buffer.pointer;
907 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
908 printk(TPACPI_ERR "Unknown _BCL data, "
909 "please report this to %s\n", TPACPI_MAIL);
910 rc = 0;
911 } else {
912 rc = obj->package.count;
914 } else {
915 return 0;
918 kfree(buffer.pointer);
919 return rc;
922 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
923 u32 lvl, void *context, void **rv)
925 char name[ACPI_PATH_SEGMENT_LENGTH];
926 struct acpi_buffer buffer = { sizeof(name), &name };
928 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
929 !strncmp("_BCL", name, sizeof(name) - 1)) {
930 BUG_ON(!rv || !*rv);
931 **(int **)rv = tpacpi_query_bcl_levels(handle);
932 return AE_CTRL_TERMINATE;
933 } else {
934 return AE_OK;
939 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
941 static int __init tpacpi_check_std_acpi_brightness_support(void)
943 int status;
944 int bcl_levels = 0;
945 void *bcl_ptr = &bcl_levels;
947 if (!vid_handle) {
948 TPACPI_ACPIHANDLE_INIT(vid);
950 if (!vid_handle)
951 return 0;
954 * Search for a _BCL method, and execute it. This is safe on all
955 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
956 * BIOS in ACPI backlight control mode. We do NOT have to care
957 * about calling the _BCL method in an enabled video device, any
958 * will do for our purposes.
961 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
962 tpacpi_acpi_walk_find_bcl, NULL,
963 &bcl_ptr);
965 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
966 tp_features.bright_acpimode = 1;
967 return (bcl_levels - 2);
970 return 0;
973 static int __init tpacpi_new_rfkill(const unsigned int id,
974 struct rfkill **rfk,
975 const enum rfkill_type rfktype,
976 const char *name,
977 const bool set_default,
978 int (*toggle_radio)(void *, enum rfkill_state),
979 int (*get_state)(void *, enum rfkill_state *))
981 int res;
982 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
984 res = get_state(NULL, &initial_state);
985 if (res < 0) {
986 printk(TPACPI_ERR
987 "failed to read initial state for %s, error %d; "
988 "will turn radio off\n", name, res);
989 } else if (set_default) {
990 /* try to set the initial state as the default for the rfkill
991 * type, since we ask the firmware to preserve it across S5 in
992 * NVRAM */
993 rfkill_set_default(rfktype,
994 (initial_state == RFKILL_STATE_UNBLOCKED) ?
995 RFKILL_STATE_UNBLOCKED :
996 RFKILL_STATE_SOFT_BLOCKED);
999 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1000 if (!*rfk) {
1001 printk(TPACPI_ERR
1002 "failed to allocate memory for rfkill class\n");
1003 return -ENOMEM;
1006 (*rfk)->name = name;
1007 (*rfk)->get_state = get_state;
1008 (*rfk)->toggle_radio = toggle_radio;
1009 (*rfk)->state = initial_state;
1011 res = rfkill_register(*rfk);
1012 if (res < 0) {
1013 printk(TPACPI_ERR
1014 "failed to register %s rfkill switch: %d\n",
1015 name, res);
1016 rfkill_free(*rfk);
1017 *rfk = NULL;
1018 return res;
1021 return 0;
1024 /*************************************************************************
1025 * thinkpad-acpi driver attributes
1028 /* interface_version --------------------------------------------------- */
1029 static ssize_t tpacpi_driver_interface_version_show(
1030 struct device_driver *drv,
1031 char *buf)
1033 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1036 static DRIVER_ATTR(interface_version, S_IRUGO,
1037 tpacpi_driver_interface_version_show, NULL);
1039 /* debug_level --------------------------------------------------------- */
1040 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1041 char *buf)
1043 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1046 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1047 const char *buf, size_t count)
1049 unsigned long t;
1051 if (parse_strtoul(buf, 0xffff, &t))
1052 return -EINVAL;
1054 dbg_level = t;
1056 return count;
1059 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1060 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1062 /* version ------------------------------------------------------------- */
1063 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1064 char *buf)
1066 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1067 TPACPI_DESC, TPACPI_VERSION);
1070 static DRIVER_ATTR(version, S_IRUGO,
1071 tpacpi_driver_version_show, NULL);
1073 /* --------------------------------------------------------------------- */
1075 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1077 static void tpacpi_send_radiosw_update(void);
1079 /* wlsw_emulstate ------------------------------------------------------ */
1080 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1081 char *buf)
1083 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1086 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1087 const char *buf, size_t count)
1089 unsigned long t;
1091 if (parse_strtoul(buf, 1, &t))
1092 return -EINVAL;
1094 if (tpacpi_wlsw_emulstate != t) {
1095 tpacpi_wlsw_emulstate = !!t;
1096 tpacpi_send_radiosw_update();
1097 } else
1098 tpacpi_wlsw_emulstate = !!t;
1100 return count;
1103 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1104 tpacpi_driver_wlsw_emulstate_show,
1105 tpacpi_driver_wlsw_emulstate_store);
1107 /* bluetooth_emulstate ------------------------------------------------- */
1108 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1109 struct device_driver *drv,
1110 char *buf)
1112 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1115 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1116 struct device_driver *drv,
1117 const char *buf, size_t count)
1119 unsigned long t;
1121 if (parse_strtoul(buf, 1, &t))
1122 return -EINVAL;
1124 tpacpi_bluetooth_emulstate = !!t;
1126 return count;
1129 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1130 tpacpi_driver_bluetooth_emulstate_show,
1131 tpacpi_driver_bluetooth_emulstate_store);
1133 /* wwan_emulstate ------------------------------------------------- */
1134 static ssize_t tpacpi_driver_wwan_emulstate_show(
1135 struct device_driver *drv,
1136 char *buf)
1138 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1141 static ssize_t tpacpi_driver_wwan_emulstate_store(
1142 struct device_driver *drv,
1143 const char *buf, size_t count)
1145 unsigned long t;
1147 if (parse_strtoul(buf, 1, &t))
1148 return -EINVAL;
1150 tpacpi_wwan_emulstate = !!t;
1152 return count;
1155 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1156 tpacpi_driver_wwan_emulstate_show,
1157 tpacpi_driver_wwan_emulstate_store);
1159 /* uwb_emulstate ------------------------------------------------- */
1160 static ssize_t tpacpi_driver_uwb_emulstate_show(
1161 struct device_driver *drv,
1162 char *buf)
1164 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1167 static ssize_t tpacpi_driver_uwb_emulstate_store(
1168 struct device_driver *drv,
1169 const char *buf, size_t count)
1171 unsigned long t;
1173 if (parse_strtoul(buf, 1, &t))
1174 return -EINVAL;
1176 tpacpi_uwb_emulstate = !!t;
1178 return count;
1181 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1182 tpacpi_driver_uwb_emulstate_show,
1183 tpacpi_driver_uwb_emulstate_store);
1184 #endif
1186 /* --------------------------------------------------------------------- */
1188 static struct driver_attribute *tpacpi_driver_attributes[] = {
1189 &driver_attr_debug_level, &driver_attr_version,
1190 &driver_attr_interface_version,
1193 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1195 int i, res;
1197 i = 0;
1198 res = 0;
1199 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1200 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1201 i++;
1204 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1205 if (!res && dbg_wlswemul)
1206 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1207 if (!res && dbg_bluetoothemul)
1208 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1209 if (!res && dbg_wwanemul)
1210 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1211 if (!res && dbg_uwbemul)
1212 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1213 #endif
1215 return res;
1218 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1220 int i;
1222 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1223 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1225 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1226 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1227 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1228 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1229 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1230 #endif
1233 /****************************************************************************
1234 ****************************************************************************
1236 * Subdrivers
1238 ****************************************************************************
1239 ****************************************************************************/
1241 /*************************************************************************
1242 * thinkpad-acpi init subdriver
1245 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1247 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1248 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1250 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1251 (thinkpad_id.bios_version_str) ?
1252 thinkpad_id.bios_version_str : "unknown",
1253 (thinkpad_id.ec_version_str) ?
1254 thinkpad_id.ec_version_str : "unknown");
1256 if (thinkpad_id.vendor && thinkpad_id.model_str)
1257 printk(TPACPI_INFO "%s %s, model %s\n",
1258 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1259 "IBM" : ((thinkpad_id.vendor ==
1260 PCI_VENDOR_ID_LENOVO) ?
1261 "Lenovo" : "Unknown vendor"),
1262 thinkpad_id.model_str,
1263 (thinkpad_id.nummodel_str) ?
1264 thinkpad_id.nummodel_str : "unknown");
1266 return 0;
1269 static int thinkpad_acpi_driver_read(char *p)
1271 int len = 0;
1273 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1274 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1276 return len;
1279 static struct ibm_struct thinkpad_acpi_driver_data = {
1280 .name = "driver",
1281 .read = thinkpad_acpi_driver_read,
1284 /*************************************************************************
1285 * Hotkey subdriver
1288 enum { /* hot key scan codes (derived from ACPI DSDT) */
1289 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1290 TP_ACPI_HOTKEYSCAN_FNF2,
1291 TP_ACPI_HOTKEYSCAN_FNF3,
1292 TP_ACPI_HOTKEYSCAN_FNF4,
1293 TP_ACPI_HOTKEYSCAN_FNF5,
1294 TP_ACPI_HOTKEYSCAN_FNF6,
1295 TP_ACPI_HOTKEYSCAN_FNF7,
1296 TP_ACPI_HOTKEYSCAN_FNF8,
1297 TP_ACPI_HOTKEYSCAN_FNF9,
1298 TP_ACPI_HOTKEYSCAN_FNF10,
1299 TP_ACPI_HOTKEYSCAN_FNF11,
1300 TP_ACPI_HOTKEYSCAN_FNF12,
1301 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1302 TP_ACPI_HOTKEYSCAN_FNINSERT,
1303 TP_ACPI_HOTKEYSCAN_FNDELETE,
1304 TP_ACPI_HOTKEYSCAN_FNHOME,
1305 TP_ACPI_HOTKEYSCAN_FNEND,
1306 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1307 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1308 TP_ACPI_HOTKEYSCAN_FNSPACE,
1309 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1310 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1311 TP_ACPI_HOTKEYSCAN_MUTE,
1312 TP_ACPI_HOTKEYSCAN_THINKPAD,
1315 enum { /* Keys available through NVRAM polling */
1316 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1317 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1320 enum { /* Positions of some of the keys in hotkey masks */
1321 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1322 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1323 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1324 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1325 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1326 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1327 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1328 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1329 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1330 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1331 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1334 enum { /* NVRAM to ACPI HKEY group map */
1335 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1336 TP_ACPI_HKEY_ZOOM_MASK |
1337 TP_ACPI_HKEY_DISPSWTCH_MASK |
1338 TP_ACPI_HKEY_HIBERNATE_MASK,
1339 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1340 TP_ACPI_HKEY_BRGHTDWN_MASK,
1341 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1342 TP_ACPI_HKEY_VOLDWN_MASK |
1343 TP_ACPI_HKEY_MUTE_MASK,
1346 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1347 struct tp_nvram_state {
1348 u16 thinkpad_toggle:1;
1349 u16 zoom_toggle:1;
1350 u16 display_toggle:1;
1351 u16 thinklight_toggle:1;
1352 u16 hibernate_toggle:1;
1353 u16 displayexp_toggle:1;
1354 u16 display_state:1;
1355 u16 brightness_toggle:1;
1356 u16 volume_toggle:1;
1357 u16 mute:1;
1359 u8 brightness_level;
1360 u8 volume_level;
1363 static struct task_struct *tpacpi_hotkey_task;
1364 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1365 static int hotkey_poll_freq = 10; /* Hz */
1366 static struct mutex hotkey_thread_mutex;
1367 static struct mutex hotkey_thread_data_mutex;
1368 static unsigned int hotkey_config_change;
1370 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1372 #define hotkey_source_mask 0U
1374 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1376 static struct mutex hotkey_mutex;
1378 static enum { /* Reasons for waking up */
1379 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1380 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1381 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1382 } hotkey_wakeup_reason;
1384 static int hotkey_autosleep_ack;
1386 static int hotkey_orig_status;
1387 static u32 hotkey_orig_mask;
1388 static u32 hotkey_all_mask;
1389 static u32 hotkey_reserved_mask;
1390 static u32 hotkey_mask;
1392 static unsigned int hotkey_report_mode;
1394 static u16 *hotkey_keycode_map;
1396 static struct attribute_set *hotkey_dev_attributes;
1398 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1399 #define HOTKEY_CONFIG_CRITICAL_START \
1400 do { \
1401 mutex_lock(&hotkey_thread_data_mutex); \
1402 hotkey_config_change++; \
1403 } while (0);
1404 #define HOTKEY_CONFIG_CRITICAL_END \
1405 mutex_unlock(&hotkey_thread_data_mutex);
1406 #else
1407 #define HOTKEY_CONFIG_CRITICAL_START
1408 #define HOTKEY_CONFIG_CRITICAL_END
1409 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1411 /* HKEY.MHKG() return bits */
1412 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1414 static int hotkey_get_wlsw(int *status)
1416 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1417 if (dbg_wlswemul) {
1418 *status = !!tpacpi_wlsw_emulstate;
1419 return 0;
1421 #endif
1422 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1423 return -EIO;
1424 return 0;
1427 static int hotkey_get_tablet_mode(int *status)
1429 int s;
1431 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1432 return -EIO;
1434 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1435 return 0;
1439 * Call with hotkey_mutex held
1441 static int hotkey_mask_get(void)
1443 u32 m = 0;
1445 if (tp_features.hotkey_mask) {
1446 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1447 return -EIO;
1449 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1451 return 0;
1455 * Call with hotkey_mutex held
1457 static int hotkey_mask_set(u32 mask)
1459 int i;
1460 int rc = 0;
1462 if (tp_features.hotkey_mask) {
1463 if (!tp_warned.hotkey_mask_ff &&
1464 (mask == 0xffff || mask == 0xffffff ||
1465 mask == 0xffffffff)) {
1466 tp_warned.hotkey_mask_ff = 1;
1467 printk(TPACPI_NOTICE
1468 "setting the hotkey mask to 0x%08x is likely "
1469 "not the best way to go about it\n", mask);
1470 printk(TPACPI_NOTICE
1471 "please consider using the driver defaults, "
1472 "and refer to up-to-date thinkpad-acpi "
1473 "documentation\n");
1476 HOTKEY_CONFIG_CRITICAL_START
1477 for (i = 0; i < 32; i++) {
1478 u32 m = 1 << i;
1479 /* enable in firmware mask only keys not in NVRAM
1480 * mode, but enable the key in the cached hotkey_mask
1481 * regardless of mode, or the key will end up
1482 * disabled by hotkey_mask_get() */
1483 if (!acpi_evalf(hkey_handle,
1484 NULL, "MHKM", "vdd", i + 1,
1485 !!((mask & ~hotkey_source_mask) & m))) {
1486 rc = -EIO;
1487 break;
1488 } else {
1489 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1492 HOTKEY_CONFIG_CRITICAL_END
1494 /* hotkey_mask_get must be called unconditionally below */
1495 if (!hotkey_mask_get() && !rc &&
1496 (hotkey_mask & ~hotkey_source_mask) !=
1497 (mask & ~hotkey_source_mask)) {
1498 printk(TPACPI_NOTICE
1499 "requested hot key mask 0x%08x, but "
1500 "firmware forced it to 0x%08x\n",
1501 mask, hotkey_mask);
1503 } else {
1504 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1505 HOTKEY_CONFIG_CRITICAL_START
1506 hotkey_mask = mask & hotkey_source_mask;
1507 HOTKEY_CONFIG_CRITICAL_END
1508 hotkey_mask_get();
1509 if (hotkey_mask != mask) {
1510 printk(TPACPI_NOTICE
1511 "requested hot key mask 0x%08x, "
1512 "forced to 0x%08x (NVRAM poll mask is "
1513 "0x%08x): no firmware mask support\n",
1514 mask, hotkey_mask, hotkey_source_mask);
1516 #else
1517 hotkey_mask_get();
1518 rc = -ENXIO;
1519 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1522 return rc;
1525 static int hotkey_status_get(int *status)
1527 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1528 return -EIO;
1530 return 0;
1533 static int hotkey_status_set(int status)
1535 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1536 return -EIO;
1538 return 0;
1541 static void tpacpi_input_send_tabletsw(void)
1543 int state;
1545 if (tp_features.hotkey_tablet &&
1546 !hotkey_get_tablet_mode(&state)) {
1547 mutex_lock(&tpacpi_inputdev_send_mutex);
1549 input_report_switch(tpacpi_inputdev,
1550 SW_TABLET_MODE, !!state);
1551 input_sync(tpacpi_inputdev);
1553 mutex_unlock(&tpacpi_inputdev_send_mutex);
1557 static void tpacpi_input_send_key(unsigned int scancode)
1559 unsigned int keycode;
1561 keycode = hotkey_keycode_map[scancode];
1563 if (keycode != KEY_RESERVED) {
1564 mutex_lock(&tpacpi_inputdev_send_mutex);
1566 input_report_key(tpacpi_inputdev, keycode, 1);
1567 if (keycode == KEY_UNKNOWN)
1568 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1569 scancode);
1570 input_sync(tpacpi_inputdev);
1572 input_report_key(tpacpi_inputdev, keycode, 0);
1573 if (keycode == KEY_UNKNOWN)
1574 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1575 scancode);
1576 input_sync(tpacpi_inputdev);
1578 mutex_unlock(&tpacpi_inputdev_send_mutex);
1582 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1583 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1585 static void tpacpi_hotkey_send_key(unsigned int scancode)
1587 tpacpi_input_send_key(scancode);
1588 if (hotkey_report_mode < 2) {
1589 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1590 0x80, 0x1001 + scancode);
1594 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1596 u8 d;
1598 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1599 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1600 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1601 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1602 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1603 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1605 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1606 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1607 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1609 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1610 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1611 n->displayexp_toggle =
1612 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1614 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1615 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1616 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1617 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1618 n->brightness_toggle =
1619 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1621 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1622 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1623 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1624 >> TP_NVRAM_POS_LEVEL_VOLUME;
1625 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1626 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1630 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1631 do { \
1632 if ((mask & (1 << __scancode)) && \
1633 oldn->__member != newn->__member) \
1634 tpacpi_hotkey_send_key(__scancode); \
1635 } while (0)
1637 #define TPACPI_MAY_SEND_KEY(__scancode) \
1638 do { if (mask & (1 << __scancode)) \
1639 tpacpi_hotkey_send_key(__scancode); } while (0)
1641 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1642 struct tp_nvram_state *newn,
1643 u32 mask)
1645 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1646 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1647 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1648 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1650 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1652 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1654 /* handle volume */
1655 if (oldn->volume_toggle != newn->volume_toggle) {
1656 if (oldn->mute != newn->mute) {
1657 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1659 if (oldn->volume_level > newn->volume_level) {
1660 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1661 } else if (oldn->volume_level < newn->volume_level) {
1662 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1663 } else if (oldn->mute == newn->mute) {
1664 /* repeated key presses that didn't change state */
1665 if (newn->mute) {
1666 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1667 } else if (newn->volume_level != 0) {
1668 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1669 } else {
1670 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1675 /* handle brightness */
1676 if (oldn->brightness_toggle != newn->brightness_toggle) {
1677 if (oldn->brightness_level < newn->brightness_level) {
1678 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1679 } else if (oldn->brightness_level > newn->brightness_level) {
1680 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1681 } else {
1682 /* repeated key presses that didn't change state */
1683 if (newn->brightness_level != 0) {
1684 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1685 } else {
1686 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1692 #undef TPACPI_COMPARE_KEY
1693 #undef TPACPI_MAY_SEND_KEY
1695 static int hotkey_kthread(void *data)
1697 struct tp_nvram_state s[2];
1698 u32 mask;
1699 unsigned int si, so;
1700 unsigned long t;
1701 unsigned int change_detector, must_reset;
1703 mutex_lock(&hotkey_thread_mutex);
1705 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1706 goto exit;
1708 set_freezable();
1710 so = 0;
1711 si = 1;
1712 t = 0;
1714 /* Initial state for compares */
1715 mutex_lock(&hotkey_thread_data_mutex);
1716 change_detector = hotkey_config_change;
1717 mask = hotkey_source_mask & hotkey_mask;
1718 mutex_unlock(&hotkey_thread_data_mutex);
1719 hotkey_read_nvram(&s[so], mask);
1721 while (!kthread_should_stop() && hotkey_poll_freq) {
1722 if (t == 0)
1723 t = 1000/hotkey_poll_freq;
1724 t = msleep_interruptible(t);
1725 if (unlikely(kthread_should_stop()))
1726 break;
1727 must_reset = try_to_freeze();
1728 if (t > 0 && !must_reset)
1729 continue;
1731 mutex_lock(&hotkey_thread_data_mutex);
1732 if (must_reset || hotkey_config_change != change_detector) {
1733 /* forget old state on thaw or config change */
1734 si = so;
1735 t = 0;
1736 change_detector = hotkey_config_change;
1738 mask = hotkey_source_mask & hotkey_mask;
1739 mutex_unlock(&hotkey_thread_data_mutex);
1741 if (likely(mask)) {
1742 hotkey_read_nvram(&s[si], mask);
1743 if (likely(si != so)) {
1744 hotkey_compare_and_issue_event(&s[so], &s[si],
1745 mask);
1749 so = si;
1750 si ^= 1;
1753 exit:
1754 mutex_unlock(&hotkey_thread_mutex);
1755 return 0;
1758 static void hotkey_poll_stop_sync(void)
1760 if (tpacpi_hotkey_task) {
1761 if (frozen(tpacpi_hotkey_task) ||
1762 freezing(tpacpi_hotkey_task))
1763 thaw_process(tpacpi_hotkey_task);
1765 kthread_stop(tpacpi_hotkey_task);
1766 tpacpi_hotkey_task = NULL;
1767 mutex_lock(&hotkey_thread_mutex);
1768 /* at this point, the thread did exit */
1769 mutex_unlock(&hotkey_thread_mutex);
1773 /* call with hotkey_mutex held */
1774 static void hotkey_poll_setup(int may_warn)
1776 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1777 hotkey_poll_freq > 0 &&
1778 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1779 if (!tpacpi_hotkey_task) {
1780 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1781 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1782 if (IS_ERR(tpacpi_hotkey_task)) {
1783 tpacpi_hotkey_task = NULL;
1784 printk(TPACPI_ERR
1785 "could not create kernel thread "
1786 "for hotkey polling\n");
1789 } else {
1790 hotkey_poll_stop_sync();
1791 if (may_warn &&
1792 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1793 printk(TPACPI_NOTICE
1794 "hot keys 0x%08x require polling, "
1795 "which is currently disabled\n",
1796 hotkey_source_mask);
1801 static void hotkey_poll_setup_safe(int may_warn)
1803 mutex_lock(&hotkey_mutex);
1804 hotkey_poll_setup(may_warn);
1805 mutex_unlock(&hotkey_mutex);
1808 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1810 static void hotkey_poll_setup_safe(int __unused)
1814 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1816 static int hotkey_inputdev_open(struct input_dev *dev)
1818 switch (tpacpi_lifecycle) {
1819 case TPACPI_LIFE_INIT:
1821 * hotkey_init will call hotkey_poll_setup_safe
1822 * at the appropriate moment
1824 return 0;
1825 case TPACPI_LIFE_EXITING:
1826 return -EBUSY;
1827 case TPACPI_LIFE_RUNNING:
1828 hotkey_poll_setup_safe(0);
1829 return 0;
1832 /* Should only happen if tpacpi_lifecycle is corrupt */
1833 BUG();
1834 return -EBUSY;
1837 static void hotkey_inputdev_close(struct input_dev *dev)
1839 /* disable hotkey polling when possible */
1840 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1841 hotkey_poll_setup_safe(0);
1844 /* sysfs hotkey enable ------------------------------------------------- */
1845 static ssize_t hotkey_enable_show(struct device *dev,
1846 struct device_attribute *attr,
1847 char *buf)
1849 int res, status;
1851 res = hotkey_status_get(&status);
1852 if (res)
1853 return res;
1855 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1858 static ssize_t hotkey_enable_store(struct device *dev,
1859 struct device_attribute *attr,
1860 const char *buf, size_t count)
1862 unsigned long t;
1863 int res;
1865 if (parse_strtoul(buf, 1, &t))
1866 return -EINVAL;
1868 res = hotkey_status_set(t);
1870 return (res) ? res : count;
1873 static struct device_attribute dev_attr_hotkey_enable =
1874 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1875 hotkey_enable_show, hotkey_enable_store);
1877 /* sysfs hotkey mask --------------------------------------------------- */
1878 static ssize_t hotkey_mask_show(struct device *dev,
1879 struct device_attribute *attr,
1880 char *buf)
1882 int res;
1884 if (mutex_lock_killable(&hotkey_mutex))
1885 return -ERESTARTSYS;
1886 res = hotkey_mask_get();
1887 mutex_unlock(&hotkey_mutex);
1889 return (res)?
1890 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1893 static ssize_t hotkey_mask_store(struct device *dev,
1894 struct device_attribute *attr,
1895 const char *buf, size_t count)
1897 unsigned long t;
1898 int res;
1900 if (parse_strtoul(buf, 0xffffffffUL, &t))
1901 return -EINVAL;
1903 if (mutex_lock_killable(&hotkey_mutex))
1904 return -ERESTARTSYS;
1906 res = hotkey_mask_set(t);
1908 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1909 hotkey_poll_setup(1);
1910 #endif
1912 mutex_unlock(&hotkey_mutex);
1914 return (res) ? res : count;
1917 static struct device_attribute dev_attr_hotkey_mask =
1918 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1919 hotkey_mask_show, hotkey_mask_store);
1921 /* sysfs hotkey bios_enabled ------------------------------------------- */
1922 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1923 struct device_attribute *attr,
1924 char *buf)
1926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1929 static struct device_attribute dev_attr_hotkey_bios_enabled =
1930 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1932 /* sysfs hotkey bios_mask ---------------------------------------------- */
1933 static ssize_t hotkey_bios_mask_show(struct device *dev,
1934 struct device_attribute *attr,
1935 char *buf)
1937 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1940 static struct device_attribute dev_attr_hotkey_bios_mask =
1941 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1943 /* sysfs hotkey all_mask ----------------------------------------------- */
1944 static ssize_t hotkey_all_mask_show(struct device *dev,
1945 struct device_attribute *attr,
1946 char *buf)
1948 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1949 hotkey_all_mask | hotkey_source_mask);
1952 static struct device_attribute dev_attr_hotkey_all_mask =
1953 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1955 /* sysfs hotkey recommended_mask --------------------------------------- */
1956 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1957 struct device_attribute *attr,
1958 char *buf)
1960 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1961 (hotkey_all_mask | hotkey_source_mask)
1962 & ~hotkey_reserved_mask);
1965 static struct device_attribute dev_attr_hotkey_recommended_mask =
1966 __ATTR(hotkey_recommended_mask, S_IRUGO,
1967 hotkey_recommended_mask_show, NULL);
1969 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1971 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1972 static ssize_t hotkey_source_mask_show(struct device *dev,
1973 struct device_attribute *attr,
1974 char *buf)
1976 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1979 static ssize_t hotkey_source_mask_store(struct device *dev,
1980 struct device_attribute *attr,
1981 const char *buf, size_t count)
1983 unsigned long t;
1985 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1986 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1987 return -EINVAL;
1989 if (mutex_lock_killable(&hotkey_mutex))
1990 return -ERESTARTSYS;
1992 HOTKEY_CONFIG_CRITICAL_START
1993 hotkey_source_mask = t;
1994 HOTKEY_CONFIG_CRITICAL_END
1996 hotkey_poll_setup(1);
1998 mutex_unlock(&hotkey_mutex);
2000 return count;
2003 static struct device_attribute dev_attr_hotkey_source_mask =
2004 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2005 hotkey_source_mask_show, hotkey_source_mask_store);
2007 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2008 static ssize_t hotkey_poll_freq_show(struct device *dev,
2009 struct device_attribute *attr,
2010 char *buf)
2012 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2015 static ssize_t hotkey_poll_freq_store(struct device *dev,
2016 struct device_attribute *attr,
2017 const char *buf, size_t count)
2019 unsigned long t;
2021 if (parse_strtoul(buf, 25, &t))
2022 return -EINVAL;
2024 if (mutex_lock_killable(&hotkey_mutex))
2025 return -ERESTARTSYS;
2027 hotkey_poll_freq = t;
2029 hotkey_poll_setup(1);
2030 mutex_unlock(&hotkey_mutex);
2032 return count;
2035 static struct device_attribute dev_attr_hotkey_poll_freq =
2036 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2037 hotkey_poll_freq_show, hotkey_poll_freq_store);
2039 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2041 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2042 static ssize_t hotkey_radio_sw_show(struct device *dev,
2043 struct device_attribute *attr,
2044 char *buf)
2046 int res, s;
2047 res = hotkey_get_wlsw(&s);
2048 if (res < 0)
2049 return res;
2051 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2054 static struct device_attribute dev_attr_hotkey_radio_sw =
2055 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2057 static void hotkey_radio_sw_notify_change(void)
2059 if (tp_features.hotkey_wlsw)
2060 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2061 "hotkey_radio_sw");
2064 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2065 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2066 struct device_attribute *attr,
2067 char *buf)
2069 int res, s;
2070 res = hotkey_get_tablet_mode(&s);
2071 if (res < 0)
2072 return res;
2074 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2077 static struct device_attribute dev_attr_hotkey_tablet_mode =
2078 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2080 static void hotkey_tablet_mode_notify_change(void)
2082 if (tp_features.hotkey_tablet)
2083 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2084 "hotkey_tablet_mode");
2087 /* sysfs hotkey report_mode -------------------------------------------- */
2088 static ssize_t hotkey_report_mode_show(struct device *dev,
2089 struct device_attribute *attr,
2090 char *buf)
2092 return snprintf(buf, PAGE_SIZE, "%d\n",
2093 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2096 static struct device_attribute dev_attr_hotkey_report_mode =
2097 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2099 /* sysfs wakeup reason (pollable) -------------------------------------- */
2100 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2101 struct device_attribute *attr,
2102 char *buf)
2104 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2107 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2108 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2110 static void hotkey_wakeup_reason_notify_change(void)
2112 if (tp_features.hotkey_mask)
2113 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2114 "wakeup_reason");
2117 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2118 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2119 struct device_attribute *attr,
2120 char *buf)
2122 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2125 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2126 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2127 hotkey_wakeup_hotunplug_complete_show, NULL);
2129 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2131 if (tp_features.hotkey_mask)
2132 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2133 "wakeup_hotunplug_complete");
2136 /* --------------------------------------------------------------------- */
2138 static struct attribute *hotkey_attributes[] __initdata = {
2139 &dev_attr_hotkey_enable.attr,
2140 &dev_attr_hotkey_bios_enabled.attr,
2141 &dev_attr_hotkey_report_mode.attr,
2142 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2143 &dev_attr_hotkey_mask.attr,
2144 &dev_attr_hotkey_all_mask.attr,
2145 &dev_attr_hotkey_recommended_mask.attr,
2146 &dev_attr_hotkey_source_mask.attr,
2147 &dev_attr_hotkey_poll_freq.attr,
2148 #endif
2151 static struct attribute *hotkey_mask_attributes[] __initdata = {
2152 &dev_attr_hotkey_bios_mask.attr,
2153 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2154 &dev_attr_hotkey_mask.attr,
2155 &dev_attr_hotkey_all_mask.attr,
2156 &dev_attr_hotkey_recommended_mask.attr,
2157 #endif
2158 &dev_attr_hotkey_wakeup_reason.attr,
2159 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2162 static void bluetooth_update_rfk(void);
2163 static void wan_update_rfk(void);
2164 static void uwb_update_rfk(void);
2165 static void tpacpi_send_radiosw_update(void)
2167 int wlsw;
2169 /* Sync these BEFORE sending any rfkill events */
2170 if (tp_features.bluetooth)
2171 bluetooth_update_rfk();
2172 if (tp_features.wan)
2173 wan_update_rfk();
2174 if (tp_features.uwb)
2175 uwb_update_rfk();
2177 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2178 mutex_lock(&tpacpi_inputdev_send_mutex);
2180 input_report_switch(tpacpi_inputdev,
2181 SW_RFKILL_ALL, !!wlsw);
2182 input_sync(tpacpi_inputdev);
2184 mutex_unlock(&tpacpi_inputdev_send_mutex);
2186 hotkey_radio_sw_notify_change();
2189 static void hotkey_exit(void)
2191 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2192 hotkey_poll_stop_sync();
2193 #endif
2195 if (hotkey_dev_attributes)
2196 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2198 kfree(hotkey_keycode_map);
2200 if (tp_features.hotkey) {
2201 dbg_printk(TPACPI_DBG_EXIT,
2202 "restoring original hot key mask\n");
2203 /* no short-circuit boolean operator below! */
2204 if ((hotkey_mask_set(hotkey_orig_mask) |
2205 hotkey_status_set(hotkey_orig_status)) != 0)
2206 printk(TPACPI_ERR
2207 "failed to restore hot key mask "
2208 "to BIOS defaults\n");
2212 static int __init hotkey_init(struct ibm_init_struct *iibm)
2214 /* Requirements for changing the default keymaps:
2216 * 1. Many of the keys are mapped to KEY_RESERVED for very
2217 * good reasons. Do not change them unless you have deep
2218 * knowledge on the IBM and Lenovo ThinkPad firmware for
2219 * the various ThinkPad models. The driver behaves
2220 * differently for KEY_RESERVED: such keys have their
2221 * hot key mask *unset* in mask_recommended, and also
2222 * in the initial hot key mask programmed into the
2223 * firmware at driver load time, which means the firm-
2224 * ware may react very differently if you change them to
2225 * something else;
2227 * 2. You must be subscribed to the linux-thinkpad and
2228 * ibm-acpi-devel mailing lists, and you should read the
2229 * list archives since 2007 if you want to change the
2230 * keymaps. This requirement exists so that you will
2231 * know the past history of problems with the thinkpad-
2232 * acpi driver keymaps, and also that you will be
2233 * listening to any bug reports;
2235 * 3. Do not send thinkpad-acpi specific patches directly to
2236 * for merging, *ever*. Send them to the linux-acpi
2237 * mailinglist for comments. Merging is to be done only
2238 * through acpi-test and the ACPI maintainer.
2240 * If the above is too much to ask, don't change the keymap.
2241 * Ask the thinkpad-acpi maintainer to do it, instead.
2243 static u16 ibm_keycode_map[] __initdata = {
2244 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2245 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2246 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2247 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2249 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2250 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2251 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2252 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2254 /* brightness: firmware always reacts to them, unless
2255 * X.org did some tricks in the radeon BIOS scratch
2256 * registers of *some* models */
2257 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2258 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2260 /* Thinklight: firmware always react to it */
2261 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2263 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2264 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2266 /* Volume: firmware always react to it and reprograms
2267 * the built-in *extra* mixer. Never map it to control
2268 * another mixer by default. */
2269 KEY_RESERVED, /* 0x14: VOLUME UP */
2270 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2271 KEY_RESERVED, /* 0x16: MUTE */
2273 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2275 /* (assignments unknown, please report if found) */
2276 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2277 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2279 static u16 lenovo_keycode_map[] __initdata = {
2280 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2281 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2282 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2283 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2285 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2286 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2287 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2288 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2290 /* These either have to go through ACPI video, or
2291 * act like in the IBM ThinkPads, so don't ever
2292 * enable them by default */
2293 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2294 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2296 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2298 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2299 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2301 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2302 * react to it and reprograms the built-in *extra* mixer.
2303 * Never map it to control another mixer by default.
2305 * T60?, T61, R60?, R61: firmware and EC tries to send
2306 * these over the regular keyboard, so these are no-ops,
2307 * but there are still weird bugs re. MUTE, so do not
2308 * change unless you get test reports from all Lenovo
2309 * models. May cause the BIOS to interfere with the
2310 * HDA mixer.
2312 KEY_RESERVED, /* 0x14: VOLUME UP */
2313 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2314 KEY_RESERVED, /* 0x16: MUTE */
2316 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2318 /* (assignments unknown, please report if found) */
2319 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2320 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2323 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2324 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2325 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2327 int res, i;
2328 int status;
2329 int hkeyv;
2331 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2333 BUG_ON(!tpacpi_inputdev);
2334 BUG_ON(tpacpi_inputdev->open != NULL ||
2335 tpacpi_inputdev->close != NULL);
2337 TPACPI_ACPIHANDLE_INIT(hkey);
2338 mutex_init(&hotkey_mutex);
2340 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2341 mutex_init(&hotkey_thread_mutex);
2342 mutex_init(&hotkey_thread_data_mutex);
2343 #endif
2345 /* hotkey not supported on 570 */
2346 tp_features.hotkey = hkey_handle != NULL;
2348 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2349 str_supported(tp_features.hotkey));
2351 if (!tp_features.hotkey)
2352 return 1;
2354 tpacpi_disable_brightness_delay();
2356 hotkey_dev_attributes = create_attr_set(13, NULL);
2357 if (!hotkey_dev_attributes)
2358 return -ENOMEM;
2359 res = add_many_to_attr_set(hotkey_dev_attributes,
2360 hotkey_attributes,
2361 ARRAY_SIZE(hotkey_attributes));
2362 if (res)
2363 goto err_exit;
2365 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2366 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2367 for HKEY interface version 0x100 */
2368 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2369 if ((hkeyv >> 8) != 1) {
2370 printk(TPACPI_ERR "unknown version of the "
2371 "HKEY interface: 0x%x\n", hkeyv);
2372 printk(TPACPI_ERR "please report this to %s\n",
2373 TPACPI_MAIL);
2374 } else {
2376 * MHKV 0x100 in A31, R40, R40e,
2377 * T4x, X31, and later
2379 tp_features.hotkey_mask = 1;
2383 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2384 str_supported(tp_features.hotkey_mask));
2386 if (tp_features.hotkey_mask) {
2387 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2388 "MHKA", "qd")) {
2389 printk(TPACPI_ERR
2390 "missing MHKA handler, "
2391 "please report this to %s\n",
2392 TPACPI_MAIL);
2393 /* FN+F12, FN+F4, FN+F3 */
2394 hotkey_all_mask = 0x080cU;
2398 /* hotkey_source_mask *must* be zero for
2399 * the first hotkey_mask_get */
2400 res = hotkey_status_get(&hotkey_orig_status);
2401 if (res)
2402 goto err_exit;
2404 if (tp_features.hotkey_mask) {
2405 res = hotkey_mask_get();
2406 if (res)
2407 goto err_exit;
2409 hotkey_orig_mask = hotkey_mask;
2410 res = add_many_to_attr_set(
2411 hotkey_dev_attributes,
2412 hotkey_mask_attributes,
2413 ARRAY_SIZE(hotkey_mask_attributes));
2414 if (res)
2415 goto err_exit;
2418 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2419 if (tp_features.hotkey_mask) {
2420 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2421 & ~hotkey_all_mask;
2422 } else {
2423 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2426 vdbg_printk(TPACPI_DBG_INIT,
2427 "hotkey source mask 0x%08x, polling freq %d\n",
2428 hotkey_source_mask, hotkey_poll_freq);
2429 #endif
2431 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2432 if (dbg_wlswemul) {
2433 tp_features.hotkey_wlsw = 1;
2434 printk(TPACPI_INFO
2435 "radio switch emulation enabled\n");
2436 } else
2437 #endif
2438 /* Not all thinkpads have a hardware radio switch */
2439 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2440 tp_features.hotkey_wlsw = 1;
2441 printk(TPACPI_INFO
2442 "radio switch found; radios are %s\n",
2443 enabled(status, 0));
2445 if (tp_features.hotkey_wlsw)
2446 res = add_to_attr_set(hotkey_dev_attributes,
2447 &dev_attr_hotkey_radio_sw.attr);
2449 /* For X41t, X60t, X61t Tablets... */
2450 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2451 tp_features.hotkey_tablet = 1;
2452 printk(TPACPI_INFO
2453 "possible tablet mode switch found; "
2454 "ThinkPad in %s mode\n",
2455 (status & TP_HOTKEY_TABLET_MASK)?
2456 "tablet" : "laptop");
2457 res = add_to_attr_set(hotkey_dev_attributes,
2458 &dev_attr_hotkey_tablet_mode.attr);
2461 if (!res)
2462 res = register_attr_set_with_sysfs(
2463 hotkey_dev_attributes,
2464 &tpacpi_pdev->dev.kobj);
2465 if (res)
2466 goto err_exit;
2468 /* Set up key map */
2470 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2471 GFP_KERNEL);
2472 if (!hotkey_keycode_map) {
2473 printk(TPACPI_ERR
2474 "failed to allocate memory for key map\n");
2475 res = -ENOMEM;
2476 goto err_exit;
2479 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2480 dbg_printk(TPACPI_DBG_INIT,
2481 "using Lenovo default hot key map\n");
2482 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2483 TPACPI_HOTKEY_MAP_SIZE);
2484 } else {
2485 dbg_printk(TPACPI_DBG_INIT,
2486 "using IBM default hot key map\n");
2487 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2488 TPACPI_HOTKEY_MAP_SIZE);
2491 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2492 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2493 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2494 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2495 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2496 tpacpi_inputdev->keycode = hotkey_keycode_map;
2497 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2498 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2499 set_bit(hotkey_keycode_map[i],
2500 tpacpi_inputdev->keybit);
2501 } else {
2502 if (i < sizeof(hotkey_reserved_mask)*8)
2503 hotkey_reserved_mask |= 1 << i;
2507 if (tp_features.hotkey_wlsw) {
2508 set_bit(EV_SW, tpacpi_inputdev->evbit);
2509 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2511 if (tp_features.hotkey_tablet) {
2512 set_bit(EV_SW, tpacpi_inputdev->evbit);
2513 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2516 /* Do not issue duplicate brightness change events to
2517 * userspace */
2518 if (!tp_features.bright_acpimode)
2519 /* update bright_acpimode... */
2520 tpacpi_check_std_acpi_brightness_support();
2522 if (tp_features.bright_acpimode) {
2523 printk(TPACPI_INFO
2524 "This ThinkPad has standard ACPI backlight "
2525 "brightness control, supported by the ACPI "
2526 "video driver\n");
2527 printk(TPACPI_NOTICE
2528 "Disabling thinkpad-acpi brightness events "
2529 "by default...\n");
2531 /* The hotkey_reserved_mask change below is not
2532 * necessary while the keys are at KEY_RESERVED in the
2533 * default map, but better safe than sorry, leave it
2534 * here as a marker of what we have to do, especially
2535 * when we finally become able to set this at runtime
2536 * on response to X.org requests */
2537 hotkey_reserved_mask |=
2538 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2539 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2542 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2543 res = hotkey_status_set(1);
2544 if (res) {
2545 hotkey_exit();
2546 return res;
2548 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2549 & ~hotkey_reserved_mask)
2550 | hotkey_orig_mask);
2551 if (res < 0 && res != -ENXIO) {
2552 hotkey_exit();
2553 return res;
2556 dbg_printk(TPACPI_DBG_INIT,
2557 "legacy hot key reporting over procfs %s\n",
2558 (hotkey_report_mode < 2) ?
2559 "enabled" : "disabled");
2561 tpacpi_inputdev->open = &hotkey_inputdev_open;
2562 tpacpi_inputdev->close = &hotkey_inputdev_close;
2564 hotkey_poll_setup_safe(1);
2565 tpacpi_send_radiosw_update();
2566 tpacpi_input_send_tabletsw();
2568 return 0;
2570 err_exit:
2571 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2572 hotkey_dev_attributes = NULL;
2574 return (res < 0)? res : 1;
2577 static bool hotkey_notify_hotkey(const u32 hkey,
2578 bool *send_acpi_ev,
2579 bool *ignore_acpi_ev)
2581 /* 0x1000-0x1FFF: key presses */
2582 unsigned int scancode = hkey & 0xfff;
2583 *send_acpi_ev = true;
2584 *ignore_acpi_ev = false;
2586 if (scancode > 0 && scancode < 0x21) {
2587 scancode--;
2588 if (!(hotkey_source_mask & (1 << scancode))) {
2589 tpacpi_input_send_key(scancode);
2590 *send_acpi_ev = false;
2591 } else {
2592 *ignore_acpi_ev = true;
2594 return true;
2596 return false;
2599 static bool hotkey_notify_wakeup(const u32 hkey,
2600 bool *send_acpi_ev,
2601 bool *ignore_acpi_ev)
2603 /* 0x2000-0x2FFF: Wakeup reason */
2604 *send_acpi_ev = true;
2605 *ignore_acpi_ev = false;
2607 switch (hkey) {
2608 case 0x2304: /* suspend, undock */
2609 case 0x2404: /* hibernation, undock */
2610 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2611 *ignore_acpi_ev = true;
2612 break;
2614 case 0x2305: /* suspend, bay eject */
2615 case 0x2405: /* hibernation, bay eject */
2616 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2617 *ignore_acpi_ev = true;
2618 break;
2620 case 0x2313: /* Battery on critical low level (S3) */
2621 case 0x2413: /* Battery on critical low level (S4) */
2622 printk(TPACPI_ALERT
2623 "EMERGENCY WAKEUP: battery almost empty\n");
2624 /* how to auto-heal: */
2625 /* 2313: woke up from S3, go to S4/S5 */
2626 /* 2413: woke up from S4, go to S5 */
2627 break;
2629 default:
2630 return false;
2633 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2634 printk(TPACPI_INFO
2635 "woke up due to a hot-unplug "
2636 "request...\n");
2637 hotkey_wakeup_reason_notify_change();
2639 return true;
2642 static bool hotkey_notify_usrevent(const u32 hkey,
2643 bool *send_acpi_ev,
2644 bool *ignore_acpi_ev)
2646 /* 0x5000-0x5FFF: human interface helpers */
2647 *send_acpi_ev = true;
2648 *ignore_acpi_ev = false;
2650 switch (hkey) {
2651 case 0x5010: /* Lenovo new BIOS: brightness changed */
2652 case 0x500b: /* X61t: tablet pen inserted into bay */
2653 case 0x500c: /* X61t: tablet pen removed from bay */
2654 return true;
2656 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2657 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2658 tpacpi_input_send_tabletsw();
2659 hotkey_tablet_mode_notify_change();
2660 *send_acpi_ev = false;
2661 return true;
2663 case 0x5001:
2664 case 0x5002:
2665 /* LID switch events. Do not propagate */
2666 *ignore_acpi_ev = true;
2667 return true;
2669 default:
2670 return false;
2674 static bool hotkey_notify_thermal(const u32 hkey,
2675 bool *send_acpi_ev,
2676 bool *ignore_acpi_ev)
2678 /* 0x6000-0x6FFF: thermal alarms */
2679 *send_acpi_ev = true;
2680 *ignore_acpi_ev = false;
2682 switch (hkey) {
2683 case 0x6011:
2684 printk(TPACPI_CRIT
2685 "THERMAL ALARM: battery is too hot!\n");
2686 /* recommended action: warn user through gui */
2687 return true;
2688 case 0x6012:
2689 printk(TPACPI_ALERT
2690 "THERMAL EMERGENCY: battery is extremely hot!\n");
2691 /* recommended action: immediate sleep/hibernate */
2692 return true;
2693 case 0x6021:
2694 printk(TPACPI_CRIT
2695 "THERMAL ALARM: "
2696 "a sensor reports something is too hot!\n");
2697 /* recommended action: warn user through gui, that */
2698 /* some internal component is too hot */
2699 return true;
2700 case 0x6022:
2701 printk(TPACPI_ALERT
2702 "THERMAL EMERGENCY: "
2703 "a sensor reports something is extremely hot!\n");
2704 /* recommended action: immediate sleep/hibernate */
2705 return true;
2706 default:
2707 printk(TPACPI_ALERT
2708 "THERMAL ALERT: unknown thermal alarm received\n");
2709 return false;
2713 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2715 u32 hkey;
2716 bool send_acpi_ev;
2717 bool ignore_acpi_ev;
2718 bool known_ev;
2720 if (event != 0x80) {
2721 printk(TPACPI_ERR
2722 "unknown HKEY notification event %d\n", event);
2723 /* forward it to userspace, maybe it knows how to handle it */
2724 acpi_bus_generate_netlink_event(
2725 ibm->acpi->device->pnp.device_class,
2726 dev_name(&ibm->acpi->device->dev),
2727 event, 0);
2728 return;
2731 while (1) {
2732 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2733 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2734 return;
2737 if (hkey == 0) {
2738 /* queue empty */
2739 return;
2742 send_acpi_ev = true;
2743 ignore_acpi_ev = false;
2745 switch (hkey >> 12) {
2746 case 1:
2747 /* 0x1000-0x1FFF: key presses */
2748 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2749 &ignore_acpi_ev);
2750 break;
2751 case 2:
2752 /* 0x2000-0x2FFF: Wakeup reason */
2753 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2754 &ignore_acpi_ev);
2755 break;
2756 case 3:
2757 /* 0x3000-0x3FFF: bay-related wakeups */
2758 if (hkey == 0x3003) {
2759 hotkey_autosleep_ack = 1;
2760 printk(TPACPI_INFO
2761 "bay ejected\n");
2762 hotkey_wakeup_hotunplug_complete_notify_change();
2763 known_ev = true;
2764 } else {
2765 known_ev = false;
2767 break;
2768 case 4:
2769 /* 0x4000-0x4FFF: dock-related wakeups */
2770 if (hkey == 0x4003) {
2771 hotkey_autosleep_ack = 1;
2772 printk(TPACPI_INFO
2773 "undocked\n");
2774 hotkey_wakeup_hotunplug_complete_notify_change();
2775 known_ev = true;
2776 } else {
2777 known_ev = false;
2779 break;
2780 case 5:
2781 /* 0x5000-0x5FFF: human interface helpers */
2782 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2783 &ignore_acpi_ev);
2784 break;
2785 case 6:
2786 /* 0x6000-0x6FFF: thermal alarms */
2787 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2788 &ignore_acpi_ev);
2789 break;
2790 case 7:
2791 /* 0x7000-0x7FFF: misc */
2792 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2793 tpacpi_send_radiosw_update();
2794 send_acpi_ev = 0;
2795 known_ev = true;
2796 break;
2798 /* fallthrough to default */
2799 default:
2800 known_ev = false;
2802 if (!known_ev) {
2803 printk(TPACPI_NOTICE
2804 "unhandled HKEY event 0x%04x\n", hkey);
2805 printk(TPACPI_NOTICE
2806 "please report the conditions when this "
2807 "event happened to %s\n", TPACPI_MAIL);
2810 /* Legacy events */
2811 if (!ignore_acpi_ev &&
2812 (send_acpi_ev || hotkey_report_mode < 2)) {
2813 acpi_bus_generate_proc_event(ibm->acpi->device,
2814 event, hkey);
2817 /* netlink events */
2818 if (!ignore_acpi_ev && send_acpi_ev) {
2819 acpi_bus_generate_netlink_event(
2820 ibm->acpi->device->pnp.device_class,
2821 dev_name(&ibm->acpi->device->dev),
2822 event, hkey);
2827 static void hotkey_suspend(pm_message_t state)
2829 /* Do these on suspend, we get the events on early resume! */
2830 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2831 hotkey_autosleep_ack = 0;
2834 static void hotkey_resume(void)
2836 tpacpi_disable_brightness_delay();
2838 if (hotkey_mask_get())
2839 printk(TPACPI_ERR
2840 "error while trying to read hot key mask "
2841 "from firmware\n");
2842 tpacpi_send_radiosw_update();
2843 hotkey_tablet_mode_notify_change();
2844 hotkey_wakeup_reason_notify_change();
2845 hotkey_wakeup_hotunplug_complete_notify_change();
2846 hotkey_poll_setup_safe(0);
2849 /* procfs -------------------------------------------------------------- */
2850 static int hotkey_read(char *p)
2852 int res, status;
2853 int len = 0;
2855 if (!tp_features.hotkey) {
2856 len += sprintf(p + len, "status:\t\tnot supported\n");
2857 return len;
2860 if (mutex_lock_killable(&hotkey_mutex))
2861 return -ERESTARTSYS;
2862 res = hotkey_status_get(&status);
2863 if (!res)
2864 res = hotkey_mask_get();
2865 mutex_unlock(&hotkey_mutex);
2866 if (res)
2867 return res;
2869 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2870 if (tp_features.hotkey_mask) {
2871 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2872 len += sprintf(p + len,
2873 "commands:\tenable, disable, reset, <mask>\n");
2874 } else {
2875 len += sprintf(p + len, "mask:\t\tnot supported\n");
2876 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2879 return len;
2882 static int hotkey_write(char *buf)
2884 int res, status;
2885 u32 mask;
2886 char *cmd;
2888 if (!tp_features.hotkey)
2889 return -ENODEV;
2891 if (mutex_lock_killable(&hotkey_mutex))
2892 return -ERESTARTSYS;
2894 status = -1;
2895 mask = hotkey_mask;
2897 res = 0;
2898 while ((cmd = next_cmd(&buf))) {
2899 if (strlencmp(cmd, "enable") == 0) {
2900 status = 1;
2901 } else if (strlencmp(cmd, "disable") == 0) {
2902 status = 0;
2903 } else if (strlencmp(cmd, "reset") == 0) {
2904 status = hotkey_orig_status;
2905 mask = hotkey_orig_mask;
2906 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2907 /* mask set */
2908 } else if (sscanf(cmd, "%x", &mask) == 1) {
2909 /* mask set */
2910 } else {
2911 res = -EINVAL;
2912 goto errexit;
2915 if (status != -1)
2916 res = hotkey_status_set(status);
2918 if (!res && mask != hotkey_mask)
2919 res = hotkey_mask_set(mask);
2921 errexit:
2922 mutex_unlock(&hotkey_mutex);
2923 return res;
2926 static const struct acpi_device_id ibm_htk_device_ids[] = {
2927 {TPACPI_ACPI_HKEY_HID, 0},
2928 {"", 0},
2931 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2932 .hid = ibm_htk_device_ids,
2933 .notify = hotkey_notify,
2934 .handle = &hkey_handle,
2935 .type = ACPI_DEVICE_NOTIFY,
2938 static struct ibm_struct hotkey_driver_data = {
2939 .name = "hotkey",
2940 .read = hotkey_read,
2941 .write = hotkey_write,
2942 .exit = hotkey_exit,
2943 .resume = hotkey_resume,
2944 .suspend = hotkey_suspend,
2945 .acpi = &ibm_hotkey_acpidriver,
2948 /*************************************************************************
2949 * Bluetooth subdriver
2952 enum {
2953 /* ACPI GBDC/SBDC bits */
2954 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2955 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2956 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2957 off / last state */
2960 enum {
2961 /* ACPI \BLTH commands */
2962 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2963 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2964 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2965 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2966 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2969 static struct rfkill *tpacpi_bluetooth_rfkill;
2971 static void bluetooth_suspend(pm_message_t state)
2973 /* Try to make sure radio will resume powered off */
2974 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2975 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2978 static int bluetooth_get_radiosw(void)
2980 int status;
2982 if (!tp_features.bluetooth)
2983 return -ENODEV;
2985 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2986 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2987 return RFKILL_STATE_HARD_BLOCKED;
2989 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2990 if (dbg_bluetoothemul)
2991 return (tpacpi_bluetooth_emulstate) ?
2992 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2993 #endif
2995 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2996 return -EIO;
2998 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2999 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3002 static void bluetooth_update_rfk(void)
3004 int status;
3006 if (!tpacpi_bluetooth_rfkill)
3007 return;
3009 status = bluetooth_get_radiosw();
3010 if (status < 0)
3011 return;
3012 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3015 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3017 int status;
3019 if (!tp_features.bluetooth)
3020 return -ENODEV;
3022 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3023 * reason to risk weird behaviour. */
3024 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3025 && radio_on)
3026 return -EPERM;
3028 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3029 if (dbg_bluetoothemul) {
3030 tpacpi_bluetooth_emulstate = !!radio_on;
3031 if (update_rfk)
3032 bluetooth_update_rfk();
3033 return 0;
3035 #endif
3037 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3038 if (radio_on)
3039 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3040 else
3041 status = 0;
3042 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3043 return -EIO;
3045 if (update_rfk)
3046 bluetooth_update_rfk();
3048 return 0;
3051 /* sysfs bluetooth enable ---------------------------------------------- */
3052 static ssize_t bluetooth_enable_show(struct device *dev,
3053 struct device_attribute *attr,
3054 char *buf)
3056 int status;
3058 status = bluetooth_get_radiosw();
3059 if (status < 0)
3060 return status;
3062 return snprintf(buf, PAGE_SIZE, "%d\n",
3063 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3066 static ssize_t bluetooth_enable_store(struct device *dev,
3067 struct device_attribute *attr,
3068 const char *buf, size_t count)
3070 unsigned long t;
3071 int res;
3073 if (parse_strtoul(buf, 1, &t))
3074 return -EINVAL;
3076 res = bluetooth_set_radiosw(t, 1);
3078 return (res) ? res : count;
3081 static struct device_attribute dev_attr_bluetooth_enable =
3082 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3083 bluetooth_enable_show, bluetooth_enable_store);
3085 /* --------------------------------------------------------------------- */
3087 static struct attribute *bluetooth_attributes[] = {
3088 &dev_attr_bluetooth_enable.attr,
3089 NULL
3092 static const struct attribute_group bluetooth_attr_group = {
3093 .attrs = bluetooth_attributes,
3096 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3098 int bts = bluetooth_get_radiosw();
3100 if (bts < 0)
3101 return bts;
3103 *state = bts;
3104 return 0;
3107 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3109 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3112 static void bluetooth_shutdown(void)
3114 /* Order firmware to save current state to NVRAM */
3115 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3116 TP_ACPI_BLTH_SAVE_STATE))
3117 printk(TPACPI_NOTICE
3118 "failed to save bluetooth state to NVRAM\n");
3121 static void bluetooth_exit(void)
3123 bluetooth_shutdown();
3125 if (tpacpi_bluetooth_rfkill)
3126 rfkill_unregister(tpacpi_bluetooth_rfkill);
3128 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3129 &bluetooth_attr_group);
3132 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3134 int res;
3135 int status = 0;
3137 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3139 TPACPI_ACPIHANDLE_INIT(hkey);
3141 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3142 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3143 tp_features.bluetooth = hkey_handle &&
3144 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3146 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3147 str_supported(tp_features.bluetooth),
3148 status);
3150 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3151 if (dbg_bluetoothemul) {
3152 tp_features.bluetooth = 1;
3153 printk(TPACPI_INFO
3154 "bluetooth switch emulation enabled\n");
3155 } else
3156 #endif
3157 if (tp_features.bluetooth &&
3158 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3159 /* no bluetooth hardware present in system */
3160 tp_features.bluetooth = 0;
3161 dbg_printk(TPACPI_DBG_INIT,
3162 "bluetooth hardware not installed\n");
3165 if (!tp_features.bluetooth)
3166 return 1;
3168 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3169 &bluetooth_attr_group);
3170 if (res)
3171 return res;
3173 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3174 &tpacpi_bluetooth_rfkill,
3175 RFKILL_TYPE_BLUETOOTH,
3176 "tpacpi_bluetooth_sw",
3177 true,
3178 tpacpi_bluetooth_rfk_set,
3179 tpacpi_bluetooth_rfk_get);
3180 if (res) {
3181 bluetooth_exit();
3182 return res;
3185 return 0;
3188 /* procfs -------------------------------------------------------------- */
3189 static int bluetooth_read(char *p)
3191 int len = 0;
3192 int status = bluetooth_get_radiosw();
3194 if (!tp_features.bluetooth)
3195 len += sprintf(p + len, "status:\t\tnot supported\n");
3196 else {
3197 len += sprintf(p + len, "status:\t\t%s\n",
3198 (status == RFKILL_STATE_UNBLOCKED) ?
3199 "enabled" : "disabled");
3200 len += sprintf(p + len, "commands:\tenable, disable\n");
3203 return len;
3206 static int bluetooth_write(char *buf)
3208 char *cmd;
3210 if (!tp_features.bluetooth)
3211 return -ENODEV;
3213 while ((cmd = next_cmd(&buf))) {
3214 if (strlencmp(cmd, "enable") == 0) {
3215 bluetooth_set_radiosw(1, 1);
3216 } else if (strlencmp(cmd, "disable") == 0) {
3217 bluetooth_set_radiosw(0, 1);
3218 } else
3219 return -EINVAL;
3222 return 0;
3225 static struct ibm_struct bluetooth_driver_data = {
3226 .name = "bluetooth",
3227 .read = bluetooth_read,
3228 .write = bluetooth_write,
3229 .exit = bluetooth_exit,
3230 .suspend = bluetooth_suspend,
3231 .shutdown = bluetooth_shutdown,
3234 /*************************************************************************
3235 * Wan subdriver
3238 enum {
3239 /* ACPI GWAN/SWAN bits */
3240 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3241 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3242 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3243 off / last state */
3246 static struct rfkill *tpacpi_wan_rfkill;
3248 static void wan_suspend(pm_message_t state)
3250 /* Try to make sure radio will resume powered off */
3251 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3252 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3255 static int wan_get_radiosw(void)
3257 int status;
3259 if (!tp_features.wan)
3260 return -ENODEV;
3262 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3263 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3264 return RFKILL_STATE_HARD_BLOCKED;
3266 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3267 if (dbg_wwanemul)
3268 return (tpacpi_wwan_emulstate) ?
3269 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3270 #endif
3272 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3273 return -EIO;
3275 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3276 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3279 static void wan_update_rfk(void)
3281 int status;
3283 if (!tpacpi_wan_rfkill)
3284 return;
3286 status = wan_get_radiosw();
3287 if (status < 0)
3288 return;
3289 rfkill_force_state(tpacpi_wan_rfkill, status);
3292 static int wan_set_radiosw(int radio_on, int update_rfk)
3294 int status;
3296 if (!tp_features.wan)
3297 return -ENODEV;
3299 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3300 * reason to risk weird behaviour. */
3301 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3302 && radio_on)
3303 return -EPERM;
3305 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3306 if (dbg_wwanemul) {
3307 tpacpi_wwan_emulstate = !!radio_on;
3308 if (update_rfk)
3309 wan_update_rfk();
3310 return 0;
3312 #endif
3314 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3315 if (radio_on)
3316 status = TP_ACPI_WANCARD_RADIOSSW;
3317 else
3318 status = 0;
3319 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3320 return -EIO;
3322 if (update_rfk)
3323 wan_update_rfk();
3325 return 0;
3328 /* sysfs wan enable ---------------------------------------------------- */
3329 static ssize_t wan_enable_show(struct device *dev,
3330 struct device_attribute *attr,
3331 char *buf)
3333 int status;
3335 status = wan_get_radiosw();
3336 if (status < 0)
3337 return status;
3339 return snprintf(buf, PAGE_SIZE, "%d\n",
3340 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3343 static ssize_t wan_enable_store(struct device *dev,
3344 struct device_attribute *attr,
3345 const char *buf, size_t count)
3347 unsigned long t;
3348 int res;
3350 if (parse_strtoul(buf, 1, &t))
3351 return -EINVAL;
3353 res = wan_set_radiosw(t, 1);
3355 return (res) ? res : count;
3358 static struct device_attribute dev_attr_wan_enable =
3359 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3360 wan_enable_show, wan_enable_store);
3362 /* --------------------------------------------------------------------- */
3364 static struct attribute *wan_attributes[] = {
3365 &dev_attr_wan_enable.attr,
3366 NULL
3369 static const struct attribute_group wan_attr_group = {
3370 .attrs = wan_attributes,
3373 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3375 int wans = wan_get_radiosw();
3377 if (wans < 0)
3378 return wans;
3380 *state = wans;
3381 return 0;
3384 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3386 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3389 static void wan_shutdown(void)
3391 /* Order firmware to save current state to NVRAM */
3392 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3393 TP_ACPI_WGSV_SAVE_STATE))
3394 printk(TPACPI_NOTICE
3395 "failed to save WWAN state to NVRAM\n");
3398 static void wan_exit(void)
3400 wan_shutdown();
3402 if (tpacpi_wan_rfkill)
3403 rfkill_unregister(tpacpi_wan_rfkill);
3405 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3406 &wan_attr_group);
3409 static int __init wan_init(struct ibm_init_struct *iibm)
3411 int res;
3412 int status = 0;
3414 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3416 TPACPI_ACPIHANDLE_INIT(hkey);
3418 tp_features.wan = hkey_handle &&
3419 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3421 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3422 str_supported(tp_features.wan),
3423 status);
3425 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3426 if (dbg_wwanemul) {
3427 tp_features.wan = 1;
3428 printk(TPACPI_INFO
3429 "wwan switch emulation enabled\n");
3430 } else
3431 #endif
3432 if (tp_features.wan &&
3433 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3434 /* no wan hardware present in system */
3435 tp_features.wan = 0;
3436 dbg_printk(TPACPI_DBG_INIT,
3437 "wan hardware not installed\n");
3440 if (!tp_features.wan)
3441 return 1;
3443 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3444 &wan_attr_group);
3445 if (res)
3446 return res;
3448 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3449 &tpacpi_wan_rfkill,
3450 RFKILL_TYPE_WWAN,
3451 "tpacpi_wwan_sw",
3452 true,
3453 tpacpi_wan_rfk_set,
3454 tpacpi_wan_rfk_get);
3455 if (res) {
3456 wan_exit();
3457 return res;
3460 return 0;
3463 /* procfs -------------------------------------------------------------- */
3464 static int wan_read(char *p)
3466 int len = 0;
3467 int status = wan_get_radiosw();
3469 if (!tp_features.wan)
3470 len += sprintf(p + len, "status:\t\tnot supported\n");
3471 else {
3472 len += sprintf(p + len, "status:\t\t%s\n",
3473 (status == RFKILL_STATE_UNBLOCKED) ?
3474 "enabled" : "disabled");
3475 len += sprintf(p + len, "commands:\tenable, disable\n");
3478 return len;
3481 static int wan_write(char *buf)
3483 char *cmd;
3485 if (!tp_features.wan)
3486 return -ENODEV;
3488 while ((cmd = next_cmd(&buf))) {
3489 if (strlencmp(cmd, "enable") == 0) {
3490 wan_set_radiosw(1, 1);
3491 } else if (strlencmp(cmd, "disable") == 0) {
3492 wan_set_radiosw(0, 1);
3493 } else
3494 return -EINVAL;
3497 return 0;
3500 static struct ibm_struct wan_driver_data = {
3501 .name = "wan",
3502 .read = wan_read,
3503 .write = wan_write,
3504 .exit = wan_exit,
3505 .suspend = wan_suspend,
3506 .shutdown = wan_shutdown,
3509 /*************************************************************************
3510 * UWB subdriver
3513 enum {
3514 /* ACPI GUWB/SUWB bits */
3515 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3516 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3519 static struct rfkill *tpacpi_uwb_rfkill;
3521 static int uwb_get_radiosw(void)
3523 int status;
3525 if (!tp_features.uwb)
3526 return -ENODEV;
3528 /* WLSW overrides UWB in firmware/hardware, reflect that */
3529 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3530 return RFKILL_STATE_HARD_BLOCKED;
3532 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3533 if (dbg_uwbemul)
3534 return (tpacpi_uwb_emulstate) ?
3535 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3536 #endif
3538 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3539 return -EIO;
3541 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3542 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3545 static void uwb_update_rfk(void)
3547 int status;
3549 if (!tpacpi_uwb_rfkill)
3550 return;
3552 status = uwb_get_radiosw();
3553 if (status < 0)
3554 return;
3555 rfkill_force_state(tpacpi_uwb_rfkill, status);
3558 static int uwb_set_radiosw(int radio_on, int update_rfk)
3560 int status;
3562 if (!tp_features.uwb)
3563 return -ENODEV;
3565 /* WLSW overrides UWB in firmware/hardware, but there is no
3566 * reason to risk weird behaviour. */
3567 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3568 && radio_on)
3569 return -EPERM;
3571 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3572 if (dbg_uwbemul) {
3573 tpacpi_uwb_emulstate = !!radio_on;
3574 if (update_rfk)
3575 uwb_update_rfk();
3576 return 0;
3578 #endif
3580 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3581 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3582 return -EIO;
3584 if (update_rfk)
3585 uwb_update_rfk();
3587 return 0;
3590 /* --------------------------------------------------------------------- */
3592 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3594 int uwbs = uwb_get_radiosw();
3596 if (uwbs < 0)
3597 return uwbs;
3599 *state = uwbs;
3600 return 0;
3603 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3605 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3608 static void uwb_exit(void)
3610 if (tpacpi_uwb_rfkill)
3611 rfkill_unregister(tpacpi_uwb_rfkill);
3614 static int __init uwb_init(struct ibm_init_struct *iibm)
3616 int res;
3617 int status = 0;
3619 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3621 TPACPI_ACPIHANDLE_INIT(hkey);
3623 tp_features.uwb = hkey_handle &&
3624 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3626 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3627 str_supported(tp_features.uwb),
3628 status);
3630 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3631 if (dbg_uwbemul) {
3632 tp_features.uwb = 1;
3633 printk(TPACPI_INFO
3634 "uwb switch emulation enabled\n");
3635 } else
3636 #endif
3637 if (tp_features.uwb &&
3638 !(status & TP_ACPI_UWB_HWPRESENT)) {
3639 /* no uwb hardware present in system */
3640 tp_features.uwb = 0;
3641 dbg_printk(TPACPI_DBG_INIT,
3642 "uwb hardware not installed\n");
3645 if (!tp_features.uwb)
3646 return 1;
3648 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3649 &tpacpi_uwb_rfkill,
3650 RFKILL_TYPE_UWB,
3651 "tpacpi_uwb_sw",
3652 false,
3653 tpacpi_uwb_rfk_set,
3654 tpacpi_uwb_rfk_get);
3656 return res;
3659 static struct ibm_struct uwb_driver_data = {
3660 .name = "uwb",
3661 .exit = uwb_exit,
3662 .flags.experimental = 1,
3665 /*************************************************************************
3666 * Video subdriver
3669 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3671 enum video_access_mode {
3672 TPACPI_VIDEO_NONE = 0,
3673 TPACPI_VIDEO_570, /* 570 */
3674 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3675 TPACPI_VIDEO_NEW, /* all others */
3678 enum { /* video status flags, based on VIDEO_570 */
3679 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3680 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3681 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3684 enum { /* TPACPI_VIDEO_570 constants */
3685 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3686 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3687 * video_status_flags */
3688 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3689 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3692 static enum video_access_mode video_supported;
3693 static int video_orig_autosw;
3695 static int video_autosw_get(void);
3696 static int video_autosw_set(int enable);
3698 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3700 static int __init video_init(struct ibm_init_struct *iibm)
3702 int ivga;
3704 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3706 TPACPI_ACPIHANDLE_INIT(vid);
3707 TPACPI_ACPIHANDLE_INIT(vid2);
3709 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3710 /* G41, assume IVGA doesn't change */
3711 vid_handle = vid2_handle;
3713 if (!vid_handle)
3714 /* video switching not supported on R30, R31 */
3715 video_supported = TPACPI_VIDEO_NONE;
3716 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3717 /* 570 */
3718 video_supported = TPACPI_VIDEO_570;
3719 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3720 /* 600e/x, 770e, 770x */
3721 video_supported = TPACPI_VIDEO_770;
3722 else
3723 /* all others */
3724 video_supported = TPACPI_VIDEO_NEW;
3726 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3727 str_supported(video_supported != TPACPI_VIDEO_NONE),
3728 video_supported);
3730 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3733 static void video_exit(void)
3735 dbg_printk(TPACPI_DBG_EXIT,
3736 "restoring original video autoswitch mode\n");
3737 if (video_autosw_set(video_orig_autosw))
3738 printk(TPACPI_ERR "error while trying to restore original "
3739 "video autoswitch mode\n");
3742 static int video_outputsw_get(void)
3744 int status = 0;
3745 int i;
3747 switch (video_supported) {
3748 case TPACPI_VIDEO_570:
3749 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3750 TP_ACPI_VIDEO_570_PHSCMD))
3751 return -EIO;
3752 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3753 break;
3754 case TPACPI_VIDEO_770:
3755 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3756 return -EIO;
3757 if (i)
3758 status |= TP_ACPI_VIDEO_S_LCD;
3759 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3760 return -EIO;
3761 if (i)
3762 status |= TP_ACPI_VIDEO_S_CRT;
3763 break;
3764 case TPACPI_VIDEO_NEW:
3765 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3766 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3767 return -EIO;
3768 if (i)
3769 status |= TP_ACPI_VIDEO_S_CRT;
3771 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3772 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3773 return -EIO;
3774 if (i)
3775 status |= TP_ACPI_VIDEO_S_LCD;
3776 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3777 return -EIO;
3778 if (i)
3779 status |= TP_ACPI_VIDEO_S_DVI;
3780 break;
3781 default:
3782 return -ENOSYS;
3785 return status;
3788 static int video_outputsw_set(int status)
3790 int autosw;
3791 int res = 0;
3793 switch (video_supported) {
3794 case TPACPI_VIDEO_570:
3795 res = acpi_evalf(NULL, NULL,
3796 "\\_SB.PHS2", "vdd",
3797 TP_ACPI_VIDEO_570_PHS2CMD,
3798 status | TP_ACPI_VIDEO_570_PHS2SET);
3799 break;
3800 case TPACPI_VIDEO_770:
3801 autosw = video_autosw_get();
3802 if (autosw < 0)
3803 return autosw;
3805 res = video_autosw_set(1);
3806 if (res)
3807 return res;
3808 res = acpi_evalf(vid_handle, NULL,
3809 "ASWT", "vdd", status * 0x100, 0);
3810 if (!autosw && video_autosw_set(autosw)) {
3811 printk(TPACPI_ERR
3812 "video auto-switch left enabled due to error\n");
3813 return -EIO;
3815 break;
3816 case TPACPI_VIDEO_NEW:
3817 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3818 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3819 break;
3820 default:
3821 return -ENOSYS;
3824 return (res)? 0 : -EIO;
3827 static int video_autosw_get(void)
3829 int autosw = 0;
3831 switch (video_supported) {
3832 case TPACPI_VIDEO_570:
3833 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3834 return -EIO;
3835 break;
3836 case TPACPI_VIDEO_770:
3837 case TPACPI_VIDEO_NEW:
3838 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3839 return -EIO;
3840 break;
3841 default:
3842 return -ENOSYS;
3845 return autosw & 1;
3848 static int video_autosw_set(int enable)
3850 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3851 return -EIO;
3852 return 0;
3855 static int video_outputsw_cycle(void)
3857 int autosw = video_autosw_get();
3858 int res;
3860 if (autosw < 0)
3861 return autosw;
3863 switch (video_supported) {
3864 case TPACPI_VIDEO_570:
3865 res = video_autosw_set(1);
3866 if (res)
3867 return res;
3868 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3869 break;
3870 case TPACPI_VIDEO_770:
3871 case TPACPI_VIDEO_NEW:
3872 res = video_autosw_set(1);
3873 if (res)
3874 return res;
3875 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3876 break;
3877 default:
3878 return -ENOSYS;
3880 if (!autosw && video_autosw_set(autosw)) {
3881 printk(TPACPI_ERR
3882 "video auto-switch left enabled due to error\n");
3883 return -EIO;
3886 return (res)? 0 : -EIO;
3889 static int video_expand_toggle(void)
3891 switch (video_supported) {
3892 case TPACPI_VIDEO_570:
3893 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3894 0 : -EIO;
3895 case TPACPI_VIDEO_770:
3896 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3897 0 : -EIO;
3898 case TPACPI_VIDEO_NEW:
3899 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3900 0 : -EIO;
3901 default:
3902 return -ENOSYS;
3904 /* not reached */
3907 static int video_read(char *p)
3909 int status, autosw;
3910 int len = 0;
3912 if (video_supported == TPACPI_VIDEO_NONE) {
3913 len += sprintf(p + len, "status:\t\tnot supported\n");
3914 return len;
3917 status = video_outputsw_get();
3918 if (status < 0)
3919 return status;
3921 autosw = video_autosw_get();
3922 if (autosw < 0)
3923 return autosw;
3925 len += sprintf(p + len, "status:\t\tsupported\n");
3926 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3927 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3928 if (video_supported == TPACPI_VIDEO_NEW)
3929 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3930 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3931 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3932 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3933 if (video_supported == TPACPI_VIDEO_NEW)
3934 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3935 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3936 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3938 return len;
3941 static int video_write(char *buf)
3943 char *cmd;
3944 int enable, disable, status;
3945 int res;
3947 if (video_supported == TPACPI_VIDEO_NONE)
3948 return -ENODEV;
3950 enable = 0;
3951 disable = 0;
3953 while ((cmd = next_cmd(&buf))) {
3954 if (strlencmp(cmd, "lcd_enable") == 0) {
3955 enable |= TP_ACPI_VIDEO_S_LCD;
3956 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3957 disable |= TP_ACPI_VIDEO_S_LCD;
3958 } else if (strlencmp(cmd, "crt_enable") == 0) {
3959 enable |= TP_ACPI_VIDEO_S_CRT;
3960 } else if (strlencmp(cmd, "crt_disable") == 0) {
3961 disable |= TP_ACPI_VIDEO_S_CRT;
3962 } else if (video_supported == TPACPI_VIDEO_NEW &&
3963 strlencmp(cmd, "dvi_enable") == 0) {
3964 enable |= TP_ACPI_VIDEO_S_DVI;
3965 } else if (video_supported == TPACPI_VIDEO_NEW &&
3966 strlencmp(cmd, "dvi_disable") == 0) {
3967 disable |= TP_ACPI_VIDEO_S_DVI;
3968 } else if (strlencmp(cmd, "auto_enable") == 0) {
3969 res = video_autosw_set(1);
3970 if (res)
3971 return res;
3972 } else if (strlencmp(cmd, "auto_disable") == 0) {
3973 res = video_autosw_set(0);
3974 if (res)
3975 return res;
3976 } else if (strlencmp(cmd, "video_switch") == 0) {
3977 res = video_outputsw_cycle();
3978 if (res)
3979 return res;
3980 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3981 res = video_expand_toggle();
3982 if (res)
3983 return res;
3984 } else
3985 return -EINVAL;
3988 if (enable || disable) {
3989 status = video_outputsw_get();
3990 if (status < 0)
3991 return status;
3992 res = video_outputsw_set((status & ~disable) | enable);
3993 if (res)
3994 return res;
3997 return 0;
4000 static struct ibm_struct video_driver_data = {
4001 .name = "video",
4002 .read = video_read,
4003 .write = video_write,
4004 .exit = video_exit,
4007 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4009 /*************************************************************************
4010 * Light (thinklight) subdriver
4013 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4014 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4016 static int light_get_status(void)
4018 int status = 0;
4020 if (tp_features.light_status) {
4021 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4022 return -EIO;
4023 return (!!status);
4026 return -ENXIO;
4029 static int light_set_status(int status)
4031 int rc;
4033 if (tp_features.light) {
4034 if (cmos_handle) {
4035 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4036 (status)?
4037 TP_CMOS_THINKLIGHT_ON :
4038 TP_CMOS_THINKLIGHT_OFF);
4039 } else {
4040 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4041 (status)? 1 : 0);
4043 return (rc)? 0 : -EIO;
4046 return -ENXIO;
4049 static void light_set_status_worker(struct work_struct *work)
4051 struct tpacpi_led_classdev *data =
4052 container_of(work, struct tpacpi_led_classdev, work);
4054 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4055 light_set_status((data->new_brightness != LED_OFF));
4058 static void light_sysfs_set(struct led_classdev *led_cdev,
4059 enum led_brightness brightness)
4061 struct tpacpi_led_classdev *data =
4062 container_of(led_cdev,
4063 struct tpacpi_led_classdev,
4064 led_classdev);
4065 data->new_brightness = brightness;
4066 queue_work(tpacpi_wq, &data->work);
4069 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4071 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4074 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4075 .led_classdev = {
4076 .name = "tpacpi::thinklight",
4077 .brightness_set = &light_sysfs_set,
4078 .brightness_get = &light_sysfs_get,
4082 static int __init light_init(struct ibm_init_struct *iibm)
4084 int rc;
4086 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4088 TPACPI_ACPIHANDLE_INIT(ledb);
4089 TPACPI_ACPIHANDLE_INIT(lght);
4090 TPACPI_ACPIHANDLE_INIT(cmos);
4091 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4093 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4094 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4096 if (tp_features.light)
4097 /* light status not supported on
4098 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4099 tp_features.light_status =
4100 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4102 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4103 str_supported(tp_features.light),
4104 str_supported(tp_features.light_status));
4106 if (!tp_features.light)
4107 return 1;
4109 rc = led_classdev_register(&tpacpi_pdev->dev,
4110 &tpacpi_led_thinklight.led_classdev);
4112 if (rc < 0) {
4113 tp_features.light = 0;
4114 tp_features.light_status = 0;
4115 } else {
4116 rc = 0;
4119 return rc;
4122 static void light_exit(void)
4124 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4125 if (work_pending(&tpacpi_led_thinklight.work))
4126 flush_workqueue(tpacpi_wq);
4129 static int light_read(char *p)
4131 int len = 0;
4132 int status;
4134 if (!tp_features.light) {
4135 len += sprintf(p + len, "status:\t\tnot supported\n");
4136 } else if (!tp_features.light_status) {
4137 len += sprintf(p + len, "status:\t\tunknown\n");
4138 len += sprintf(p + len, "commands:\ton, off\n");
4139 } else {
4140 status = light_get_status();
4141 if (status < 0)
4142 return status;
4143 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4144 len += sprintf(p + len, "commands:\ton, off\n");
4147 return len;
4150 static int light_write(char *buf)
4152 char *cmd;
4153 int newstatus = 0;
4155 if (!tp_features.light)
4156 return -ENODEV;
4158 while ((cmd = next_cmd(&buf))) {
4159 if (strlencmp(cmd, "on") == 0) {
4160 newstatus = 1;
4161 } else if (strlencmp(cmd, "off") == 0) {
4162 newstatus = 0;
4163 } else
4164 return -EINVAL;
4167 return light_set_status(newstatus);
4170 static struct ibm_struct light_driver_data = {
4171 .name = "light",
4172 .read = light_read,
4173 .write = light_write,
4174 .exit = light_exit,
4177 /*************************************************************************
4178 * Dock subdriver
4181 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4183 static void dock_notify(struct ibm_struct *ibm, u32 event);
4184 static int dock_read(char *p);
4185 static int dock_write(char *buf);
4187 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4188 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4189 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4190 "\\_SB.PCI.ISA.SLCE", /* 570 */
4191 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4193 /* don't list other alternatives as we install a notify handler on the 570 */
4194 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4196 static const struct acpi_device_id ibm_pci_device_ids[] = {
4197 {PCI_ROOT_HID_STRING, 0},
4198 {"", 0},
4201 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4203 .notify = dock_notify,
4204 .handle = &dock_handle,
4205 .type = ACPI_SYSTEM_NOTIFY,
4208 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4209 * We just use it to get notifications of dock hotplug
4210 * in very old thinkpads */
4211 .hid = ibm_pci_device_ids,
4212 .notify = dock_notify,
4213 .handle = &pci_handle,
4214 .type = ACPI_SYSTEM_NOTIFY,
4218 static struct ibm_struct dock_driver_data[2] = {
4220 .name = "dock",
4221 .read = dock_read,
4222 .write = dock_write,
4223 .acpi = &ibm_dock_acpidriver[0],
4226 .name = "dock",
4227 .acpi = &ibm_dock_acpidriver[1],
4231 #define dock_docked() (_sta(dock_handle) & 1)
4233 static int __init dock_init(struct ibm_init_struct *iibm)
4235 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4237 TPACPI_ACPIHANDLE_INIT(dock);
4239 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4240 str_supported(dock_handle != NULL));
4242 return (dock_handle)? 0 : 1;
4245 static int __init dock_init2(struct ibm_init_struct *iibm)
4247 int dock2_needed;
4249 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4251 if (dock_driver_data[0].flags.acpi_driver_registered &&
4252 dock_driver_data[0].flags.acpi_notify_installed) {
4253 TPACPI_ACPIHANDLE_INIT(pci);
4254 dock2_needed = (pci_handle != NULL);
4255 vdbg_printk(TPACPI_DBG_INIT,
4256 "dock PCI handler for the TP 570 is %s\n",
4257 str_supported(dock2_needed));
4258 } else {
4259 vdbg_printk(TPACPI_DBG_INIT,
4260 "dock subdriver part 2 not required\n");
4261 dock2_needed = 0;
4264 return (dock2_needed)? 0 : 1;
4267 static void dock_notify(struct ibm_struct *ibm, u32 event)
4269 int docked = dock_docked();
4270 int pci = ibm->acpi->hid && ibm->acpi->device &&
4271 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4272 int data;
4274 if (event == 1 && !pci) /* 570 */
4275 data = 1; /* button */
4276 else if (event == 1 && pci) /* 570 */
4277 data = 3; /* dock */
4278 else if (event == 3 && docked)
4279 data = 1; /* button */
4280 else if (event == 3 && !docked)
4281 data = 2; /* undock */
4282 else if (event == 0 && docked)
4283 data = 3; /* dock */
4284 else {
4285 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4286 event, _sta(dock_handle));
4287 data = 0; /* unknown */
4289 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4290 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4291 dev_name(&ibm->acpi->device->dev),
4292 event, data);
4295 static int dock_read(char *p)
4297 int len = 0;
4298 int docked = dock_docked();
4300 if (!dock_handle)
4301 len += sprintf(p + len, "status:\t\tnot supported\n");
4302 else if (!docked)
4303 len += sprintf(p + len, "status:\t\tundocked\n");
4304 else {
4305 len += sprintf(p + len, "status:\t\tdocked\n");
4306 len += sprintf(p + len, "commands:\tdock, undock\n");
4309 return len;
4312 static int dock_write(char *buf)
4314 char *cmd;
4316 if (!dock_docked())
4317 return -ENODEV;
4319 while ((cmd = next_cmd(&buf))) {
4320 if (strlencmp(cmd, "undock") == 0) {
4321 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4322 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4323 return -EIO;
4324 } else if (strlencmp(cmd, "dock") == 0) {
4325 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4326 return -EIO;
4327 } else
4328 return -EINVAL;
4331 return 0;
4334 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4336 /*************************************************************************
4337 * Bay subdriver
4340 #ifdef CONFIG_THINKPAD_ACPI_BAY
4342 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4343 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4344 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4345 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4346 ); /* A21e, R30, R31 */
4347 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4348 "_EJ0", /* all others */
4349 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4350 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4351 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4352 ); /* all others */
4353 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4354 "_EJ0", /* 770x */
4355 ); /* all others */
4357 static int __init bay_init(struct ibm_init_struct *iibm)
4359 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4361 TPACPI_ACPIHANDLE_INIT(bay);
4362 if (bay_handle)
4363 TPACPI_ACPIHANDLE_INIT(bay_ej);
4364 TPACPI_ACPIHANDLE_INIT(bay2);
4365 if (bay2_handle)
4366 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4368 tp_features.bay_status = bay_handle &&
4369 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4370 tp_features.bay_status2 = bay2_handle &&
4371 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4373 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4374 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4375 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4376 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4378 vdbg_printk(TPACPI_DBG_INIT,
4379 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4380 str_supported(tp_features.bay_status),
4381 str_supported(tp_features.bay_eject),
4382 str_supported(tp_features.bay_status2),
4383 str_supported(tp_features.bay_eject2));
4385 return (tp_features.bay_status || tp_features.bay_eject ||
4386 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4389 static void bay_notify(struct ibm_struct *ibm, u32 event)
4391 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4392 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4393 dev_name(&ibm->acpi->device->dev),
4394 event, 0);
4397 #define bay_occupied(b) (_sta(b##_handle) & 1)
4399 static int bay_read(char *p)
4401 int len = 0;
4402 int occupied = bay_occupied(bay);
4403 int occupied2 = bay_occupied(bay2);
4404 int eject, eject2;
4406 len += sprintf(p + len, "status:\t\t%s\n",
4407 tp_features.bay_status ?
4408 (occupied ? "occupied" : "unoccupied") :
4409 "not supported");
4410 if (tp_features.bay_status2)
4411 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4412 "occupied" : "unoccupied");
4414 eject = tp_features.bay_eject && occupied;
4415 eject2 = tp_features.bay_eject2 && occupied2;
4417 if (eject && eject2)
4418 len += sprintf(p + len, "commands:\teject, eject2\n");
4419 else if (eject)
4420 len += sprintf(p + len, "commands:\teject\n");
4421 else if (eject2)
4422 len += sprintf(p + len, "commands:\teject2\n");
4424 return len;
4427 static int bay_write(char *buf)
4429 char *cmd;
4431 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4432 return -ENODEV;
4434 while ((cmd = next_cmd(&buf))) {
4435 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4436 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4437 return -EIO;
4438 } else if (tp_features.bay_eject2 &&
4439 strlencmp(cmd, "eject2") == 0) {
4440 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4441 return -EIO;
4442 } else
4443 return -EINVAL;
4446 return 0;
4449 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4450 .notify = bay_notify,
4451 .handle = &bay_handle,
4452 .type = ACPI_SYSTEM_NOTIFY,
4455 static struct ibm_struct bay_driver_data = {
4456 .name = "bay",
4457 .read = bay_read,
4458 .write = bay_write,
4459 .acpi = &ibm_bay_acpidriver,
4462 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4464 /*************************************************************************
4465 * CMOS subdriver
4468 /* sysfs cmos_command -------------------------------------------------- */
4469 static ssize_t cmos_command_store(struct device *dev,
4470 struct device_attribute *attr,
4471 const char *buf, size_t count)
4473 unsigned long cmos_cmd;
4474 int res;
4476 if (parse_strtoul(buf, 21, &cmos_cmd))
4477 return -EINVAL;
4479 res = issue_thinkpad_cmos_command(cmos_cmd);
4480 return (res)? res : count;
4483 static struct device_attribute dev_attr_cmos_command =
4484 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4486 /* --------------------------------------------------------------------- */
4488 static int __init cmos_init(struct ibm_init_struct *iibm)
4490 int res;
4492 vdbg_printk(TPACPI_DBG_INIT,
4493 "initializing cmos commands subdriver\n");
4495 TPACPI_ACPIHANDLE_INIT(cmos);
4497 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4498 str_supported(cmos_handle != NULL));
4500 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4501 if (res)
4502 return res;
4504 return (cmos_handle)? 0 : 1;
4507 static void cmos_exit(void)
4509 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4512 static int cmos_read(char *p)
4514 int len = 0;
4516 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4517 R30, R31, T20-22, X20-21 */
4518 if (!cmos_handle)
4519 len += sprintf(p + len, "status:\t\tnot supported\n");
4520 else {
4521 len += sprintf(p + len, "status:\t\tsupported\n");
4522 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4525 return len;
4528 static int cmos_write(char *buf)
4530 char *cmd;
4531 int cmos_cmd, res;
4533 while ((cmd = next_cmd(&buf))) {
4534 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4535 cmos_cmd >= 0 && cmos_cmd <= 21) {
4536 /* cmos_cmd set */
4537 } else
4538 return -EINVAL;
4540 res = issue_thinkpad_cmos_command(cmos_cmd);
4541 if (res)
4542 return res;
4545 return 0;
4548 static struct ibm_struct cmos_driver_data = {
4549 .name = "cmos",
4550 .read = cmos_read,
4551 .write = cmos_write,
4552 .exit = cmos_exit,
4555 /*************************************************************************
4556 * LED subdriver
4559 enum led_access_mode {
4560 TPACPI_LED_NONE = 0,
4561 TPACPI_LED_570, /* 570 */
4562 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4563 TPACPI_LED_NEW, /* all others */
4566 enum { /* For TPACPI_LED_OLD */
4567 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4568 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4569 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4572 enum led_status_t {
4573 TPACPI_LED_OFF = 0,
4574 TPACPI_LED_ON,
4575 TPACPI_LED_BLINK,
4578 static enum led_access_mode led_supported;
4580 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4581 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4582 /* T20-22, X20-21 */
4583 "LED", /* all others */
4584 ); /* R30, R31 */
4586 #define TPACPI_LED_NUMLEDS 8
4587 static struct tpacpi_led_classdev *tpacpi_leds;
4588 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4589 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4590 /* there's a limit of 19 chars + NULL before 2.6.26 */
4591 "tpacpi::power",
4592 "tpacpi:orange:batt",
4593 "tpacpi:green:batt",
4594 "tpacpi::dock_active",
4595 "tpacpi::bay_active",
4596 "tpacpi::dock_batt",
4597 "tpacpi::unknown_led",
4598 "tpacpi::standby",
4601 static int led_get_status(const unsigned int led)
4603 int status;
4604 enum led_status_t led_s;
4606 switch (led_supported) {
4607 case TPACPI_LED_570:
4608 if (!acpi_evalf(ec_handle,
4609 &status, "GLED", "dd", 1 << led))
4610 return -EIO;
4611 led_s = (status == 0)?
4612 TPACPI_LED_OFF :
4613 ((status == 1)?
4614 TPACPI_LED_ON :
4615 TPACPI_LED_BLINK);
4616 tpacpi_led_state_cache[led] = led_s;
4617 return led_s;
4618 default:
4619 return -ENXIO;
4622 /* not reached */
4625 static int led_set_status(const unsigned int led,
4626 const enum led_status_t ledstatus)
4628 /* off, on, blink. Index is led_status_t */
4629 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4630 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4632 int rc = 0;
4634 switch (led_supported) {
4635 case TPACPI_LED_570:
4636 /* 570 */
4637 if (led > 7)
4638 return -EINVAL;
4639 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4640 (1 << led), led_sled_arg1[ledstatus]))
4641 rc = -EIO;
4642 break;
4643 case TPACPI_LED_OLD:
4644 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4645 if (led > 7)
4646 return -EINVAL;
4647 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4648 if (rc >= 0)
4649 rc = ec_write(TPACPI_LED_EC_HLBL,
4650 (ledstatus == TPACPI_LED_BLINK) << led);
4651 if (rc >= 0)
4652 rc = ec_write(TPACPI_LED_EC_HLCL,
4653 (ledstatus != TPACPI_LED_OFF) << led);
4654 break;
4655 case TPACPI_LED_NEW:
4656 /* all others */
4657 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4658 led, led_led_arg1[ledstatus]))
4659 rc = -EIO;
4660 break;
4661 default:
4662 rc = -ENXIO;
4665 if (!rc)
4666 tpacpi_led_state_cache[led] = ledstatus;
4668 return rc;
4671 static void led_sysfs_set_status(unsigned int led,
4672 enum led_brightness brightness)
4674 led_set_status(led,
4675 (brightness == LED_OFF) ?
4676 TPACPI_LED_OFF :
4677 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4678 TPACPI_LED_BLINK : TPACPI_LED_ON);
4681 static void led_set_status_worker(struct work_struct *work)
4683 struct tpacpi_led_classdev *data =
4684 container_of(work, struct tpacpi_led_classdev, work);
4686 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4687 led_sysfs_set_status(data->led, data->new_brightness);
4690 static void led_sysfs_set(struct led_classdev *led_cdev,
4691 enum led_brightness brightness)
4693 struct tpacpi_led_classdev *data = container_of(led_cdev,
4694 struct tpacpi_led_classdev, led_classdev);
4696 data->new_brightness = brightness;
4697 queue_work(tpacpi_wq, &data->work);
4700 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4701 unsigned long *delay_on, unsigned long *delay_off)
4703 struct tpacpi_led_classdev *data = container_of(led_cdev,
4704 struct tpacpi_led_classdev, led_classdev);
4706 /* Can we choose the flash rate? */
4707 if (*delay_on == 0 && *delay_off == 0) {
4708 /* yes. set them to the hardware blink rate (1 Hz) */
4709 *delay_on = 500; /* ms */
4710 *delay_off = 500; /* ms */
4711 } else if ((*delay_on != 500) || (*delay_off != 500))
4712 return -EINVAL;
4714 data->new_brightness = TPACPI_LED_BLINK;
4715 queue_work(tpacpi_wq, &data->work);
4717 return 0;
4720 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4722 int rc;
4724 struct tpacpi_led_classdev *data = container_of(led_cdev,
4725 struct tpacpi_led_classdev, led_classdev);
4727 rc = led_get_status(data->led);
4729 if (rc == TPACPI_LED_OFF || rc < 0)
4730 rc = LED_OFF; /* no error handling in led class :( */
4731 else
4732 rc = LED_FULL;
4734 return rc;
4737 static void led_exit(void)
4739 unsigned int i;
4741 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4742 if (tpacpi_leds[i].led_classdev.name)
4743 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4746 kfree(tpacpi_leds);
4749 static int __init led_init(struct ibm_init_struct *iibm)
4751 unsigned int i;
4752 int rc;
4754 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4756 TPACPI_ACPIHANDLE_INIT(led);
4758 if (!led_handle)
4759 /* led not supported on R30, R31 */
4760 led_supported = TPACPI_LED_NONE;
4761 else if (strlencmp(led_path, "SLED") == 0)
4762 /* 570 */
4763 led_supported = TPACPI_LED_570;
4764 else if (strlencmp(led_path, "SYSL") == 0)
4765 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4766 led_supported = TPACPI_LED_OLD;
4767 else
4768 /* all others */
4769 led_supported = TPACPI_LED_NEW;
4771 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4772 str_supported(led_supported), led_supported);
4774 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4775 GFP_KERNEL);
4776 if (!tpacpi_leds) {
4777 printk(TPACPI_ERR "Out of memory for LED data\n");
4778 return -ENOMEM;
4781 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4782 tpacpi_leds[i].led = i;
4784 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4785 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4786 if (led_supported == TPACPI_LED_570)
4787 tpacpi_leds[i].led_classdev.brightness_get =
4788 &led_sysfs_get;
4790 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4792 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4794 rc = led_classdev_register(&tpacpi_pdev->dev,
4795 &tpacpi_leds[i].led_classdev);
4796 if (rc < 0) {
4797 tpacpi_leds[i].led_classdev.name = NULL;
4798 led_exit();
4799 return rc;
4803 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4806 #define str_led_status(s) \
4807 ((s) == TPACPI_LED_OFF ? "off" : \
4808 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4810 static int led_read(char *p)
4812 int len = 0;
4814 if (!led_supported) {
4815 len += sprintf(p + len, "status:\t\tnot supported\n");
4816 return len;
4818 len += sprintf(p + len, "status:\t\tsupported\n");
4820 if (led_supported == TPACPI_LED_570) {
4821 /* 570 */
4822 int i, status;
4823 for (i = 0; i < 8; i++) {
4824 status = led_get_status(i);
4825 if (status < 0)
4826 return -EIO;
4827 len += sprintf(p + len, "%d:\t\t%s\n",
4828 i, str_led_status(status));
4832 len += sprintf(p + len, "commands:\t"
4833 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4835 return len;
4838 static int led_write(char *buf)
4840 char *cmd;
4841 int led, rc;
4842 enum led_status_t s;
4844 if (!led_supported)
4845 return -ENODEV;
4847 while ((cmd = next_cmd(&buf))) {
4848 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4849 return -EINVAL;
4851 if (strstr(cmd, "off")) {
4852 s = TPACPI_LED_OFF;
4853 } else if (strstr(cmd, "on")) {
4854 s = TPACPI_LED_ON;
4855 } else if (strstr(cmd, "blink")) {
4856 s = TPACPI_LED_BLINK;
4857 } else {
4858 return -EINVAL;
4861 rc = led_set_status(led, s);
4862 if (rc < 0)
4863 return rc;
4866 return 0;
4869 static struct ibm_struct led_driver_data = {
4870 .name = "led",
4871 .read = led_read,
4872 .write = led_write,
4873 .exit = led_exit,
4876 /*************************************************************************
4877 * Beep subdriver
4880 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4882 static int __init beep_init(struct ibm_init_struct *iibm)
4884 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4886 TPACPI_ACPIHANDLE_INIT(beep);
4888 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4889 str_supported(beep_handle != NULL));
4891 return (beep_handle)? 0 : 1;
4894 static int beep_read(char *p)
4896 int len = 0;
4898 if (!beep_handle)
4899 len += sprintf(p + len, "status:\t\tnot supported\n");
4900 else {
4901 len += sprintf(p + len, "status:\t\tsupported\n");
4902 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4905 return len;
4908 static int beep_write(char *buf)
4910 char *cmd;
4911 int beep_cmd;
4913 if (!beep_handle)
4914 return -ENODEV;
4916 while ((cmd = next_cmd(&buf))) {
4917 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4918 beep_cmd >= 0 && beep_cmd <= 17) {
4919 /* beep_cmd set */
4920 } else
4921 return -EINVAL;
4922 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4923 return -EIO;
4926 return 0;
4929 static struct ibm_struct beep_driver_data = {
4930 .name = "beep",
4931 .read = beep_read,
4932 .write = beep_write,
4935 /*************************************************************************
4936 * Thermal subdriver
4939 enum thermal_access_mode {
4940 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4941 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4942 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4943 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4944 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4947 enum { /* TPACPI_THERMAL_TPEC_* */
4948 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4949 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4950 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4953 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4954 struct ibm_thermal_sensors_struct {
4955 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4958 static enum thermal_access_mode thermal_read_mode;
4960 /* idx is zero-based */
4961 static int thermal_get_sensor(int idx, s32 *value)
4963 int t;
4964 s8 tmp;
4965 char tmpi[5];
4967 t = TP_EC_THERMAL_TMP0;
4969 switch (thermal_read_mode) {
4970 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4971 case TPACPI_THERMAL_TPEC_16:
4972 if (idx >= 8 && idx <= 15) {
4973 t = TP_EC_THERMAL_TMP8;
4974 idx -= 8;
4976 /* fallthrough */
4977 #endif
4978 case TPACPI_THERMAL_TPEC_8:
4979 if (idx <= 7) {
4980 if (!acpi_ec_read(t + idx, &tmp))
4981 return -EIO;
4982 *value = tmp * 1000;
4983 return 0;
4985 break;
4987 case TPACPI_THERMAL_ACPI_UPDT:
4988 if (idx <= 7) {
4989 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4990 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4991 return -EIO;
4992 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4993 return -EIO;
4994 *value = (t - 2732) * 100;
4995 return 0;
4997 break;
4999 case TPACPI_THERMAL_ACPI_TMP07:
5000 if (idx <= 7) {
5001 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5002 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5003 return -EIO;
5004 if (t > 127 || t < -127)
5005 t = TP_EC_THERMAL_TMP_NA;
5006 *value = t * 1000;
5007 return 0;
5009 break;
5011 case TPACPI_THERMAL_NONE:
5012 default:
5013 return -ENOSYS;
5016 return -EINVAL;
5019 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5021 int res, i;
5022 int n;
5024 n = 8;
5025 i = 0;
5027 if (!s)
5028 return -EINVAL;
5030 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5031 n = 16;
5033 for (i = 0 ; i < n; i++) {
5034 res = thermal_get_sensor(i, &s->temp[i]);
5035 if (res)
5036 return res;
5039 return n;
5042 /* sysfs temp##_input -------------------------------------------------- */
5044 static ssize_t thermal_temp_input_show(struct device *dev,
5045 struct device_attribute *attr,
5046 char *buf)
5048 struct sensor_device_attribute *sensor_attr =
5049 to_sensor_dev_attr(attr);
5050 int idx = sensor_attr->index;
5051 s32 value;
5052 int res;
5054 res = thermal_get_sensor(idx, &value);
5055 if (res)
5056 return res;
5057 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5058 return -ENXIO;
5060 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5063 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5064 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5065 thermal_temp_input_show, NULL, _idxB)
5067 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5068 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5069 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5070 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5071 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5072 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5073 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5074 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5075 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5076 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5077 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5078 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5079 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5080 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5081 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5082 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5083 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5086 #define THERMAL_ATTRS(X) \
5087 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5089 static struct attribute *thermal_temp_input_attr[] = {
5090 THERMAL_ATTRS(8),
5091 THERMAL_ATTRS(9),
5092 THERMAL_ATTRS(10),
5093 THERMAL_ATTRS(11),
5094 THERMAL_ATTRS(12),
5095 THERMAL_ATTRS(13),
5096 THERMAL_ATTRS(14),
5097 THERMAL_ATTRS(15),
5098 THERMAL_ATTRS(0),
5099 THERMAL_ATTRS(1),
5100 THERMAL_ATTRS(2),
5101 THERMAL_ATTRS(3),
5102 THERMAL_ATTRS(4),
5103 THERMAL_ATTRS(5),
5104 THERMAL_ATTRS(6),
5105 THERMAL_ATTRS(7),
5106 NULL
5109 static const struct attribute_group thermal_temp_input16_group = {
5110 .attrs = thermal_temp_input_attr
5113 static const struct attribute_group thermal_temp_input8_group = {
5114 .attrs = &thermal_temp_input_attr[8]
5117 #undef THERMAL_SENSOR_ATTR_TEMP
5118 #undef THERMAL_ATTRS
5120 /* --------------------------------------------------------------------- */
5122 static int __init thermal_init(struct ibm_init_struct *iibm)
5124 u8 t, ta1, ta2;
5125 int i;
5126 int acpi_tmp7;
5127 int res;
5129 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5131 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5133 if (thinkpad_id.ec_model) {
5135 * Direct EC access mode: sensors at registers
5136 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5137 * non-implemented, thermal sensors return 0x80 when
5138 * not available
5141 ta1 = ta2 = 0;
5142 for (i = 0; i < 8; i++) {
5143 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5144 ta1 |= t;
5145 } else {
5146 ta1 = 0;
5147 break;
5149 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5150 ta2 |= t;
5151 } else {
5152 ta1 = 0;
5153 break;
5156 if (ta1 == 0) {
5157 /* This is sheer paranoia, but we handle it anyway */
5158 if (acpi_tmp7) {
5159 printk(TPACPI_ERR
5160 "ThinkPad ACPI EC access misbehaving, "
5161 "falling back to ACPI TMPx access "
5162 "mode\n");
5163 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5164 } else {
5165 printk(TPACPI_ERR
5166 "ThinkPad ACPI EC access misbehaving, "
5167 "disabling thermal sensors access\n");
5168 thermal_read_mode = TPACPI_THERMAL_NONE;
5170 } else {
5171 thermal_read_mode =
5172 (ta2 != 0) ?
5173 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5175 } else if (acpi_tmp7) {
5176 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5177 /* 600e/x, 770e, 770x */
5178 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5179 } else {
5180 /* Standard ACPI TMPx access, max 8 sensors */
5181 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5183 } else {
5184 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5185 thermal_read_mode = TPACPI_THERMAL_NONE;
5188 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5189 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5190 thermal_read_mode);
5192 switch (thermal_read_mode) {
5193 case TPACPI_THERMAL_TPEC_16:
5194 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5195 &thermal_temp_input16_group);
5196 if (res)
5197 return res;
5198 break;
5199 case TPACPI_THERMAL_TPEC_8:
5200 case TPACPI_THERMAL_ACPI_TMP07:
5201 case TPACPI_THERMAL_ACPI_UPDT:
5202 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5203 &thermal_temp_input8_group);
5204 if (res)
5205 return res;
5206 break;
5207 case TPACPI_THERMAL_NONE:
5208 default:
5209 return 1;
5212 return 0;
5215 static void thermal_exit(void)
5217 switch (thermal_read_mode) {
5218 case TPACPI_THERMAL_TPEC_16:
5219 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5220 &thermal_temp_input16_group);
5221 break;
5222 case TPACPI_THERMAL_TPEC_8:
5223 case TPACPI_THERMAL_ACPI_TMP07:
5224 case TPACPI_THERMAL_ACPI_UPDT:
5225 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5226 &thermal_temp_input16_group);
5227 break;
5228 case TPACPI_THERMAL_NONE:
5229 default:
5230 break;
5234 static int thermal_read(char *p)
5236 int len = 0;
5237 int n, i;
5238 struct ibm_thermal_sensors_struct t;
5240 n = thermal_get_sensors(&t);
5241 if (unlikely(n < 0))
5242 return n;
5244 len += sprintf(p + len, "temperatures:\t");
5246 if (n > 0) {
5247 for (i = 0; i < (n - 1); i++)
5248 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5249 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5250 } else
5251 len += sprintf(p + len, "not supported\n");
5253 return len;
5256 static struct ibm_struct thermal_driver_data = {
5257 .name = "thermal",
5258 .read = thermal_read,
5259 .exit = thermal_exit,
5262 /*************************************************************************
5263 * EC Dump subdriver
5266 static u8 ecdump_regs[256];
5268 static int ecdump_read(char *p)
5270 int len = 0;
5271 int i, j;
5272 u8 v;
5274 len += sprintf(p + len, "EC "
5275 " +00 +01 +02 +03 +04 +05 +06 +07"
5276 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5277 for (i = 0; i < 256; i += 16) {
5278 len += sprintf(p + len, "EC 0x%02x:", i);
5279 for (j = 0; j < 16; j++) {
5280 if (!acpi_ec_read(i + j, &v))
5281 break;
5282 if (v != ecdump_regs[i + j])
5283 len += sprintf(p + len, " *%02x", v);
5284 else
5285 len += sprintf(p + len, " %02x", v);
5286 ecdump_regs[i + j] = v;
5288 len += sprintf(p + len, "\n");
5289 if (j != 16)
5290 break;
5293 /* These are way too dangerous to advertise openly... */
5294 #if 0
5295 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5296 " (<offset> is 00-ff, <value> is 00-ff)\n");
5297 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5298 " (<offset> is 00-ff, <value> is 0-255)\n");
5299 #endif
5300 return len;
5303 static int ecdump_write(char *buf)
5305 char *cmd;
5306 int i, v;
5308 while ((cmd = next_cmd(&buf))) {
5309 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5310 /* i and v set */
5311 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5312 /* i and v set */
5313 } else
5314 return -EINVAL;
5315 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5316 if (!acpi_ec_write(i, v))
5317 return -EIO;
5318 } else
5319 return -EINVAL;
5322 return 0;
5325 static struct ibm_struct ecdump_driver_data = {
5326 .name = "ecdump",
5327 .read = ecdump_read,
5328 .write = ecdump_write,
5329 .flags.experimental = 1,
5332 /*************************************************************************
5333 * Backlight/brightness subdriver
5336 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5338 enum {
5339 TP_EC_BACKLIGHT = 0x31,
5341 /* TP_EC_BACKLIGHT bitmasks */
5342 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5343 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5344 TP_EC_BACKLIGHT_MAPSW = 0x20,
5347 static struct backlight_device *ibm_backlight_device;
5348 static int brightness_mode;
5349 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5351 static struct mutex brightness_mutex;
5354 * ThinkPads can read brightness from two places: EC 0x31, or
5355 * CMOS NVRAM byte 0x5E, bits 0-3.
5357 * EC 0x31 has the following layout
5358 * Bit 7: unknown function
5359 * Bit 6: unknown function
5360 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5361 * Bit 4: must be set to zero to avoid problems
5362 * Bit 3-0: backlight brightness level
5364 * brightness_get_raw returns status data in the EC 0x31 layout
5366 static int brightness_get_raw(int *status)
5368 u8 lec = 0, lcmos = 0, level = 0;
5370 if (brightness_mode & 1) {
5371 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5372 return -EIO;
5373 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5375 if (brightness_mode & 2) {
5376 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5377 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5378 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5379 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5380 level = lcmos;
5383 if (brightness_mode == 3) {
5384 *status = lec; /* Prefer EC, CMOS is just a backing store */
5385 lec &= TP_EC_BACKLIGHT_LVLMSK;
5386 if (lec == lcmos)
5387 tp_warned.bright_cmos_ec_unsync = 0;
5388 else {
5389 if (!tp_warned.bright_cmos_ec_unsync) {
5390 printk(TPACPI_ERR
5391 "CMOS NVRAM (%u) and EC (%u) do not "
5392 "agree on display brightness level\n",
5393 (unsigned int) lcmos,
5394 (unsigned int) lec);
5395 tp_warned.bright_cmos_ec_unsync = 1;
5397 return -EIO;
5399 } else {
5400 *status = level;
5403 return 0;
5406 /* May return EINTR which can always be mapped to ERESTARTSYS */
5407 static int brightness_set(int value)
5409 int cmos_cmd, inc, i, res;
5410 int current_value;
5411 int command_bits;
5413 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5414 value < 0)
5415 return -EINVAL;
5417 res = mutex_lock_killable(&brightness_mutex);
5418 if (res < 0)
5419 return res;
5421 res = brightness_get_raw(&current_value);
5422 if (res < 0)
5423 goto errout;
5425 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5426 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5428 cmos_cmd = value > current_value ?
5429 TP_CMOS_BRIGHTNESS_UP :
5430 TP_CMOS_BRIGHTNESS_DOWN;
5431 inc = (value > current_value)? 1 : -1;
5433 res = 0;
5434 for (i = current_value; i != value; i += inc) {
5435 if ((brightness_mode & 2) &&
5436 issue_thinkpad_cmos_command(cmos_cmd)) {
5437 res = -EIO;
5438 goto errout;
5440 if ((brightness_mode & 1) &&
5441 !acpi_ec_write(TP_EC_BACKLIGHT,
5442 (i + inc) | command_bits)) {
5443 res = -EIO;
5444 goto errout;;
5448 errout:
5449 mutex_unlock(&brightness_mutex);
5450 return res;
5453 /* sysfs backlight class ----------------------------------------------- */
5455 static int brightness_update_status(struct backlight_device *bd)
5457 /* it is the backlight class's job (caller) to handle
5458 * EINTR and other errors properly */
5459 return brightness_set(
5460 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5461 bd->props.power == FB_BLANK_UNBLANK) ?
5462 bd->props.brightness : 0);
5465 static int brightness_get(struct backlight_device *bd)
5467 int status, res;
5469 res = brightness_get_raw(&status);
5470 if (res < 0)
5471 return 0; /* FIXME: teach backlight about error handling */
5473 return status & TP_EC_BACKLIGHT_LVLMSK;
5476 static struct backlight_ops ibm_backlight_data = {
5477 .get_brightness = brightness_get,
5478 .update_status = brightness_update_status,
5481 /* --------------------------------------------------------------------- */
5483 static int __init brightness_init(struct ibm_init_struct *iibm)
5485 int b;
5487 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5489 mutex_init(&brightness_mutex);
5492 * We always attempt to detect acpi support, so as to switch
5493 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5494 * going to publish a backlight interface
5496 b = tpacpi_check_std_acpi_brightness_support();
5497 if (b > 0) {
5498 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5499 printk(TPACPI_NOTICE
5500 "Lenovo BIOS switched to ACPI backlight "
5501 "control mode\n");
5503 if (brightness_enable > 1) {
5504 printk(TPACPI_NOTICE
5505 "standard ACPI backlight interface "
5506 "available, not loading native one...\n");
5507 return 1;
5511 if (!brightness_enable) {
5512 dbg_printk(TPACPI_DBG_INIT,
5513 "brightness support disabled by "
5514 "module parameter\n");
5515 return 1;
5518 if (b > 16) {
5519 printk(TPACPI_ERR
5520 "Unsupported brightness interface, "
5521 "please contact %s\n", TPACPI_MAIL);
5522 return 1;
5524 if (b == 16)
5525 tp_features.bright_16levels = 1;
5527 if (!brightness_mode) {
5528 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5529 brightness_mode = 2;
5530 else
5531 brightness_mode = 3;
5533 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5534 brightness_mode);
5537 if (brightness_mode > 3)
5538 return -EINVAL;
5540 if (brightness_get_raw(&b) < 0)
5541 return 1;
5543 if (tp_features.bright_16levels)
5544 printk(TPACPI_INFO
5545 "detected a 16-level brightness capable ThinkPad\n");
5547 ibm_backlight_device = backlight_device_register(
5548 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5549 &ibm_backlight_data);
5550 if (IS_ERR(ibm_backlight_device)) {
5551 printk(TPACPI_ERR "Could not register backlight device\n");
5552 return PTR_ERR(ibm_backlight_device);
5554 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5556 ibm_backlight_device->props.max_brightness =
5557 (tp_features.bright_16levels)? 15 : 7;
5558 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5559 backlight_update_status(ibm_backlight_device);
5561 return 0;
5564 static void brightness_exit(void)
5566 if (ibm_backlight_device) {
5567 vdbg_printk(TPACPI_DBG_EXIT,
5568 "calling backlight_device_unregister()\n");
5569 backlight_device_unregister(ibm_backlight_device);
5573 static int brightness_read(char *p)
5575 int len = 0;
5576 int level;
5578 level = brightness_get(NULL);
5579 if (level < 0) {
5580 len += sprintf(p + len, "level:\t\tunreadable\n");
5581 } else {
5582 len += sprintf(p + len, "level:\t\t%d\n", level);
5583 len += sprintf(p + len, "commands:\tup, down\n");
5584 len += sprintf(p + len, "commands:\tlevel <level>"
5585 " (<level> is 0-%d)\n",
5586 (tp_features.bright_16levels) ? 15 : 7);
5589 return len;
5592 static int brightness_write(char *buf)
5594 int level;
5595 int rc;
5596 char *cmd;
5597 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5599 level = brightness_get(NULL);
5600 if (level < 0)
5601 return level;
5603 while ((cmd = next_cmd(&buf))) {
5604 if (strlencmp(cmd, "up") == 0) {
5605 if (level < max_level)
5606 level++;
5607 } else if (strlencmp(cmd, "down") == 0) {
5608 if (level > 0)
5609 level--;
5610 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5611 level >= 0 && level <= max_level) {
5612 /* new level set */
5613 } else
5614 return -EINVAL;
5618 * Now we know what the final level should be, so we try to set it.
5619 * Doing it this way makes the syscall restartable in case of EINTR
5621 rc = brightness_set(level);
5622 return (rc == -EINTR)? ERESTARTSYS : rc;
5625 static struct ibm_struct brightness_driver_data = {
5626 .name = "brightness",
5627 .read = brightness_read,
5628 .write = brightness_write,
5629 .exit = brightness_exit,
5632 /*************************************************************************
5633 * Volume subdriver
5636 static int volume_offset = 0x30;
5638 static int volume_read(char *p)
5640 int len = 0;
5641 u8 level;
5643 if (!acpi_ec_read(volume_offset, &level)) {
5644 len += sprintf(p + len, "level:\t\tunreadable\n");
5645 } else {
5646 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5647 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5648 len += sprintf(p + len, "commands:\tup, down, mute\n");
5649 len += sprintf(p + len, "commands:\tlevel <level>"
5650 " (<level> is 0-15)\n");
5653 return len;
5656 static int volume_write(char *buf)
5658 int cmos_cmd, inc, i;
5659 u8 level, mute;
5660 int new_level, new_mute;
5661 char *cmd;
5663 while ((cmd = next_cmd(&buf))) {
5664 if (!acpi_ec_read(volume_offset, &level))
5665 return -EIO;
5666 new_mute = mute = level & 0x40;
5667 new_level = level = level & 0xf;
5669 if (strlencmp(cmd, "up") == 0) {
5670 if (mute)
5671 new_mute = 0;
5672 else
5673 new_level = level == 15 ? 15 : level + 1;
5674 } else if (strlencmp(cmd, "down") == 0) {
5675 if (mute)
5676 new_mute = 0;
5677 else
5678 new_level = level == 0 ? 0 : level - 1;
5679 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5680 new_level >= 0 && new_level <= 15) {
5681 /* new_level set */
5682 } else if (strlencmp(cmd, "mute") == 0) {
5683 new_mute = 0x40;
5684 } else
5685 return -EINVAL;
5687 if (new_level != level) {
5688 /* mute doesn't change */
5690 cmos_cmd = (new_level > level) ?
5691 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5692 inc = new_level > level ? 1 : -1;
5694 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5695 !acpi_ec_write(volume_offset, level)))
5696 return -EIO;
5698 for (i = level; i != new_level; i += inc)
5699 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5700 !acpi_ec_write(volume_offset, i + inc))
5701 return -EIO;
5703 if (mute &&
5704 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5705 !acpi_ec_write(volume_offset, new_level + mute))) {
5706 return -EIO;
5710 if (new_mute != mute) {
5711 /* level doesn't change */
5713 cmos_cmd = (new_mute) ?
5714 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5716 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5717 !acpi_ec_write(volume_offset, level + new_mute))
5718 return -EIO;
5722 return 0;
5725 static struct ibm_struct volume_driver_data = {
5726 .name = "volume",
5727 .read = volume_read,
5728 .write = volume_write,
5731 /*************************************************************************
5732 * Fan subdriver
5736 * FAN ACCESS MODES
5738 * TPACPI_FAN_RD_ACPI_GFAN:
5739 * ACPI GFAN method: returns fan level
5741 * see TPACPI_FAN_WR_ACPI_SFAN
5742 * EC 0x2f (HFSP) not available if GFAN exists
5744 * TPACPI_FAN_WR_ACPI_SFAN:
5745 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5747 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5748 * it for writing.
5750 * TPACPI_FAN_WR_TPEC:
5751 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5752 * Supported on almost all ThinkPads
5754 * Fan speed changes of any sort (including those caused by the
5755 * disengaged mode) are usually done slowly by the firmware as the
5756 * maximum ammount of fan duty cycle change per second seems to be
5757 * limited.
5759 * Reading is not available if GFAN exists.
5760 * Writing is not available if SFAN exists.
5762 * Bits
5763 * 7 automatic mode engaged;
5764 * (default operation mode of the ThinkPad)
5765 * fan level is ignored in this mode.
5766 * 6 full speed mode (takes precedence over bit 7);
5767 * not available on all thinkpads. May disable
5768 * the tachometer while the fan controller ramps up
5769 * the speed (which can take up to a few *minutes*).
5770 * Speeds up fan to 100% duty-cycle, which is far above
5771 * the standard RPM levels. It is not impossible that
5772 * it could cause hardware damage.
5773 * 5-3 unused in some models. Extra bits for fan level
5774 * in others, but still useless as all values above
5775 * 7 map to the same speed as level 7 in these models.
5776 * 2-0 fan level (0..7 usually)
5777 * 0x00 = stop
5778 * 0x07 = max (set when temperatures critical)
5779 * Some ThinkPads may have other levels, see
5780 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5782 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5783 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5784 * does so, its initial value is meaningless (0x07).
5786 * For firmware bugs, refer to:
5787 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5789 * ----
5791 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5792 * Main fan tachometer reading (in RPM)
5794 * This register is present on all ThinkPads with a new-style EC, and
5795 * it is known not to be present on the A21m/e, and T22, as there is
5796 * something else in offset 0x84 according to the ACPI DSDT. Other
5797 * ThinkPads from this same time period (and earlier) probably lack the
5798 * tachometer as well.
5800 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5801 * was never fixed by IBM to report the EC firmware version string
5802 * probably support the tachometer (like the early X models), so
5803 * detecting it is quite hard. We need more data to know for sure.
5805 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5806 * might result.
5808 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5809 * mode.
5811 * For firmware bugs, refer to:
5812 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5814 * TPACPI_FAN_WR_ACPI_FANS:
5815 * ThinkPad X31, X40, X41. Not available in the X60.
5817 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5818 * high speed. ACPI DSDT seems to map these three speeds to levels
5819 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5820 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5822 * The speeds are stored on handles
5823 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5825 * There are three default speed sets, acessible as handles:
5826 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5828 * ACPI DSDT switches which set is in use depending on various
5829 * factors.
5831 * TPACPI_FAN_WR_TPEC is also available and should be used to
5832 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5833 * but the ACPI tables just mention level 7.
5836 enum { /* Fan control constants */
5837 fan_status_offset = 0x2f, /* EC register 0x2f */
5838 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5839 * 0x84 must be read before 0x85 */
5841 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5842 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5844 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5847 enum fan_status_access_mode {
5848 TPACPI_FAN_NONE = 0, /* No fan status or control */
5849 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5850 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5853 enum fan_control_access_mode {
5854 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5855 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5856 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5857 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5860 enum fan_control_commands {
5861 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5862 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5863 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5864 * and also watchdog cmd */
5867 static int fan_control_allowed;
5869 static enum fan_status_access_mode fan_status_access_mode;
5870 static enum fan_control_access_mode fan_control_access_mode;
5871 static enum fan_control_commands fan_control_commands;
5873 static u8 fan_control_initial_status;
5874 static u8 fan_control_desired_level;
5875 static u8 fan_control_resume_level;
5876 static int fan_watchdog_maxinterval;
5878 static struct mutex fan_mutex;
5880 static void fan_watchdog_fire(struct work_struct *ignored);
5881 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5883 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5884 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5885 "\\FSPD", /* 600e/x, 770e, 770x */
5886 ); /* all others */
5887 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5888 "JFNS", /* 770x-JL */
5889 ); /* all others */
5892 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5893 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5894 * be in auto mode (0x80).
5896 * This is corrected by any write to HFSP either by the driver, or
5897 * by the firmware.
5899 * We assume 0x07 really means auto mode while this quirk is active,
5900 * as this is far more likely than the ThinkPad being in level 7,
5901 * which is only used by the firmware during thermal emergencies.
5904 static void fan_quirk1_detect(void)
5906 /* In some ThinkPads, neither the EC nor the ACPI
5907 * DSDT initialize the HFSP register, and it ends up
5908 * being initially set to 0x07 when it *could* be
5909 * either 0x07 or 0x80.
5911 * Enable for TP-1Y (T43), TP-78 (R51e),
5912 * TP-76 (R52), TP-70 (T43, R52), which are known
5913 * to be buggy. */
5914 if (fan_control_initial_status == 0x07) {
5915 switch (thinkpad_id.ec_model) {
5916 case 0x5931: /* TP-1Y */
5917 case 0x3837: /* TP-78 */
5918 case 0x3637: /* TP-76 */
5919 case 0x3037: /* TP-70 */
5920 printk(TPACPI_NOTICE
5921 "fan_init: initial fan status is unknown, "
5922 "assuming it is in auto mode\n");
5923 tp_features.fan_ctrl_status_undef = 1;
5929 static void fan_quirk1_handle(u8 *fan_status)
5931 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5932 if (*fan_status != fan_control_initial_status) {
5933 /* something changed the HFSP regisnter since
5934 * driver init time, so it is not undefined
5935 * anymore */
5936 tp_features.fan_ctrl_status_undef = 0;
5937 } else {
5938 /* Return most likely status. In fact, it
5939 * might be the only possible status */
5940 *fan_status = TP_EC_FAN_AUTO;
5946 * Call with fan_mutex held
5948 static void fan_update_desired_level(u8 status)
5950 if ((status &
5951 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5952 if (status > 7)
5953 fan_control_desired_level = 7;
5954 else
5955 fan_control_desired_level = status;
5959 static int fan_get_status(u8 *status)
5961 u8 s;
5963 /* TODO:
5964 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5966 switch (fan_status_access_mode) {
5967 case TPACPI_FAN_RD_ACPI_GFAN:
5968 /* 570, 600e/x, 770e, 770x */
5970 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5971 return -EIO;
5973 if (likely(status))
5974 *status = s & 0x07;
5976 break;
5978 case TPACPI_FAN_RD_TPEC:
5979 /* all except 570, 600e/x, 770e, 770x */
5980 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5981 return -EIO;
5983 if (likely(status)) {
5984 *status = s;
5985 fan_quirk1_handle(status);
5988 break;
5990 default:
5991 return -ENXIO;
5994 return 0;
5997 static int fan_get_status_safe(u8 *status)
5999 int rc;
6000 u8 s;
6002 if (mutex_lock_killable(&fan_mutex))
6003 return -ERESTARTSYS;
6004 rc = fan_get_status(&s);
6005 if (!rc)
6006 fan_update_desired_level(s);
6007 mutex_unlock(&fan_mutex);
6009 if (status)
6010 *status = s;
6012 return rc;
6015 static int fan_get_speed(unsigned int *speed)
6017 u8 hi, lo;
6019 switch (fan_status_access_mode) {
6020 case TPACPI_FAN_RD_TPEC:
6021 /* all except 570, 600e/x, 770e, 770x */
6022 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6023 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6024 return -EIO;
6026 if (likely(speed))
6027 *speed = (hi << 8) | lo;
6029 break;
6031 default:
6032 return -ENXIO;
6035 return 0;
6038 static int fan_set_level(int level)
6040 if (!fan_control_allowed)
6041 return -EPERM;
6043 switch (fan_control_access_mode) {
6044 case TPACPI_FAN_WR_ACPI_SFAN:
6045 if (level >= 0 && level <= 7) {
6046 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6047 return -EIO;
6048 } else
6049 return -EINVAL;
6050 break;
6052 case TPACPI_FAN_WR_ACPI_FANS:
6053 case TPACPI_FAN_WR_TPEC:
6054 if (!(level & TP_EC_FAN_AUTO) &&
6055 !(level & TP_EC_FAN_FULLSPEED) &&
6056 ((level < 0) || (level > 7)))
6057 return -EINVAL;
6059 /* safety net should the EC not support AUTO
6060 * or FULLSPEED mode bits and just ignore them */
6061 if (level & TP_EC_FAN_FULLSPEED)
6062 level |= 7; /* safety min speed 7 */
6063 else if (level & TP_EC_FAN_AUTO)
6064 level |= 4; /* safety min speed 4 */
6066 if (!acpi_ec_write(fan_status_offset, level))
6067 return -EIO;
6068 else
6069 tp_features.fan_ctrl_status_undef = 0;
6070 break;
6072 default:
6073 return -ENXIO;
6075 return 0;
6078 static int fan_set_level_safe(int level)
6080 int rc;
6082 if (!fan_control_allowed)
6083 return -EPERM;
6085 if (mutex_lock_killable(&fan_mutex))
6086 return -ERESTARTSYS;
6088 if (level == TPACPI_FAN_LAST_LEVEL)
6089 level = fan_control_desired_level;
6091 rc = fan_set_level(level);
6092 if (!rc)
6093 fan_update_desired_level(level);
6095 mutex_unlock(&fan_mutex);
6096 return rc;
6099 static int fan_set_enable(void)
6101 u8 s;
6102 int rc;
6104 if (!fan_control_allowed)
6105 return -EPERM;
6107 if (mutex_lock_killable(&fan_mutex))
6108 return -ERESTARTSYS;
6110 switch (fan_control_access_mode) {
6111 case TPACPI_FAN_WR_ACPI_FANS:
6112 case TPACPI_FAN_WR_TPEC:
6113 rc = fan_get_status(&s);
6114 if (rc < 0)
6115 break;
6117 /* Don't go out of emergency fan mode */
6118 if (s != 7) {
6119 s &= 0x07;
6120 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6123 if (!acpi_ec_write(fan_status_offset, s))
6124 rc = -EIO;
6125 else {
6126 tp_features.fan_ctrl_status_undef = 0;
6127 rc = 0;
6129 break;
6131 case TPACPI_FAN_WR_ACPI_SFAN:
6132 rc = fan_get_status(&s);
6133 if (rc < 0)
6134 break;
6136 s &= 0x07;
6138 /* Set fan to at least level 4 */
6139 s |= 4;
6141 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6142 rc = -EIO;
6143 else
6144 rc = 0;
6145 break;
6147 default:
6148 rc = -ENXIO;
6151 mutex_unlock(&fan_mutex);
6152 return rc;
6155 static int fan_set_disable(void)
6157 int rc;
6159 if (!fan_control_allowed)
6160 return -EPERM;
6162 if (mutex_lock_killable(&fan_mutex))
6163 return -ERESTARTSYS;
6165 rc = 0;
6166 switch (fan_control_access_mode) {
6167 case TPACPI_FAN_WR_ACPI_FANS:
6168 case TPACPI_FAN_WR_TPEC:
6169 if (!acpi_ec_write(fan_status_offset, 0x00))
6170 rc = -EIO;
6171 else {
6172 fan_control_desired_level = 0;
6173 tp_features.fan_ctrl_status_undef = 0;
6175 break;
6177 case TPACPI_FAN_WR_ACPI_SFAN:
6178 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6179 rc = -EIO;
6180 else
6181 fan_control_desired_level = 0;
6182 break;
6184 default:
6185 rc = -ENXIO;
6189 mutex_unlock(&fan_mutex);
6190 return rc;
6193 static int fan_set_speed(int speed)
6195 int rc;
6197 if (!fan_control_allowed)
6198 return -EPERM;
6200 if (mutex_lock_killable(&fan_mutex))
6201 return -ERESTARTSYS;
6203 rc = 0;
6204 switch (fan_control_access_mode) {
6205 case TPACPI_FAN_WR_ACPI_FANS:
6206 if (speed >= 0 && speed <= 65535) {
6207 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6208 speed, speed, speed))
6209 rc = -EIO;
6210 } else
6211 rc = -EINVAL;
6212 break;
6214 default:
6215 rc = -ENXIO;
6218 mutex_unlock(&fan_mutex);
6219 return rc;
6222 static void fan_watchdog_reset(void)
6224 static int fan_watchdog_active;
6226 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6227 return;
6229 if (fan_watchdog_active)
6230 cancel_delayed_work(&fan_watchdog_task);
6232 if (fan_watchdog_maxinterval > 0 &&
6233 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6234 fan_watchdog_active = 1;
6235 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6236 msecs_to_jiffies(fan_watchdog_maxinterval
6237 * 1000))) {
6238 printk(TPACPI_ERR
6239 "failed to queue the fan watchdog, "
6240 "watchdog will not trigger\n");
6242 } else
6243 fan_watchdog_active = 0;
6246 static void fan_watchdog_fire(struct work_struct *ignored)
6248 int rc;
6250 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6251 return;
6253 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6254 rc = fan_set_enable();
6255 if (rc < 0) {
6256 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6257 "will try again later...\n", -rc);
6258 /* reschedule for later */
6259 fan_watchdog_reset();
6264 * SYSFS fan layout: hwmon compatible (device)
6266 * pwm*_enable:
6267 * 0: "disengaged" mode
6268 * 1: manual mode
6269 * 2: native EC "auto" mode (recommended, hardware default)
6271 * pwm*: set speed in manual mode, ignored otherwise.
6272 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6273 * interpolation.
6275 * fan*_input: tachometer reading, RPM
6278 * SYSFS fan layout: extensions
6280 * fan_watchdog (driver):
6281 * fan watchdog interval in seconds, 0 disables (default), max 120
6284 /* sysfs fan pwm1_enable ----------------------------------------------- */
6285 static ssize_t fan_pwm1_enable_show(struct device *dev,
6286 struct device_attribute *attr,
6287 char *buf)
6289 int res, mode;
6290 u8 status;
6292 res = fan_get_status_safe(&status);
6293 if (res)
6294 return res;
6296 if (status & TP_EC_FAN_FULLSPEED) {
6297 mode = 0;
6298 } else if (status & TP_EC_FAN_AUTO) {
6299 mode = 2;
6300 } else
6301 mode = 1;
6303 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6306 static ssize_t fan_pwm1_enable_store(struct device *dev,
6307 struct device_attribute *attr,
6308 const char *buf, size_t count)
6310 unsigned long t;
6311 int res, level;
6313 if (parse_strtoul(buf, 2, &t))
6314 return -EINVAL;
6316 switch (t) {
6317 case 0:
6318 level = TP_EC_FAN_FULLSPEED;
6319 break;
6320 case 1:
6321 level = TPACPI_FAN_LAST_LEVEL;
6322 break;
6323 case 2:
6324 level = TP_EC_FAN_AUTO;
6325 break;
6326 case 3:
6327 /* reserved for software-controlled auto mode */
6328 return -ENOSYS;
6329 default:
6330 return -EINVAL;
6333 res = fan_set_level_safe(level);
6334 if (res == -ENXIO)
6335 return -EINVAL;
6336 else if (res < 0)
6337 return res;
6339 fan_watchdog_reset();
6341 return count;
6344 static struct device_attribute dev_attr_fan_pwm1_enable =
6345 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6346 fan_pwm1_enable_show, fan_pwm1_enable_store);
6348 /* sysfs fan pwm1 ------------------------------------------------------ */
6349 static ssize_t fan_pwm1_show(struct device *dev,
6350 struct device_attribute *attr,
6351 char *buf)
6353 int res;
6354 u8 status;
6356 res = fan_get_status_safe(&status);
6357 if (res)
6358 return res;
6360 if ((status &
6361 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6362 status = fan_control_desired_level;
6364 if (status > 7)
6365 status = 7;
6367 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6370 static ssize_t fan_pwm1_store(struct device *dev,
6371 struct device_attribute *attr,
6372 const char *buf, size_t count)
6374 unsigned long s;
6375 int rc;
6376 u8 status, newlevel;
6378 if (parse_strtoul(buf, 255, &s))
6379 return -EINVAL;
6381 /* scale down from 0-255 to 0-7 */
6382 newlevel = (s >> 5) & 0x07;
6384 if (mutex_lock_killable(&fan_mutex))
6385 return -ERESTARTSYS;
6387 rc = fan_get_status(&status);
6388 if (!rc && (status &
6389 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6390 rc = fan_set_level(newlevel);
6391 if (rc == -ENXIO)
6392 rc = -EINVAL;
6393 else if (!rc) {
6394 fan_update_desired_level(newlevel);
6395 fan_watchdog_reset();
6399 mutex_unlock(&fan_mutex);
6400 return (rc)? rc : count;
6403 static struct device_attribute dev_attr_fan_pwm1 =
6404 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6405 fan_pwm1_show, fan_pwm1_store);
6407 /* sysfs fan fan1_input ------------------------------------------------ */
6408 static ssize_t fan_fan1_input_show(struct device *dev,
6409 struct device_attribute *attr,
6410 char *buf)
6412 int res;
6413 unsigned int speed;
6415 res = fan_get_speed(&speed);
6416 if (res < 0)
6417 return res;
6419 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6422 static struct device_attribute dev_attr_fan_fan1_input =
6423 __ATTR(fan1_input, S_IRUGO,
6424 fan_fan1_input_show, NULL);
6426 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6427 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6428 char *buf)
6430 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6433 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6434 const char *buf, size_t count)
6436 unsigned long t;
6438 if (parse_strtoul(buf, 120, &t))
6439 return -EINVAL;
6441 if (!fan_control_allowed)
6442 return -EPERM;
6444 fan_watchdog_maxinterval = t;
6445 fan_watchdog_reset();
6447 return count;
6450 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6451 fan_fan_watchdog_show, fan_fan_watchdog_store);
6453 /* --------------------------------------------------------------------- */
6454 static struct attribute *fan_attributes[] = {
6455 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6456 &dev_attr_fan_fan1_input.attr,
6457 NULL
6460 static const struct attribute_group fan_attr_group = {
6461 .attrs = fan_attributes,
6464 static int __init fan_init(struct ibm_init_struct *iibm)
6466 int rc;
6468 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6470 mutex_init(&fan_mutex);
6471 fan_status_access_mode = TPACPI_FAN_NONE;
6472 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6473 fan_control_commands = 0;
6474 fan_watchdog_maxinterval = 0;
6475 tp_features.fan_ctrl_status_undef = 0;
6476 fan_control_desired_level = 7;
6478 TPACPI_ACPIHANDLE_INIT(fans);
6479 TPACPI_ACPIHANDLE_INIT(gfan);
6480 TPACPI_ACPIHANDLE_INIT(sfan);
6482 if (gfan_handle) {
6483 /* 570, 600e/x, 770e, 770x */
6484 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6485 } else {
6486 /* all other ThinkPads: note that even old-style
6487 * ThinkPad ECs supports the fan control register */
6488 if (likely(acpi_ec_read(fan_status_offset,
6489 &fan_control_initial_status))) {
6490 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6491 fan_quirk1_detect();
6492 } else {
6493 printk(TPACPI_ERR
6494 "ThinkPad ACPI EC access misbehaving, "
6495 "fan status and control unavailable\n");
6496 return 1;
6500 if (sfan_handle) {
6501 /* 570, 770x-JL */
6502 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6503 fan_control_commands |=
6504 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6505 } else {
6506 if (!gfan_handle) {
6507 /* gfan without sfan means no fan control */
6508 /* all other models implement TP EC 0x2f control */
6510 if (fans_handle) {
6511 /* X31, X40, X41 */
6512 fan_control_access_mode =
6513 TPACPI_FAN_WR_ACPI_FANS;
6514 fan_control_commands |=
6515 TPACPI_FAN_CMD_SPEED |
6516 TPACPI_FAN_CMD_LEVEL |
6517 TPACPI_FAN_CMD_ENABLE;
6518 } else {
6519 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6520 fan_control_commands |=
6521 TPACPI_FAN_CMD_LEVEL |
6522 TPACPI_FAN_CMD_ENABLE;
6527 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6528 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6529 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6530 fan_status_access_mode, fan_control_access_mode);
6532 /* fan control master switch */
6533 if (!fan_control_allowed) {
6534 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6535 fan_control_commands = 0;
6536 dbg_printk(TPACPI_DBG_INIT,
6537 "fan control features disabled by parameter\n");
6540 /* update fan_control_desired_level */
6541 if (fan_status_access_mode != TPACPI_FAN_NONE)
6542 fan_get_status_safe(NULL);
6544 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6545 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6546 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6547 &fan_attr_group);
6548 if (rc < 0)
6549 return rc;
6551 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6552 &driver_attr_fan_watchdog);
6553 if (rc < 0) {
6554 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6555 &fan_attr_group);
6556 return rc;
6558 return 0;
6559 } else
6560 return 1;
6563 static void fan_exit(void)
6565 vdbg_printk(TPACPI_DBG_EXIT,
6566 "cancelling any pending fan watchdog tasks\n");
6568 /* FIXME: can we really do this unconditionally? */
6569 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6570 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6571 &driver_attr_fan_watchdog);
6573 cancel_delayed_work(&fan_watchdog_task);
6574 flush_workqueue(tpacpi_wq);
6577 static void fan_suspend(pm_message_t state)
6579 int rc;
6581 if (!fan_control_allowed)
6582 return;
6584 /* Store fan status in cache */
6585 fan_control_resume_level = 0;
6586 rc = fan_get_status_safe(&fan_control_resume_level);
6587 if (rc < 0)
6588 printk(TPACPI_NOTICE
6589 "failed to read fan level for later "
6590 "restore during resume: %d\n", rc);
6592 /* if it is undefined, don't attempt to restore it.
6593 * KEEP THIS LAST */
6594 if (tp_features.fan_ctrl_status_undef)
6595 fan_control_resume_level = 0;
6598 static void fan_resume(void)
6600 u8 current_level = 7;
6601 bool do_set = false;
6602 int rc;
6604 /* DSDT *always* updates status on resume */
6605 tp_features.fan_ctrl_status_undef = 0;
6607 if (!fan_control_allowed ||
6608 !fan_control_resume_level ||
6609 (fan_get_status_safe(&current_level) < 0))
6610 return;
6612 switch (fan_control_access_mode) {
6613 case TPACPI_FAN_WR_ACPI_SFAN:
6614 /* never decrease fan level */
6615 do_set = (fan_control_resume_level > current_level);
6616 break;
6617 case TPACPI_FAN_WR_ACPI_FANS:
6618 case TPACPI_FAN_WR_TPEC:
6619 /* never decrease fan level, scale is:
6620 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6622 * We expect the firmware to set either 7 or AUTO, but we
6623 * handle FULLSPEED out of paranoia.
6625 * So, we can safely only restore FULLSPEED or 7, anything
6626 * else could slow the fan. Restoring AUTO is useless, at
6627 * best that's exactly what the DSDT already set (it is the
6628 * slower it uses).
6630 * Always keep in mind that the DSDT *will* have set the
6631 * fans to what the vendor supposes is the best level. We
6632 * muck with it only to speed the fan up.
6634 if (fan_control_resume_level != 7 &&
6635 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6636 return;
6637 else
6638 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6639 (current_level != fan_control_resume_level);
6640 break;
6641 default:
6642 return;
6644 if (do_set) {
6645 printk(TPACPI_NOTICE
6646 "restoring fan level to 0x%02x\n",
6647 fan_control_resume_level);
6648 rc = fan_set_level_safe(fan_control_resume_level);
6649 if (rc < 0)
6650 printk(TPACPI_NOTICE
6651 "failed to restore fan level: %d\n", rc);
6655 static int fan_read(char *p)
6657 int len = 0;
6658 int rc;
6659 u8 status;
6660 unsigned int speed = 0;
6662 switch (fan_status_access_mode) {
6663 case TPACPI_FAN_RD_ACPI_GFAN:
6664 /* 570, 600e/x, 770e, 770x */
6665 rc = fan_get_status_safe(&status);
6666 if (rc < 0)
6667 return rc;
6669 len += sprintf(p + len, "status:\t\t%s\n"
6670 "level:\t\t%d\n",
6671 (status != 0) ? "enabled" : "disabled", status);
6672 break;
6674 case TPACPI_FAN_RD_TPEC:
6675 /* all except 570, 600e/x, 770e, 770x */
6676 rc = fan_get_status_safe(&status);
6677 if (rc < 0)
6678 return rc;
6680 len += sprintf(p + len, "status:\t\t%s\n",
6681 (status != 0) ? "enabled" : "disabled");
6683 rc = fan_get_speed(&speed);
6684 if (rc < 0)
6685 return rc;
6687 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6689 if (status & TP_EC_FAN_FULLSPEED)
6690 /* Disengaged mode takes precedence */
6691 len += sprintf(p + len, "level:\t\tdisengaged\n");
6692 else if (status & TP_EC_FAN_AUTO)
6693 len += sprintf(p + len, "level:\t\tauto\n");
6694 else
6695 len += sprintf(p + len, "level:\t\t%d\n", status);
6696 break;
6698 case TPACPI_FAN_NONE:
6699 default:
6700 len += sprintf(p + len, "status:\t\tnot supported\n");
6703 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6704 len += sprintf(p + len, "commands:\tlevel <level>");
6706 switch (fan_control_access_mode) {
6707 case TPACPI_FAN_WR_ACPI_SFAN:
6708 len += sprintf(p + len, " (<level> is 0-7)\n");
6709 break;
6711 default:
6712 len += sprintf(p + len, " (<level> is 0-7, "
6713 "auto, disengaged, full-speed)\n");
6714 break;
6718 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6719 len += sprintf(p + len, "commands:\tenable, disable\n"
6720 "commands:\twatchdog <timeout> (<timeout> "
6721 "is 0 (off), 1-120 (seconds))\n");
6723 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6724 len += sprintf(p + len, "commands:\tspeed <speed>"
6725 " (<speed> is 0-65535)\n");
6727 return len;
6730 static int fan_write_cmd_level(const char *cmd, int *rc)
6732 int level;
6734 if (strlencmp(cmd, "level auto") == 0)
6735 level = TP_EC_FAN_AUTO;
6736 else if ((strlencmp(cmd, "level disengaged") == 0) |
6737 (strlencmp(cmd, "level full-speed") == 0))
6738 level = TP_EC_FAN_FULLSPEED;
6739 else if (sscanf(cmd, "level %d", &level) != 1)
6740 return 0;
6742 *rc = fan_set_level_safe(level);
6743 if (*rc == -ENXIO)
6744 printk(TPACPI_ERR "level command accepted for unsupported "
6745 "access mode %d", fan_control_access_mode);
6747 return 1;
6750 static int fan_write_cmd_enable(const char *cmd, int *rc)
6752 if (strlencmp(cmd, "enable") != 0)
6753 return 0;
6755 *rc = fan_set_enable();
6756 if (*rc == -ENXIO)
6757 printk(TPACPI_ERR "enable command accepted for unsupported "
6758 "access mode %d", fan_control_access_mode);
6760 return 1;
6763 static int fan_write_cmd_disable(const char *cmd, int *rc)
6765 if (strlencmp(cmd, "disable") != 0)
6766 return 0;
6768 *rc = fan_set_disable();
6769 if (*rc == -ENXIO)
6770 printk(TPACPI_ERR "disable command accepted for unsupported "
6771 "access mode %d", fan_control_access_mode);
6773 return 1;
6776 static int fan_write_cmd_speed(const char *cmd, int *rc)
6778 int speed;
6780 /* TODO:
6781 * Support speed <low> <medium> <high> ? */
6783 if (sscanf(cmd, "speed %d", &speed) != 1)
6784 return 0;
6786 *rc = fan_set_speed(speed);
6787 if (*rc == -ENXIO)
6788 printk(TPACPI_ERR "speed command accepted for unsupported "
6789 "access mode %d", fan_control_access_mode);
6791 return 1;
6794 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6796 int interval;
6798 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6799 return 0;
6801 if (interval < 0 || interval > 120)
6802 *rc = -EINVAL;
6803 else
6804 fan_watchdog_maxinterval = interval;
6806 return 1;
6809 static int fan_write(char *buf)
6811 char *cmd;
6812 int rc = 0;
6814 while (!rc && (cmd = next_cmd(&buf))) {
6815 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6816 fan_write_cmd_level(cmd, &rc)) &&
6817 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6818 (fan_write_cmd_enable(cmd, &rc) ||
6819 fan_write_cmd_disable(cmd, &rc) ||
6820 fan_write_cmd_watchdog(cmd, &rc))) &&
6821 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6822 fan_write_cmd_speed(cmd, &rc))
6824 rc = -EINVAL;
6825 else if (!rc)
6826 fan_watchdog_reset();
6829 return rc;
6832 static struct ibm_struct fan_driver_data = {
6833 .name = "fan",
6834 .read = fan_read,
6835 .write = fan_write,
6836 .exit = fan_exit,
6837 .suspend = fan_suspend,
6838 .resume = fan_resume,
6841 /****************************************************************************
6842 ****************************************************************************
6844 * Infrastructure
6846 ****************************************************************************
6847 ****************************************************************************/
6849 /* sysfs name ---------------------------------------------------------- */
6850 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6851 struct device_attribute *attr,
6852 char *buf)
6854 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6857 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6858 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6860 /* --------------------------------------------------------------------- */
6862 /* /proc support */
6863 static struct proc_dir_entry *proc_dir;
6866 * Module and infrastructure proble, init and exit handling
6869 static int force_load;
6871 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6872 static const char * __init str_supported(int is_supported)
6874 static char text_unsupported[] __initdata = "not supported";
6876 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6878 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6880 static void ibm_exit(struct ibm_struct *ibm)
6882 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6884 list_del_init(&ibm->all_drivers);
6886 if (ibm->flags.acpi_notify_installed) {
6887 dbg_printk(TPACPI_DBG_EXIT,
6888 "%s: acpi_remove_notify_handler\n", ibm->name);
6889 BUG_ON(!ibm->acpi);
6890 acpi_remove_notify_handler(*ibm->acpi->handle,
6891 ibm->acpi->type,
6892 dispatch_acpi_notify);
6893 ibm->flags.acpi_notify_installed = 0;
6894 ibm->flags.acpi_notify_installed = 0;
6897 if (ibm->flags.proc_created) {
6898 dbg_printk(TPACPI_DBG_EXIT,
6899 "%s: remove_proc_entry\n", ibm->name);
6900 remove_proc_entry(ibm->name, proc_dir);
6901 ibm->flags.proc_created = 0;
6904 if (ibm->flags.acpi_driver_registered) {
6905 dbg_printk(TPACPI_DBG_EXIT,
6906 "%s: acpi_bus_unregister_driver\n", ibm->name);
6907 BUG_ON(!ibm->acpi);
6908 acpi_bus_unregister_driver(ibm->acpi->driver);
6909 kfree(ibm->acpi->driver);
6910 ibm->acpi->driver = NULL;
6911 ibm->flags.acpi_driver_registered = 0;
6914 if (ibm->flags.init_called && ibm->exit) {
6915 ibm->exit();
6916 ibm->flags.init_called = 0;
6919 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6922 static int __init ibm_init(struct ibm_init_struct *iibm)
6924 int ret;
6925 struct ibm_struct *ibm = iibm->data;
6926 struct proc_dir_entry *entry;
6928 BUG_ON(ibm == NULL);
6930 INIT_LIST_HEAD(&ibm->all_drivers);
6932 if (ibm->flags.experimental && !experimental)
6933 return 0;
6935 dbg_printk(TPACPI_DBG_INIT,
6936 "probing for %s\n", ibm->name);
6938 if (iibm->init) {
6939 ret = iibm->init(iibm);
6940 if (ret > 0)
6941 return 0; /* probe failed */
6942 if (ret)
6943 return ret;
6945 ibm->flags.init_called = 1;
6948 if (ibm->acpi) {
6949 if (ibm->acpi->hid) {
6950 ret = register_tpacpi_subdriver(ibm);
6951 if (ret)
6952 goto err_out;
6955 if (ibm->acpi->notify) {
6956 ret = setup_acpi_notify(ibm);
6957 if (ret == -ENODEV) {
6958 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6959 ibm->name);
6960 ret = 0;
6961 goto err_out;
6963 if (ret < 0)
6964 goto err_out;
6968 dbg_printk(TPACPI_DBG_INIT,
6969 "%s installed\n", ibm->name);
6971 if (ibm->read) {
6972 entry = create_proc_entry(ibm->name,
6973 S_IFREG | S_IRUGO | S_IWUSR,
6974 proc_dir);
6975 if (!entry) {
6976 printk(TPACPI_ERR "unable to create proc entry %s\n",
6977 ibm->name);
6978 ret = -ENODEV;
6979 goto err_out;
6981 entry->owner = THIS_MODULE;
6982 entry->data = ibm;
6983 entry->read_proc = &dispatch_procfs_read;
6984 if (ibm->write)
6985 entry->write_proc = &dispatch_procfs_write;
6986 ibm->flags.proc_created = 1;
6989 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6991 return 0;
6993 err_out:
6994 dbg_printk(TPACPI_DBG_INIT,
6995 "%s: at error exit path with result %d\n",
6996 ibm->name, ret);
6998 ibm_exit(ibm);
6999 return (ret < 0)? ret : 0;
7002 /* Probing */
7004 /* returns 0 - probe ok, or < 0 - probe error.
7005 * Probe ok doesn't mean thinkpad found.
7006 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7007 static int __must_check __init get_thinkpad_model_data(
7008 struct thinkpad_id_data *tp)
7010 const struct dmi_device *dev = NULL;
7011 char ec_fw_string[18];
7012 char const *s;
7014 if (!tp)
7015 return -EINVAL;
7017 memset(tp, 0, sizeof(*tp));
7019 if (dmi_name_in_vendors("IBM"))
7020 tp->vendor = PCI_VENDOR_ID_IBM;
7021 else if (dmi_name_in_vendors("LENOVO"))
7022 tp->vendor = PCI_VENDOR_ID_LENOVO;
7023 else
7024 return 0;
7026 s = dmi_get_system_info(DMI_BIOS_VERSION);
7027 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7028 if (s && !tp->bios_version_str)
7029 return -ENOMEM;
7030 if (!tp->bios_version_str)
7031 return 0;
7032 tp->bios_model = tp->bios_version_str[0]
7033 | (tp->bios_version_str[1] << 8);
7036 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7037 * X32 or newer, all Z series; Some models must have an
7038 * up-to-date BIOS or they will not be detected.
7040 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7042 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7043 if (sscanf(dev->name,
7044 "IBM ThinkPad Embedded Controller -[%17c",
7045 ec_fw_string) == 1) {
7046 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7047 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7049 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7050 if (!tp->ec_version_str)
7051 return -ENOMEM;
7052 tp->ec_model = ec_fw_string[0]
7053 | (ec_fw_string[1] << 8);
7054 break;
7058 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7059 if (s && !strnicmp(s, "ThinkPad", 8)) {
7060 tp->model_str = kstrdup(s, GFP_KERNEL);
7061 if (!tp->model_str)
7062 return -ENOMEM;
7065 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7066 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7067 if (s && !tp->nummodel_str)
7068 return -ENOMEM;
7070 return 0;
7073 static int __init probe_for_thinkpad(void)
7075 int is_thinkpad;
7077 if (acpi_disabled)
7078 return -ENODEV;
7081 * Non-ancient models have better DMI tagging, but very old models
7082 * don't.
7084 is_thinkpad = (thinkpad_id.model_str != NULL);
7086 /* ec is required because many other handles are relative to it */
7087 TPACPI_ACPIHANDLE_INIT(ec);
7088 if (!ec_handle) {
7089 if (is_thinkpad)
7090 printk(TPACPI_ERR
7091 "Not yet supported ThinkPad detected!\n");
7092 return -ENODEV;
7096 * Risks a regression on very old machines, but reduces potential
7097 * false positives a damn great deal
7099 if (!is_thinkpad)
7100 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7102 if (!is_thinkpad && !force_load)
7103 return -ENODEV;
7105 return 0;
7109 /* Module init, exit, parameters */
7111 static struct ibm_init_struct ibms_init[] __initdata = {
7113 .init = thinkpad_acpi_driver_init,
7114 .data = &thinkpad_acpi_driver_data,
7117 .init = hotkey_init,
7118 .data = &hotkey_driver_data,
7121 .init = bluetooth_init,
7122 .data = &bluetooth_driver_data,
7125 .init = wan_init,
7126 .data = &wan_driver_data,
7129 .init = uwb_init,
7130 .data = &uwb_driver_data,
7132 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7134 .init = video_init,
7135 .data = &video_driver_data,
7137 #endif
7139 .init = light_init,
7140 .data = &light_driver_data,
7142 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7144 .init = dock_init,
7145 .data = &dock_driver_data[0],
7148 .init = dock_init2,
7149 .data = &dock_driver_data[1],
7151 #endif
7152 #ifdef CONFIG_THINKPAD_ACPI_BAY
7154 .init = bay_init,
7155 .data = &bay_driver_data,
7157 #endif
7159 .init = cmos_init,
7160 .data = &cmos_driver_data,
7163 .init = led_init,
7164 .data = &led_driver_data,
7167 .init = beep_init,
7168 .data = &beep_driver_data,
7171 .init = thermal_init,
7172 .data = &thermal_driver_data,
7175 .data = &ecdump_driver_data,
7178 .init = brightness_init,
7179 .data = &brightness_driver_data,
7182 .data = &volume_driver_data,
7185 .init = fan_init,
7186 .data = &fan_driver_data,
7190 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7192 unsigned int i;
7193 struct ibm_struct *ibm;
7195 if (!kp || !kp->name || !val)
7196 return -EINVAL;
7198 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7199 ibm = ibms_init[i].data;
7200 WARN_ON(ibm == NULL);
7202 if (!ibm || !ibm->name)
7203 continue;
7205 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7206 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7207 return -ENOSPC;
7208 strcpy(ibms_init[i].param, val);
7209 strcat(ibms_init[i].param, ",");
7210 return 0;
7214 return -EINVAL;
7217 module_param(experimental, int, 0);
7218 MODULE_PARM_DESC(experimental,
7219 "Enables experimental features when non-zero");
7221 module_param_named(debug, dbg_level, uint, 0);
7222 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7224 module_param(force_load, bool, 0);
7225 MODULE_PARM_DESC(force_load,
7226 "Attempts to load the driver even on a "
7227 "mis-identified ThinkPad when true");
7229 module_param_named(fan_control, fan_control_allowed, bool, 0);
7230 MODULE_PARM_DESC(fan_control,
7231 "Enables setting fan parameters features when true");
7233 module_param_named(brightness_mode, brightness_mode, int, 0);
7234 MODULE_PARM_DESC(brightness_mode,
7235 "Selects brightness control strategy: "
7236 "0=auto, 1=EC, 2=CMOS, 3=both");
7238 module_param(brightness_enable, uint, 0);
7239 MODULE_PARM_DESC(brightness_enable,
7240 "Enables backlight control when 1, disables when 0");
7242 module_param(hotkey_report_mode, uint, 0);
7243 MODULE_PARM_DESC(hotkey_report_mode,
7244 "used for backwards compatibility with userspace, "
7245 "see documentation");
7247 #define TPACPI_PARAM(feature) \
7248 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7249 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7250 "at module load, see documentation")
7252 TPACPI_PARAM(hotkey);
7253 TPACPI_PARAM(bluetooth);
7254 TPACPI_PARAM(video);
7255 TPACPI_PARAM(light);
7256 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7257 TPACPI_PARAM(dock);
7258 #endif
7259 #ifdef CONFIG_THINKPAD_ACPI_BAY
7260 TPACPI_PARAM(bay);
7261 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7262 TPACPI_PARAM(cmos);
7263 TPACPI_PARAM(led);
7264 TPACPI_PARAM(beep);
7265 TPACPI_PARAM(ecdump);
7266 TPACPI_PARAM(brightness);
7267 TPACPI_PARAM(volume);
7268 TPACPI_PARAM(fan);
7270 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7271 module_param(dbg_wlswemul, uint, 0);
7272 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7273 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7274 MODULE_PARM_DESC(wlsw_state,
7275 "Initial state of the emulated WLSW switch");
7277 module_param(dbg_bluetoothemul, uint, 0);
7278 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7279 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7280 MODULE_PARM_DESC(bluetooth_state,
7281 "Initial state of the emulated bluetooth switch");
7283 module_param(dbg_wwanemul, uint, 0);
7284 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7285 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7286 MODULE_PARM_DESC(wwan_state,
7287 "Initial state of the emulated WWAN switch");
7289 module_param(dbg_uwbemul, uint, 0);
7290 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7291 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7292 MODULE_PARM_DESC(uwb_state,
7293 "Initial state of the emulated UWB switch");
7294 #endif
7296 static void thinkpad_acpi_module_exit(void)
7298 struct ibm_struct *ibm, *itmp;
7300 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7302 list_for_each_entry_safe_reverse(ibm, itmp,
7303 &tpacpi_all_drivers,
7304 all_drivers) {
7305 ibm_exit(ibm);
7308 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7310 if (tpacpi_inputdev) {
7311 if (tp_features.input_device_registered)
7312 input_unregister_device(tpacpi_inputdev);
7313 else
7314 input_free_device(tpacpi_inputdev);
7317 if (tpacpi_hwmon)
7318 hwmon_device_unregister(tpacpi_hwmon);
7320 if (tp_features.sensors_pdev_attrs_registered)
7321 device_remove_file(&tpacpi_sensors_pdev->dev,
7322 &dev_attr_thinkpad_acpi_pdev_name);
7323 if (tpacpi_sensors_pdev)
7324 platform_device_unregister(tpacpi_sensors_pdev);
7325 if (tpacpi_pdev)
7326 platform_device_unregister(tpacpi_pdev);
7328 if (tp_features.sensors_pdrv_attrs_registered)
7329 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7330 if (tp_features.platform_drv_attrs_registered)
7331 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7333 if (tp_features.sensors_pdrv_registered)
7334 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7336 if (tp_features.platform_drv_registered)
7337 platform_driver_unregister(&tpacpi_pdriver);
7339 if (proc_dir)
7340 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7342 if (tpacpi_wq)
7343 destroy_workqueue(tpacpi_wq);
7345 kfree(thinkpad_id.bios_version_str);
7346 kfree(thinkpad_id.ec_version_str);
7347 kfree(thinkpad_id.model_str);
7351 static int __init thinkpad_acpi_module_init(void)
7353 int ret, i;
7355 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7357 /* Parameter checking */
7358 if (hotkey_report_mode > 2)
7359 return -EINVAL;
7361 /* Driver-level probe */
7363 ret = get_thinkpad_model_data(&thinkpad_id);
7364 if (ret) {
7365 printk(TPACPI_ERR
7366 "unable to get DMI data: %d\n", ret);
7367 thinkpad_acpi_module_exit();
7368 return ret;
7370 ret = probe_for_thinkpad();
7371 if (ret) {
7372 thinkpad_acpi_module_exit();
7373 return ret;
7376 /* Driver initialization */
7378 TPACPI_ACPIHANDLE_INIT(ecrd);
7379 TPACPI_ACPIHANDLE_INIT(ecwr);
7381 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7382 if (!tpacpi_wq) {
7383 thinkpad_acpi_module_exit();
7384 return -ENOMEM;
7387 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7388 if (!proc_dir) {
7389 printk(TPACPI_ERR
7390 "unable to create proc dir " TPACPI_PROC_DIR);
7391 thinkpad_acpi_module_exit();
7392 return -ENODEV;
7394 proc_dir->owner = THIS_MODULE;
7396 ret = platform_driver_register(&tpacpi_pdriver);
7397 if (ret) {
7398 printk(TPACPI_ERR
7399 "unable to register main platform driver\n");
7400 thinkpad_acpi_module_exit();
7401 return ret;
7403 tp_features.platform_drv_registered = 1;
7405 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7406 if (ret) {
7407 printk(TPACPI_ERR
7408 "unable to register hwmon platform driver\n");
7409 thinkpad_acpi_module_exit();
7410 return ret;
7412 tp_features.sensors_pdrv_registered = 1;
7414 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7415 if (!ret) {
7416 tp_features.platform_drv_attrs_registered = 1;
7417 ret = tpacpi_create_driver_attributes(
7418 &tpacpi_hwmon_pdriver.driver);
7420 if (ret) {
7421 printk(TPACPI_ERR
7422 "unable to create sysfs driver attributes\n");
7423 thinkpad_acpi_module_exit();
7424 return ret;
7426 tp_features.sensors_pdrv_attrs_registered = 1;
7429 /* Device initialization */
7430 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7431 NULL, 0);
7432 if (IS_ERR(tpacpi_pdev)) {
7433 ret = PTR_ERR(tpacpi_pdev);
7434 tpacpi_pdev = NULL;
7435 printk(TPACPI_ERR "unable to register platform device\n");
7436 thinkpad_acpi_module_exit();
7437 return ret;
7439 tpacpi_sensors_pdev = platform_device_register_simple(
7440 TPACPI_HWMON_DRVR_NAME,
7441 -1, NULL, 0);
7442 if (IS_ERR(tpacpi_sensors_pdev)) {
7443 ret = PTR_ERR(tpacpi_sensors_pdev);
7444 tpacpi_sensors_pdev = NULL;
7445 printk(TPACPI_ERR
7446 "unable to register hwmon platform device\n");
7447 thinkpad_acpi_module_exit();
7448 return ret;
7450 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7451 &dev_attr_thinkpad_acpi_pdev_name);
7452 if (ret) {
7453 printk(TPACPI_ERR
7454 "unable to create sysfs hwmon device attributes\n");
7455 thinkpad_acpi_module_exit();
7456 return ret;
7458 tp_features.sensors_pdev_attrs_registered = 1;
7459 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7460 if (IS_ERR(tpacpi_hwmon)) {
7461 ret = PTR_ERR(tpacpi_hwmon);
7462 tpacpi_hwmon = NULL;
7463 printk(TPACPI_ERR "unable to register hwmon device\n");
7464 thinkpad_acpi_module_exit();
7465 return ret;
7467 mutex_init(&tpacpi_inputdev_send_mutex);
7468 tpacpi_inputdev = input_allocate_device();
7469 if (!tpacpi_inputdev) {
7470 printk(TPACPI_ERR "unable to allocate input device\n");
7471 thinkpad_acpi_module_exit();
7472 return -ENOMEM;
7473 } else {
7474 /* Prepare input device, but don't register */
7475 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7476 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7477 tpacpi_inputdev->id.bustype = BUS_HOST;
7478 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7479 thinkpad_id.vendor :
7480 PCI_VENDOR_ID_IBM;
7481 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7482 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7484 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7485 ret = ibm_init(&ibms_init[i]);
7486 if (ret >= 0 && *ibms_init[i].param)
7487 ret = ibms_init[i].data->write(ibms_init[i].param);
7488 if (ret < 0) {
7489 thinkpad_acpi_module_exit();
7490 return ret;
7493 ret = input_register_device(tpacpi_inputdev);
7494 if (ret < 0) {
7495 printk(TPACPI_ERR "unable to register input device\n");
7496 thinkpad_acpi_module_exit();
7497 return ret;
7498 } else {
7499 tp_features.input_device_registered = 1;
7502 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7503 return 0;
7506 /* Please remove this in year 2009 */
7507 MODULE_ALIAS("ibm_acpi");
7509 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7512 * DMI matching for module autoloading
7514 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7515 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7517 * Only models listed in thinkwiki will be supported, so add yours
7518 * if it is not there yet.
7520 #define IBM_BIOS_MODULE_ALIAS(__type) \
7521 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
7523 /* Non-ancient thinkpads */
7524 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7525 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7527 /* Ancient thinkpad BIOSes have to be identified by
7528 * BIOS type or model number, and there are far less
7529 * BIOS types than model numbers... */
7530 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
7531 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
7532 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
7534 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7535 MODULE_DESCRIPTION(TPACPI_DESC);
7536 MODULE_VERSION(TPACPI_VERSION);
7537 MODULE_LICENSE("GPL");
7539 module_init(thinkpad_acpi_module_init);
7540 module_exit(thinkpad_acpi_module_exit);