thinkpad-acpi: restrict access to some firmware LEDs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobacbf64fbaee64df74defc559919d2c5b91fd53ce
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.22"
25 #define TPACPI_SYSFS_VERSION 0x020300
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/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.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 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSETASK 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
193 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
194 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
195 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
198 /****************************************************************************
199 * Driver-wide structs and misc. variables
202 struct ibm_struct;
204 struct tp_acpi_drv_struct {
205 const struct acpi_device_id *hid;
206 struct acpi_driver *driver;
208 void (*notify) (struct ibm_struct *, u32);
209 acpi_handle *handle;
210 u32 type;
211 struct acpi_device *device;
214 struct ibm_struct {
215 char *name;
217 int (*read) (char *);
218 int (*write) (char *);
219 void (*exit) (void);
220 void (*resume) (void);
221 void (*suspend) (pm_message_t state);
222 void (*shutdown) (void);
224 struct list_head all_drivers;
226 struct tp_acpi_drv_struct *acpi;
228 struct {
229 u8 acpi_driver_registered:1;
230 u8 acpi_notify_installed:1;
231 u8 proc_created:1;
232 u8 init_called:1;
233 u8 experimental:1;
234 } flags;
237 struct ibm_init_struct {
238 char param[32];
240 int (*init) (struct ibm_init_struct *);
241 struct ibm_struct *data;
244 static struct {
245 #ifdef CONFIG_THINKPAD_ACPI_BAY
246 u32 bay_status:1;
247 u32 bay_eject:1;
248 u32 bay_status2:1;
249 u32 bay_eject2:1;
250 #endif
251 u32 bluetooth:1;
252 u32 hotkey:1;
253 u32 hotkey_mask:1;
254 u32 hotkey_wlsw:1;
255 u32 hotkey_tablet:1;
256 u32 light:1;
257 u32 light_status:1;
258 u32 bright_16levels:1;
259 u32 bright_acpimode:1;
260 u32 wan:1;
261 u32 uwb:1;
262 u32 fan_ctrl_status_undef:1;
263 u32 input_device_registered:1;
264 u32 platform_drv_registered:1;
265 u32 platform_drv_attrs_registered:1;
266 u32 sensors_pdrv_registered:1;
267 u32 sensors_pdrv_attrs_registered:1;
268 u32 sensors_pdev_attrs_registered:1;
269 u32 hotkey_poll_active:1;
270 } tp_features;
272 static struct {
273 u16 hotkey_mask_ff:1;
274 u16 bright_cmos_ec_unsync:1;
275 } tp_warned;
277 struct thinkpad_id_data {
278 unsigned int vendor; /* ThinkPad vendor:
279 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
281 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
282 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
284 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
285 u16 ec_model;
287 char *model_str; /* ThinkPad T43 */
288 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
290 static struct thinkpad_id_data thinkpad_id;
292 static enum {
293 TPACPI_LIFE_INIT = 0,
294 TPACPI_LIFE_RUNNING,
295 TPACPI_LIFE_EXITING,
296 } tpacpi_lifecycle;
298 static int experimental;
299 static u32 dbg_level;
301 static struct workqueue_struct *tpacpi_wq;
303 enum led_status_t {
304 TPACPI_LED_OFF = 0,
305 TPACPI_LED_ON,
306 TPACPI_LED_BLINK,
309 /* Special LED class that can defer work */
310 struct tpacpi_led_classdev {
311 struct led_classdev led_classdev;
312 struct work_struct work;
313 enum led_status_t new_state;
314 unsigned int led;
317 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
318 static int dbg_wlswemul;
319 static int tpacpi_wlsw_emulstate;
320 static int dbg_bluetoothemul;
321 static int tpacpi_bluetooth_emulstate;
322 static int dbg_wwanemul;
323 static int tpacpi_wwan_emulstate;
324 static int dbg_uwbemul;
325 static int tpacpi_uwb_emulstate;
326 #endif
329 /*************************************************************************
330 * Debugging helpers
333 #define dbg_printk(a_dbg_level, format, arg...) \
334 do { if (dbg_level & (a_dbg_level)) \
335 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
336 } while (0)
338 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
339 #define vdbg_printk dbg_printk
340 static const char *str_supported(int is_supported);
341 #else
342 #define vdbg_printk(a_dbg_level, format, arg...) \
343 do { } while (0)
344 #endif
346 static void tpacpi_log_usertask(const char * const what)
348 printk(TPACPI_DEBUG "%s: access by process with PID %d\n",
349 what, task_tgid_vnr(current));
352 #define tpacpi_disclose_usertask(what, format, arg...) \
353 do { \
354 if (unlikely( \
355 (dbg_level & TPACPI_DBG_DISCLOSETASK) && \
356 (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) { \
357 printk(TPACPI_DEBUG "%s: PID %d: " format, \
358 what, task_tgid_vnr(current), ## arg); \
360 } while (0)
362 /****************************************************************************
363 ****************************************************************************
365 * ACPI Helpers and device model
367 ****************************************************************************
368 ****************************************************************************/
370 /*************************************************************************
371 * ACPI basic handles
374 static acpi_handle root_handle;
376 #define TPACPI_HANDLE(object, parent, paths...) \
377 static acpi_handle object##_handle; \
378 static acpi_handle *object##_parent = &parent##_handle; \
379 static char *object##_path; \
380 static char *object##_paths[] = { paths }
382 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
383 "\\_SB.PCI.ISA.EC", /* 570 */
384 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
385 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
386 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
387 "\\_SB.PCI0.ICH3.EC0", /* R31 */
388 "\\_SB.PCI0.LPC.EC", /* all others */
391 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
392 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
394 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
395 /* T4x, X31, X40 */
396 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
397 "\\CMS", /* R40, R40e */
398 ); /* all others */
400 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
401 "^HKEY", /* R30, R31 */
402 "HKEY", /* all others */
403 ); /* 570 */
405 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
406 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
407 "\\_SB.PCI0.VID0", /* 770e */
408 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
409 "\\_SB.PCI0.AGP.VID", /* all others */
410 ); /* R30, R31 */
413 /*************************************************************************
414 * ACPI helpers
417 static int acpi_evalf(acpi_handle handle,
418 void *res, char *method, char *fmt, ...)
420 char *fmt0 = fmt;
421 struct acpi_object_list params;
422 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
423 struct acpi_buffer result, *resultp;
424 union acpi_object out_obj;
425 acpi_status status;
426 va_list ap;
427 char res_type;
428 int success;
429 int quiet;
431 if (!*fmt) {
432 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
433 return 0;
436 if (*fmt == 'q') {
437 quiet = 1;
438 fmt++;
439 } else
440 quiet = 0;
442 res_type = *(fmt++);
444 params.count = 0;
445 params.pointer = &in_objs[0];
447 va_start(ap, fmt);
448 while (*fmt) {
449 char c = *(fmt++);
450 switch (c) {
451 case 'd': /* int */
452 in_objs[params.count].integer.value = va_arg(ap, int);
453 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
454 break;
455 /* add more types as needed */
456 default:
457 printk(TPACPI_ERR "acpi_evalf() called "
458 "with invalid format character '%c'\n", c);
459 return 0;
462 va_end(ap);
464 if (res_type != 'v') {
465 result.length = sizeof(out_obj);
466 result.pointer = &out_obj;
467 resultp = &result;
468 } else
469 resultp = NULL;
471 status = acpi_evaluate_object(handle, method, &params, resultp);
473 switch (res_type) {
474 case 'd': /* int */
475 if (res)
476 *(int *)res = out_obj.integer.value;
477 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
478 break;
479 case 'v': /* void */
480 success = status == AE_OK;
481 break;
482 /* add more types as needed */
483 default:
484 printk(TPACPI_ERR "acpi_evalf() called "
485 "with invalid format character '%c'\n", res_type);
486 return 0;
489 if (!success && !quiet)
490 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
491 method, fmt0, status);
493 return success;
496 static int acpi_ec_read(int i, u8 *p)
498 int v;
500 if (ecrd_handle) {
501 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
502 return 0;
503 *p = v;
504 } else {
505 if (ec_read(i, p) < 0)
506 return 0;
509 return 1;
512 static int acpi_ec_write(int i, u8 v)
514 if (ecwr_handle) {
515 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
516 return 0;
517 } else {
518 if (ec_write(i, v) < 0)
519 return 0;
522 return 1;
525 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
526 static int _sta(acpi_handle handle)
528 int status;
530 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
531 status = 0;
533 return status;
535 #endif
537 static int issue_thinkpad_cmos_command(int cmos_cmd)
539 if (!cmos_handle)
540 return -ENXIO;
542 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
543 return -EIO;
545 return 0;
548 /*************************************************************************
549 * ACPI device model
552 #define TPACPI_ACPIHANDLE_INIT(object) \
553 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
554 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
556 static void drv_acpi_handle_init(char *name,
557 acpi_handle *handle, acpi_handle parent,
558 char **paths, int num_paths, char **path)
560 int i;
561 acpi_status status;
563 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
564 name);
566 for (i = 0; i < num_paths; i++) {
567 status = acpi_get_handle(parent, paths[i], handle);
568 if (ACPI_SUCCESS(status)) {
569 *path = paths[i];
570 dbg_printk(TPACPI_DBG_INIT,
571 "Found ACPI handle %s for %s\n",
572 *path, name);
573 return;
577 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
578 name);
579 *handle = NULL;
582 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
584 struct ibm_struct *ibm = data;
586 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
587 return;
589 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
590 return;
592 ibm->acpi->notify(ibm, event);
595 static int __init setup_acpi_notify(struct ibm_struct *ibm)
597 acpi_status status;
598 int rc;
600 BUG_ON(!ibm->acpi);
602 if (!*ibm->acpi->handle)
603 return 0;
605 vdbg_printk(TPACPI_DBG_INIT,
606 "setting up ACPI notify for %s\n", ibm->name);
608 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
609 if (rc < 0) {
610 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
611 ibm->name, rc);
612 return -ENODEV;
615 ibm->acpi->device->driver_data = ibm;
616 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
617 TPACPI_ACPI_EVENT_PREFIX,
618 ibm->name);
620 status = acpi_install_notify_handler(*ibm->acpi->handle,
621 ibm->acpi->type, dispatch_acpi_notify, ibm);
622 if (ACPI_FAILURE(status)) {
623 if (status == AE_ALREADY_EXISTS) {
624 printk(TPACPI_NOTICE
625 "another device driver is already "
626 "handling %s events\n", ibm->name);
627 } else {
628 printk(TPACPI_ERR
629 "acpi_install_notify_handler(%s) failed: %d\n",
630 ibm->name, status);
632 return -ENODEV;
634 ibm->flags.acpi_notify_installed = 1;
635 return 0;
638 static int __init tpacpi_device_add(struct acpi_device *device)
640 return 0;
643 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
645 int rc;
647 dbg_printk(TPACPI_DBG_INIT,
648 "registering %s as an ACPI driver\n", ibm->name);
650 BUG_ON(!ibm->acpi);
652 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
653 if (!ibm->acpi->driver) {
654 printk(TPACPI_ERR
655 "failed to allocate memory for ibm->acpi->driver\n");
656 return -ENOMEM;
659 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
660 ibm->acpi->driver->ids = ibm->acpi->hid;
662 ibm->acpi->driver->ops.add = &tpacpi_device_add;
664 rc = acpi_bus_register_driver(ibm->acpi->driver);
665 if (rc < 0) {
666 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
667 ibm->name, rc);
668 kfree(ibm->acpi->driver);
669 ibm->acpi->driver = NULL;
670 } else if (!rc)
671 ibm->flags.acpi_driver_registered = 1;
673 return rc;
677 /****************************************************************************
678 ****************************************************************************
680 * Procfs Helpers
682 ****************************************************************************
683 ****************************************************************************/
685 static int dispatch_procfs_read(char *page, char **start, off_t off,
686 int count, int *eof, void *data)
688 struct ibm_struct *ibm = data;
689 int len;
691 if (!ibm || !ibm->read)
692 return -EINVAL;
694 len = ibm->read(page);
695 if (len < 0)
696 return len;
698 if (len <= off + count)
699 *eof = 1;
700 *start = page + off;
701 len -= off;
702 if (len > count)
703 len = count;
704 if (len < 0)
705 len = 0;
707 return len;
710 static int dispatch_procfs_write(struct file *file,
711 const char __user *userbuf,
712 unsigned long count, void *data)
714 struct ibm_struct *ibm = data;
715 char *kernbuf;
716 int ret;
718 if (!ibm || !ibm->write)
719 return -EINVAL;
721 kernbuf = kmalloc(count + 2, GFP_KERNEL);
722 if (!kernbuf)
723 return -ENOMEM;
725 if (copy_from_user(kernbuf, userbuf, count)) {
726 kfree(kernbuf);
727 return -EFAULT;
730 kernbuf[count] = 0;
731 strcat(kernbuf, ",");
732 ret = ibm->write(kernbuf);
733 if (ret == 0)
734 ret = count;
736 kfree(kernbuf);
738 return ret;
741 static char *next_cmd(char **cmds)
743 char *start = *cmds;
744 char *end;
746 while ((end = strchr(start, ',')) && end == start)
747 start = end + 1;
749 if (!end)
750 return NULL;
752 *end = 0;
753 *cmds = end + 1;
754 return start;
758 /****************************************************************************
759 ****************************************************************************
761 * Device model: input, hwmon and platform
763 ****************************************************************************
764 ****************************************************************************/
766 static struct platform_device *tpacpi_pdev;
767 static struct platform_device *tpacpi_sensors_pdev;
768 static struct device *tpacpi_hwmon;
769 static struct input_dev *tpacpi_inputdev;
770 static struct mutex tpacpi_inputdev_send_mutex;
771 static LIST_HEAD(tpacpi_all_drivers);
773 static int tpacpi_suspend_handler(struct platform_device *pdev,
774 pm_message_t state)
776 struct ibm_struct *ibm, *itmp;
778 list_for_each_entry_safe(ibm, itmp,
779 &tpacpi_all_drivers,
780 all_drivers) {
781 if (ibm->suspend)
782 (ibm->suspend)(state);
785 return 0;
788 static int tpacpi_resume_handler(struct platform_device *pdev)
790 struct ibm_struct *ibm, *itmp;
792 list_for_each_entry_safe(ibm, itmp,
793 &tpacpi_all_drivers,
794 all_drivers) {
795 if (ibm->resume)
796 (ibm->resume)();
799 return 0;
802 static void tpacpi_shutdown_handler(struct platform_device *pdev)
804 struct ibm_struct *ibm, *itmp;
806 list_for_each_entry_safe(ibm, itmp,
807 &tpacpi_all_drivers,
808 all_drivers) {
809 if (ibm->shutdown)
810 (ibm->shutdown)();
814 static struct platform_driver tpacpi_pdriver = {
815 .driver = {
816 .name = TPACPI_DRVR_NAME,
817 .owner = THIS_MODULE,
819 .suspend = tpacpi_suspend_handler,
820 .resume = tpacpi_resume_handler,
821 .shutdown = tpacpi_shutdown_handler,
824 static struct platform_driver tpacpi_hwmon_pdriver = {
825 .driver = {
826 .name = TPACPI_HWMON_DRVR_NAME,
827 .owner = THIS_MODULE,
831 /*************************************************************************
832 * sysfs support helpers
835 struct attribute_set {
836 unsigned int members, max_members;
837 struct attribute_group group;
840 struct attribute_set_obj {
841 struct attribute_set s;
842 struct attribute *a;
843 } __attribute__((packed));
845 static struct attribute_set *create_attr_set(unsigned int max_members,
846 const char *name)
848 struct attribute_set_obj *sobj;
850 if (max_members == 0)
851 return NULL;
853 /* Allocates space for implicit NULL at the end too */
854 sobj = kzalloc(sizeof(struct attribute_set_obj) +
855 max_members * sizeof(struct attribute *),
856 GFP_KERNEL);
857 if (!sobj)
858 return NULL;
859 sobj->s.max_members = max_members;
860 sobj->s.group.attrs = &sobj->a;
861 sobj->s.group.name = name;
863 return &sobj->s;
866 #define destroy_attr_set(_set) \
867 kfree(_set);
869 /* not multi-threaded safe, use it in a single thread per set */
870 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
872 if (!s || !attr)
873 return -EINVAL;
875 if (s->members >= s->max_members)
876 return -ENOMEM;
878 s->group.attrs[s->members] = attr;
879 s->members++;
881 return 0;
884 static int add_many_to_attr_set(struct attribute_set *s,
885 struct attribute **attr,
886 unsigned int count)
888 int i, res;
890 for (i = 0; i < count; i++) {
891 res = add_to_attr_set(s, attr[i]);
892 if (res)
893 return res;
896 return 0;
899 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
901 sysfs_remove_group(kobj, &s->group);
902 destroy_attr_set(s);
905 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
906 sysfs_create_group(_kobj, &_attr_set->group)
908 static int parse_strtoul(const char *buf,
909 unsigned long max, unsigned long *value)
911 char *endp;
913 while (*buf && isspace(*buf))
914 buf++;
915 *value = simple_strtoul(buf, &endp, 0);
916 while (*endp && isspace(*endp))
917 endp++;
918 if (*endp || *value > max)
919 return -EINVAL;
921 return 0;
924 static void tpacpi_disable_brightness_delay(void)
926 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
927 printk(TPACPI_NOTICE
928 "ACPI backlight control delay disabled\n");
931 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
933 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
934 union acpi_object *obj;
935 int rc;
937 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
938 obj = (union acpi_object *)buffer.pointer;
939 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
940 printk(TPACPI_ERR "Unknown _BCL data, "
941 "please report this to %s\n", TPACPI_MAIL);
942 rc = 0;
943 } else {
944 rc = obj->package.count;
946 } else {
947 return 0;
950 kfree(buffer.pointer);
951 return rc;
954 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
955 u32 lvl, void *context, void **rv)
957 char name[ACPI_PATH_SEGMENT_LENGTH];
958 struct acpi_buffer buffer = { sizeof(name), &name };
960 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
961 !strncmp("_BCL", name, sizeof(name) - 1)) {
962 BUG_ON(!rv || !*rv);
963 **(int **)rv = tpacpi_query_bcl_levels(handle);
964 return AE_CTRL_TERMINATE;
965 } else {
966 return AE_OK;
971 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
973 static int __init tpacpi_check_std_acpi_brightness_support(void)
975 int status;
976 int bcl_levels = 0;
977 void *bcl_ptr = &bcl_levels;
979 if (!vid_handle) {
980 TPACPI_ACPIHANDLE_INIT(vid);
982 if (!vid_handle)
983 return 0;
986 * Search for a _BCL method, and execute it. This is safe on all
987 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
988 * BIOS in ACPI backlight control mode. We do NOT have to care
989 * about calling the _BCL method in an enabled video device, any
990 * will do for our purposes.
993 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
994 tpacpi_acpi_walk_find_bcl, NULL,
995 &bcl_ptr);
997 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
998 tp_features.bright_acpimode = 1;
999 return (bcl_levels - 2);
1002 return 0;
1005 static int __init tpacpi_new_rfkill(const unsigned int id,
1006 struct rfkill **rfk,
1007 const enum rfkill_type rfktype,
1008 const char *name,
1009 const bool set_default,
1010 int (*toggle_radio)(void *, enum rfkill_state),
1011 int (*get_state)(void *, enum rfkill_state *))
1013 int res;
1014 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1016 res = get_state(NULL, &initial_state);
1017 if (res < 0) {
1018 printk(TPACPI_ERR
1019 "failed to read initial state for %s, error %d; "
1020 "will turn radio off\n", name, res);
1021 } else if (set_default) {
1022 /* try to set the initial state as the default for the rfkill
1023 * type, since we ask the firmware to preserve it across S5 in
1024 * NVRAM */
1025 rfkill_set_default(rfktype,
1026 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1027 RFKILL_STATE_UNBLOCKED :
1028 RFKILL_STATE_SOFT_BLOCKED);
1031 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1032 if (!*rfk) {
1033 printk(TPACPI_ERR
1034 "failed to allocate memory for rfkill class\n");
1035 return -ENOMEM;
1038 (*rfk)->name = name;
1039 (*rfk)->get_state = get_state;
1040 (*rfk)->toggle_radio = toggle_radio;
1041 (*rfk)->state = initial_state;
1043 res = rfkill_register(*rfk);
1044 if (res < 0) {
1045 printk(TPACPI_ERR
1046 "failed to register %s rfkill switch: %d\n",
1047 name, res);
1048 rfkill_free(*rfk);
1049 *rfk = NULL;
1050 return res;
1053 return 0;
1056 static void printk_deprecated_attribute(const char * const what,
1057 const char * const details)
1059 tpacpi_log_usertask("deprecated sysfs attribute");
1060 printk(TPACPI_WARN "WARNING: sysfs attribute %s is deprecated and "
1061 "will be removed. %s\n",
1062 what, details);
1065 static void printk_deprecated_rfkill_attribute(const char * const what)
1067 printk_deprecated_attribute(what,
1068 "Please switch to generic rfkill before year 2010");
1071 /*************************************************************************
1072 * thinkpad-acpi driver attributes
1075 /* interface_version --------------------------------------------------- */
1076 static ssize_t tpacpi_driver_interface_version_show(
1077 struct device_driver *drv,
1078 char *buf)
1080 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1083 static DRIVER_ATTR(interface_version, S_IRUGO,
1084 tpacpi_driver_interface_version_show, NULL);
1086 /* debug_level --------------------------------------------------------- */
1087 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1088 char *buf)
1090 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1093 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1094 const char *buf, size_t count)
1096 unsigned long t;
1098 if (parse_strtoul(buf, 0xffff, &t))
1099 return -EINVAL;
1101 dbg_level = t;
1103 return count;
1106 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1107 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1109 /* version ------------------------------------------------------------- */
1110 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1111 char *buf)
1113 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1114 TPACPI_DESC, TPACPI_VERSION);
1117 static DRIVER_ATTR(version, S_IRUGO,
1118 tpacpi_driver_version_show, NULL);
1120 /* --------------------------------------------------------------------- */
1122 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1124 static void tpacpi_send_radiosw_update(void);
1126 /* wlsw_emulstate ------------------------------------------------------ */
1127 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1128 char *buf)
1130 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1133 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1134 const char *buf, size_t count)
1136 unsigned long t;
1138 if (parse_strtoul(buf, 1, &t))
1139 return -EINVAL;
1141 if (tpacpi_wlsw_emulstate != t) {
1142 tpacpi_wlsw_emulstate = !!t;
1143 tpacpi_send_radiosw_update();
1144 } else
1145 tpacpi_wlsw_emulstate = !!t;
1147 return count;
1150 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1151 tpacpi_driver_wlsw_emulstate_show,
1152 tpacpi_driver_wlsw_emulstate_store);
1154 /* bluetooth_emulstate ------------------------------------------------- */
1155 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1156 struct device_driver *drv,
1157 char *buf)
1159 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1162 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1163 struct device_driver *drv,
1164 const char *buf, size_t count)
1166 unsigned long t;
1168 if (parse_strtoul(buf, 1, &t))
1169 return -EINVAL;
1171 tpacpi_bluetooth_emulstate = !!t;
1173 return count;
1176 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1177 tpacpi_driver_bluetooth_emulstate_show,
1178 tpacpi_driver_bluetooth_emulstate_store);
1180 /* wwan_emulstate ------------------------------------------------- */
1181 static ssize_t tpacpi_driver_wwan_emulstate_show(
1182 struct device_driver *drv,
1183 char *buf)
1185 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1188 static ssize_t tpacpi_driver_wwan_emulstate_store(
1189 struct device_driver *drv,
1190 const char *buf, size_t count)
1192 unsigned long t;
1194 if (parse_strtoul(buf, 1, &t))
1195 return -EINVAL;
1197 tpacpi_wwan_emulstate = !!t;
1199 return count;
1202 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1203 tpacpi_driver_wwan_emulstate_show,
1204 tpacpi_driver_wwan_emulstate_store);
1206 /* uwb_emulstate ------------------------------------------------- */
1207 static ssize_t tpacpi_driver_uwb_emulstate_show(
1208 struct device_driver *drv,
1209 char *buf)
1211 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1214 static ssize_t tpacpi_driver_uwb_emulstate_store(
1215 struct device_driver *drv,
1216 const char *buf, size_t count)
1218 unsigned long t;
1220 if (parse_strtoul(buf, 1, &t))
1221 return -EINVAL;
1223 tpacpi_uwb_emulstate = !!t;
1225 return count;
1228 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1229 tpacpi_driver_uwb_emulstate_show,
1230 tpacpi_driver_uwb_emulstate_store);
1231 #endif
1233 /* --------------------------------------------------------------------- */
1235 static struct driver_attribute *tpacpi_driver_attributes[] = {
1236 &driver_attr_debug_level, &driver_attr_version,
1237 &driver_attr_interface_version,
1240 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1242 int i, res;
1244 i = 0;
1245 res = 0;
1246 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1247 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1248 i++;
1251 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1252 if (!res && dbg_wlswemul)
1253 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1254 if (!res && dbg_bluetoothemul)
1255 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1256 if (!res && dbg_wwanemul)
1257 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1258 if (!res && dbg_uwbemul)
1259 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1260 #endif
1262 return res;
1265 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1267 int i;
1269 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1270 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1272 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1273 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1274 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1275 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1276 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1277 #endif
1280 /****************************************************************************
1281 ****************************************************************************
1283 * Subdrivers
1285 ****************************************************************************
1286 ****************************************************************************/
1288 /*************************************************************************
1289 * thinkpad-acpi init subdriver
1292 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1294 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1295 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1297 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1298 (thinkpad_id.bios_version_str) ?
1299 thinkpad_id.bios_version_str : "unknown",
1300 (thinkpad_id.ec_version_str) ?
1301 thinkpad_id.ec_version_str : "unknown");
1303 if (thinkpad_id.vendor && thinkpad_id.model_str)
1304 printk(TPACPI_INFO "%s %s, model %s\n",
1305 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1306 "IBM" : ((thinkpad_id.vendor ==
1307 PCI_VENDOR_ID_LENOVO) ?
1308 "Lenovo" : "Unknown vendor"),
1309 thinkpad_id.model_str,
1310 (thinkpad_id.nummodel_str) ?
1311 thinkpad_id.nummodel_str : "unknown");
1313 return 0;
1316 static int thinkpad_acpi_driver_read(char *p)
1318 int len = 0;
1320 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1321 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1323 return len;
1326 static struct ibm_struct thinkpad_acpi_driver_data = {
1327 .name = "driver",
1328 .read = thinkpad_acpi_driver_read,
1331 /*************************************************************************
1332 * Hotkey subdriver
1335 enum { /* hot key scan codes (derived from ACPI DSDT) */
1336 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1337 TP_ACPI_HOTKEYSCAN_FNF2,
1338 TP_ACPI_HOTKEYSCAN_FNF3,
1339 TP_ACPI_HOTKEYSCAN_FNF4,
1340 TP_ACPI_HOTKEYSCAN_FNF5,
1341 TP_ACPI_HOTKEYSCAN_FNF6,
1342 TP_ACPI_HOTKEYSCAN_FNF7,
1343 TP_ACPI_HOTKEYSCAN_FNF8,
1344 TP_ACPI_HOTKEYSCAN_FNF9,
1345 TP_ACPI_HOTKEYSCAN_FNF10,
1346 TP_ACPI_HOTKEYSCAN_FNF11,
1347 TP_ACPI_HOTKEYSCAN_FNF12,
1348 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1349 TP_ACPI_HOTKEYSCAN_FNINSERT,
1350 TP_ACPI_HOTKEYSCAN_FNDELETE,
1351 TP_ACPI_HOTKEYSCAN_FNHOME,
1352 TP_ACPI_HOTKEYSCAN_FNEND,
1353 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1354 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1355 TP_ACPI_HOTKEYSCAN_FNSPACE,
1356 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1357 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1358 TP_ACPI_HOTKEYSCAN_MUTE,
1359 TP_ACPI_HOTKEYSCAN_THINKPAD,
1362 enum { /* Keys available through NVRAM polling */
1363 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1364 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1367 enum { /* Positions of some of the keys in hotkey masks */
1368 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1369 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1370 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1371 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1372 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1373 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1374 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1375 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1376 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1377 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1378 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1381 enum { /* NVRAM to ACPI HKEY group map */
1382 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1383 TP_ACPI_HKEY_ZOOM_MASK |
1384 TP_ACPI_HKEY_DISPSWTCH_MASK |
1385 TP_ACPI_HKEY_HIBERNATE_MASK,
1386 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1387 TP_ACPI_HKEY_BRGHTDWN_MASK,
1388 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1389 TP_ACPI_HKEY_VOLDWN_MASK |
1390 TP_ACPI_HKEY_MUTE_MASK,
1393 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1394 struct tp_nvram_state {
1395 u16 thinkpad_toggle:1;
1396 u16 zoom_toggle:1;
1397 u16 display_toggle:1;
1398 u16 thinklight_toggle:1;
1399 u16 hibernate_toggle:1;
1400 u16 displayexp_toggle:1;
1401 u16 display_state:1;
1402 u16 brightness_toggle:1;
1403 u16 volume_toggle:1;
1404 u16 mute:1;
1406 u8 brightness_level;
1407 u8 volume_level;
1410 static struct task_struct *tpacpi_hotkey_task;
1411 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1412 static int hotkey_poll_freq = 10; /* Hz */
1413 static struct mutex hotkey_thread_mutex;
1414 static struct mutex hotkey_thread_data_mutex;
1415 static unsigned int hotkey_config_change;
1417 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1419 #define hotkey_source_mask 0U
1421 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1423 static struct mutex hotkey_mutex;
1425 static enum { /* Reasons for waking up */
1426 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1427 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1428 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1429 } hotkey_wakeup_reason;
1431 static int hotkey_autosleep_ack;
1433 static u32 hotkey_orig_mask;
1434 static u32 hotkey_all_mask;
1435 static u32 hotkey_reserved_mask;
1436 static u32 hotkey_mask;
1438 static unsigned int hotkey_report_mode;
1440 static u16 *hotkey_keycode_map;
1442 static struct attribute_set *hotkey_dev_attributes;
1444 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1445 #define HOTKEY_CONFIG_CRITICAL_START \
1446 do { \
1447 mutex_lock(&hotkey_thread_data_mutex); \
1448 hotkey_config_change++; \
1449 } while (0);
1450 #define HOTKEY_CONFIG_CRITICAL_END \
1451 mutex_unlock(&hotkey_thread_data_mutex);
1452 #else
1453 #define HOTKEY_CONFIG_CRITICAL_START
1454 #define HOTKEY_CONFIG_CRITICAL_END
1455 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1457 /* HKEY.MHKG() return bits */
1458 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1460 static int hotkey_get_wlsw(int *status)
1462 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1463 if (dbg_wlswemul) {
1464 *status = !!tpacpi_wlsw_emulstate;
1465 return 0;
1467 #endif
1468 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1469 return -EIO;
1470 return 0;
1473 static int hotkey_get_tablet_mode(int *status)
1475 int s;
1477 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1478 return -EIO;
1480 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1481 return 0;
1485 * Call with hotkey_mutex held
1487 static int hotkey_mask_get(void)
1489 u32 m = 0;
1491 if (tp_features.hotkey_mask) {
1492 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1493 return -EIO;
1495 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1497 return 0;
1501 * Call with hotkey_mutex held
1503 static int hotkey_mask_set(u32 mask)
1505 int i;
1506 int rc = 0;
1508 if (tp_features.hotkey_mask) {
1509 if (!tp_warned.hotkey_mask_ff &&
1510 (mask == 0xffff || mask == 0xffffff ||
1511 mask == 0xffffffff)) {
1512 tp_warned.hotkey_mask_ff = 1;
1513 printk(TPACPI_NOTICE
1514 "setting the hotkey mask to 0x%08x is likely "
1515 "not the best way to go about it\n", mask);
1516 printk(TPACPI_NOTICE
1517 "please consider using the driver defaults, "
1518 "and refer to up-to-date thinkpad-acpi "
1519 "documentation\n");
1522 HOTKEY_CONFIG_CRITICAL_START
1523 for (i = 0; i < 32; i++) {
1524 u32 m = 1 << i;
1525 /* enable in firmware mask only keys not in NVRAM
1526 * mode, but enable the key in the cached hotkey_mask
1527 * regardless of mode, or the key will end up
1528 * disabled by hotkey_mask_get() */
1529 if (!acpi_evalf(hkey_handle,
1530 NULL, "MHKM", "vdd", i + 1,
1531 !!((mask & ~hotkey_source_mask) & m))) {
1532 rc = -EIO;
1533 break;
1534 } else {
1535 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1538 HOTKEY_CONFIG_CRITICAL_END
1540 /* hotkey_mask_get must be called unconditionally below */
1541 if (!hotkey_mask_get() && !rc &&
1542 (hotkey_mask & ~hotkey_source_mask) !=
1543 (mask & ~hotkey_source_mask)) {
1544 printk(TPACPI_NOTICE
1545 "requested hot key mask 0x%08x, but "
1546 "firmware forced it to 0x%08x\n",
1547 mask, hotkey_mask);
1549 } else {
1550 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1551 HOTKEY_CONFIG_CRITICAL_START
1552 hotkey_mask = mask & hotkey_source_mask;
1553 HOTKEY_CONFIG_CRITICAL_END
1554 hotkey_mask_get();
1555 if (hotkey_mask != mask) {
1556 printk(TPACPI_NOTICE
1557 "requested hot key mask 0x%08x, "
1558 "forced to 0x%08x (NVRAM poll mask is "
1559 "0x%08x): no firmware mask support\n",
1560 mask, hotkey_mask, hotkey_source_mask);
1562 #else
1563 hotkey_mask_get();
1564 rc = -ENXIO;
1565 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1568 return rc;
1571 static int hotkey_status_get(int *status)
1573 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1574 return -EIO;
1576 return 0;
1579 static int hotkey_status_set(bool enable)
1581 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1582 return -EIO;
1584 return 0;
1587 static void tpacpi_input_send_tabletsw(void)
1589 int state;
1591 if (tp_features.hotkey_tablet &&
1592 !hotkey_get_tablet_mode(&state)) {
1593 mutex_lock(&tpacpi_inputdev_send_mutex);
1595 input_report_switch(tpacpi_inputdev,
1596 SW_TABLET_MODE, !!state);
1597 input_sync(tpacpi_inputdev);
1599 mutex_unlock(&tpacpi_inputdev_send_mutex);
1603 static void tpacpi_input_send_key(unsigned int scancode)
1605 unsigned int keycode;
1607 keycode = hotkey_keycode_map[scancode];
1609 if (keycode != KEY_RESERVED) {
1610 mutex_lock(&tpacpi_inputdev_send_mutex);
1612 input_report_key(tpacpi_inputdev, keycode, 1);
1613 if (keycode == KEY_UNKNOWN)
1614 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1615 scancode);
1616 input_sync(tpacpi_inputdev);
1618 input_report_key(tpacpi_inputdev, keycode, 0);
1619 if (keycode == KEY_UNKNOWN)
1620 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1621 scancode);
1622 input_sync(tpacpi_inputdev);
1624 mutex_unlock(&tpacpi_inputdev_send_mutex);
1628 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1629 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1631 static void tpacpi_hotkey_send_key(unsigned int scancode)
1633 tpacpi_input_send_key(scancode);
1634 if (hotkey_report_mode < 2) {
1635 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1636 0x80, 0x1001 + scancode);
1640 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1642 u8 d;
1644 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1645 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1646 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1647 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1648 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1649 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1651 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1652 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1653 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1655 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1656 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1657 n->displayexp_toggle =
1658 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1660 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1661 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1662 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1663 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1664 n->brightness_toggle =
1665 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1667 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1668 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1669 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1670 >> TP_NVRAM_POS_LEVEL_VOLUME;
1671 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1672 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1676 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1677 do { \
1678 if ((mask & (1 << __scancode)) && \
1679 oldn->__member != newn->__member) \
1680 tpacpi_hotkey_send_key(__scancode); \
1681 } while (0)
1683 #define TPACPI_MAY_SEND_KEY(__scancode) \
1684 do { if (mask & (1 << __scancode)) \
1685 tpacpi_hotkey_send_key(__scancode); } while (0)
1687 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1688 struct tp_nvram_state *newn,
1689 u32 mask)
1691 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1694 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1696 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1698 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1700 /* handle volume */
1701 if (oldn->volume_toggle != newn->volume_toggle) {
1702 if (oldn->mute != newn->mute) {
1703 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1705 if (oldn->volume_level > newn->volume_level) {
1706 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1707 } else if (oldn->volume_level < newn->volume_level) {
1708 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1709 } else if (oldn->mute == newn->mute) {
1710 /* repeated key presses that didn't change state */
1711 if (newn->mute) {
1712 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1713 } else if (newn->volume_level != 0) {
1714 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1715 } else {
1716 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1721 /* handle brightness */
1722 if (oldn->brightness_toggle != newn->brightness_toggle) {
1723 if (oldn->brightness_level < newn->brightness_level) {
1724 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1725 } else if (oldn->brightness_level > newn->brightness_level) {
1726 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1727 } else {
1728 /* repeated key presses that didn't change state */
1729 if (newn->brightness_level != 0) {
1730 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1731 } else {
1732 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1738 #undef TPACPI_COMPARE_KEY
1739 #undef TPACPI_MAY_SEND_KEY
1741 static int hotkey_kthread(void *data)
1743 struct tp_nvram_state s[2];
1744 u32 mask;
1745 unsigned int si, so;
1746 unsigned long t;
1747 unsigned int change_detector, must_reset;
1749 mutex_lock(&hotkey_thread_mutex);
1751 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1752 goto exit;
1754 set_freezable();
1756 so = 0;
1757 si = 1;
1758 t = 0;
1760 /* Initial state for compares */
1761 mutex_lock(&hotkey_thread_data_mutex);
1762 change_detector = hotkey_config_change;
1763 mask = hotkey_source_mask & hotkey_mask;
1764 mutex_unlock(&hotkey_thread_data_mutex);
1765 hotkey_read_nvram(&s[so], mask);
1767 while (!kthread_should_stop() && hotkey_poll_freq) {
1768 if (t == 0)
1769 t = 1000/hotkey_poll_freq;
1770 t = msleep_interruptible(t);
1771 if (unlikely(kthread_should_stop()))
1772 break;
1773 must_reset = try_to_freeze();
1774 if (t > 0 && !must_reset)
1775 continue;
1777 mutex_lock(&hotkey_thread_data_mutex);
1778 if (must_reset || hotkey_config_change != change_detector) {
1779 /* forget old state on thaw or config change */
1780 si = so;
1781 t = 0;
1782 change_detector = hotkey_config_change;
1784 mask = hotkey_source_mask & hotkey_mask;
1785 mutex_unlock(&hotkey_thread_data_mutex);
1787 if (likely(mask)) {
1788 hotkey_read_nvram(&s[si], mask);
1789 if (likely(si != so)) {
1790 hotkey_compare_and_issue_event(&s[so], &s[si],
1791 mask);
1795 so = si;
1796 si ^= 1;
1799 exit:
1800 mutex_unlock(&hotkey_thread_mutex);
1801 return 0;
1804 static void hotkey_poll_stop_sync(void)
1806 if (tpacpi_hotkey_task) {
1807 if (frozen(tpacpi_hotkey_task) ||
1808 freezing(tpacpi_hotkey_task))
1809 thaw_process(tpacpi_hotkey_task);
1811 kthread_stop(tpacpi_hotkey_task);
1812 tpacpi_hotkey_task = NULL;
1813 mutex_lock(&hotkey_thread_mutex);
1814 /* at this point, the thread did exit */
1815 mutex_unlock(&hotkey_thread_mutex);
1819 /* call with hotkey_mutex held */
1820 static void hotkey_poll_setup(int may_warn)
1822 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1823 hotkey_poll_freq > 0 &&
1824 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1825 if (!tpacpi_hotkey_task) {
1826 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1827 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1828 if (IS_ERR(tpacpi_hotkey_task)) {
1829 tpacpi_hotkey_task = NULL;
1830 printk(TPACPI_ERR
1831 "could not create kernel thread "
1832 "for hotkey polling\n");
1835 } else {
1836 hotkey_poll_stop_sync();
1837 if (may_warn &&
1838 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1839 printk(TPACPI_NOTICE
1840 "hot keys 0x%08x require polling, "
1841 "which is currently disabled\n",
1842 hotkey_source_mask);
1847 static void hotkey_poll_setup_safe(int may_warn)
1849 mutex_lock(&hotkey_mutex);
1850 hotkey_poll_setup(may_warn);
1851 mutex_unlock(&hotkey_mutex);
1854 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1856 static void hotkey_poll_setup_safe(int __unused)
1860 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1862 static int hotkey_inputdev_open(struct input_dev *dev)
1864 switch (tpacpi_lifecycle) {
1865 case TPACPI_LIFE_INIT:
1867 * hotkey_init will call hotkey_poll_setup_safe
1868 * at the appropriate moment
1870 return 0;
1871 case TPACPI_LIFE_EXITING:
1872 return -EBUSY;
1873 case TPACPI_LIFE_RUNNING:
1874 hotkey_poll_setup_safe(0);
1875 return 0;
1878 /* Should only happen if tpacpi_lifecycle is corrupt */
1879 BUG();
1880 return -EBUSY;
1883 static void hotkey_inputdev_close(struct input_dev *dev)
1885 /* disable hotkey polling when possible */
1886 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1887 hotkey_poll_setup_safe(0);
1890 /* sysfs hotkey enable ------------------------------------------------- */
1891 static ssize_t hotkey_enable_show(struct device *dev,
1892 struct device_attribute *attr,
1893 char *buf)
1895 int res, status;
1897 printk_deprecated_attribute("hotkey_enable",
1898 "Hotkey reporting is always enabled");
1900 res = hotkey_status_get(&status);
1901 if (res)
1902 return res;
1904 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1907 static ssize_t hotkey_enable_store(struct device *dev,
1908 struct device_attribute *attr,
1909 const char *buf, size_t count)
1911 unsigned long t;
1913 printk_deprecated_attribute("hotkey_enable",
1914 "Hotkeys can be disabled through hotkey_mask");
1916 if (parse_strtoul(buf, 1, &t))
1917 return -EINVAL;
1919 if (t == 0)
1920 return -EPERM;
1922 return count;
1925 static struct device_attribute dev_attr_hotkey_enable =
1926 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1927 hotkey_enable_show, hotkey_enable_store);
1929 /* sysfs hotkey mask --------------------------------------------------- */
1930 static ssize_t hotkey_mask_show(struct device *dev,
1931 struct device_attribute *attr,
1932 char *buf)
1934 int res;
1936 if (mutex_lock_killable(&hotkey_mutex))
1937 return -ERESTARTSYS;
1938 res = hotkey_mask_get();
1939 mutex_unlock(&hotkey_mutex);
1941 return (res)?
1942 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1945 static ssize_t hotkey_mask_store(struct device *dev,
1946 struct device_attribute *attr,
1947 const char *buf, size_t count)
1949 unsigned long t;
1950 int res;
1952 if (parse_strtoul(buf, 0xffffffffUL, &t))
1953 return -EINVAL;
1955 if (mutex_lock_killable(&hotkey_mutex))
1956 return -ERESTARTSYS;
1958 res = hotkey_mask_set(t);
1960 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1961 hotkey_poll_setup(1);
1962 #endif
1964 mutex_unlock(&hotkey_mutex);
1966 return (res) ? res : count;
1969 static struct device_attribute dev_attr_hotkey_mask =
1970 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1971 hotkey_mask_show, hotkey_mask_store);
1973 /* sysfs hotkey bios_enabled ------------------------------------------- */
1974 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1975 struct device_attribute *attr,
1976 char *buf)
1978 return sprintf(buf, "0\n");
1981 static struct device_attribute dev_attr_hotkey_bios_enabled =
1982 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1984 /* sysfs hotkey bios_mask ---------------------------------------------- */
1985 static ssize_t hotkey_bios_mask_show(struct device *dev,
1986 struct device_attribute *attr,
1987 char *buf)
1989 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1992 static struct device_attribute dev_attr_hotkey_bios_mask =
1993 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1995 /* sysfs hotkey all_mask ----------------------------------------------- */
1996 static ssize_t hotkey_all_mask_show(struct device *dev,
1997 struct device_attribute *attr,
1998 char *buf)
2000 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2001 hotkey_all_mask | hotkey_source_mask);
2004 static struct device_attribute dev_attr_hotkey_all_mask =
2005 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2007 /* sysfs hotkey recommended_mask --------------------------------------- */
2008 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2009 struct device_attribute *attr,
2010 char *buf)
2012 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2013 (hotkey_all_mask | hotkey_source_mask)
2014 & ~hotkey_reserved_mask);
2017 static struct device_attribute dev_attr_hotkey_recommended_mask =
2018 __ATTR(hotkey_recommended_mask, S_IRUGO,
2019 hotkey_recommended_mask_show, NULL);
2021 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2023 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2024 static ssize_t hotkey_source_mask_show(struct device *dev,
2025 struct device_attribute *attr,
2026 char *buf)
2028 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2031 static ssize_t hotkey_source_mask_store(struct device *dev,
2032 struct device_attribute *attr,
2033 const char *buf, size_t count)
2035 unsigned long t;
2037 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2038 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2039 return -EINVAL;
2041 if (mutex_lock_killable(&hotkey_mutex))
2042 return -ERESTARTSYS;
2044 HOTKEY_CONFIG_CRITICAL_START
2045 hotkey_source_mask = t;
2046 HOTKEY_CONFIG_CRITICAL_END
2048 hotkey_poll_setup(1);
2050 mutex_unlock(&hotkey_mutex);
2052 return count;
2055 static struct device_attribute dev_attr_hotkey_source_mask =
2056 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2057 hotkey_source_mask_show, hotkey_source_mask_store);
2059 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2060 static ssize_t hotkey_poll_freq_show(struct device *dev,
2061 struct device_attribute *attr,
2062 char *buf)
2064 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2067 static ssize_t hotkey_poll_freq_store(struct device *dev,
2068 struct device_attribute *attr,
2069 const char *buf, size_t count)
2071 unsigned long t;
2073 if (parse_strtoul(buf, 25, &t))
2074 return -EINVAL;
2076 if (mutex_lock_killable(&hotkey_mutex))
2077 return -ERESTARTSYS;
2079 hotkey_poll_freq = t;
2081 hotkey_poll_setup(1);
2082 mutex_unlock(&hotkey_mutex);
2084 return count;
2087 static struct device_attribute dev_attr_hotkey_poll_freq =
2088 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2089 hotkey_poll_freq_show, hotkey_poll_freq_store);
2091 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2093 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2094 static ssize_t hotkey_radio_sw_show(struct device *dev,
2095 struct device_attribute *attr,
2096 char *buf)
2098 int res, s;
2099 res = hotkey_get_wlsw(&s);
2100 if (res < 0)
2101 return res;
2103 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2106 static struct device_attribute dev_attr_hotkey_radio_sw =
2107 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2109 static void hotkey_radio_sw_notify_change(void)
2111 if (tp_features.hotkey_wlsw)
2112 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2113 "hotkey_radio_sw");
2116 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2117 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2118 struct device_attribute *attr,
2119 char *buf)
2121 int res, s;
2122 res = hotkey_get_tablet_mode(&s);
2123 if (res < 0)
2124 return res;
2126 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2129 static struct device_attribute dev_attr_hotkey_tablet_mode =
2130 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2132 static void hotkey_tablet_mode_notify_change(void)
2134 if (tp_features.hotkey_tablet)
2135 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2136 "hotkey_tablet_mode");
2139 /* sysfs hotkey report_mode -------------------------------------------- */
2140 static ssize_t hotkey_report_mode_show(struct device *dev,
2141 struct device_attribute *attr,
2142 char *buf)
2144 return snprintf(buf, PAGE_SIZE, "%d\n",
2145 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2148 static struct device_attribute dev_attr_hotkey_report_mode =
2149 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2151 /* sysfs wakeup reason (pollable) -------------------------------------- */
2152 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2153 struct device_attribute *attr,
2154 char *buf)
2156 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2159 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2160 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2162 static void hotkey_wakeup_reason_notify_change(void)
2164 if (tp_features.hotkey_mask)
2165 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2166 "wakeup_reason");
2169 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2170 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2171 struct device_attribute *attr,
2172 char *buf)
2174 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2177 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2178 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2179 hotkey_wakeup_hotunplug_complete_show, NULL);
2181 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2183 if (tp_features.hotkey_mask)
2184 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2185 "wakeup_hotunplug_complete");
2188 /* --------------------------------------------------------------------- */
2190 static struct attribute *hotkey_attributes[] __initdata = {
2191 &dev_attr_hotkey_enable.attr,
2192 &dev_attr_hotkey_bios_enabled.attr,
2193 &dev_attr_hotkey_report_mode.attr,
2194 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2195 &dev_attr_hotkey_mask.attr,
2196 &dev_attr_hotkey_all_mask.attr,
2197 &dev_attr_hotkey_recommended_mask.attr,
2198 &dev_attr_hotkey_source_mask.attr,
2199 &dev_attr_hotkey_poll_freq.attr,
2200 #endif
2203 static struct attribute *hotkey_mask_attributes[] __initdata = {
2204 &dev_attr_hotkey_bios_mask.attr,
2205 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2206 &dev_attr_hotkey_mask.attr,
2207 &dev_attr_hotkey_all_mask.attr,
2208 &dev_attr_hotkey_recommended_mask.attr,
2209 #endif
2210 &dev_attr_hotkey_wakeup_reason.attr,
2211 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2214 static void bluetooth_update_rfk(void);
2215 static void wan_update_rfk(void);
2216 static void uwb_update_rfk(void);
2217 static void tpacpi_send_radiosw_update(void)
2219 int wlsw;
2221 /* Sync these BEFORE sending any rfkill events */
2222 if (tp_features.bluetooth)
2223 bluetooth_update_rfk();
2224 if (tp_features.wan)
2225 wan_update_rfk();
2226 if (tp_features.uwb)
2227 uwb_update_rfk();
2229 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2230 mutex_lock(&tpacpi_inputdev_send_mutex);
2232 input_report_switch(tpacpi_inputdev,
2233 SW_RFKILL_ALL, !!wlsw);
2234 input_sync(tpacpi_inputdev);
2236 mutex_unlock(&tpacpi_inputdev_send_mutex);
2238 hotkey_radio_sw_notify_change();
2241 static void hotkey_exit(void)
2243 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2244 hotkey_poll_stop_sync();
2245 #endif
2247 if (hotkey_dev_attributes)
2248 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2250 kfree(hotkey_keycode_map);
2252 if (tp_features.hotkey) {
2253 dbg_printk(TPACPI_DBG_EXIT,
2254 "restoring original hot key mask\n");
2255 /* no short-circuit boolean operator below! */
2256 if ((hotkey_mask_set(hotkey_orig_mask) |
2257 hotkey_status_set(false)) != 0)
2258 printk(TPACPI_ERR
2259 "failed to restore hot key mask "
2260 "to BIOS defaults\n");
2264 static int __init hotkey_init(struct ibm_init_struct *iibm)
2266 /* Requirements for changing the default keymaps:
2268 * 1. Many of the keys are mapped to KEY_RESERVED for very
2269 * good reasons. Do not change them unless you have deep
2270 * knowledge on the IBM and Lenovo ThinkPad firmware for
2271 * the various ThinkPad models. The driver behaves
2272 * differently for KEY_RESERVED: such keys have their
2273 * hot key mask *unset* in mask_recommended, and also
2274 * in the initial hot key mask programmed into the
2275 * firmware at driver load time, which means the firm-
2276 * ware may react very differently if you change them to
2277 * something else;
2279 * 2. You must be subscribed to the linux-thinkpad and
2280 * ibm-acpi-devel mailing lists, and you should read the
2281 * list archives since 2007 if you want to change the
2282 * keymaps. This requirement exists so that you will
2283 * know the past history of problems with the thinkpad-
2284 * acpi driver keymaps, and also that you will be
2285 * listening to any bug reports;
2287 * 3. Do not send thinkpad-acpi specific patches directly to
2288 * for merging, *ever*. Send them to the linux-acpi
2289 * mailinglist for comments. Merging is to be done only
2290 * through acpi-test and the ACPI maintainer.
2292 * If the above is too much to ask, don't change the keymap.
2293 * Ask the thinkpad-acpi maintainer to do it, instead.
2295 static u16 ibm_keycode_map[] __initdata = {
2296 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2297 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2298 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2299 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2301 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2302 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2303 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2304 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2306 /* brightness: firmware always reacts to them, unless
2307 * X.org did some tricks in the radeon BIOS scratch
2308 * registers of *some* models */
2309 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2310 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2312 /* Thinklight: firmware always react to it */
2313 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2315 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2316 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2318 /* Volume: firmware always react to it and reprograms
2319 * the built-in *extra* mixer. Never map it to control
2320 * another mixer by default. */
2321 KEY_RESERVED, /* 0x14: VOLUME UP */
2322 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2323 KEY_RESERVED, /* 0x16: MUTE */
2325 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2327 /* (assignments unknown, please report if found) */
2328 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2329 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2331 static u16 lenovo_keycode_map[] __initdata = {
2332 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2333 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2334 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2335 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2337 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2338 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2339 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2340 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2342 /* These either have to go through ACPI video, or
2343 * act like in the IBM ThinkPads, so don't ever
2344 * enable them by default */
2345 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2346 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2348 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2350 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2351 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2353 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2354 * react to it and reprograms the built-in *extra* mixer.
2355 * Never map it to control another mixer by default.
2357 * T60?, T61, R60?, R61: firmware and EC tries to send
2358 * these over the regular keyboard, so these are no-ops,
2359 * but there are still weird bugs re. MUTE, so do not
2360 * change unless you get test reports from all Lenovo
2361 * models. May cause the BIOS to interfere with the
2362 * HDA mixer.
2364 KEY_RESERVED, /* 0x14: VOLUME UP */
2365 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2366 KEY_RESERVED, /* 0x16: MUTE */
2368 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2370 /* (assignments unknown, please report if found) */
2371 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2372 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2375 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2376 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2377 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2379 int res, i;
2380 int status;
2381 int hkeyv;
2383 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2385 BUG_ON(!tpacpi_inputdev);
2386 BUG_ON(tpacpi_inputdev->open != NULL ||
2387 tpacpi_inputdev->close != NULL);
2389 TPACPI_ACPIHANDLE_INIT(hkey);
2390 mutex_init(&hotkey_mutex);
2392 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2393 mutex_init(&hotkey_thread_mutex);
2394 mutex_init(&hotkey_thread_data_mutex);
2395 #endif
2397 /* hotkey not supported on 570 */
2398 tp_features.hotkey = hkey_handle != NULL;
2400 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2401 str_supported(tp_features.hotkey));
2403 if (!tp_features.hotkey)
2404 return 1;
2406 tpacpi_disable_brightness_delay();
2408 hotkey_dev_attributes = create_attr_set(13, NULL);
2409 if (!hotkey_dev_attributes)
2410 return -ENOMEM;
2411 res = add_many_to_attr_set(hotkey_dev_attributes,
2412 hotkey_attributes,
2413 ARRAY_SIZE(hotkey_attributes));
2414 if (res)
2415 goto err_exit;
2417 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2418 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2419 for HKEY interface version 0x100 */
2420 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2421 if ((hkeyv >> 8) != 1) {
2422 printk(TPACPI_ERR "unknown version of the "
2423 "HKEY interface: 0x%x\n", hkeyv);
2424 printk(TPACPI_ERR "please report this to %s\n",
2425 TPACPI_MAIL);
2426 } else {
2428 * MHKV 0x100 in A31, R40, R40e,
2429 * T4x, X31, and later
2431 tp_features.hotkey_mask = 1;
2435 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2436 str_supported(tp_features.hotkey_mask));
2438 if (tp_features.hotkey_mask) {
2439 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2440 "MHKA", "qd")) {
2441 printk(TPACPI_ERR
2442 "missing MHKA handler, "
2443 "please report this to %s\n",
2444 TPACPI_MAIL);
2445 /* FN+F12, FN+F4, FN+F3 */
2446 hotkey_all_mask = 0x080cU;
2450 /* hotkey_source_mask *must* be zero for
2451 * the first hotkey_mask_get */
2452 if (tp_features.hotkey_mask) {
2453 res = hotkey_mask_get();
2454 if (res)
2455 goto err_exit;
2457 hotkey_orig_mask = hotkey_mask;
2458 res = add_many_to_attr_set(
2459 hotkey_dev_attributes,
2460 hotkey_mask_attributes,
2461 ARRAY_SIZE(hotkey_mask_attributes));
2462 if (res)
2463 goto err_exit;
2466 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2467 if (tp_features.hotkey_mask) {
2468 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2469 & ~hotkey_all_mask;
2470 } else {
2471 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2474 vdbg_printk(TPACPI_DBG_INIT,
2475 "hotkey source mask 0x%08x, polling freq %d\n",
2476 hotkey_source_mask, hotkey_poll_freq);
2477 #endif
2479 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2480 if (dbg_wlswemul) {
2481 tp_features.hotkey_wlsw = 1;
2482 printk(TPACPI_INFO
2483 "radio switch emulation enabled\n");
2484 } else
2485 #endif
2486 /* Not all thinkpads have a hardware radio switch */
2487 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2488 tp_features.hotkey_wlsw = 1;
2489 printk(TPACPI_INFO
2490 "radio switch found; radios are %s\n",
2491 enabled(status, 0));
2493 if (tp_features.hotkey_wlsw)
2494 res = add_to_attr_set(hotkey_dev_attributes,
2495 &dev_attr_hotkey_radio_sw.attr);
2497 /* For X41t, X60t, X61t Tablets... */
2498 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2499 tp_features.hotkey_tablet = 1;
2500 printk(TPACPI_INFO
2501 "possible tablet mode switch found; "
2502 "ThinkPad in %s mode\n",
2503 (status & TP_HOTKEY_TABLET_MASK)?
2504 "tablet" : "laptop");
2505 res = add_to_attr_set(hotkey_dev_attributes,
2506 &dev_attr_hotkey_tablet_mode.attr);
2509 if (!res)
2510 res = register_attr_set_with_sysfs(
2511 hotkey_dev_attributes,
2512 &tpacpi_pdev->dev.kobj);
2513 if (res)
2514 goto err_exit;
2516 /* Set up key map */
2518 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2519 GFP_KERNEL);
2520 if (!hotkey_keycode_map) {
2521 printk(TPACPI_ERR
2522 "failed to allocate memory for key map\n");
2523 res = -ENOMEM;
2524 goto err_exit;
2527 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2528 dbg_printk(TPACPI_DBG_INIT,
2529 "using Lenovo default hot key map\n");
2530 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2531 TPACPI_HOTKEY_MAP_SIZE);
2532 } else {
2533 dbg_printk(TPACPI_DBG_INIT,
2534 "using IBM default hot key map\n");
2535 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2536 TPACPI_HOTKEY_MAP_SIZE);
2539 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2540 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2541 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2542 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2543 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2544 tpacpi_inputdev->keycode = hotkey_keycode_map;
2545 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2546 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2547 set_bit(hotkey_keycode_map[i],
2548 tpacpi_inputdev->keybit);
2549 } else {
2550 if (i < sizeof(hotkey_reserved_mask)*8)
2551 hotkey_reserved_mask |= 1 << i;
2555 if (tp_features.hotkey_wlsw) {
2556 set_bit(EV_SW, tpacpi_inputdev->evbit);
2557 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2559 if (tp_features.hotkey_tablet) {
2560 set_bit(EV_SW, tpacpi_inputdev->evbit);
2561 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2564 /* Do not issue duplicate brightness change events to
2565 * userspace */
2566 if (!tp_features.bright_acpimode)
2567 /* update bright_acpimode... */
2568 tpacpi_check_std_acpi_brightness_support();
2570 if (tp_features.bright_acpimode) {
2571 printk(TPACPI_INFO
2572 "This ThinkPad has standard ACPI backlight "
2573 "brightness control, supported by the ACPI "
2574 "video driver\n");
2575 printk(TPACPI_NOTICE
2576 "Disabling thinkpad-acpi brightness events "
2577 "by default...\n");
2579 /* The hotkey_reserved_mask change below is not
2580 * necessary while the keys are at KEY_RESERVED in the
2581 * default map, but better safe than sorry, leave it
2582 * here as a marker of what we have to do, especially
2583 * when we finally become able to set this at runtime
2584 * on response to X.org requests */
2585 hotkey_reserved_mask |=
2586 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2587 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2590 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2591 res = hotkey_status_set(true);
2592 if (res) {
2593 hotkey_exit();
2594 return res;
2596 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2597 & ~hotkey_reserved_mask)
2598 | hotkey_orig_mask);
2599 if (res < 0 && res != -ENXIO) {
2600 hotkey_exit();
2601 return res;
2604 dbg_printk(TPACPI_DBG_INIT,
2605 "legacy hot key reporting over procfs %s\n",
2606 (hotkey_report_mode < 2) ?
2607 "enabled" : "disabled");
2609 tpacpi_inputdev->open = &hotkey_inputdev_open;
2610 tpacpi_inputdev->close = &hotkey_inputdev_close;
2612 hotkey_poll_setup_safe(1);
2613 tpacpi_send_radiosw_update();
2614 tpacpi_input_send_tabletsw();
2616 return 0;
2618 err_exit:
2619 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2620 hotkey_dev_attributes = NULL;
2622 return (res < 0)? res : 1;
2625 static bool hotkey_notify_hotkey(const u32 hkey,
2626 bool *send_acpi_ev,
2627 bool *ignore_acpi_ev)
2629 /* 0x1000-0x1FFF: key presses */
2630 unsigned int scancode = hkey & 0xfff;
2631 *send_acpi_ev = true;
2632 *ignore_acpi_ev = false;
2634 if (scancode > 0 && scancode < 0x21) {
2635 scancode--;
2636 if (!(hotkey_source_mask & (1 << scancode))) {
2637 tpacpi_input_send_key(scancode);
2638 *send_acpi_ev = false;
2639 } else {
2640 *ignore_acpi_ev = true;
2642 return true;
2644 return false;
2647 static bool hotkey_notify_wakeup(const u32 hkey,
2648 bool *send_acpi_ev,
2649 bool *ignore_acpi_ev)
2651 /* 0x2000-0x2FFF: Wakeup reason */
2652 *send_acpi_ev = true;
2653 *ignore_acpi_ev = false;
2655 switch (hkey) {
2656 case 0x2304: /* suspend, undock */
2657 case 0x2404: /* hibernation, undock */
2658 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2659 *ignore_acpi_ev = true;
2660 break;
2662 case 0x2305: /* suspend, bay eject */
2663 case 0x2405: /* hibernation, bay eject */
2664 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2665 *ignore_acpi_ev = true;
2666 break;
2668 case 0x2313: /* Battery on critical low level (S3) */
2669 case 0x2413: /* Battery on critical low level (S4) */
2670 printk(TPACPI_ALERT
2671 "EMERGENCY WAKEUP: battery almost empty\n");
2672 /* how to auto-heal: */
2673 /* 2313: woke up from S3, go to S4/S5 */
2674 /* 2413: woke up from S4, go to S5 */
2675 break;
2677 default:
2678 return false;
2681 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2682 printk(TPACPI_INFO
2683 "woke up due to a hot-unplug "
2684 "request...\n");
2685 hotkey_wakeup_reason_notify_change();
2687 return true;
2690 static bool hotkey_notify_usrevent(const u32 hkey,
2691 bool *send_acpi_ev,
2692 bool *ignore_acpi_ev)
2694 /* 0x5000-0x5FFF: human interface helpers */
2695 *send_acpi_ev = true;
2696 *ignore_acpi_ev = false;
2698 switch (hkey) {
2699 case 0x5010: /* Lenovo new BIOS: brightness changed */
2700 case 0x500b: /* X61t: tablet pen inserted into bay */
2701 case 0x500c: /* X61t: tablet pen removed from bay */
2702 return true;
2704 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2705 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2706 tpacpi_input_send_tabletsw();
2707 hotkey_tablet_mode_notify_change();
2708 *send_acpi_ev = false;
2709 return true;
2711 case 0x5001:
2712 case 0x5002:
2713 /* LID switch events. Do not propagate */
2714 *ignore_acpi_ev = true;
2715 return true;
2717 default:
2718 return false;
2722 static bool hotkey_notify_thermal(const u32 hkey,
2723 bool *send_acpi_ev,
2724 bool *ignore_acpi_ev)
2726 /* 0x6000-0x6FFF: thermal alarms */
2727 *send_acpi_ev = true;
2728 *ignore_acpi_ev = false;
2730 switch (hkey) {
2731 case 0x6011:
2732 printk(TPACPI_CRIT
2733 "THERMAL ALARM: battery is too hot!\n");
2734 /* recommended action: warn user through gui */
2735 return true;
2736 case 0x6012:
2737 printk(TPACPI_ALERT
2738 "THERMAL EMERGENCY: battery is extremely hot!\n");
2739 /* recommended action: immediate sleep/hibernate */
2740 return true;
2741 case 0x6021:
2742 printk(TPACPI_CRIT
2743 "THERMAL ALARM: "
2744 "a sensor reports something is too hot!\n");
2745 /* recommended action: warn user through gui, that */
2746 /* some internal component is too hot */
2747 return true;
2748 case 0x6022:
2749 printk(TPACPI_ALERT
2750 "THERMAL EMERGENCY: "
2751 "a sensor reports something is extremely hot!\n");
2752 /* recommended action: immediate sleep/hibernate */
2753 return true;
2754 case 0x6030:
2755 printk(TPACPI_INFO
2756 "EC reports that Thermal Table has changed\n");
2757 /* recommended action: do nothing, we don't have
2758 * Lenovo ATM information */
2759 return true;
2760 default:
2761 printk(TPACPI_ALERT
2762 "THERMAL ALERT: unknown thermal alarm received\n");
2763 return false;
2767 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2769 u32 hkey;
2770 bool send_acpi_ev;
2771 bool ignore_acpi_ev;
2772 bool known_ev;
2774 if (event != 0x80) {
2775 printk(TPACPI_ERR
2776 "unknown HKEY notification event %d\n", event);
2777 /* forward it to userspace, maybe it knows how to handle it */
2778 acpi_bus_generate_netlink_event(
2779 ibm->acpi->device->pnp.device_class,
2780 dev_name(&ibm->acpi->device->dev),
2781 event, 0);
2782 return;
2785 while (1) {
2786 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2787 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2788 return;
2791 if (hkey == 0) {
2792 /* queue empty */
2793 return;
2796 send_acpi_ev = true;
2797 ignore_acpi_ev = false;
2799 switch (hkey >> 12) {
2800 case 1:
2801 /* 0x1000-0x1FFF: key presses */
2802 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2803 &ignore_acpi_ev);
2804 break;
2805 case 2:
2806 /* 0x2000-0x2FFF: Wakeup reason */
2807 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2808 &ignore_acpi_ev);
2809 break;
2810 case 3:
2811 /* 0x3000-0x3FFF: bay-related wakeups */
2812 if (hkey == 0x3003) {
2813 hotkey_autosleep_ack = 1;
2814 printk(TPACPI_INFO
2815 "bay ejected\n");
2816 hotkey_wakeup_hotunplug_complete_notify_change();
2817 known_ev = true;
2818 } else {
2819 known_ev = false;
2821 break;
2822 case 4:
2823 /* 0x4000-0x4FFF: dock-related wakeups */
2824 if (hkey == 0x4003) {
2825 hotkey_autosleep_ack = 1;
2826 printk(TPACPI_INFO
2827 "undocked\n");
2828 hotkey_wakeup_hotunplug_complete_notify_change();
2829 known_ev = true;
2830 } else {
2831 known_ev = false;
2833 break;
2834 case 5:
2835 /* 0x5000-0x5FFF: human interface helpers */
2836 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2837 &ignore_acpi_ev);
2838 break;
2839 case 6:
2840 /* 0x6000-0x6FFF: thermal alarms */
2841 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2842 &ignore_acpi_ev);
2843 break;
2844 case 7:
2845 /* 0x7000-0x7FFF: misc */
2846 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2847 tpacpi_send_radiosw_update();
2848 send_acpi_ev = 0;
2849 known_ev = true;
2850 break;
2852 /* fallthrough to default */
2853 default:
2854 known_ev = false;
2856 if (!known_ev) {
2857 printk(TPACPI_NOTICE
2858 "unhandled HKEY event 0x%04x\n", hkey);
2859 printk(TPACPI_NOTICE
2860 "please report the conditions when this "
2861 "event happened to %s\n", TPACPI_MAIL);
2864 /* Legacy events */
2865 if (!ignore_acpi_ev &&
2866 (send_acpi_ev || hotkey_report_mode < 2)) {
2867 acpi_bus_generate_proc_event(ibm->acpi->device,
2868 event, hkey);
2871 /* netlink events */
2872 if (!ignore_acpi_ev && send_acpi_ev) {
2873 acpi_bus_generate_netlink_event(
2874 ibm->acpi->device->pnp.device_class,
2875 dev_name(&ibm->acpi->device->dev),
2876 event, hkey);
2881 static void hotkey_suspend(pm_message_t state)
2883 /* Do these on suspend, we get the events on early resume! */
2884 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2885 hotkey_autosleep_ack = 0;
2888 static void hotkey_resume(void)
2890 tpacpi_disable_brightness_delay();
2892 if (hotkey_mask_get())
2893 printk(TPACPI_ERR
2894 "error while trying to read hot key mask "
2895 "from firmware\n");
2896 tpacpi_send_radiosw_update();
2897 hotkey_tablet_mode_notify_change();
2898 hotkey_wakeup_reason_notify_change();
2899 hotkey_wakeup_hotunplug_complete_notify_change();
2900 hotkey_poll_setup_safe(0);
2903 /* procfs -------------------------------------------------------------- */
2904 static int hotkey_read(char *p)
2906 int res, status;
2907 int len = 0;
2909 if (!tp_features.hotkey) {
2910 len += sprintf(p + len, "status:\t\tnot supported\n");
2911 return len;
2914 if (mutex_lock_killable(&hotkey_mutex))
2915 return -ERESTARTSYS;
2916 res = hotkey_status_get(&status);
2917 if (!res)
2918 res = hotkey_mask_get();
2919 mutex_unlock(&hotkey_mutex);
2920 if (res)
2921 return res;
2923 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2924 if (tp_features.hotkey_mask) {
2925 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2926 len += sprintf(p + len,
2927 "commands:\tenable, disable, reset, <mask>\n");
2928 } else {
2929 len += sprintf(p + len, "mask:\t\tnot supported\n");
2930 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2933 return len;
2936 static void hotkey_enabledisable_warn(void)
2938 tpacpi_log_usertask("procfs hotkey enable/disable");
2939 WARN(1, TPACPI_WARN
2940 "hotkey enable/disable functionality has been "
2941 "removed from the driver. Hotkeys are always enabled.\n");
2944 static int hotkey_write(char *buf)
2946 int res;
2947 u32 mask;
2948 char *cmd;
2950 if (!tp_features.hotkey)
2951 return -ENODEV;
2953 if (mutex_lock_killable(&hotkey_mutex))
2954 return -ERESTARTSYS;
2956 mask = hotkey_mask;
2958 res = 0;
2959 while ((cmd = next_cmd(&buf))) {
2960 if (strlencmp(cmd, "enable") == 0) {
2961 hotkey_enabledisable_warn();
2962 } else if (strlencmp(cmd, "disable") == 0) {
2963 hotkey_enabledisable_warn();
2964 res = -EPERM;
2965 } else if (strlencmp(cmd, "reset") == 0) {
2966 mask = hotkey_orig_mask;
2967 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2968 /* mask set */
2969 } else if (sscanf(cmd, "%x", &mask) == 1) {
2970 /* mask set */
2971 } else {
2972 res = -EINVAL;
2973 goto errexit;
2976 if (!res && mask != hotkey_mask)
2977 res = hotkey_mask_set(mask);
2979 errexit:
2980 mutex_unlock(&hotkey_mutex);
2981 return res;
2984 static const struct acpi_device_id ibm_htk_device_ids[] = {
2985 {TPACPI_ACPI_HKEY_HID, 0},
2986 {"", 0},
2989 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2990 .hid = ibm_htk_device_ids,
2991 .notify = hotkey_notify,
2992 .handle = &hkey_handle,
2993 .type = ACPI_DEVICE_NOTIFY,
2996 static struct ibm_struct hotkey_driver_data = {
2997 .name = "hotkey",
2998 .read = hotkey_read,
2999 .write = hotkey_write,
3000 .exit = hotkey_exit,
3001 .resume = hotkey_resume,
3002 .suspend = hotkey_suspend,
3003 .acpi = &ibm_hotkey_acpidriver,
3006 /*************************************************************************
3007 * Bluetooth subdriver
3010 enum {
3011 /* ACPI GBDC/SBDC bits */
3012 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3013 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3014 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3015 off / last state */
3018 enum {
3019 /* ACPI \BLTH commands */
3020 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3021 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3022 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3023 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3024 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3027 static struct rfkill *tpacpi_bluetooth_rfkill;
3029 static void bluetooth_suspend(pm_message_t state)
3031 /* Try to make sure radio will resume powered off */
3032 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3033 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
3036 static int bluetooth_get_radiosw(void)
3038 int status;
3040 if (!tp_features.bluetooth)
3041 return -ENODEV;
3043 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3044 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3045 return RFKILL_STATE_HARD_BLOCKED;
3047 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3048 if (dbg_bluetoothemul)
3049 return (tpacpi_bluetooth_emulstate) ?
3050 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3051 #endif
3053 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3054 return -EIO;
3056 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3057 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3060 static void bluetooth_update_rfk(void)
3062 int status;
3064 if (!tpacpi_bluetooth_rfkill)
3065 return;
3067 status = bluetooth_get_radiosw();
3068 if (status < 0)
3069 return;
3070 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3073 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3075 int status;
3077 if (!tp_features.bluetooth)
3078 return -ENODEV;
3080 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3081 * reason to risk weird behaviour. */
3082 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3083 && radio_on)
3084 return -EPERM;
3086 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3087 if (dbg_bluetoothemul) {
3088 tpacpi_bluetooth_emulstate = !!radio_on;
3089 if (update_rfk)
3090 bluetooth_update_rfk();
3091 return 0;
3093 #endif
3095 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3096 if (radio_on)
3097 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3098 else
3099 status = 0;
3100 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3101 return -EIO;
3103 if (update_rfk)
3104 bluetooth_update_rfk();
3106 return 0;
3109 /* sysfs bluetooth enable ---------------------------------------------- */
3110 static ssize_t bluetooth_enable_show(struct device *dev,
3111 struct device_attribute *attr,
3112 char *buf)
3114 int status;
3116 printk_deprecated_rfkill_attribute("bluetooth_enable");
3118 status = bluetooth_get_radiosw();
3119 if (status < 0)
3120 return status;
3122 return snprintf(buf, PAGE_SIZE, "%d\n",
3123 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3126 static ssize_t bluetooth_enable_store(struct device *dev,
3127 struct device_attribute *attr,
3128 const char *buf, size_t count)
3130 unsigned long t;
3131 int res;
3133 printk_deprecated_rfkill_attribute("bluetooth_enable");
3135 if (parse_strtoul(buf, 1, &t))
3136 return -EINVAL;
3138 res = bluetooth_set_radiosw(t, 1);
3140 return (res) ? res : count;
3143 static struct device_attribute dev_attr_bluetooth_enable =
3144 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3145 bluetooth_enable_show, bluetooth_enable_store);
3147 /* --------------------------------------------------------------------- */
3149 static struct attribute *bluetooth_attributes[] = {
3150 &dev_attr_bluetooth_enable.attr,
3151 NULL
3154 static const struct attribute_group bluetooth_attr_group = {
3155 .attrs = bluetooth_attributes,
3158 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3160 int bts = bluetooth_get_radiosw();
3162 if (bts < 0)
3163 return bts;
3165 *state = bts;
3166 return 0;
3169 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3171 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3174 static void bluetooth_shutdown(void)
3176 /* Order firmware to save current state to NVRAM */
3177 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3178 TP_ACPI_BLTH_SAVE_STATE))
3179 printk(TPACPI_NOTICE
3180 "failed to save bluetooth state to NVRAM\n");
3183 static void bluetooth_exit(void)
3185 bluetooth_shutdown();
3187 if (tpacpi_bluetooth_rfkill)
3188 rfkill_unregister(tpacpi_bluetooth_rfkill);
3190 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3191 &bluetooth_attr_group);
3194 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3196 int res;
3197 int status = 0;
3199 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3201 TPACPI_ACPIHANDLE_INIT(hkey);
3203 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3204 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3205 tp_features.bluetooth = hkey_handle &&
3206 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3208 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3209 str_supported(tp_features.bluetooth),
3210 status);
3212 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3213 if (dbg_bluetoothemul) {
3214 tp_features.bluetooth = 1;
3215 printk(TPACPI_INFO
3216 "bluetooth switch emulation enabled\n");
3217 } else
3218 #endif
3219 if (tp_features.bluetooth &&
3220 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3221 /* no bluetooth hardware present in system */
3222 tp_features.bluetooth = 0;
3223 dbg_printk(TPACPI_DBG_INIT,
3224 "bluetooth hardware not installed\n");
3227 if (!tp_features.bluetooth)
3228 return 1;
3230 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3231 &bluetooth_attr_group);
3232 if (res)
3233 return res;
3235 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3236 &tpacpi_bluetooth_rfkill,
3237 RFKILL_TYPE_BLUETOOTH,
3238 "tpacpi_bluetooth_sw",
3239 true,
3240 tpacpi_bluetooth_rfk_set,
3241 tpacpi_bluetooth_rfk_get);
3242 if (res) {
3243 bluetooth_exit();
3244 return res;
3247 return 0;
3250 /* procfs -------------------------------------------------------------- */
3251 static int bluetooth_read(char *p)
3253 int len = 0;
3254 int status = bluetooth_get_radiosw();
3256 if (!tp_features.bluetooth)
3257 len += sprintf(p + len, "status:\t\tnot supported\n");
3258 else {
3259 len += sprintf(p + len, "status:\t\t%s\n",
3260 (status == RFKILL_STATE_UNBLOCKED) ?
3261 "enabled" : "disabled");
3262 len += sprintf(p + len, "commands:\tenable, disable\n");
3265 return len;
3268 static int bluetooth_write(char *buf)
3270 char *cmd;
3272 if (!tp_features.bluetooth)
3273 return -ENODEV;
3275 while ((cmd = next_cmd(&buf))) {
3276 if (strlencmp(cmd, "enable") == 0) {
3277 bluetooth_set_radiosw(1, 1);
3278 } else if (strlencmp(cmd, "disable") == 0) {
3279 bluetooth_set_radiosw(0, 1);
3280 } else
3281 return -EINVAL;
3284 return 0;
3287 static struct ibm_struct bluetooth_driver_data = {
3288 .name = "bluetooth",
3289 .read = bluetooth_read,
3290 .write = bluetooth_write,
3291 .exit = bluetooth_exit,
3292 .suspend = bluetooth_suspend,
3293 .shutdown = bluetooth_shutdown,
3296 /*************************************************************************
3297 * Wan subdriver
3300 enum {
3301 /* ACPI GWAN/SWAN bits */
3302 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3303 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3304 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3305 off / last state */
3308 static struct rfkill *tpacpi_wan_rfkill;
3310 static void wan_suspend(pm_message_t state)
3312 /* Try to make sure radio will resume powered off */
3313 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3314 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3317 static int wan_get_radiosw(void)
3319 int status;
3321 if (!tp_features.wan)
3322 return -ENODEV;
3324 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3325 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3326 return RFKILL_STATE_HARD_BLOCKED;
3328 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3329 if (dbg_wwanemul)
3330 return (tpacpi_wwan_emulstate) ?
3331 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3332 #endif
3334 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3335 return -EIO;
3337 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3338 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3341 static void wan_update_rfk(void)
3343 int status;
3345 if (!tpacpi_wan_rfkill)
3346 return;
3348 status = wan_get_radiosw();
3349 if (status < 0)
3350 return;
3351 rfkill_force_state(tpacpi_wan_rfkill, status);
3354 static int wan_set_radiosw(int radio_on, int update_rfk)
3356 int status;
3358 if (!tp_features.wan)
3359 return -ENODEV;
3361 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3362 * reason to risk weird behaviour. */
3363 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3364 && radio_on)
3365 return -EPERM;
3367 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3368 if (dbg_wwanemul) {
3369 tpacpi_wwan_emulstate = !!radio_on;
3370 if (update_rfk)
3371 wan_update_rfk();
3372 return 0;
3374 #endif
3376 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3377 if (radio_on)
3378 status = TP_ACPI_WANCARD_RADIOSSW;
3379 else
3380 status = 0;
3381 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3382 return -EIO;
3384 if (update_rfk)
3385 wan_update_rfk();
3387 return 0;
3390 /* sysfs wan enable ---------------------------------------------------- */
3391 static ssize_t wan_enable_show(struct device *dev,
3392 struct device_attribute *attr,
3393 char *buf)
3395 int status;
3397 printk_deprecated_rfkill_attribute("wwan_enable");
3399 status = wan_get_radiosw();
3400 if (status < 0)
3401 return status;
3403 return snprintf(buf, PAGE_SIZE, "%d\n",
3404 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3407 static ssize_t wan_enable_store(struct device *dev,
3408 struct device_attribute *attr,
3409 const char *buf, size_t count)
3411 unsigned long t;
3412 int res;
3414 printk_deprecated_rfkill_attribute("wwan_enable");
3416 if (parse_strtoul(buf, 1, &t))
3417 return -EINVAL;
3419 res = wan_set_radiosw(t, 1);
3421 return (res) ? res : count;
3424 static struct device_attribute dev_attr_wan_enable =
3425 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3426 wan_enable_show, wan_enable_store);
3428 /* --------------------------------------------------------------------- */
3430 static struct attribute *wan_attributes[] = {
3431 &dev_attr_wan_enable.attr,
3432 NULL
3435 static const struct attribute_group wan_attr_group = {
3436 .attrs = wan_attributes,
3439 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3441 int wans = wan_get_radiosw();
3443 if (wans < 0)
3444 return wans;
3446 *state = wans;
3447 return 0;
3450 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3452 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3455 static void wan_shutdown(void)
3457 /* Order firmware to save current state to NVRAM */
3458 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3459 TP_ACPI_WGSV_SAVE_STATE))
3460 printk(TPACPI_NOTICE
3461 "failed to save WWAN state to NVRAM\n");
3464 static void wan_exit(void)
3466 wan_shutdown();
3468 if (tpacpi_wan_rfkill)
3469 rfkill_unregister(tpacpi_wan_rfkill);
3471 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3472 &wan_attr_group);
3475 static int __init wan_init(struct ibm_init_struct *iibm)
3477 int res;
3478 int status = 0;
3480 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3482 TPACPI_ACPIHANDLE_INIT(hkey);
3484 tp_features.wan = hkey_handle &&
3485 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3487 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3488 str_supported(tp_features.wan),
3489 status);
3491 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3492 if (dbg_wwanemul) {
3493 tp_features.wan = 1;
3494 printk(TPACPI_INFO
3495 "wwan switch emulation enabled\n");
3496 } else
3497 #endif
3498 if (tp_features.wan &&
3499 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3500 /* no wan hardware present in system */
3501 tp_features.wan = 0;
3502 dbg_printk(TPACPI_DBG_INIT,
3503 "wan hardware not installed\n");
3506 if (!tp_features.wan)
3507 return 1;
3509 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3510 &wan_attr_group);
3511 if (res)
3512 return res;
3514 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3515 &tpacpi_wan_rfkill,
3516 RFKILL_TYPE_WWAN,
3517 "tpacpi_wwan_sw",
3518 true,
3519 tpacpi_wan_rfk_set,
3520 tpacpi_wan_rfk_get);
3521 if (res) {
3522 wan_exit();
3523 return res;
3526 return 0;
3529 /* procfs -------------------------------------------------------------- */
3530 static int wan_read(char *p)
3532 int len = 0;
3533 int status = wan_get_radiosw();
3535 if (!tp_features.wan)
3536 len += sprintf(p + len, "status:\t\tnot supported\n");
3537 else {
3538 len += sprintf(p + len, "status:\t\t%s\n",
3539 (status == RFKILL_STATE_UNBLOCKED) ?
3540 "enabled" : "disabled");
3541 len += sprintf(p + len, "commands:\tenable, disable\n");
3544 return len;
3547 static int wan_write(char *buf)
3549 char *cmd;
3551 if (!tp_features.wan)
3552 return -ENODEV;
3554 while ((cmd = next_cmd(&buf))) {
3555 if (strlencmp(cmd, "enable") == 0) {
3556 wan_set_radiosw(1, 1);
3557 } else if (strlencmp(cmd, "disable") == 0) {
3558 wan_set_radiosw(0, 1);
3559 } else
3560 return -EINVAL;
3563 return 0;
3566 static struct ibm_struct wan_driver_data = {
3567 .name = "wan",
3568 .read = wan_read,
3569 .write = wan_write,
3570 .exit = wan_exit,
3571 .suspend = wan_suspend,
3572 .shutdown = wan_shutdown,
3575 /*************************************************************************
3576 * UWB subdriver
3579 enum {
3580 /* ACPI GUWB/SUWB bits */
3581 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3582 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3585 static struct rfkill *tpacpi_uwb_rfkill;
3587 static int uwb_get_radiosw(void)
3589 int status;
3591 if (!tp_features.uwb)
3592 return -ENODEV;
3594 /* WLSW overrides UWB in firmware/hardware, reflect that */
3595 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3596 return RFKILL_STATE_HARD_BLOCKED;
3598 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3599 if (dbg_uwbemul)
3600 return (tpacpi_uwb_emulstate) ?
3601 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3602 #endif
3604 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3605 return -EIO;
3607 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3608 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3611 static void uwb_update_rfk(void)
3613 int status;
3615 if (!tpacpi_uwb_rfkill)
3616 return;
3618 status = uwb_get_radiosw();
3619 if (status < 0)
3620 return;
3621 rfkill_force_state(tpacpi_uwb_rfkill, status);
3624 static int uwb_set_radiosw(int radio_on, int update_rfk)
3626 int status;
3628 if (!tp_features.uwb)
3629 return -ENODEV;
3631 /* WLSW overrides UWB in firmware/hardware, but there is no
3632 * reason to risk weird behaviour. */
3633 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3634 && radio_on)
3635 return -EPERM;
3637 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3638 if (dbg_uwbemul) {
3639 tpacpi_uwb_emulstate = !!radio_on;
3640 if (update_rfk)
3641 uwb_update_rfk();
3642 return 0;
3644 #endif
3646 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3647 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3648 return -EIO;
3650 if (update_rfk)
3651 uwb_update_rfk();
3653 return 0;
3656 /* --------------------------------------------------------------------- */
3658 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3660 int uwbs = uwb_get_radiosw();
3662 if (uwbs < 0)
3663 return uwbs;
3665 *state = uwbs;
3666 return 0;
3669 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3671 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3674 static void uwb_exit(void)
3676 if (tpacpi_uwb_rfkill)
3677 rfkill_unregister(tpacpi_uwb_rfkill);
3680 static int __init uwb_init(struct ibm_init_struct *iibm)
3682 int res;
3683 int status = 0;
3685 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3687 TPACPI_ACPIHANDLE_INIT(hkey);
3689 tp_features.uwb = hkey_handle &&
3690 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3692 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3693 str_supported(tp_features.uwb),
3694 status);
3696 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3697 if (dbg_uwbemul) {
3698 tp_features.uwb = 1;
3699 printk(TPACPI_INFO
3700 "uwb switch emulation enabled\n");
3701 } else
3702 #endif
3703 if (tp_features.uwb &&
3704 !(status & TP_ACPI_UWB_HWPRESENT)) {
3705 /* no uwb hardware present in system */
3706 tp_features.uwb = 0;
3707 dbg_printk(TPACPI_DBG_INIT,
3708 "uwb hardware not installed\n");
3711 if (!tp_features.uwb)
3712 return 1;
3714 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3715 &tpacpi_uwb_rfkill,
3716 RFKILL_TYPE_UWB,
3717 "tpacpi_uwb_sw",
3718 false,
3719 tpacpi_uwb_rfk_set,
3720 tpacpi_uwb_rfk_get);
3722 return res;
3725 static struct ibm_struct uwb_driver_data = {
3726 .name = "uwb",
3727 .exit = uwb_exit,
3728 .flags.experimental = 1,
3731 /*************************************************************************
3732 * Video subdriver
3735 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3737 enum video_access_mode {
3738 TPACPI_VIDEO_NONE = 0,
3739 TPACPI_VIDEO_570, /* 570 */
3740 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3741 TPACPI_VIDEO_NEW, /* all others */
3744 enum { /* video status flags, based on VIDEO_570 */
3745 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3746 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3747 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3750 enum { /* TPACPI_VIDEO_570 constants */
3751 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3752 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3753 * video_status_flags */
3754 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3755 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3758 static enum video_access_mode video_supported;
3759 static int video_orig_autosw;
3761 static int video_autosw_get(void);
3762 static int video_autosw_set(int enable);
3764 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3766 static int __init video_init(struct ibm_init_struct *iibm)
3768 int ivga;
3770 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3772 TPACPI_ACPIHANDLE_INIT(vid);
3773 TPACPI_ACPIHANDLE_INIT(vid2);
3775 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3776 /* G41, assume IVGA doesn't change */
3777 vid_handle = vid2_handle;
3779 if (!vid_handle)
3780 /* video switching not supported on R30, R31 */
3781 video_supported = TPACPI_VIDEO_NONE;
3782 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3783 /* 570 */
3784 video_supported = TPACPI_VIDEO_570;
3785 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3786 /* 600e/x, 770e, 770x */
3787 video_supported = TPACPI_VIDEO_770;
3788 else
3789 /* all others */
3790 video_supported = TPACPI_VIDEO_NEW;
3792 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3793 str_supported(video_supported != TPACPI_VIDEO_NONE),
3794 video_supported);
3796 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3799 static void video_exit(void)
3801 dbg_printk(TPACPI_DBG_EXIT,
3802 "restoring original video autoswitch mode\n");
3803 if (video_autosw_set(video_orig_autosw))
3804 printk(TPACPI_ERR "error while trying to restore original "
3805 "video autoswitch mode\n");
3808 static int video_outputsw_get(void)
3810 int status = 0;
3811 int i;
3813 switch (video_supported) {
3814 case TPACPI_VIDEO_570:
3815 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3816 TP_ACPI_VIDEO_570_PHSCMD))
3817 return -EIO;
3818 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3819 break;
3820 case TPACPI_VIDEO_770:
3821 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3822 return -EIO;
3823 if (i)
3824 status |= TP_ACPI_VIDEO_S_LCD;
3825 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3826 return -EIO;
3827 if (i)
3828 status |= TP_ACPI_VIDEO_S_CRT;
3829 break;
3830 case TPACPI_VIDEO_NEW:
3831 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3832 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3833 return -EIO;
3834 if (i)
3835 status |= TP_ACPI_VIDEO_S_CRT;
3837 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3838 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3839 return -EIO;
3840 if (i)
3841 status |= TP_ACPI_VIDEO_S_LCD;
3842 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3843 return -EIO;
3844 if (i)
3845 status |= TP_ACPI_VIDEO_S_DVI;
3846 break;
3847 default:
3848 return -ENOSYS;
3851 return status;
3854 static int video_outputsw_set(int status)
3856 int autosw;
3857 int res = 0;
3859 switch (video_supported) {
3860 case TPACPI_VIDEO_570:
3861 res = acpi_evalf(NULL, NULL,
3862 "\\_SB.PHS2", "vdd",
3863 TP_ACPI_VIDEO_570_PHS2CMD,
3864 status | TP_ACPI_VIDEO_570_PHS2SET);
3865 break;
3866 case TPACPI_VIDEO_770:
3867 autosw = video_autosw_get();
3868 if (autosw < 0)
3869 return autosw;
3871 res = video_autosw_set(1);
3872 if (res)
3873 return res;
3874 res = acpi_evalf(vid_handle, NULL,
3875 "ASWT", "vdd", status * 0x100, 0);
3876 if (!autosw && video_autosw_set(autosw)) {
3877 printk(TPACPI_ERR
3878 "video auto-switch left enabled due to error\n");
3879 return -EIO;
3881 break;
3882 case TPACPI_VIDEO_NEW:
3883 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3884 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3885 break;
3886 default:
3887 return -ENOSYS;
3890 return (res)? 0 : -EIO;
3893 static int video_autosw_get(void)
3895 int autosw = 0;
3897 switch (video_supported) {
3898 case TPACPI_VIDEO_570:
3899 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3900 return -EIO;
3901 break;
3902 case TPACPI_VIDEO_770:
3903 case TPACPI_VIDEO_NEW:
3904 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3905 return -EIO;
3906 break;
3907 default:
3908 return -ENOSYS;
3911 return autosw & 1;
3914 static int video_autosw_set(int enable)
3916 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3917 return -EIO;
3918 return 0;
3921 static int video_outputsw_cycle(void)
3923 int autosw = video_autosw_get();
3924 int res;
3926 if (autosw < 0)
3927 return autosw;
3929 switch (video_supported) {
3930 case TPACPI_VIDEO_570:
3931 res = video_autosw_set(1);
3932 if (res)
3933 return res;
3934 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3935 break;
3936 case TPACPI_VIDEO_770:
3937 case TPACPI_VIDEO_NEW:
3938 res = video_autosw_set(1);
3939 if (res)
3940 return res;
3941 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3942 break;
3943 default:
3944 return -ENOSYS;
3946 if (!autosw && video_autosw_set(autosw)) {
3947 printk(TPACPI_ERR
3948 "video auto-switch left enabled due to error\n");
3949 return -EIO;
3952 return (res)? 0 : -EIO;
3955 static int video_expand_toggle(void)
3957 switch (video_supported) {
3958 case TPACPI_VIDEO_570:
3959 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3960 0 : -EIO;
3961 case TPACPI_VIDEO_770:
3962 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3963 0 : -EIO;
3964 case TPACPI_VIDEO_NEW:
3965 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3966 0 : -EIO;
3967 default:
3968 return -ENOSYS;
3970 /* not reached */
3973 static int video_read(char *p)
3975 int status, autosw;
3976 int len = 0;
3978 if (video_supported == TPACPI_VIDEO_NONE) {
3979 len += sprintf(p + len, "status:\t\tnot supported\n");
3980 return len;
3983 status = video_outputsw_get();
3984 if (status < 0)
3985 return status;
3987 autosw = video_autosw_get();
3988 if (autosw < 0)
3989 return autosw;
3991 len += sprintf(p + len, "status:\t\tsupported\n");
3992 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3993 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3994 if (video_supported == TPACPI_VIDEO_NEW)
3995 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3996 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3997 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3998 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3999 if (video_supported == TPACPI_VIDEO_NEW)
4000 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
4001 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
4002 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
4004 return len;
4007 static int video_write(char *buf)
4009 char *cmd;
4010 int enable, disable, status;
4011 int res;
4013 if (video_supported == TPACPI_VIDEO_NONE)
4014 return -ENODEV;
4016 enable = 0;
4017 disable = 0;
4019 while ((cmd = next_cmd(&buf))) {
4020 if (strlencmp(cmd, "lcd_enable") == 0) {
4021 enable |= TP_ACPI_VIDEO_S_LCD;
4022 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4023 disable |= TP_ACPI_VIDEO_S_LCD;
4024 } else if (strlencmp(cmd, "crt_enable") == 0) {
4025 enable |= TP_ACPI_VIDEO_S_CRT;
4026 } else if (strlencmp(cmd, "crt_disable") == 0) {
4027 disable |= TP_ACPI_VIDEO_S_CRT;
4028 } else if (video_supported == TPACPI_VIDEO_NEW &&
4029 strlencmp(cmd, "dvi_enable") == 0) {
4030 enable |= TP_ACPI_VIDEO_S_DVI;
4031 } else if (video_supported == TPACPI_VIDEO_NEW &&
4032 strlencmp(cmd, "dvi_disable") == 0) {
4033 disable |= TP_ACPI_VIDEO_S_DVI;
4034 } else if (strlencmp(cmd, "auto_enable") == 0) {
4035 res = video_autosw_set(1);
4036 if (res)
4037 return res;
4038 } else if (strlencmp(cmd, "auto_disable") == 0) {
4039 res = video_autosw_set(0);
4040 if (res)
4041 return res;
4042 } else if (strlencmp(cmd, "video_switch") == 0) {
4043 res = video_outputsw_cycle();
4044 if (res)
4045 return res;
4046 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4047 res = video_expand_toggle();
4048 if (res)
4049 return res;
4050 } else
4051 return -EINVAL;
4054 if (enable || disable) {
4055 status = video_outputsw_get();
4056 if (status < 0)
4057 return status;
4058 res = video_outputsw_set((status & ~disable) | enable);
4059 if (res)
4060 return res;
4063 return 0;
4066 static struct ibm_struct video_driver_data = {
4067 .name = "video",
4068 .read = video_read,
4069 .write = video_write,
4070 .exit = video_exit,
4073 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4075 /*************************************************************************
4076 * Light (thinklight) subdriver
4079 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4080 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4082 static int light_get_status(void)
4084 int status = 0;
4086 if (tp_features.light_status) {
4087 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4088 return -EIO;
4089 return (!!status);
4092 return -ENXIO;
4095 static int light_set_status(int status)
4097 int rc;
4099 if (tp_features.light) {
4100 if (cmos_handle) {
4101 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4102 (status)?
4103 TP_CMOS_THINKLIGHT_ON :
4104 TP_CMOS_THINKLIGHT_OFF);
4105 } else {
4106 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4107 (status)? 1 : 0);
4109 return (rc)? 0 : -EIO;
4112 return -ENXIO;
4115 static void light_set_status_worker(struct work_struct *work)
4117 struct tpacpi_led_classdev *data =
4118 container_of(work, struct tpacpi_led_classdev, work);
4120 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4121 light_set_status((data->new_state != TPACPI_LED_OFF));
4124 static void light_sysfs_set(struct led_classdev *led_cdev,
4125 enum led_brightness brightness)
4127 struct tpacpi_led_classdev *data =
4128 container_of(led_cdev,
4129 struct tpacpi_led_classdev,
4130 led_classdev);
4131 data->new_state = (brightness != LED_OFF) ?
4132 TPACPI_LED_ON : TPACPI_LED_OFF;
4133 queue_work(tpacpi_wq, &data->work);
4136 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4138 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4141 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4142 .led_classdev = {
4143 .name = "tpacpi::thinklight",
4144 .brightness_set = &light_sysfs_set,
4145 .brightness_get = &light_sysfs_get,
4149 static int __init light_init(struct ibm_init_struct *iibm)
4151 int rc;
4153 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4155 TPACPI_ACPIHANDLE_INIT(ledb);
4156 TPACPI_ACPIHANDLE_INIT(lght);
4157 TPACPI_ACPIHANDLE_INIT(cmos);
4158 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4160 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4161 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4163 if (tp_features.light)
4164 /* light status not supported on
4165 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4166 tp_features.light_status =
4167 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4169 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4170 str_supported(tp_features.light),
4171 str_supported(tp_features.light_status));
4173 if (!tp_features.light)
4174 return 1;
4176 rc = led_classdev_register(&tpacpi_pdev->dev,
4177 &tpacpi_led_thinklight.led_classdev);
4179 if (rc < 0) {
4180 tp_features.light = 0;
4181 tp_features.light_status = 0;
4182 } else {
4183 rc = 0;
4186 return rc;
4189 static void light_exit(void)
4191 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4192 if (work_pending(&tpacpi_led_thinklight.work))
4193 flush_workqueue(tpacpi_wq);
4196 static int light_read(char *p)
4198 int len = 0;
4199 int status;
4201 if (!tp_features.light) {
4202 len += sprintf(p + len, "status:\t\tnot supported\n");
4203 } else if (!tp_features.light_status) {
4204 len += sprintf(p + len, "status:\t\tunknown\n");
4205 len += sprintf(p + len, "commands:\ton, off\n");
4206 } else {
4207 status = light_get_status();
4208 if (status < 0)
4209 return status;
4210 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4211 len += sprintf(p + len, "commands:\ton, off\n");
4214 return len;
4217 static int light_write(char *buf)
4219 char *cmd;
4220 int newstatus = 0;
4222 if (!tp_features.light)
4223 return -ENODEV;
4225 while ((cmd = next_cmd(&buf))) {
4226 if (strlencmp(cmd, "on") == 0) {
4227 newstatus = 1;
4228 } else if (strlencmp(cmd, "off") == 0) {
4229 newstatus = 0;
4230 } else
4231 return -EINVAL;
4234 return light_set_status(newstatus);
4237 static struct ibm_struct light_driver_data = {
4238 .name = "light",
4239 .read = light_read,
4240 .write = light_write,
4241 .exit = light_exit,
4244 /*************************************************************************
4245 * Dock subdriver
4248 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4250 static void dock_notify(struct ibm_struct *ibm, u32 event);
4251 static int dock_read(char *p);
4252 static int dock_write(char *buf);
4254 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4255 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4256 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4257 "\\_SB.PCI.ISA.SLCE", /* 570 */
4258 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4260 /* don't list other alternatives as we install a notify handler on the 570 */
4261 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4263 static const struct acpi_device_id ibm_pci_device_ids[] = {
4264 {PCI_ROOT_HID_STRING, 0},
4265 {"", 0},
4268 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4270 .notify = dock_notify,
4271 .handle = &dock_handle,
4272 .type = ACPI_SYSTEM_NOTIFY,
4275 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4276 * We just use it to get notifications of dock hotplug
4277 * in very old thinkpads */
4278 .hid = ibm_pci_device_ids,
4279 .notify = dock_notify,
4280 .handle = &pci_handle,
4281 .type = ACPI_SYSTEM_NOTIFY,
4285 static struct ibm_struct dock_driver_data[2] = {
4287 .name = "dock",
4288 .read = dock_read,
4289 .write = dock_write,
4290 .acpi = &ibm_dock_acpidriver[0],
4293 .name = "dock",
4294 .acpi = &ibm_dock_acpidriver[1],
4298 #define dock_docked() (_sta(dock_handle) & 1)
4300 static int __init dock_init(struct ibm_init_struct *iibm)
4302 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4304 TPACPI_ACPIHANDLE_INIT(dock);
4306 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4307 str_supported(dock_handle != NULL));
4309 return (dock_handle)? 0 : 1;
4312 static int __init dock_init2(struct ibm_init_struct *iibm)
4314 int dock2_needed;
4316 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4318 if (dock_driver_data[0].flags.acpi_driver_registered &&
4319 dock_driver_data[0].flags.acpi_notify_installed) {
4320 TPACPI_ACPIHANDLE_INIT(pci);
4321 dock2_needed = (pci_handle != NULL);
4322 vdbg_printk(TPACPI_DBG_INIT,
4323 "dock PCI handler for the TP 570 is %s\n",
4324 str_supported(dock2_needed));
4325 } else {
4326 vdbg_printk(TPACPI_DBG_INIT,
4327 "dock subdriver part 2 not required\n");
4328 dock2_needed = 0;
4331 return (dock2_needed)? 0 : 1;
4334 static void dock_notify(struct ibm_struct *ibm, u32 event)
4336 int docked = dock_docked();
4337 int pci = ibm->acpi->hid && ibm->acpi->device &&
4338 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4339 int data;
4341 if (event == 1 && !pci) /* 570 */
4342 data = 1; /* button */
4343 else if (event == 1 && pci) /* 570 */
4344 data = 3; /* dock */
4345 else if (event == 3 && docked)
4346 data = 1; /* button */
4347 else if (event == 3 && !docked)
4348 data = 2; /* undock */
4349 else if (event == 0 && docked)
4350 data = 3; /* dock */
4351 else {
4352 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4353 event, _sta(dock_handle));
4354 data = 0; /* unknown */
4356 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4357 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4358 dev_name(&ibm->acpi->device->dev),
4359 event, data);
4362 static int dock_read(char *p)
4364 int len = 0;
4365 int docked = dock_docked();
4367 if (!dock_handle)
4368 len += sprintf(p + len, "status:\t\tnot supported\n");
4369 else if (!docked)
4370 len += sprintf(p + len, "status:\t\tundocked\n");
4371 else {
4372 len += sprintf(p + len, "status:\t\tdocked\n");
4373 len += sprintf(p + len, "commands:\tdock, undock\n");
4376 return len;
4379 static int dock_write(char *buf)
4381 char *cmd;
4383 if (!dock_docked())
4384 return -ENODEV;
4386 while ((cmd = next_cmd(&buf))) {
4387 if (strlencmp(cmd, "undock") == 0) {
4388 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4389 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4390 return -EIO;
4391 } else if (strlencmp(cmd, "dock") == 0) {
4392 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4393 return -EIO;
4394 } else
4395 return -EINVAL;
4398 return 0;
4401 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4403 /*************************************************************************
4404 * Bay subdriver
4407 #ifdef CONFIG_THINKPAD_ACPI_BAY
4409 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4410 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4411 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4412 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4413 ); /* A21e, R30, R31 */
4414 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4415 "_EJ0", /* all others */
4416 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4417 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4418 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4419 ); /* all others */
4420 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4421 "_EJ0", /* 770x */
4422 ); /* all others */
4424 static int __init bay_init(struct ibm_init_struct *iibm)
4426 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4428 TPACPI_ACPIHANDLE_INIT(bay);
4429 if (bay_handle)
4430 TPACPI_ACPIHANDLE_INIT(bay_ej);
4431 TPACPI_ACPIHANDLE_INIT(bay2);
4432 if (bay2_handle)
4433 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4435 tp_features.bay_status = bay_handle &&
4436 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4437 tp_features.bay_status2 = bay2_handle &&
4438 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4440 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4441 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4442 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4443 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4445 vdbg_printk(TPACPI_DBG_INIT,
4446 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4447 str_supported(tp_features.bay_status),
4448 str_supported(tp_features.bay_eject),
4449 str_supported(tp_features.bay_status2),
4450 str_supported(tp_features.bay_eject2));
4452 return (tp_features.bay_status || tp_features.bay_eject ||
4453 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4456 static void bay_notify(struct ibm_struct *ibm, u32 event)
4458 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4459 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4460 dev_name(&ibm->acpi->device->dev),
4461 event, 0);
4464 #define bay_occupied(b) (_sta(b##_handle) & 1)
4466 static int bay_read(char *p)
4468 int len = 0;
4469 int occupied = bay_occupied(bay);
4470 int occupied2 = bay_occupied(bay2);
4471 int eject, eject2;
4473 len += sprintf(p + len, "status:\t\t%s\n",
4474 tp_features.bay_status ?
4475 (occupied ? "occupied" : "unoccupied") :
4476 "not supported");
4477 if (tp_features.bay_status2)
4478 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4479 "occupied" : "unoccupied");
4481 eject = tp_features.bay_eject && occupied;
4482 eject2 = tp_features.bay_eject2 && occupied2;
4484 if (eject && eject2)
4485 len += sprintf(p + len, "commands:\teject, eject2\n");
4486 else if (eject)
4487 len += sprintf(p + len, "commands:\teject\n");
4488 else if (eject2)
4489 len += sprintf(p + len, "commands:\teject2\n");
4491 return len;
4494 static int bay_write(char *buf)
4496 char *cmd;
4498 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4499 return -ENODEV;
4501 while ((cmd = next_cmd(&buf))) {
4502 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4503 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4504 return -EIO;
4505 } else if (tp_features.bay_eject2 &&
4506 strlencmp(cmd, "eject2") == 0) {
4507 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4508 return -EIO;
4509 } else
4510 return -EINVAL;
4513 return 0;
4516 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4517 .notify = bay_notify,
4518 .handle = &bay_handle,
4519 .type = ACPI_SYSTEM_NOTIFY,
4522 static struct ibm_struct bay_driver_data = {
4523 .name = "bay",
4524 .read = bay_read,
4525 .write = bay_write,
4526 .acpi = &ibm_bay_acpidriver,
4529 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4531 /*************************************************************************
4532 * CMOS subdriver
4535 /* sysfs cmos_command -------------------------------------------------- */
4536 static ssize_t cmos_command_store(struct device *dev,
4537 struct device_attribute *attr,
4538 const char *buf, size_t count)
4540 unsigned long cmos_cmd;
4541 int res;
4543 if (parse_strtoul(buf, 21, &cmos_cmd))
4544 return -EINVAL;
4546 res = issue_thinkpad_cmos_command(cmos_cmd);
4547 return (res)? res : count;
4550 static struct device_attribute dev_attr_cmos_command =
4551 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4553 /* --------------------------------------------------------------------- */
4555 static int __init cmos_init(struct ibm_init_struct *iibm)
4557 int res;
4559 vdbg_printk(TPACPI_DBG_INIT,
4560 "initializing cmos commands subdriver\n");
4562 TPACPI_ACPIHANDLE_INIT(cmos);
4564 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4565 str_supported(cmos_handle != NULL));
4567 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4568 if (res)
4569 return res;
4571 return (cmos_handle)? 0 : 1;
4574 static void cmos_exit(void)
4576 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4579 static int cmos_read(char *p)
4581 int len = 0;
4583 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4584 R30, R31, T20-22, X20-21 */
4585 if (!cmos_handle)
4586 len += sprintf(p + len, "status:\t\tnot supported\n");
4587 else {
4588 len += sprintf(p + len, "status:\t\tsupported\n");
4589 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4592 return len;
4595 static int cmos_write(char *buf)
4597 char *cmd;
4598 int cmos_cmd, res;
4600 while ((cmd = next_cmd(&buf))) {
4601 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4602 cmos_cmd >= 0 && cmos_cmd <= 21) {
4603 /* cmos_cmd set */
4604 } else
4605 return -EINVAL;
4607 res = issue_thinkpad_cmos_command(cmos_cmd);
4608 if (res)
4609 return res;
4612 return 0;
4615 static struct ibm_struct cmos_driver_data = {
4616 .name = "cmos",
4617 .read = cmos_read,
4618 .write = cmos_write,
4619 .exit = cmos_exit,
4622 /*************************************************************************
4623 * LED subdriver
4626 enum led_access_mode {
4627 TPACPI_LED_NONE = 0,
4628 TPACPI_LED_570, /* 570 */
4629 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4630 TPACPI_LED_NEW, /* all others */
4633 enum { /* For TPACPI_LED_OLD */
4634 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4635 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4636 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4639 static enum led_access_mode led_supported;
4641 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4642 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4643 /* T20-22, X20-21 */
4644 "LED", /* all others */
4645 ); /* R30, R31 */
4647 #define TPACPI_LED_NUMLEDS 8
4648 static struct tpacpi_led_classdev *tpacpi_leds;
4649 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4650 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4651 /* there's a limit of 19 chars + NULL before 2.6.26 */
4652 "tpacpi::power",
4653 "tpacpi:orange:batt",
4654 "tpacpi:green:batt",
4655 "tpacpi::dock_active",
4656 "tpacpi::bay_active",
4657 "tpacpi::dock_batt",
4658 "tpacpi::unknown_led",
4659 "tpacpi::standby",
4661 #define TPACPI_SAFE_LEDS 0x0081U
4663 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4665 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4666 return false;
4667 #else
4668 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4669 #endif
4672 static int led_get_status(const unsigned int led)
4674 int status;
4675 enum led_status_t led_s;
4677 switch (led_supported) {
4678 case TPACPI_LED_570:
4679 if (!acpi_evalf(ec_handle,
4680 &status, "GLED", "dd", 1 << led))
4681 return -EIO;
4682 led_s = (status == 0)?
4683 TPACPI_LED_OFF :
4684 ((status == 1)?
4685 TPACPI_LED_ON :
4686 TPACPI_LED_BLINK);
4687 tpacpi_led_state_cache[led] = led_s;
4688 return led_s;
4689 default:
4690 return -ENXIO;
4693 /* not reached */
4696 static int led_set_status(const unsigned int led,
4697 const enum led_status_t ledstatus)
4699 /* off, on, blink. Index is led_status_t */
4700 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4701 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4703 int rc = 0;
4705 switch (led_supported) {
4706 case TPACPI_LED_570:
4707 /* 570 */
4708 if (unlikely(led > 7))
4709 return -EINVAL;
4710 if (unlikely(tpacpi_is_led_restricted(led)))
4711 return -EPERM;
4712 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4713 (1 << led), led_sled_arg1[ledstatus]))
4714 rc = -EIO;
4715 break;
4716 case TPACPI_LED_OLD:
4717 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4718 if (unlikely(led > 7))
4719 return -EINVAL;
4720 if (unlikely(tpacpi_is_led_restricted(led)))
4721 return -EPERM;
4722 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4723 if (rc >= 0)
4724 rc = ec_write(TPACPI_LED_EC_HLBL,
4725 (ledstatus == TPACPI_LED_BLINK) << led);
4726 if (rc >= 0)
4727 rc = ec_write(TPACPI_LED_EC_HLCL,
4728 (ledstatus != TPACPI_LED_OFF) << led);
4729 break;
4730 case TPACPI_LED_NEW:
4731 /* all others */
4732 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4733 return -EINVAL;
4734 if (unlikely(tpacpi_is_led_restricted(led)))
4735 return -EPERM;
4736 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4737 led, led_led_arg1[ledstatus]))
4738 rc = -EIO;
4739 break;
4740 default:
4741 rc = -ENXIO;
4744 if (!rc)
4745 tpacpi_led_state_cache[led] = ledstatus;
4747 return rc;
4750 static void led_set_status_worker(struct work_struct *work)
4752 struct tpacpi_led_classdev *data =
4753 container_of(work, struct tpacpi_led_classdev, work);
4755 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4756 led_set_status(data->led, data->new_state);
4759 static void led_sysfs_set(struct led_classdev *led_cdev,
4760 enum led_brightness brightness)
4762 struct tpacpi_led_classdev *data = container_of(led_cdev,
4763 struct tpacpi_led_classdev, led_classdev);
4765 if (brightness == LED_OFF)
4766 data->new_state = TPACPI_LED_OFF;
4767 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4768 data->new_state = TPACPI_LED_ON;
4769 else
4770 data->new_state = TPACPI_LED_BLINK;
4772 queue_work(tpacpi_wq, &data->work);
4775 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4776 unsigned long *delay_on, unsigned long *delay_off)
4778 struct tpacpi_led_classdev *data = container_of(led_cdev,
4779 struct tpacpi_led_classdev, led_classdev);
4781 /* Can we choose the flash rate? */
4782 if (*delay_on == 0 && *delay_off == 0) {
4783 /* yes. set them to the hardware blink rate (1 Hz) */
4784 *delay_on = 500; /* ms */
4785 *delay_off = 500; /* ms */
4786 } else if ((*delay_on != 500) || (*delay_off != 500))
4787 return -EINVAL;
4789 data->new_state = TPACPI_LED_BLINK;
4790 queue_work(tpacpi_wq, &data->work);
4792 return 0;
4795 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4797 int rc;
4799 struct tpacpi_led_classdev *data = container_of(led_cdev,
4800 struct tpacpi_led_classdev, led_classdev);
4802 rc = led_get_status(data->led);
4804 if (rc == TPACPI_LED_OFF || rc < 0)
4805 rc = LED_OFF; /* no error handling in led class :( */
4806 else
4807 rc = LED_FULL;
4809 return rc;
4812 static void led_exit(void)
4814 unsigned int i;
4816 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4817 if (tpacpi_leds[i].led_classdev.name)
4818 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4821 kfree(tpacpi_leds);
4824 static int __init tpacpi_init_led(unsigned int led)
4826 int rc;
4828 tpacpi_leds[led].led = led;
4830 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4831 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4832 if (led_supported == TPACPI_LED_570)
4833 tpacpi_leds[led].led_classdev.brightness_get =
4834 &led_sysfs_get;
4836 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4838 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4840 rc = led_classdev_register(&tpacpi_pdev->dev,
4841 &tpacpi_leds[led].led_classdev);
4842 if (rc < 0)
4843 tpacpi_leds[led].led_classdev.name = NULL;
4845 return rc;
4848 static int __init led_init(struct ibm_init_struct *iibm)
4850 unsigned int i;
4851 int rc;
4853 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4855 TPACPI_ACPIHANDLE_INIT(led);
4857 if (!led_handle)
4858 /* led not supported on R30, R31 */
4859 led_supported = TPACPI_LED_NONE;
4860 else if (strlencmp(led_path, "SLED") == 0)
4861 /* 570 */
4862 led_supported = TPACPI_LED_570;
4863 else if (strlencmp(led_path, "SYSL") == 0)
4864 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4865 led_supported = TPACPI_LED_OLD;
4866 else
4867 /* all others */
4868 led_supported = TPACPI_LED_NEW;
4870 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4871 str_supported(led_supported), led_supported);
4873 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4874 GFP_KERNEL);
4875 if (!tpacpi_leds) {
4876 printk(TPACPI_ERR "Out of memory for LED data\n");
4877 return -ENOMEM;
4880 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4881 if (!tpacpi_is_led_restricted(i)) {
4882 rc = tpacpi_init_led(i);
4883 if (rc < 0) {
4884 led_exit();
4885 return rc;
4890 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4891 if (led_supported != TPACPI_LED_NONE)
4892 printk(TPACPI_NOTICE
4893 "warning: userspace override of important "
4894 "firmware LEDs is enabled\n");
4895 #endif
4896 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4899 #define str_led_status(s) \
4900 ((s) == TPACPI_LED_OFF ? "off" : \
4901 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4903 static int led_read(char *p)
4905 int len = 0;
4907 if (!led_supported) {
4908 len += sprintf(p + len, "status:\t\tnot supported\n");
4909 return len;
4911 len += sprintf(p + len, "status:\t\tsupported\n");
4913 if (led_supported == TPACPI_LED_570) {
4914 /* 570 */
4915 int i, status;
4916 for (i = 0; i < 8; i++) {
4917 status = led_get_status(i);
4918 if (status < 0)
4919 return -EIO;
4920 len += sprintf(p + len, "%d:\t\t%s\n",
4921 i, str_led_status(status));
4925 len += sprintf(p + len, "commands:\t"
4926 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4928 return len;
4931 static int led_write(char *buf)
4933 char *cmd;
4934 int led, rc;
4935 enum led_status_t s;
4937 if (!led_supported)
4938 return -ENODEV;
4940 while ((cmd = next_cmd(&buf))) {
4941 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4942 return -EINVAL;
4944 if (strstr(cmd, "off")) {
4945 s = TPACPI_LED_OFF;
4946 } else if (strstr(cmd, "on")) {
4947 s = TPACPI_LED_ON;
4948 } else if (strstr(cmd, "blink")) {
4949 s = TPACPI_LED_BLINK;
4950 } else {
4951 return -EINVAL;
4954 rc = led_set_status(led, s);
4955 if (rc < 0)
4956 return rc;
4959 return 0;
4962 static struct ibm_struct led_driver_data = {
4963 .name = "led",
4964 .read = led_read,
4965 .write = led_write,
4966 .exit = led_exit,
4969 /*************************************************************************
4970 * Beep subdriver
4973 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4975 static int __init beep_init(struct ibm_init_struct *iibm)
4977 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4979 TPACPI_ACPIHANDLE_INIT(beep);
4981 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4982 str_supported(beep_handle != NULL));
4984 return (beep_handle)? 0 : 1;
4987 static int beep_read(char *p)
4989 int len = 0;
4991 if (!beep_handle)
4992 len += sprintf(p + len, "status:\t\tnot supported\n");
4993 else {
4994 len += sprintf(p + len, "status:\t\tsupported\n");
4995 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4998 return len;
5001 static int beep_write(char *buf)
5003 char *cmd;
5004 int beep_cmd;
5006 if (!beep_handle)
5007 return -ENODEV;
5009 while ((cmd = next_cmd(&buf))) {
5010 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
5011 beep_cmd >= 0 && beep_cmd <= 17) {
5012 /* beep_cmd set */
5013 } else
5014 return -EINVAL;
5015 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
5016 return -EIO;
5019 return 0;
5022 static struct ibm_struct beep_driver_data = {
5023 .name = "beep",
5024 .read = beep_read,
5025 .write = beep_write,
5028 /*************************************************************************
5029 * Thermal subdriver
5032 enum thermal_access_mode {
5033 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5034 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5035 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5036 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5037 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5040 enum { /* TPACPI_THERMAL_TPEC_* */
5041 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5042 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5043 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5046 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5047 struct ibm_thermal_sensors_struct {
5048 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5051 static enum thermal_access_mode thermal_read_mode;
5053 /* idx is zero-based */
5054 static int thermal_get_sensor(int idx, s32 *value)
5056 int t;
5057 s8 tmp;
5058 char tmpi[5];
5060 t = TP_EC_THERMAL_TMP0;
5062 switch (thermal_read_mode) {
5063 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5064 case TPACPI_THERMAL_TPEC_16:
5065 if (idx >= 8 && idx <= 15) {
5066 t = TP_EC_THERMAL_TMP8;
5067 idx -= 8;
5069 /* fallthrough */
5070 #endif
5071 case TPACPI_THERMAL_TPEC_8:
5072 if (idx <= 7) {
5073 if (!acpi_ec_read(t + idx, &tmp))
5074 return -EIO;
5075 *value = tmp * 1000;
5076 return 0;
5078 break;
5080 case TPACPI_THERMAL_ACPI_UPDT:
5081 if (idx <= 7) {
5082 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5083 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5084 return -EIO;
5085 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5086 return -EIO;
5087 *value = (t - 2732) * 100;
5088 return 0;
5090 break;
5092 case TPACPI_THERMAL_ACPI_TMP07:
5093 if (idx <= 7) {
5094 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5095 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5096 return -EIO;
5097 if (t > 127 || t < -127)
5098 t = TP_EC_THERMAL_TMP_NA;
5099 *value = t * 1000;
5100 return 0;
5102 break;
5104 case TPACPI_THERMAL_NONE:
5105 default:
5106 return -ENOSYS;
5109 return -EINVAL;
5112 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5114 int res, i;
5115 int n;
5117 n = 8;
5118 i = 0;
5120 if (!s)
5121 return -EINVAL;
5123 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5124 n = 16;
5126 for (i = 0 ; i < n; i++) {
5127 res = thermal_get_sensor(i, &s->temp[i]);
5128 if (res)
5129 return res;
5132 return n;
5135 /* sysfs temp##_input -------------------------------------------------- */
5137 static ssize_t thermal_temp_input_show(struct device *dev,
5138 struct device_attribute *attr,
5139 char *buf)
5141 struct sensor_device_attribute *sensor_attr =
5142 to_sensor_dev_attr(attr);
5143 int idx = sensor_attr->index;
5144 s32 value;
5145 int res;
5147 res = thermal_get_sensor(idx, &value);
5148 if (res)
5149 return res;
5150 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5151 return -ENXIO;
5153 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5156 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5157 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5158 thermal_temp_input_show, NULL, _idxB)
5160 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5161 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5162 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5163 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5164 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5165 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5166 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5167 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5168 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5169 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5170 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5171 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5172 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5173 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5174 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5175 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5176 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5179 #define THERMAL_ATTRS(X) \
5180 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5182 static struct attribute *thermal_temp_input_attr[] = {
5183 THERMAL_ATTRS(8),
5184 THERMAL_ATTRS(9),
5185 THERMAL_ATTRS(10),
5186 THERMAL_ATTRS(11),
5187 THERMAL_ATTRS(12),
5188 THERMAL_ATTRS(13),
5189 THERMAL_ATTRS(14),
5190 THERMAL_ATTRS(15),
5191 THERMAL_ATTRS(0),
5192 THERMAL_ATTRS(1),
5193 THERMAL_ATTRS(2),
5194 THERMAL_ATTRS(3),
5195 THERMAL_ATTRS(4),
5196 THERMAL_ATTRS(5),
5197 THERMAL_ATTRS(6),
5198 THERMAL_ATTRS(7),
5199 NULL
5202 static const struct attribute_group thermal_temp_input16_group = {
5203 .attrs = thermal_temp_input_attr
5206 static const struct attribute_group thermal_temp_input8_group = {
5207 .attrs = &thermal_temp_input_attr[8]
5210 #undef THERMAL_SENSOR_ATTR_TEMP
5211 #undef THERMAL_ATTRS
5213 /* --------------------------------------------------------------------- */
5215 static int __init thermal_init(struct ibm_init_struct *iibm)
5217 u8 t, ta1, ta2;
5218 int i;
5219 int acpi_tmp7;
5220 int res;
5222 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5224 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5226 if (thinkpad_id.ec_model) {
5228 * Direct EC access mode: sensors at registers
5229 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5230 * non-implemented, thermal sensors return 0x80 when
5231 * not available
5234 ta1 = ta2 = 0;
5235 for (i = 0; i < 8; i++) {
5236 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5237 ta1 |= t;
5238 } else {
5239 ta1 = 0;
5240 break;
5242 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5243 ta2 |= t;
5244 } else {
5245 ta1 = 0;
5246 break;
5249 if (ta1 == 0) {
5250 /* This is sheer paranoia, but we handle it anyway */
5251 if (acpi_tmp7) {
5252 printk(TPACPI_ERR
5253 "ThinkPad ACPI EC access misbehaving, "
5254 "falling back to ACPI TMPx access "
5255 "mode\n");
5256 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5257 } else {
5258 printk(TPACPI_ERR
5259 "ThinkPad ACPI EC access misbehaving, "
5260 "disabling thermal sensors access\n");
5261 thermal_read_mode = TPACPI_THERMAL_NONE;
5263 } else {
5264 thermal_read_mode =
5265 (ta2 != 0) ?
5266 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5268 } else if (acpi_tmp7) {
5269 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5270 /* 600e/x, 770e, 770x */
5271 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5272 } else {
5273 /* Standard ACPI TMPx access, max 8 sensors */
5274 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5276 } else {
5277 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5278 thermal_read_mode = TPACPI_THERMAL_NONE;
5281 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5282 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5283 thermal_read_mode);
5285 switch (thermal_read_mode) {
5286 case TPACPI_THERMAL_TPEC_16:
5287 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5288 &thermal_temp_input16_group);
5289 if (res)
5290 return res;
5291 break;
5292 case TPACPI_THERMAL_TPEC_8:
5293 case TPACPI_THERMAL_ACPI_TMP07:
5294 case TPACPI_THERMAL_ACPI_UPDT:
5295 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5296 &thermal_temp_input8_group);
5297 if (res)
5298 return res;
5299 break;
5300 case TPACPI_THERMAL_NONE:
5301 default:
5302 return 1;
5305 return 0;
5308 static void thermal_exit(void)
5310 switch (thermal_read_mode) {
5311 case TPACPI_THERMAL_TPEC_16:
5312 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5313 &thermal_temp_input16_group);
5314 break;
5315 case TPACPI_THERMAL_TPEC_8:
5316 case TPACPI_THERMAL_ACPI_TMP07:
5317 case TPACPI_THERMAL_ACPI_UPDT:
5318 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5319 &thermal_temp_input16_group);
5320 break;
5321 case TPACPI_THERMAL_NONE:
5322 default:
5323 break;
5327 static int thermal_read(char *p)
5329 int len = 0;
5330 int n, i;
5331 struct ibm_thermal_sensors_struct t;
5333 n = thermal_get_sensors(&t);
5334 if (unlikely(n < 0))
5335 return n;
5337 len += sprintf(p + len, "temperatures:\t");
5339 if (n > 0) {
5340 for (i = 0; i < (n - 1); i++)
5341 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5342 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5343 } else
5344 len += sprintf(p + len, "not supported\n");
5346 return len;
5349 static struct ibm_struct thermal_driver_data = {
5350 .name = "thermal",
5351 .read = thermal_read,
5352 .exit = thermal_exit,
5355 /*************************************************************************
5356 * EC Dump subdriver
5359 static u8 ecdump_regs[256];
5361 static int ecdump_read(char *p)
5363 int len = 0;
5364 int i, j;
5365 u8 v;
5367 len += sprintf(p + len, "EC "
5368 " +00 +01 +02 +03 +04 +05 +06 +07"
5369 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5370 for (i = 0; i < 256; i += 16) {
5371 len += sprintf(p + len, "EC 0x%02x:", i);
5372 for (j = 0; j < 16; j++) {
5373 if (!acpi_ec_read(i + j, &v))
5374 break;
5375 if (v != ecdump_regs[i + j])
5376 len += sprintf(p + len, " *%02x", v);
5377 else
5378 len += sprintf(p + len, " %02x", v);
5379 ecdump_regs[i + j] = v;
5381 len += sprintf(p + len, "\n");
5382 if (j != 16)
5383 break;
5386 /* These are way too dangerous to advertise openly... */
5387 #if 0
5388 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5389 " (<offset> is 00-ff, <value> is 00-ff)\n");
5390 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5391 " (<offset> is 00-ff, <value> is 0-255)\n");
5392 #endif
5393 return len;
5396 static int ecdump_write(char *buf)
5398 char *cmd;
5399 int i, v;
5401 while ((cmd = next_cmd(&buf))) {
5402 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5403 /* i and v set */
5404 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5405 /* i and v set */
5406 } else
5407 return -EINVAL;
5408 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5409 if (!acpi_ec_write(i, v))
5410 return -EIO;
5411 } else
5412 return -EINVAL;
5415 return 0;
5418 static struct ibm_struct ecdump_driver_data = {
5419 .name = "ecdump",
5420 .read = ecdump_read,
5421 .write = ecdump_write,
5422 .flags.experimental = 1,
5425 /*************************************************************************
5426 * Backlight/brightness subdriver
5429 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5431 enum {
5432 TP_EC_BACKLIGHT = 0x31,
5434 /* TP_EC_BACKLIGHT bitmasks */
5435 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5436 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5437 TP_EC_BACKLIGHT_MAPSW = 0x20,
5440 static struct backlight_device *ibm_backlight_device;
5441 static int brightness_mode;
5442 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5444 static struct mutex brightness_mutex;
5447 * ThinkPads can read brightness from two places: EC 0x31, or
5448 * CMOS NVRAM byte 0x5E, bits 0-3.
5450 * EC 0x31 has the following layout
5451 * Bit 7: unknown function
5452 * Bit 6: unknown function
5453 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5454 * Bit 4: must be set to zero to avoid problems
5455 * Bit 3-0: backlight brightness level
5457 * brightness_get_raw returns status data in the EC 0x31 layout
5459 static int brightness_get_raw(int *status)
5461 u8 lec = 0, lcmos = 0, level = 0;
5463 if (brightness_mode & 1) {
5464 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5465 return -EIO;
5466 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5468 if (brightness_mode & 2) {
5469 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5470 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5471 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5472 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5473 level = lcmos;
5476 if (brightness_mode == 3) {
5477 *status = lec; /* Prefer EC, CMOS is just a backing store */
5478 lec &= TP_EC_BACKLIGHT_LVLMSK;
5479 if (lec == lcmos)
5480 tp_warned.bright_cmos_ec_unsync = 0;
5481 else {
5482 if (!tp_warned.bright_cmos_ec_unsync) {
5483 printk(TPACPI_ERR
5484 "CMOS NVRAM (%u) and EC (%u) do not "
5485 "agree on display brightness level\n",
5486 (unsigned int) lcmos,
5487 (unsigned int) lec);
5488 tp_warned.bright_cmos_ec_unsync = 1;
5490 return -EIO;
5492 } else {
5493 *status = level;
5496 return 0;
5499 /* May return EINTR which can always be mapped to ERESTARTSYS */
5500 static int brightness_set(int value)
5502 int cmos_cmd, inc, i, res;
5503 int current_value;
5504 int command_bits;
5506 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5507 value < 0)
5508 return -EINVAL;
5510 res = mutex_lock_killable(&brightness_mutex);
5511 if (res < 0)
5512 return res;
5514 res = brightness_get_raw(&current_value);
5515 if (res < 0)
5516 goto errout;
5518 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5519 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5521 cmos_cmd = value > current_value ?
5522 TP_CMOS_BRIGHTNESS_UP :
5523 TP_CMOS_BRIGHTNESS_DOWN;
5524 inc = (value > current_value)? 1 : -1;
5526 res = 0;
5527 for (i = current_value; i != value; i += inc) {
5528 if ((brightness_mode & 2) &&
5529 issue_thinkpad_cmos_command(cmos_cmd)) {
5530 res = -EIO;
5531 goto errout;
5533 if ((brightness_mode & 1) &&
5534 !acpi_ec_write(TP_EC_BACKLIGHT,
5535 (i + inc) | command_bits)) {
5536 res = -EIO;
5537 goto errout;;
5541 errout:
5542 mutex_unlock(&brightness_mutex);
5543 return res;
5546 /* sysfs backlight class ----------------------------------------------- */
5548 static int brightness_update_status(struct backlight_device *bd)
5550 /* it is the backlight class's job (caller) to handle
5551 * EINTR and other errors properly */
5552 return brightness_set(
5553 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5554 bd->props.power == FB_BLANK_UNBLANK) ?
5555 bd->props.brightness : 0);
5558 static int brightness_get(struct backlight_device *bd)
5560 int status, res;
5562 res = brightness_get_raw(&status);
5563 if (res < 0)
5564 return 0; /* FIXME: teach backlight about error handling */
5566 return status & TP_EC_BACKLIGHT_LVLMSK;
5569 static struct backlight_ops ibm_backlight_data = {
5570 .get_brightness = brightness_get,
5571 .update_status = brightness_update_status,
5574 /* --------------------------------------------------------------------- */
5576 static int __init brightness_init(struct ibm_init_struct *iibm)
5578 int b;
5580 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5582 mutex_init(&brightness_mutex);
5585 * We always attempt to detect acpi support, so as to switch
5586 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5587 * going to publish a backlight interface
5589 b = tpacpi_check_std_acpi_brightness_support();
5590 if (b > 0) {
5592 if (acpi_video_backlight_support()) {
5593 if (brightness_enable > 1) {
5594 printk(TPACPI_NOTICE
5595 "Standard ACPI backlight interface "
5596 "available, not loading native one.\n");
5597 return 1;
5598 } else if (brightness_enable == 1) {
5599 printk(TPACPI_NOTICE
5600 "Backlight control force enabled, even if standard "
5601 "ACPI backlight interface is available\n");
5603 } else {
5604 if (brightness_enable > 1) {
5605 printk(TPACPI_NOTICE
5606 "Standard ACPI backlight interface not "
5607 "available, thinkpad_acpi native "
5608 "brightness control enabled\n");
5613 if (!brightness_enable) {
5614 dbg_printk(TPACPI_DBG_INIT,
5615 "brightness support disabled by "
5616 "module parameter\n");
5617 return 1;
5620 if (b > 16) {
5621 printk(TPACPI_ERR
5622 "Unsupported brightness interface, "
5623 "please contact %s\n", TPACPI_MAIL);
5624 return 1;
5626 if (b == 16)
5627 tp_features.bright_16levels = 1;
5629 if (!brightness_mode) {
5630 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5631 brightness_mode = 2;
5632 else
5633 brightness_mode = 3;
5635 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5636 brightness_mode);
5639 if (brightness_mode > 3)
5640 return -EINVAL;
5642 if (brightness_get_raw(&b) < 0)
5643 return 1;
5645 if (tp_features.bright_16levels)
5646 printk(TPACPI_INFO
5647 "detected a 16-level brightness capable ThinkPad\n");
5649 ibm_backlight_device = backlight_device_register(
5650 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5651 &ibm_backlight_data);
5652 if (IS_ERR(ibm_backlight_device)) {
5653 printk(TPACPI_ERR "Could not register backlight device\n");
5654 return PTR_ERR(ibm_backlight_device);
5656 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5658 ibm_backlight_device->props.max_brightness =
5659 (tp_features.bright_16levels)? 15 : 7;
5660 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5661 backlight_update_status(ibm_backlight_device);
5663 return 0;
5666 static void brightness_exit(void)
5668 if (ibm_backlight_device) {
5669 vdbg_printk(TPACPI_DBG_EXIT,
5670 "calling backlight_device_unregister()\n");
5671 backlight_device_unregister(ibm_backlight_device);
5675 static int brightness_read(char *p)
5677 int len = 0;
5678 int level;
5680 level = brightness_get(NULL);
5681 if (level < 0) {
5682 len += sprintf(p + len, "level:\t\tunreadable\n");
5683 } else {
5684 len += sprintf(p + len, "level:\t\t%d\n", level);
5685 len += sprintf(p + len, "commands:\tup, down\n");
5686 len += sprintf(p + len, "commands:\tlevel <level>"
5687 " (<level> is 0-%d)\n",
5688 (tp_features.bright_16levels) ? 15 : 7);
5691 return len;
5694 static int brightness_write(char *buf)
5696 int level;
5697 int rc;
5698 char *cmd;
5699 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5701 level = brightness_get(NULL);
5702 if (level < 0)
5703 return level;
5705 while ((cmd = next_cmd(&buf))) {
5706 if (strlencmp(cmd, "up") == 0) {
5707 if (level < max_level)
5708 level++;
5709 } else if (strlencmp(cmd, "down") == 0) {
5710 if (level > 0)
5711 level--;
5712 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5713 level >= 0 && level <= max_level) {
5714 /* new level set */
5715 } else
5716 return -EINVAL;
5720 * Now we know what the final level should be, so we try to set it.
5721 * Doing it this way makes the syscall restartable in case of EINTR
5723 rc = brightness_set(level);
5724 return (rc == -EINTR)? ERESTARTSYS : rc;
5727 static struct ibm_struct brightness_driver_data = {
5728 .name = "brightness",
5729 .read = brightness_read,
5730 .write = brightness_write,
5731 .exit = brightness_exit,
5734 /*************************************************************************
5735 * Volume subdriver
5738 static int volume_offset = 0x30;
5740 static int volume_read(char *p)
5742 int len = 0;
5743 u8 level;
5745 if (!acpi_ec_read(volume_offset, &level)) {
5746 len += sprintf(p + len, "level:\t\tunreadable\n");
5747 } else {
5748 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5749 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5750 len += sprintf(p + len, "commands:\tup, down, mute\n");
5751 len += sprintf(p + len, "commands:\tlevel <level>"
5752 " (<level> is 0-15)\n");
5755 return len;
5758 static int volume_write(char *buf)
5760 int cmos_cmd, inc, i;
5761 u8 level, mute;
5762 int new_level, new_mute;
5763 char *cmd;
5765 while ((cmd = next_cmd(&buf))) {
5766 if (!acpi_ec_read(volume_offset, &level))
5767 return -EIO;
5768 new_mute = mute = level & 0x40;
5769 new_level = level = level & 0xf;
5771 if (strlencmp(cmd, "up") == 0) {
5772 if (mute)
5773 new_mute = 0;
5774 else
5775 new_level = level == 15 ? 15 : level + 1;
5776 } else if (strlencmp(cmd, "down") == 0) {
5777 if (mute)
5778 new_mute = 0;
5779 else
5780 new_level = level == 0 ? 0 : level - 1;
5781 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5782 new_level >= 0 && new_level <= 15) {
5783 /* new_level set */
5784 } else if (strlencmp(cmd, "mute") == 0) {
5785 new_mute = 0x40;
5786 } else
5787 return -EINVAL;
5789 if (new_level != level) {
5790 /* mute doesn't change */
5792 cmos_cmd = (new_level > level) ?
5793 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5794 inc = new_level > level ? 1 : -1;
5796 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5797 !acpi_ec_write(volume_offset, level)))
5798 return -EIO;
5800 for (i = level; i != new_level; i += inc)
5801 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5802 !acpi_ec_write(volume_offset, i + inc))
5803 return -EIO;
5805 if (mute &&
5806 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5807 !acpi_ec_write(volume_offset, new_level + mute))) {
5808 return -EIO;
5812 if (new_mute != mute) {
5813 /* level doesn't change */
5815 cmos_cmd = (new_mute) ?
5816 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5818 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5819 !acpi_ec_write(volume_offset, level + new_mute))
5820 return -EIO;
5824 return 0;
5827 static struct ibm_struct volume_driver_data = {
5828 .name = "volume",
5829 .read = volume_read,
5830 .write = volume_write,
5833 /*************************************************************************
5834 * Fan subdriver
5838 * FAN ACCESS MODES
5840 * TPACPI_FAN_RD_ACPI_GFAN:
5841 * ACPI GFAN method: returns fan level
5843 * see TPACPI_FAN_WR_ACPI_SFAN
5844 * EC 0x2f (HFSP) not available if GFAN exists
5846 * TPACPI_FAN_WR_ACPI_SFAN:
5847 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5849 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5850 * it for writing.
5852 * TPACPI_FAN_WR_TPEC:
5853 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5854 * Supported on almost all ThinkPads
5856 * Fan speed changes of any sort (including those caused by the
5857 * disengaged mode) are usually done slowly by the firmware as the
5858 * maximum ammount of fan duty cycle change per second seems to be
5859 * limited.
5861 * Reading is not available if GFAN exists.
5862 * Writing is not available if SFAN exists.
5864 * Bits
5865 * 7 automatic mode engaged;
5866 * (default operation mode of the ThinkPad)
5867 * fan level is ignored in this mode.
5868 * 6 full speed mode (takes precedence over bit 7);
5869 * not available on all thinkpads. May disable
5870 * the tachometer while the fan controller ramps up
5871 * the speed (which can take up to a few *minutes*).
5872 * Speeds up fan to 100% duty-cycle, which is far above
5873 * the standard RPM levels. It is not impossible that
5874 * it could cause hardware damage.
5875 * 5-3 unused in some models. Extra bits for fan level
5876 * in others, but still useless as all values above
5877 * 7 map to the same speed as level 7 in these models.
5878 * 2-0 fan level (0..7 usually)
5879 * 0x00 = stop
5880 * 0x07 = max (set when temperatures critical)
5881 * Some ThinkPads may have other levels, see
5882 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5884 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5885 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5886 * does so, its initial value is meaningless (0x07).
5888 * For firmware bugs, refer to:
5889 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5891 * ----
5893 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5894 * Main fan tachometer reading (in RPM)
5896 * This register is present on all ThinkPads with a new-style EC, and
5897 * it is known not to be present on the A21m/e, and T22, as there is
5898 * something else in offset 0x84 according to the ACPI DSDT. Other
5899 * ThinkPads from this same time period (and earlier) probably lack the
5900 * tachometer as well.
5902 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5903 * was never fixed by IBM to report the EC firmware version string
5904 * probably support the tachometer (like the early X models), so
5905 * detecting it is quite hard. We need more data to know for sure.
5907 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5908 * might result.
5910 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5911 * mode.
5913 * For firmware bugs, refer to:
5914 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5916 * TPACPI_FAN_WR_ACPI_FANS:
5917 * ThinkPad X31, X40, X41. Not available in the X60.
5919 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5920 * high speed. ACPI DSDT seems to map these three speeds to levels
5921 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5922 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5924 * The speeds are stored on handles
5925 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5927 * There are three default speed sets, acessible as handles:
5928 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5930 * ACPI DSDT switches which set is in use depending on various
5931 * factors.
5933 * TPACPI_FAN_WR_TPEC is also available and should be used to
5934 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5935 * but the ACPI tables just mention level 7.
5938 enum { /* Fan control constants */
5939 fan_status_offset = 0x2f, /* EC register 0x2f */
5940 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5941 * 0x84 must be read before 0x85 */
5943 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5944 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5946 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5949 enum fan_status_access_mode {
5950 TPACPI_FAN_NONE = 0, /* No fan status or control */
5951 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5952 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5955 enum fan_control_access_mode {
5956 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5957 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5958 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5959 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5962 enum fan_control_commands {
5963 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5964 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5965 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5966 * and also watchdog cmd */
5969 static int fan_control_allowed;
5971 static enum fan_status_access_mode fan_status_access_mode;
5972 static enum fan_control_access_mode fan_control_access_mode;
5973 static enum fan_control_commands fan_control_commands;
5975 static u8 fan_control_initial_status;
5976 static u8 fan_control_desired_level;
5977 static u8 fan_control_resume_level;
5978 static int fan_watchdog_maxinterval;
5980 static struct mutex fan_mutex;
5982 static void fan_watchdog_fire(struct work_struct *ignored);
5983 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5985 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5986 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5987 "\\FSPD", /* 600e/x, 770e, 770x */
5988 ); /* all others */
5989 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5990 "JFNS", /* 770x-JL */
5991 ); /* all others */
5994 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5995 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5996 * be in auto mode (0x80).
5998 * This is corrected by any write to HFSP either by the driver, or
5999 * by the firmware.
6001 * We assume 0x07 really means auto mode while this quirk is active,
6002 * as this is far more likely than the ThinkPad being in level 7,
6003 * which is only used by the firmware during thermal emergencies.
6006 static void fan_quirk1_detect(void)
6008 /* In some ThinkPads, neither the EC nor the ACPI
6009 * DSDT initialize the HFSP register, and it ends up
6010 * being initially set to 0x07 when it *could* be
6011 * either 0x07 or 0x80.
6013 * Enable for TP-1Y (T43), TP-78 (R51e),
6014 * TP-76 (R52), TP-70 (T43, R52), which are known
6015 * to be buggy. */
6016 if (fan_control_initial_status == 0x07) {
6017 switch (thinkpad_id.ec_model) {
6018 case 0x5931: /* TP-1Y */
6019 case 0x3837: /* TP-78 */
6020 case 0x3637: /* TP-76 */
6021 case 0x3037: /* TP-70 */
6022 printk(TPACPI_NOTICE
6023 "fan_init: initial fan status is unknown, "
6024 "assuming it is in auto mode\n");
6025 tp_features.fan_ctrl_status_undef = 1;
6031 static void fan_quirk1_handle(u8 *fan_status)
6033 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6034 if (*fan_status != fan_control_initial_status) {
6035 /* something changed the HFSP regisnter since
6036 * driver init time, so it is not undefined
6037 * anymore */
6038 tp_features.fan_ctrl_status_undef = 0;
6039 } else {
6040 /* Return most likely status. In fact, it
6041 * might be the only possible status */
6042 *fan_status = TP_EC_FAN_AUTO;
6048 * Call with fan_mutex held
6050 static void fan_update_desired_level(u8 status)
6052 if ((status &
6053 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6054 if (status > 7)
6055 fan_control_desired_level = 7;
6056 else
6057 fan_control_desired_level = status;
6061 static int fan_get_status(u8 *status)
6063 u8 s;
6065 /* TODO:
6066 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6068 switch (fan_status_access_mode) {
6069 case TPACPI_FAN_RD_ACPI_GFAN:
6070 /* 570, 600e/x, 770e, 770x */
6072 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6073 return -EIO;
6075 if (likely(status))
6076 *status = s & 0x07;
6078 break;
6080 case TPACPI_FAN_RD_TPEC:
6081 /* all except 570, 600e/x, 770e, 770x */
6082 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6083 return -EIO;
6085 if (likely(status)) {
6086 *status = s;
6087 fan_quirk1_handle(status);
6090 break;
6092 default:
6093 return -ENXIO;
6096 return 0;
6099 static int fan_get_status_safe(u8 *status)
6101 int rc;
6102 u8 s;
6104 if (mutex_lock_killable(&fan_mutex))
6105 return -ERESTARTSYS;
6106 rc = fan_get_status(&s);
6107 if (!rc)
6108 fan_update_desired_level(s);
6109 mutex_unlock(&fan_mutex);
6111 if (status)
6112 *status = s;
6114 return rc;
6117 static int fan_get_speed(unsigned int *speed)
6119 u8 hi, lo;
6121 switch (fan_status_access_mode) {
6122 case TPACPI_FAN_RD_TPEC:
6123 /* all except 570, 600e/x, 770e, 770x */
6124 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6125 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6126 return -EIO;
6128 if (likely(speed))
6129 *speed = (hi << 8) | lo;
6131 break;
6133 default:
6134 return -ENXIO;
6137 return 0;
6140 static int fan_set_level(int level)
6142 if (!fan_control_allowed)
6143 return -EPERM;
6145 switch (fan_control_access_mode) {
6146 case TPACPI_FAN_WR_ACPI_SFAN:
6147 if (level >= 0 && level <= 7) {
6148 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6149 return -EIO;
6150 } else
6151 return -EINVAL;
6152 break;
6154 case TPACPI_FAN_WR_ACPI_FANS:
6155 case TPACPI_FAN_WR_TPEC:
6156 if (!(level & TP_EC_FAN_AUTO) &&
6157 !(level & TP_EC_FAN_FULLSPEED) &&
6158 ((level < 0) || (level > 7)))
6159 return -EINVAL;
6161 /* safety net should the EC not support AUTO
6162 * or FULLSPEED mode bits and just ignore them */
6163 if (level & TP_EC_FAN_FULLSPEED)
6164 level |= 7; /* safety min speed 7 */
6165 else if (level & TP_EC_FAN_AUTO)
6166 level |= 4; /* safety min speed 4 */
6168 if (!acpi_ec_write(fan_status_offset, level))
6169 return -EIO;
6170 else
6171 tp_features.fan_ctrl_status_undef = 0;
6172 break;
6174 default:
6175 return -ENXIO;
6177 return 0;
6180 static int fan_set_level_safe(int level)
6182 int rc;
6184 if (!fan_control_allowed)
6185 return -EPERM;
6187 if (mutex_lock_killable(&fan_mutex))
6188 return -ERESTARTSYS;
6190 if (level == TPACPI_FAN_LAST_LEVEL)
6191 level = fan_control_desired_level;
6193 rc = fan_set_level(level);
6194 if (!rc)
6195 fan_update_desired_level(level);
6197 mutex_unlock(&fan_mutex);
6198 return rc;
6201 static int fan_set_enable(void)
6203 u8 s;
6204 int rc;
6206 if (!fan_control_allowed)
6207 return -EPERM;
6209 if (mutex_lock_killable(&fan_mutex))
6210 return -ERESTARTSYS;
6212 switch (fan_control_access_mode) {
6213 case TPACPI_FAN_WR_ACPI_FANS:
6214 case TPACPI_FAN_WR_TPEC:
6215 rc = fan_get_status(&s);
6216 if (rc < 0)
6217 break;
6219 /* Don't go out of emergency fan mode */
6220 if (s != 7) {
6221 s &= 0x07;
6222 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6225 if (!acpi_ec_write(fan_status_offset, s))
6226 rc = -EIO;
6227 else {
6228 tp_features.fan_ctrl_status_undef = 0;
6229 rc = 0;
6231 break;
6233 case TPACPI_FAN_WR_ACPI_SFAN:
6234 rc = fan_get_status(&s);
6235 if (rc < 0)
6236 break;
6238 s &= 0x07;
6240 /* Set fan to at least level 4 */
6241 s |= 4;
6243 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6244 rc = -EIO;
6245 else
6246 rc = 0;
6247 break;
6249 default:
6250 rc = -ENXIO;
6253 mutex_unlock(&fan_mutex);
6254 return rc;
6257 static int fan_set_disable(void)
6259 int rc;
6261 if (!fan_control_allowed)
6262 return -EPERM;
6264 if (mutex_lock_killable(&fan_mutex))
6265 return -ERESTARTSYS;
6267 rc = 0;
6268 switch (fan_control_access_mode) {
6269 case TPACPI_FAN_WR_ACPI_FANS:
6270 case TPACPI_FAN_WR_TPEC:
6271 if (!acpi_ec_write(fan_status_offset, 0x00))
6272 rc = -EIO;
6273 else {
6274 fan_control_desired_level = 0;
6275 tp_features.fan_ctrl_status_undef = 0;
6277 break;
6279 case TPACPI_FAN_WR_ACPI_SFAN:
6280 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6281 rc = -EIO;
6282 else
6283 fan_control_desired_level = 0;
6284 break;
6286 default:
6287 rc = -ENXIO;
6291 mutex_unlock(&fan_mutex);
6292 return rc;
6295 static int fan_set_speed(int speed)
6297 int rc;
6299 if (!fan_control_allowed)
6300 return -EPERM;
6302 if (mutex_lock_killable(&fan_mutex))
6303 return -ERESTARTSYS;
6305 rc = 0;
6306 switch (fan_control_access_mode) {
6307 case TPACPI_FAN_WR_ACPI_FANS:
6308 if (speed >= 0 && speed <= 65535) {
6309 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6310 speed, speed, speed))
6311 rc = -EIO;
6312 } else
6313 rc = -EINVAL;
6314 break;
6316 default:
6317 rc = -ENXIO;
6320 mutex_unlock(&fan_mutex);
6321 return rc;
6324 static void fan_watchdog_reset(void)
6326 static int fan_watchdog_active;
6328 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6329 return;
6331 if (fan_watchdog_active)
6332 cancel_delayed_work(&fan_watchdog_task);
6334 if (fan_watchdog_maxinterval > 0 &&
6335 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6336 fan_watchdog_active = 1;
6337 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6338 msecs_to_jiffies(fan_watchdog_maxinterval
6339 * 1000))) {
6340 printk(TPACPI_ERR
6341 "failed to queue the fan watchdog, "
6342 "watchdog will not trigger\n");
6344 } else
6345 fan_watchdog_active = 0;
6348 static void fan_watchdog_fire(struct work_struct *ignored)
6350 int rc;
6352 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6353 return;
6355 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6356 rc = fan_set_enable();
6357 if (rc < 0) {
6358 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6359 "will try again later...\n", -rc);
6360 /* reschedule for later */
6361 fan_watchdog_reset();
6366 * SYSFS fan layout: hwmon compatible (device)
6368 * pwm*_enable:
6369 * 0: "disengaged" mode
6370 * 1: manual mode
6371 * 2: native EC "auto" mode (recommended, hardware default)
6373 * pwm*: set speed in manual mode, ignored otherwise.
6374 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6375 * interpolation.
6377 * fan*_input: tachometer reading, RPM
6380 * SYSFS fan layout: extensions
6382 * fan_watchdog (driver):
6383 * fan watchdog interval in seconds, 0 disables (default), max 120
6386 /* sysfs fan pwm1_enable ----------------------------------------------- */
6387 static ssize_t fan_pwm1_enable_show(struct device *dev,
6388 struct device_attribute *attr,
6389 char *buf)
6391 int res, mode;
6392 u8 status;
6394 res = fan_get_status_safe(&status);
6395 if (res)
6396 return res;
6398 if (status & TP_EC_FAN_FULLSPEED) {
6399 mode = 0;
6400 } else if (status & TP_EC_FAN_AUTO) {
6401 mode = 2;
6402 } else
6403 mode = 1;
6405 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6408 static ssize_t fan_pwm1_enable_store(struct device *dev,
6409 struct device_attribute *attr,
6410 const char *buf, size_t count)
6412 unsigned long t;
6413 int res, level;
6415 if (parse_strtoul(buf, 2, &t))
6416 return -EINVAL;
6418 switch (t) {
6419 case 0:
6420 level = TP_EC_FAN_FULLSPEED;
6421 break;
6422 case 1:
6423 level = TPACPI_FAN_LAST_LEVEL;
6424 break;
6425 case 2:
6426 level = TP_EC_FAN_AUTO;
6427 break;
6428 case 3:
6429 /* reserved for software-controlled auto mode */
6430 return -ENOSYS;
6431 default:
6432 return -EINVAL;
6435 res = fan_set_level_safe(level);
6436 if (res == -ENXIO)
6437 return -EINVAL;
6438 else if (res < 0)
6439 return res;
6441 fan_watchdog_reset();
6443 return count;
6446 static struct device_attribute dev_attr_fan_pwm1_enable =
6447 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6448 fan_pwm1_enable_show, fan_pwm1_enable_store);
6450 /* sysfs fan pwm1 ------------------------------------------------------ */
6451 static ssize_t fan_pwm1_show(struct device *dev,
6452 struct device_attribute *attr,
6453 char *buf)
6455 int res;
6456 u8 status;
6458 res = fan_get_status_safe(&status);
6459 if (res)
6460 return res;
6462 if ((status &
6463 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6464 status = fan_control_desired_level;
6466 if (status > 7)
6467 status = 7;
6469 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6472 static ssize_t fan_pwm1_store(struct device *dev,
6473 struct device_attribute *attr,
6474 const char *buf, size_t count)
6476 unsigned long s;
6477 int rc;
6478 u8 status, newlevel;
6480 if (parse_strtoul(buf, 255, &s))
6481 return -EINVAL;
6483 /* scale down from 0-255 to 0-7 */
6484 newlevel = (s >> 5) & 0x07;
6486 if (mutex_lock_killable(&fan_mutex))
6487 return -ERESTARTSYS;
6489 rc = fan_get_status(&status);
6490 if (!rc && (status &
6491 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6492 rc = fan_set_level(newlevel);
6493 if (rc == -ENXIO)
6494 rc = -EINVAL;
6495 else if (!rc) {
6496 fan_update_desired_level(newlevel);
6497 fan_watchdog_reset();
6501 mutex_unlock(&fan_mutex);
6502 return (rc)? rc : count;
6505 static struct device_attribute dev_attr_fan_pwm1 =
6506 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6507 fan_pwm1_show, fan_pwm1_store);
6509 /* sysfs fan fan1_input ------------------------------------------------ */
6510 static ssize_t fan_fan1_input_show(struct device *dev,
6511 struct device_attribute *attr,
6512 char *buf)
6514 int res;
6515 unsigned int speed;
6517 res = fan_get_speed(&speed);
6518 if (res < 0)
6519 return res;
6521 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6524 static struct device_attribute dev_attr_fan_fan1_input =
6525 __ATTR(fan1_input, S_IRUGO,
6526 fan_fan1_input_show, NULL);
6528 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6529 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6530 char *buf)
6532 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6535 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6536 const char *buf, size_t count)
6538 unsigned long t;
6540 if (parse_strtoul(buf, 120, &t))
6541 return -EINVAL;
6543 if (!fan_control_allowed)
6544 return -EPERM;
6546 fan_watchdog_maxinterval = t;
6547 fan_watchdog_reset();
6549 return count;
6552 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6553 fan_fan_watchdog_show, fan_fan_watchdog_store);
6555 /* --------------------------------------------------------------------- */
6556 static struct attribute *fan_attributes[] = {
6557 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6558 &dev_attr_fan_fan1_input.attr,
6559 NULL
6562 static const struct attribute_group fan_attr_group = {
6563 .attrs = fan_attributes,
6566 static int __init fan_init(struct ibm_init_struct *iibm)
6568 int rc;
6570 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6572 mutex_init(&fan_mutex);
6573 fan_status_access_mode = TPACPI_FAN_NONE;
6574 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6575 fan_control_commands = 0;
6576 fan_watchdog_maxinterval = 0;
6577 tp_features.fan_ctrl_status_undef = 0;
6578 fan_control_desired_level = 7;
6580 TPACPI_ACPIHANDLE_INIT(fans);
6581 TPACPI_ACPIHANDLE_INIT(gfan);
6582 TPACPI_ACPIHANDLE_INIT(sfan);
6584 if (gfan_handle) {
6585 /* 570, 600e/x, 770e, 770x */
6586 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6587 } else {
6588 /* all other ThinkPads: note that even old-style
6589 * ThinkPad ECs supports the fan control register */
6590 if (likely(acpi_ec_read(fan_status_offset,
6591 &fan_control_initial_status))) {
6592 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6593 fan_quirk1_detect();
6594 } else {
6595 printk(TPACPI_ERR
6596 "ThinkPad ACPI EC access misbehaving, "
6597 "fan status and control unavailable\n");
6598 return 1;
6602 if (sfan_handle) {
6603 /* 570, 770x-JL */
6604 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6605 fan_control_commands |=
6606 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6607 } else {
6608 if (!gfan_handle) {
6609 /* gfan without sfan means no fan control */
6610 /* all other models implement TP EC 0x2f control */
6612 if (fans_handle) {
6613 /* X31, X40, X41 */
6614 fan_control_access_mode =
6615 TPACPI_FAN_WR_ACPI_FANS;
6616 fan_control_commands |=
6617 TPACPI_FAN_CMD_SPEED |
6618 TPACPI_FAN_CMD_LEVEL |
6619 TPACPI_FAN_CMD_ENABLE;
6620 } else {
6621 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6622 fan_control_commands |=
6623 TPACPI_FAN_CMD_LEVEL |
6624 TPACPI_FAN_CMD_ENABLE;
6629 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6630 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6631 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6632 fan_status_access_mode, fan_control_access_mode);
6634 /* fan control master switch */
6635 if (!fan_control_allowed) {
6636 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6637 fan_control_commands = 0;
6638 dbg_printk(TPACPI_DBG_INIT,
6639 "fan control features disabled by parameter\n");
6642 /* update fan_control_desired_level */
6643 if (fan_status_access_mode != TPACPI_FAN_NONE)
6644 fan_get_status_safe(NULL);
6646 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6647 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6648 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6649 &fan_attr_group);
6650 if (rc < 0)
6651 return rc;
6653 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6654 &driver_attr_fan_watchdog);
6655 if (rc < 0) {
6656 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6657 &fan_attr_group);
6658 return rc;
6660 return 0;
6661 } else
6662 return 1;
6665 static void fan_exit(void)
6667 vdbg_printk(TPACPI_DBG_EXIT,
6668 "cancelling any pending fan watchdog tasks\n");
6670 /* FIXME: can we really do this unconditionally? */
6671 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6672 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6673 &driver_attr_fan_watchdog);
6675 cancel_delayed_work(&fan_watchdog_task);
6676 flush_workqueue(tpacpi_wq);
6679 static void fan_suspend(pm_message_t state)
6681 int rc;
6683 if (!fan_control_allowed)
6684 return;
6686 /* Store fan status in cache */
6687 fan_control_resume_level = 0;
6688 rc = fan_get_status_safe(&fan_control_resume_level);
6689 if (rc < 0)
6690 printk(TPACPI_NOTICE
6691 "failed to read fan level for later "
6692 "restore during resume: %d\n", rc);
6694 /* if it is undefined, don't attempt to restore it.
6695 * KEEP THIS LAST */
6696 if (tp_features.fan_ctrl_status_undef)
6697 fan_control_resume_level = 0;
6700 static void fan_resume(void)
6702 u8 current_level = 7;
6703 bool do_set = false;
6704 int rc;
6706 /* DSDT *always* updates status on resume */
6707 tp_features.fan_ctrl_status_undef = 0;
6709 if (!fan_control_allowed ||
6710 !fan_control_resume_level ||
6711 (fan_get_status_safe(&current_level) < 0))
6712 return;
6714 switch (fan_control_access_mode) {
6715 case TPACPI_FAN_WR_ACPI_SFAN:
6716 /* never decrease fan level */
6717 do_set = (fan_control_resume_level > current_level);
6718 break;
6719 case TPACPI_FAN_WR_ACPI_FANS:
6720 case TPACPI_FAN_WR_TPEC:
6721 /* never decrease fan level, scale is:
6722 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6724 * We expect the firmware to set either 7 or AUTO, but we
6725 * handle FULLSPEED out of paranoia.
6727 * So, we can safely only restore FULLSPEED or 7, anything
6728 * else could slow the fan. Restoring AUTO is useless, at
6729 * best that's exactly what the DSDT already set (it is the
6730 * slower it uses).
6732 * Always keep in mind that the DSDT *will* have set the
6733 * fans to what the vendor supposes is the best level. We
6734 * muck with it only to speed the fan up.
6736 if (fan_control_resume_level != 7 &&
6737 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6738 return;
6739 else
6740 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6741 (current_level != fan_control_resume_level);
6742 break;
6743 default:
6744 return;
6746 if (do_set) {
6747 printk(TPACPI_NOTICE
6748 "restoring fan level to 0x%02x\n",
6749 fan_control_resume_level);
6750 rc = fan_set_level_safe(fan_control_resume_level);
6751 if (rc < 0)
6752 printk(TPACPI_NOTICE
6753 "failed to restore fan level: %d\n", rc);
6757 static int fan_read(char *p)
6759 int len = 0;
6760 int rc;
6761 u8 status;
6762 unsigned int speed = 0;
6764 switch (fan_status_access_mode) {
6765 case TPACPI_FAN_RD_ACPI_GFAN:
6766 /* 570, 600e/x, 770e, 770x */
6767 rc = fan_get_status_safe(&status);
6768 if (rc < 0)
6769 return rc;
6771 len += sprintf(p + len, "status:\t\t%s\n"
6772 "level:\t\t%d\n",
6773 (status != 0) ? "enabled" : "disabled", status);
6774 break;
6776 case TPACPI_FAN_RD_TPEC:
6777 /* all except 570, 600e/x, 770e, 770x */
6778 rc = fan_get_status_safe(&status);
6779 if (rc < 0)
6780 return rc;
6782 len += sprintf(p + len, "status:\t\t%s\n",
6783 (status != 0) ? "enabled" : "disabled");
6785 rc = fan_get_speed(&speed);
6786 if (rc < 0)
6787 return rc;
6789 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6791 if (status & TP_EC_FAN_FULLSPEED)
6792 /* Disengaged mode takes precedence */
6793 len += sprintf(p + len, "level:\t\tdisengaged\n");
6794 else if (status & TP_EC_FAN_AUTO)
6795 len += sprintf(p + len, "level:\t\tauto\n");
6796 else
6797 len += sprintf(p + len, "level:\t\t%d\n", status);
6798 break;
6800 case TPACPI_FAN_NONE:
6801 default:
6802 len += sprintf(p + len, "status:\t\tnot supported\n");
6805 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6806 len += sprintf(p + len, "commands:\tlevel <level>");
6808 switch (fan_control_access_mode) {
6809 case TPACPI_FAN_WR_ACPI_SFAN:
6810 len += sprintf(p + len, " (<level> is 0-7)\n");
6811 break;
6813 default:
6814 len += sprintf(p + len, " (<level> is 0-7, "
6815 "auto, disengaged, full-speed)\n");
6816 break;
6820 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6821 len += sprintf(p + len, "commands:\tenable, disable\n"
6822 "commands:\twatchdog <timeout> (<timeout> "
6823 "is 0 (off), 1-120 (seconds))\n");
6825 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6826 len += sprintf(p + len, "commands:\tspeed <speed>"
6827 " (<speed> is 0-65535)\n");
6829 return len;
6832 static int fan_write_cmd_level(const char *cmd, int *rc)
6834 int level;
6836 if (strlencmp(cmd, "level auto") == 0)
6837 level = TP_EC_FAN_AUTO;
6838 else if ((strlencmp(cmd, "level disengaged") == 0) |
6839 (strlencmp(cmd, "level full-speed") == 0))
6840 level = TP_EC_FAN_FULLSPEED;
6841 else if (sscanf(cmd, "level %d", &level) != 1)
6842 return 0;
6844 *rc = fan_set_level_safe(level);
6845 if (*rc == -ENXIO)
6846 printk(TPACPI_ERR "level command accepted for unsupported "
6847 "access mode %d", fan_control_access_mode);
6849 return 1;
6852 static int fan_write_cmd_enable(const char *cmd, int *rc)
6854 if (strlencmp(cmd, "enable") != 0)
6855 return 0;
6857 *rc = fan_set_enable();
6858 if (*rc == -ENXIO)
6859 printk(TPACPI_ERR "enable command accepted for unsupported "
6860 "access mode %d", fan_control_access_mode);
6862 return 1;
6865 static int fan_write_cmd_disable(const char *cmd, int *rc)
6867 if (strlencmp(cmd, "disable") != 0)
6868 return 0;
6870 *rc = fan_set_disable();
6871 if (*rc == -ENXIO)
6872 printk(TPACPI_ERR "disable command accepted for unsupported "
6873 "access mode %d", fan_control_access_mode);
6875 return 1;
6878 static int fan_write_cmd_speed(const char *cmd, int *rc)
6880 int speed;
6882 /* TODO:
6883 * Support speed <low> <medium> <high> ? */
6885 if (sscanf(cmd, "speed %d", &speed) != 1)
6886 return 0;
6888 *rc = fan_set_speed(speed);
6889 if (*rc == -ENXIO)
6890 printk(TPACPI_ERR "speed command accepted for unsupported "
6891 "access mode %d", fan_control_access_mode);
6893 return 1;
6896 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6898 int interval;
6900 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6901 return 0;
6903 if (interval < 0 || interval > 120)
6904 *rc = -EINVAL;
6905 else
6906 fan_watchdog_maxinterval = interval;
6908 return 1;
6911 static int fan_write(char *buf)
6913 char *cmd;
6914 int rc = 0;
6916 while (!rc && (cmd = next_cmd(&buf))) {
6917 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6918 fan_write_cmd_level(cmd, &rc)) &&
6919 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6920 (fan_write_cmd_enable(cmd, &rc) ||
6921 fan_write_cmd_disable(cmd, &rc) ||
6922 fan_write_cmd_watchdog(cmd, &rc))) &&
6923 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6924 fan_write_cmd_speed(cmd, &rc))
6926 rc = -EINVAL;
6927 else if (!rc)
6928 fan_watchdog_reset();
6931 return rc;
6934 static struct ibm_struct fan_driver_data = {
6935 .name = "fan",
6936 .read = fan_read,
6937 .write = fan_write,
6938 .exit = fan_exit,
6939 .suspend = fan_suspend,
6940 .resume = fan_resume,
6943 /****************************************************************************
6944 ****************************************************************************
6946 * Infrastructure
6948 ****************************************************************************
6949 ****************************************************************************/
6951 /* sysfs name ---------------------------------------------------------- */
6952 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6953 struct device_attribute *attr,
6954 char *buf)
6956 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6959 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6960 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6962 /* --------------------------------------------------------------------- */
6964 /* /proc support */
6965 static struct proc_dir_entry *proc_dir;
6968 * Module and infrastructure proble, init and exit handling
6971 static int force_load;
6973 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6974 static const char * __init str_supported(int is_supported)
6976 static char text_unsupported[] __initdata = "not supported";
6978 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6980 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6982 static void ibm_exit(struct ibm_struct *ibm)
6984 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6986 list_del_init(&ibm->all_drivers);
6988 if (ibm->flags.acpi_notify_installed) {
6989 dbg_printk(TPACPI_DBG_EXIT,
6990 "%s: acpi_remove_notify_handler\n", ibm->name);
6991 BUG_ON(!ibm->acpi);
6992 acpi_remove_notify_handler(*ibm->acpi->handle,
6993 ibm->acpi->type,
6994 dispatch_acpi_notify);
6995 ibm->flags.acpi_notify_installed = 0;
6996 ibm->flags.acpi_notify_installed = 0;
6999 if (ibm->flags.proc_created) {
7000 dbg_printk(TPACPI_DBG_EXIT,
7001 "%s: remove_proc_entry\n", ibm->name);
7002 remove_proc_entry(ibm->name, proc_dir);
7003 ibm->flags.proc_created = 0;
7006 if (ibm->flags.acpi_driver_registered) {
7007 dbg_printk(TPACPI_DBG_EXIT,
7008 "%s: acpi_bus_unregister_driver\n", ibm->name);
7009 BUG_ON(!ibm->acpi);
7010 acpi_bus_unregister_driver(ibm->acpi->driver);
7011 kfree(ibm->acpi->driver);
7012 ibm->acpi->driver = NULL;
7013 ibm->flags.acpi_driver_registered = 0;
7016 if (ibm->flags.init_called && ibm->exit) {
7017 ibm->exit();
7018 ibm->flags.init_called = 0;
7021 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7024 static int __init ibm_init(struct ibm_init_struct *iibm)
7026 int ret;
7027 struct ibm_struct *ibm = iibm->data;
7028 struct proc_dir_entry *entry;
7030 BUG_ON(ibm == NULL);
7032 INIT_LIST_HEAD(&ibm->all_drivers);
7034 if (ibm->flags.experimental && !experimental)
7035 return 0;
7037 dbg_printk(TPACPI_DBG_INIT,
7038 "probing for %s\n", ibm->name);
7040 if (iibm->init) {
7041 ret = iibm->init(iibm);
7042 if (ret > 0)
7043 return 0; /* probe failed */
7044 if (ret)
7045 return ret;
7047 ibm->flags.init_called = 1;
7050 if (ibm->acpi) {
7051 if (ibm->acpi->hid) {
7052 ret = register_tpacpi_subdriver(ibm);
7053 if (ret)
7054 goto err_out;
7057 if (ibm->acpi->notify) {
7058 ret = setup_acpi_notify(ibm);
7059 if (ret == -ENODEV) {
7060 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7061 ibm->name);
7062 ret = 0;
7063 goto err_out;
7065 if (ret < 0)
7066 goto err_out;
7070 dbg_printk(TPACPI_DBG_INIT,
7071 "%s installed\n", ibm->name);
7073 if (ibm->read) {
7074 entry = create_proc_entry(ibm->name,
7075 S_IFREG | S_IRUGO | S_IWUSR,
7076 proc_dir);
7077 if (!entry) {
7078 printk(TPACPI_ERR "unable to create proc entry %s\n",
7079 ibm->name);
7080 ret = -ENODEV;
7081 goto err_out;
7083 entry->owner = THIS_MODULE;
7084 entry->data = ibm;
7085 entry->read_proc = &dispatch_procfs_read;
7086 if (ibm->write)
7087 entry->write_proc = &dispatch_procfs_write;
7088 ibm->flags.proc_created = 1;
7091 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7093 return 0;
7095 err_out:
7096 dbg_printk(TPACPI_DBG_INIT,
7097 "%s: at error exit path with result %d\n",
7098 ibm->name, ret);
7100 ibm_exit(ibm);
7101 return (ret < 0)? ret : 0;
7104 /* Probing */
7106 /* returns 0 - probe ok, or < 0 - probe error.
7107 * Probe ok doesn't mean thinkpad found.
7108 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7109 static int __must_check __init get_thinkpad_model_data(
7110 struct thinkpad_id_data *tp)
7112 const struct dmi_device *dev = NULL;
7113 char ec_fw_string[18];
7114 char const *s;
7116 if (!tp)
7117 return -EINVAL;
7119 memset(tp, 0, sizeof(*tp));
7121 if (dmi_name_in_vendors("IBM"))
7122 tp->vendor = PCI_VENDOR_ID_IBM;
7123 else if (dmi_name_in_vendors("LENOVO"))
7124 tp->vendor = PCI_VENDOR_ID_LENOVO;
7125 else
7126 return 0;
7128 s = dmi_get_system_info(DMI_BIOS_VERSION);
7129 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7130 if (s && !tp->bios_version_str)
7131 return -ENOMEM;
7132 if (!tp->bios_version_str)
7133 return 0;
7134 tp->bios_model = tp->bios_version_str[0]
7135 | (tp->bios_version_str[1] << 8);
7138 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7139 * X32 or newer, all Z series; Some models must have an
7140 * up-to-date BIOS or they will not be detected.
7142 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7144 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7145 if (sscanf(dev->name,
7146 "IBM ThinkPad Embedded Controller -[%17c",
7147 ec_fw_string) == 1) {
7148 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7149 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7151 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7152 if (!tp->ec_version_str)
7153 return -ENOMEM;
7154 tp->ec_model = ec_fw_string[0]
7155 | (ec_fw_string[1] << 8);
7156 break;
7160 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7161 if (s && !strnicmp(s, "ThinkPad", 8)) {
7162 tp->model_str = kstrdup(s, GFP_KERNEL);
7163 if (!tp->model_str)
7164 return -ENOMEM;
7167 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7168 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7169 if (s && !tp->nummodel_str)
7170 return -ENOMEM;
7172 return 0;
7175 static int __init probe_for_thinkpad(void)
7177 int is_thinkpad;
7179 if (acpi_disabled)
7180 return -ENODEV;
7183 * Non-ancient models have better DMI tagging, but very old models
7184 * don't.
7186 is_thinkpad = (thinkpad_id.model_str != NULL);
7188 /* ec is required because many other handles are relative to it */
7189 TPACPI_ACPIHANDLE_INIT(ec);
7190 if (!ec_handle) {
7191 if (is_thinkpad)
7192 printk(TPACPI_ERR
7193 "Not yet supported ThinkPad detected!\n");
7194 return -ENODEV;
7198 * Risks a regression on very old machines, but reduces potential
7199 * false positives a damn great deal
7201 if (!is_thinkpad)
7202 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7204 if (!is_thinkpad && !force_load)
7205 return -ENODEV;
7207 return 0;
7211 /* Module init, exit, parameters */
7213 static struct ibm_init_struct ibms_init[] __initdata = {
7215 .init = thinkpad_acpi_driver_init,
7216 .data = &thinkpad_acpi_driver_data,
7219 .init = hotkey_init,
7220 .data = &hotkey_driver_data,
7223 .init = bluetooth_init,
7224 .data = &bluetooth_driver_data,
7227 .init = wan_init,
7228 .data = &wan_driver_data,
7231 .init = uwb_init,
7232 .data = &uwb_driver_data,
7234 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7236 .init = video_init,
7237 .data = &video_driver_data,
7239 #endif
7241 .init = light_init,
7242 .data = &light_driver_data,
7244 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7246 .init = dock_init,
7247 .data = &dock_driver_data[0],
7250 .init = dock_init2,
7251 .data = &dock_driver_data[1],
7253 #endif
7254 #ifdef CONFIG_THINKPAD_ACPI_BAY
7256 .init = bay_init,
7257 .data = &bay_driver_data,
7259 #endif
7261 .init = cmos_init,
7262 .data = &cmos_driver_data,
7265 .init = led_init,
7266 .data = &led_driver_data,
7269 .init = beep_init,
7270 .data = &beep_driver_data,
7273 .init = thermal_init,
7274 .data = &thermal_driver_data,
7277 .data = &ecdump_driver_data,
7280 .init = brightness_init,
7281 .data = &brightness_driver_data,
7284 .data = &volume_driver_data,
7287 .init = fan_init,
7288 .data = &fan_driver_data,
7292 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7294 unsigned int i;
7295 struct ibm_struct *ibm;
7297 if (!kp || !kp->name || !val)
7298 return -EINVAL;
7300 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7301 ibm = ibms_init[i].data;
7302 WARN_ON(ibm == NULL);
7304 if (!ibm || !ibm->name)
7305 continue;
7307 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7308 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7309 return -ENOSPC;
7310 strcpy(ibms_init[i].param, val);
7311 strcat(ibms_init[i].param, ",");
7312 return 0;
7316 return -EINVAL;
7319 module_param(experimental, int, 0);
7320 MODULE_PARM_DESC(experimental,
7321 "Enables experimental features when non-zero");
7323 module_param_named(debug, dbg_level, uint, 0);
7324 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7326 module_param(force_load, bool, 0);
7327 MODULE_PARM_DESC(force_load,
7328 "Attempts to load the driver even on a "
7329 "mis-identified ThinkPad when true");
7331 module_param_named(fan_control, fan_control_allowed, bool, 0);
7332 MODULE_PARM_DESC(fan_control,
7333 "Enables setting fan parameters features when true");
7335 module_param_named(brightness_mode, brightness_mode, int, 0);
7336 MODULE_PARM_DESC(brightness_mode,
7337 "Selects brightness control strategy: "
7338 "0=auto, 1=EC, 2=CMOS, 3=both");
7340 module_param(brightness_enable, uint, 0);
7341 MODULE_PARM_DESC(brightness_enable,
7342 "Enables backlight control when 1, disables when 0");
7344 module_param(hotkey_report_mode, uint, 0);
7345 MODULE_PARM_DESC(hotkey_report_mode,
7346 "used for backwards compatibility with userspace, "
7347 "see documentation");
7349 #define TPACPI_PARAM(feature) \
7350 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7351 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7352 "at module load, see documentation")
7354 TPACPI_PARAM(hotkey);
7355 TPACPI_PARAM(bluetooth);
7356 TPACPI_PARAM(video);
7357 TPACPI_PARAM(light);
7358 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7359 TPACPI_PARAM(dock);
7360 #endif
7361 #ifdef CONFIG_THINKPAD_ACPI_BAY
7362 TPACPI_PARAM(bay);
7363 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7364 TPACPI_PARAM(cmos);
7365 TPACPI_PARAM(led);
7366 TPACPI_PARAM(beep);
7367 TPACPI_PARAM(ecdump);
7368 TPACPI_PARAM(brightness);
7369 TPACPI_PARAM(volume);
7370 TPACPI_PARAM(fan);
7372 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7373 module_param(dbg_wlswemul, uint, 0);
7374 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7375 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7376 MODULE_PARM_DESC(wlsw_state,
7377 "Initial state of the emulated WLSW switch");
7379 module_param(dbg_bluetoothemul, uint, 0);
7380 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7381 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7382 MODULE_PARM_DESC(bluetooth_state,
7383 "Initial state of the emulated bluetooth switch");
7385 module_param(dbg_wwanemul, uint, 0);
7386 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7387 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7388 MODULE_PARM_DESC(wwan_state,
7389 "Initial state of the emulated WWAN switch");
7391 module_param(dbg_uwbemul, uint, 0);
7392 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7393 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7394 MODULE_PARM_DESC(uwb_state,
7395 "Initial state of the emulated UWB switch");
7396 #endif
7398 static void thinkpad_acpi_module_exit(void)
7400 struct ibm_struct *ibm, *itmp;
7402 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7404 list_for_each_entry_safe_reverse(ibm, itmp,
7405 &tpacpi_all_drivers,
7406 all_drivers) {
7407 ibm_exit(ibm);
7410 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7412 if (tpacpi_inputdev) {
7413 if (tp_features.input_device_registered)
7414 input_unregister_device(tpacpi_inputdev);
7415 else
7416 input_free_device(tpacpi_inputdev);
7419 if (tpacpi_hwmon)
7420 hwmon_device_unregister(tpacpi_hwmon);
7422 if (tp_features.sensors_pdev_attrs_registered)
7423 device_remove_file(&tpacpi_sensors_pdev->dev,
7424 &dev_attr_thinkpad_acpi_pdev_name);
7425 if (tpacpi_sensors_pdev)
7426 platform_device_unregister(tpacpi_sensors_pdev);
7427 if (tpacpi_pdev)
7428 platform_device_unregister(tpacpi_pdev);
7430 if (tp_features.sensors_pdrv_attrs_registered)
7431 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7432 if (tp_features.platform_drv_attrs_registered)
7433 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7435 if (tp_features.sensors_pdrv_registered)
7436 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7438 if (tp_features.platform_drv_registered)
7439 platform_driver_unregister(&tpacpi_pdriver);
7441 if (proc_dir)
7442 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7444 if (tpacpi_wq)
7445 destroy_workqueue(tpacpi_wq);
7447 kfree(thinkpad_id.bios_version_str);
7448 kfree(thinkpad_id.ec_version_str);
7449 kfree(thinkpad_id.model_str);
7453 static int __init thinkpad_acpi_module_init(void)
7455 int ret, i;
7457 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7459 /* Parameter checking */
7460 if (hotkey_report_mode > 2)
7461 return -EINVAL;
7463 /* Driver-level probe */
7465 ret = get_thinkpad_model_data(&thinkpad_id);
7466 if (ret) {
7467 printk(TPACPI_ERR
7468 "unable to get DMI data: %d\n", ret);
7469 thinkpad_acpi_module_exit();
7470 return ret;
7472 ret = probe_for_thinkpad();
7473 if (ret) {
7474 thinkpad_acpi_module_exit();
7475 return ret;
7478 /* Driver initialization */
7480 TPACPI_ACPIHANDLE_INIT(ecrd);
7481 TPACPI_ACPIHANDLE_INIT(ecwr);
7483 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7484 if (!tpacpi_wq) {
7485 thinkpad_acpi_module_exit();
7486 return -ENOMEM;
7489 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7490 if (!proc_dir) {
7491 printk(TPACPI_ERR
7492 "unable to create proc dir " TPACPI_PROC_DIR);
7493 thinkpad_acpi_module_exit();
7494 return -ENODEV;
7496 proc_dir->owner = THIS_MODULE;
7498 ret = platform_driver_register(&tpacpi_pdriver);
7499 if (ret) {
7500 printk(TPACPI_ERR
7501 "unable to register main platform driver\n");
7502 thinkpad_acpi_module_exit();
7503 return ret;
7505 tp_features.platform_drv_registered = 1;
7507 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7508 if (ret) {
7509 printk(TPACPI_ERR
7510 "unable to register hwmon platform driver\n");
7511 thinkpad_acpi_module_exit();
7512 return ret;
7514 tp_features.sensors_pdrv_registered = 1;
7516 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7517 if (!ret) {
7518 tp_features.platform_drv_attrs_registered = 1;
7519 ret = tpacpi_create_driver_attributes(
7520 &tpacpi_hwmon_pdriver.driver);
7522 if (ret) {
7523 printk(TPACPI_ERR
7524 "unable to create sysfs driver attributes\n");
7525 thinkpad_acpi_module_exit();
7526 return ret;
7528 tp_features.sensors_pdrv_attrs_registered = 1;
7531 /* Device initialization */
7532 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7533 NULL, 0);
7534 if (IS_ERR(tpacpi_pdev)) {
7535 ret = PTR_ERR(tpacpi_pdev);
7536 tpacpi_pdev = NULL;
7537 printk(TPACPI_ERR "unable to register platform device\n");
7538 thinkpad_acpi_module_exit();
7539 return ret;
7541 tpacpi_sensors_pdev = platform_device_register_simple(
7542 TPACPI_HWMON_DRVR_NAME,
7543 -1, NULL, 0);
7544 if (IS_ERR(tpacpi_sensors_pdev)) {
7545 ret = PTR_ERR(tpacpi_sensors_pdev);
7546 tpacpi_sensors_pdev = NULL;
7547 printk(TPACPI_ERR
7548 "unable to register hwmon platform device\n");
7549 thinkpad_acpi_module_exit();
7550 return ret;
7552 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7553 &dev_attr_thinkpad_acpi_pdev_name);
7554 if (ret) {
7555 printk(TPACPI_ERR
7556 "unable to create sysfs hwmon device attributes\n");
7557 thinkpad_acpi_module_exit();
7558 return ret;
7560 tp_features.sensors_pdev_attrs_registered = 1;
7561 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7562 if (IS_ERR(tpacpi_hwmon)) {
7563 ret = PTR_ERR(tpacpi_hwmon);
7564 tpacpi_hwmon = NULL;
7565 printk(TPACPI_ERR "unable to register hwmon device\n");
7566 thinkpad_acpi_module_exit();
7567 return ret;
7569 mutex_init(&tpacpi_inputdev_send_mutex);
7570 tpacpi_inputdev = input_allocate_device();
7571 if (!tpacpi_inputdev) {
7572 printk(TPACPI_ERR "unable to allocate input device\n");
7573 thinkpad_acpi_module_exit();
7574 return -ENOMEM;
7575 } else {
7576 /* Prepare input device, but don't register */
7577 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7578 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7579 tpacpi_inputdev->id.bustype = BUS_HOST;
7580 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7581 thinkpad_id.vendor :
7582 PCI_VENDOR_ID_IBM;
7583 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7584 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7586 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7587 ret = ibm_init(&ibms_init[i]);
7588 if (ret >= 0 && *ibms_init[i].param)
7589 ret = ibms_init[i].data->write(ibms_init[i].param);
7590 if (ret < 0) {
7591 thinkpad_acpi_module_exit();
7592 return ret;
7595 ret = input_register_device(tpacpi_inputdev);
7596 if (ret < 0) {
7597 printk(TPACPI_ERR "unable to register input device\n");
7598 thinkpad_acpi_module_exit();
7599 return ret;
7600 } else {
7601 tp_features.input_device_registered = 1;
7604 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7605 return 0;
7608 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7611 * DMI matching for module autoloading
7613 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7614 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7616 * Only models listed in thinkwiki will be supported, so add yours
7617 * if it is not there yet.
7619 #define IBM_BIOS_MODULE_ALIAS(__type) \
7620 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7622 /* Non-ancient thinkpads */
7623 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7624 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7626 /* Ancient thinkpad BIOSes have to be identified by
7627 * BIOS type or model number, and there are far less
7628 * BIOS types than model numbers... */
7629 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7630 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7631 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7633 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7634 MODULE_DESCRIPTION(TPACPI_DESC);
7635 MODULE_VERSION(TPACPI_VERSION);
7636 MODULE_LICENSE("GPL");
7638 module_init(thinkpad_acpi_module_init);
7639 module_exit(thinkpad_acpi_module_exit);