thinkpad-acpi: add new debug helpers and warn of deprecated atts
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobb55f115bf6eb1396339400a85da7f17daa07fa86
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.22"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/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 acpi_driver_data(ibm->acpi->device) = 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 int hotkey_orig_status;
1434 static u32 hotkey_orig_mask;
1435 static u32 hotkey_all_mask;
1436 static u32 hotkey_reserved_mask;
1437 static u32 hotkey_mask;
1439 static unsigned int hotkey_report_mode;
1441 static u16 *hotkey_keycode_map;
1443 static struct attribute_set *hotkey_dev_attributes;
1445 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1446 #define HOTKEY_CONFIG_CRITICAL_START \
1447 do { \
1448 mutex_lock(&hotkey_thread_data_mutex); \
1449 hotkey_config_change++; \
1450 } while (0);
1451 #define HOTKEY_CONFIG_CRITICAL_END \
1452 mutex_unlock(&hotkey_thread_data_mutex);
1453 #else
1454 #define HOTKEY_CONFIG_CRITICAL_START
1455 #define HOTKEY_CONFIG_CRITICAL_END
1456 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1458 /* HKEY.MHKG() return bits */
1459 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1461 static int hotkey_get_wlsw(int *status)
1463 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1464 if (dbg_wlswemul) {
1465 *status = !!tpacpi_wlsw_emulstate;
1466 return 0;
1468 #endif
1469 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1470 return -EIO;
1471 return 0;
1474 static int hotkey_get_tablet_mode(int *status)
1476 int s;
1478 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1479 return -EIO;
1481 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1482 return 0;
1486 * Call with hotkey_mutex held
1488 static int hotkey_mask_get(void)
1490 u32 m = 0;
1492 if (tp_features.hotkey_mask) {
1493 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1494 return -EIO;
1496 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1498 return 0;
1502 * Call with hotkey_mutex held
1504 static int hotkey_mask_set(u32 mask)
1506 int i;
1507 int rc = 0;
1509 if (tp_features.hotkey_mask) {
1510 if (!tp_warned.hotkey_mask_ff &&
1511 (mask == 0xffff || mask == 0xffffff ||
1512 mask == 0xffffffff)) {
1513 tp_warned.hotkey_mask_ff = 1;
1514 printk(TPACPI_NOTICE
1515 "setting the hotkey mask to 0x%08x is likely "
1516 "not the best way to go about it\n", mask);
1517 printk(TPACPI_NOTICE
1518 "please consider using the driver defaults, "
1519 "and refer to up-to-date thinkpad-acpi "
1520 "documentation\n");
1523 HOTKEY_CONFIG_CRITICAL_START
1524 for (i = 0; i < 32; i++) {
1525 u32 m = 1 << i;
1526 /* enable in firmware mask only keys not in NVRAM
1527 * mode, but enable the key in the cached hotkey_mask
1528 * regardless of mode, or the key will end up
1529 * disabled by hotkey_mask_get() */
1530 if (!acpi_evalf(hkey_handle,
1531 NULL, "MHKM", "vdd", i + 1,
1532 !!((mask & ~hotkey_source_mask) & m))) {
1533 rc = -EIO;
1534 break;
1535 } else {
1536 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1539 HOTKEY_CONFIG_CRITICAL_END
1541 /* hotkey_mask_get must be called unconditionally below */
1542 if (!hotkey_mask_get() && !rc &&
1543 (hotkey_mask & ~hotkey_source_mask) !=
1544 (mask & ~hotkey_source_mask)) {
1545 printk(TPACPI_NOTICE
1546 "requested hot key mask 0x%08x, but "
1547 "firmware forced it to 0x%08x\n",
1548 mask, hotkey_mask);
1550 } else {
1551 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1552 HOTKEY_CONFIG_CRITICAL_START
1553 hotkey_mask = mask & hotkey_source_mask;
1554 HOTKEY_CONFIG_CRITICAL_END
1555 hotkey_mask_get();
1556 if (hotkey_mask != mask) {
1557 printk(TPACPI_NOTICE
1558 "requested hot key mask 0x%08x, "
1559 "forced to 0x%08x (NVRAM poll mask is "
1560 "0x%08x): no firmware mask support\n",
1561 mask, hotkey_mask, hotkey_source_mask);
1563 #else
1564 hotkey_mask_get();
1565 rc = -ENXIO;
1566 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1569 return rc;
1572 static int hotkey_status_get(int *status)
1574 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1575 return -EIO;
1577 return 0;
1580 static int hotkey_status_set(int status)
1582 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1583 return -EIO;
1585 return 0;
1588 static void tpacpi_input_send_tabletsw(void)
1590 int state;
1592 if (tp_features.hotkey_tablet &&
1593 !hotkey_get_tablet_mode(&state)) {
1594 mutex_lock(&tpacpi_inputdev_send_mutex);
1596 input_report_switch(tpacpi_inputdev,
1597 SW_TABLET_MODE, !!state);
1598 input_sync(tpacpi_inputdev);
1600 mutex_unlock(&tpacpi_inputdev_send_mutex);
1604 static void tpacpi_input_send_key(unsigned int scancode)
1606 unsigned int keycode;
1608 keycode = hotkey_keycode_map[scancode];
1610 if (keycode != KEY_RESERVED) {
1611 mutex_lock(&tpacpi_inputdev_send_mutex);
1613 input_report_key(tpacpi_inputdev, keycode, 1);
1614 if (keycode == KEY_UNKNOWN)
1615 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1616 scancode);
1617 input_sync(tpacpi_inputdev);
1619 input_report_key(tpacpi_inputdev, keycode, 0);
1620 if (keycode == KEY_UNKNOWN)
1621 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1622 scancode);
1623 input_sync(tpacpi_inputdev);
1625 mutex_unlock(&tpacpi_inputdev_send_mutex);
1629 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1630 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1632 static void tpacpi_hotkey_send_key(unsigned int scancode)
1634 tpacpi_input_send_key(scancode);
1635 if (hotkey_report_mode < 2) {
1636 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1637 0x80, 0x1001 + scancode);
1641 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1643 u8 d;
1645 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1646 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1647 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1648 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1649 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1650 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1652 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1653 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1654 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1656 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1657 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1658 n->displayexp_toggle =
1659 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1661 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1662 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1663 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1664 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1665 n->brightness_toggle =
1666 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1668 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1669 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1670 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1671 >> TP_NVRAM_POS_LEVEL_VOLUME;
1672 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1673 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1677 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1678 do { \
1679 if ((mask & (1 << __scancode)) && \
1680 oldn->__member != newn->__member) \
1681 tpacpi_hotkey_send_key(__scancode); \
1682 } while (0)
1684 #define TPACPI_MAY_SEND_KEY(__scancode) \
1685 do { if (mask & (1 << __scancode)) \
1686 tpacpi_hotkey_send_key(__scancode); } while (0)
1688 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1689 struct tp_nvram_state *newn,
1690 u32 mask)
1692 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1693 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1694 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1695 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1697 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1699 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1701 /* handle volume */
1702 if (oldn->volume_toggle != newn->volume_toggle) {
1703 if (oldn->mute != newn->mute) {
1704 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1706 if (oldn->volume_level > newn->volume_level) {
1707 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1708 } else if (oldn->volume_level < newn->volume_level) {
1709 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1710 } else if (oldn->mute == newn->mute) {
1711 /* repeated key presses that didn't change state */
1712 if (newn->mute) {
1713 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1714 } else if (newn->volume_level != 0) {
1715 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1716 } else {
1717 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1722 /* handle brightness */
1723 if (oldn->brightness_toggle != newn->brightness_toggle) {
1724 if (oldn->brightness_level < newn->brightness_level) {
1725 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1726 } else if (oldn->brightness_level > newn->brightness_level) {
1727 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1728 } else {
1729 /* repeated key presses that didn't change state */
1730 if (newn->brightness_level != 0) {
1731 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1732 } else {
1733 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1739 #undef TPACPI_COMPARE_KEY
1740 #undef TPACPI_MAY_SEND_KEY
1742 static int hotkey_kthread(void *data)
1744 struct tp_nvram_state s[2];
1745 u32 mask;
1746 unsigned int si, so;
1747 unsigned long t;
1748 unsigned int change_detector, must_reset;
1750 mutex_lock(&hotkey_thread_mutex);
1752 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1753 goto exit;
1755 set_freezable();
1757 so = 0;
1758 si = 1;
1759 t = 0;
1761 /* Initial state for compares */
1762 mutex_lock(&hotkey_thread_data_mutex);
1763 change_detector = hotkey_config_change;
1764 mask = hotkey_source_mask & hotkey_mask;
1765 mutex_unlock(&hotkey_thread_data_mutex);
1766 hotkey_read_nvram(&s[so], mask);
1768 while (!kthread_should_stop() && hotkey_poll_freq) {
1769 if (t == 0)
1770 t = 1000/hotkey_poll_freq;
1771 t = msleep_interruptible(t);
1772 if (unlikely(kthread_should_stop()))
1773 break;
1774 must_reset = try_to_freeze();
1775 if (t > 0 && !must_reset)
1776 continue;
1778 mutex_lock(&hotkey_thread_data_mutex);
1779 if (must_reset || hotkey_config_change != change_detector) {
1780 /* forget old state on thaw or config change */
1781 si = so;
1782 t = 0;
1783 change_detector = hotkey_config_change;
1785 mask = hotkey_source_mask & hotkey_mask;
1786 mutex_unlock(&hotkey_thread_data_mutex);
1788 if (likely(mask)) {
1789 hotkey_read_nvram(&s[si], mask);
1790 if (likely(si != so)) {
1791 hotkey_compare_and_issue_event(&s[so], &s[si],
1792 mask);
1796 so = si;
1797 si ^= 1;
1800 exit:
1801 mutex_unlock(&hotkey_thread_mutex);
1802 return 0;
1805 static void hotkey_poll_stop_sync(void)
1807 if (tpacpi_hotkey_task) {
1808 if (frozen(tpacpi_hotkey_task) ||
1809 freezing(tpacpi_hotkey_task))
1810 thaw_process(tpacpi_hotkey_task);
1812 kthread_stop(tpacpi_hotkey_task);
1813 tpacpi_hotkey_task = NULL;
1814 mutex_lock(&hotkey_thread_mutex);
1815 /* at this point, the thread did exit */
1816 mutex_unlock(&hotkey_thread_mutex);
1820 /* call with hotkey_mutex held */
1821 static void hotkey_poll_setup(int may_warn)
1823 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1824 hotkey_poll_freq > 0 &&
1825 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1826 if (!tpacpi_hotkey_task) {
1827 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1828 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1829 if (IS_ERR(tpacpi_hotkey_task)) {
1830 tpacpi_hotkey_task = NULL;
1831 printk(TPACPI_ERR
1832 "could not create kernel thread "
1833 "for hotkey polling\n");
1836 } else {
1837 hotkey_poll_stop_sync();
1838 if (may_warn &&
1839 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1840 printk(TPACPI_NOTICE
1841 "hot keys 0x%08x require polling, "
1842 "which is currently disabled\n",
1843 hotkey_source_mask);
1848 static void hotkey_poll_setup_safe(int may_warn)
1850 mutex_lock(&hotkey_mutex);
1851 hotkey_poll_setup(may_warn);
1852 mutex_unlock(&hotkey_mutex);
1855 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1857 static void hotkey_poll_setup_safe(int __unused)
1861 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1863 static int hotkey_inputdev_open(struct input_dev *dev)
1865 switch (tpacpi_lifecycle) {
1866 case TPACPI_LIFE_INIT:
1868 * hotkey_init will call hotkey_poll_setup_safe
1869 * at the appropriate moment
1871 return 0;
1872 case TPACPI_LIFE_EXITING:
1873 return -EBUSY;
1874 case TPACPI_LIFE_RUNNING:
1875 hotkey_poll_setup_safe(0);
1876 return 0;
1879 /* Should only happen if tpacpi_lifecycle is corrupt */
1880 BUG();
1881 return -EBUSY;
1884 static void hotkey_inputdev_close(struct input_dev *dev)
1886 /* disable hotkey polling when possible */
1887 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1888 hotkey_poll_setup_safe(0);
1891 /* sysfs hotkey enable ------------------------------------------------- */
1892 static ssize_t hotkey_enable_show(struct device *dev,
1893 struct device_attribute *attr,
1894 char *buf)
1896 int res, status;
1898 res = hotkey_status_get(&status);
1899 if (res)
1900 return res;
1902 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1905 static ssize_t hotkey_enable_store(struct device *dev,
1906 struct device_attribute *attr,
1907 const char *buf, size_t count)
1909 unsigned long t;
1910 int res;
1912 if (parse_strtoul(buf, 1, &t))
1913 return -EINVAL;
1915 res = hotkey_status_set(t);
1917 return (res) ? res : count;
1920 static struct device_attribute dev_attr_hotkey_enable =
1921 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1922 hotkey_enable_show, hotkey_enable_store);
1924 /* sysfs hotkey mask --------------------------------------------------- */
1925 static ssize_t hotkey_mask_show(struct device *dev,
1926 struct device_attribute *attr,
1927 char *buf)
1929 int res;
1931 if (mutex_lock_killable(&hotkey_mutex))
1932 return -ERESTARTSYS;
1933 res = hotkey_mask_get();
1934 mutex_unlock(&hotkey_mutex);
1936 return (res)?
1937 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1940 static ssize_t hotkey_mask_store(struct device *dev,
1941 struct device_attribute *attr,
1942 const char *buf, size_t count)
1944 unsigned long t;
1945 int res;
1947 if (parse_strtoul(buf, 0xffffffffUL, &t))
1948 return -EINVAL;
1950 if (mutex_lock_killable(&hotkey_mutex))
1951 return -ERESTARTSYS;
1953 res = hotkey_mask_set(t);
1955 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1956 hotkey_poll_setup(1);
1957 #endif
1959 mutex_unlock(&hotkey_mutex);
1961 return (res) ? res : count;
1964 static struct device_attribute dev_attr_hotkey_mask =
1965 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1966 hotkey_mask_show, hotkey_mask_store);
1968 /* sysfs hotkey bios_enabled ------------------------------------------- */
1969 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1970 struct device_attribute *attr,
1971 char *buf)
1973 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1976 static struct device_attribute dev_attr_hotkey_bios_enabled =
1977 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1979 /* sysfs hotkey bios_mask ---------------------------------------------- */
1980 static ssize_t hotkey_bios_mask_show(struct device *dev,
1981 struct device_attribute *attr,
1982 char *buf)
1984 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1987 static struct device_attribute dev_attr_hotkey_bios_mask =
1988 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1990 /* sysfs hotkey all_mask ----------------------------------------------- */
1991 static ssize_t hotkey_all_mask_show(struct device *dev,
1992 struct device_attribute *attr,
1993 char *buf)
1995 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1996 hotkey_all_mask | hotkey_source_mask);
1999 static struct device_attribute dev_attr_hotkey_all_mask =
2000 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
2002 /* sysfs hotkey recommended_mask --------------------------------------- */
2003 static ssize_t hotkey_recommended_mask_show(struct device *dev,
2004 struct device_attribute *attr,
2005 char *buf)
2007 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
2008 (hotkey_all_mask | hotkey_source_mask)
2009 & ~hotkey_reserved_mask);
2012 static struct device_attribute dev_attr_hotkey_recommended_mask =
2013 __ATTR(hotkey_recommended_mask, S_IRUGO,
2014 hotkey_recommended_mask_show, NULL);
2016 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2018 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
2019 static ssize_t hotkey_source_mask_show(struct device *dev,
2020 struct device_attribute *attr,
2021 char *buf)
2023 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
2026 static ssize_t hotkey_source_mask_store(struct device *dev,
2027 struct device_attribute *attr,
2028 const char *buf, size_t count)
2030 unsigned long t;
2032 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2033 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2034 return -EINVAL;
2036 if (mutex_lock_killable(&hotkey_mutex))
2037 return -ERESTARTSYS;
2039 HOTKEY_CONFIG_CRITICAL_START
2040 hotkey_source_mask = t;
2041 HOTKEY_CONFIG_CRITICAL_END
2043 hotkey_poll_setup(1);
2045 mutex_unlock(&hotkey_mutex);
2047 return count;
2050 static struct device_attribute dev_attr_hotkey_source_mask =
2051 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2052 hotkey_source_mask_show, hotkey_source_mask_store);
2054 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2055 static ssize_t hotkey_poll_freq_show(struct device *dev,
2056 struct device_attribute *attr,
2057 char *buf)
2059 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2062 static ssize_t hotkey_poll_freq_store(struct device *dev,
2063 struct device_attribute *attr,
2064 const char *buf, size_t count)
2066 unsigned long t;
2068 if (parse_strtoul(buf, 25, &t))
2069 return -EINVAL;
2071 if (mutex_lock_killable(&hotkey_mutex))
2072 return -ERESTARTSYS;
2074 hotkey_poll_freq = t;
2076 hotkey_poll_setup(1);
2077 mutex_unlock(&hotkey_mutex);
2079 return count;
2082 static struct device_attribute dev_attr_hotkey_poll_freq =
2083 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2084 hotkey_poll_freq_show, hotkey_poll_freq_store);
2086 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2088 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2089 static ssize_t hotkey_radio_sw_show(struct device *dev,
2090 struct device_attribute *attr,
2091 char *buf)
2093 int res, s;
2094 res = hotkey_get_wlsw(&s);
2095 if (res < 0)
2096 return res;
2098 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2101 static struct device_attribute dev_attr_hotkey_radio_sw =
2102 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2104 static void hotkey_radio_sw_notify_change(void)
2106 if (tp_features.hotkey_wlsw)
2107 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2108 "hotkey_radio_sw");
2111 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2112 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2113 struct device_attribute *attr,
2114 char *buf)
2116 int res, s;
2117 res = hotkey_get_tablet_mode(&s);
2118 if (res < 0)
2119 return res;
2121 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2124 static struct device_attribute dev_attr_hotkey_tablet_mode =
2125 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2127 static void hotkey_tablet_mode_notify_change(void)
2129 if (tp_features.hotkey_tablet)
2130 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2131 "hotkey_tablet_mode");
2134 /* sysfs hotkey report_mode -------------------------------------------- */
2135 static ssize_t hotkey_report_mode_show(struct device *dev,
2136 struct device_attribute *attr,
2137 char *buf)
2139 return snprintf(buf, PAGE_SIZE, "%d\n",
2140 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2143 static struct device_attribute dev_attr_hotkey_report_mode =
2144 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2146 /* sysfs wakeup reason (pollable) -------------------------------------- */
2147 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2148 struct device_attribute *attr,
2149 char *buf)
2151 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2154 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2155 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2157 static void hotkey_wakeup_reason_notify_change(void)
2159 if (tp_features.hotkey_mask)
2160 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2161 "wakeup_reason");
2164 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2165 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2166 struct device_attribute *attr,
2167 char *buf)
2169 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2172 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2173 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2174 hotkey_wakeup_hotunplug_complete_show, NULL);
2176 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2178 if (tp_features.hotkey_mask)
2179 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2180 "wakeup_hotunplug_complete");
2183 /* --------------------------------------------------------------------- */
2185 static struct attribute *hotkey_attributes[] __initdata = {
2186 &dev_attr_hotkey_enable.attr,
2187 &dev_attr_hotkey_bios_enabled.attr,
2188 &dev_attr_hotkey_report_mode.attr,
2189 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2190 &dev_attr_hotkey_mask.attr,
2191 &dev_attr_hotkey_all_mask.attr,
2192 &dev_attr_hotkey_recommended_mask.attr,
2193 &dev_attr_hotkey_source_mask.attr,
2194 &dev_attr_hotkey_poll_freq.attr,
2195 #endif
2198 static struct attribute *hotkey_mask_attributes[] __initdata = {
2199 &dev_attr_hotkey_bios_mask.attr,
2200 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2201 &dev_attr_hotkey_mask.attr,
2202 &dev_attr_hotkey_all_mask.attr,
2203 &dev_attr_hotkey_recommended_mask.attr,
2204 #endif
2205 &dev_attr_hotkey_wakeup_reason.attr,
2206 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2209 static void bluetooth_update_rfk(void);
2210 static void wan_update_rfk(void);
2211 static void uwb_update_rfk(void);
2212 static void tpacpi_send_radiosw_update(void)
2214 int wlsw;
2216 /* Sync these BEFORE sending any rfkill events */
2217 if (tp_features.bluetooth)
2218 bluetooth_update_rfk();
2219 if (tp_features.wan)
2220 wan_update_rfk();
2221 if (tp_features.uwb)
2222 uwb_update_rfk();
2224 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2225 mutex_lock(&tpacpi_inputdev_send_mutex);
2227 input_report_switch(tpacpi_inputdev,
2228 SW_RFKILL_ALL, !!wlsw);
2229 input_sync(tpacpi_inputdev);
2231 mutex_unlock(&tpacpi_inputdev_send_mutex);
2233 hotkey_radio_sw_notify_change();
2236 static void hotkey_exit(void)
2238 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2239 hotkey_poll_stop_sync();
2240 #endif
2242 if (hotkey_dev_attributes)
2243 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2245 kfree(hotkey_keycode_map);
2247 if (tp_features.hotkey) {
2248 dbg_printk(TPACPI_DBG_EXIT,
2249 "restoring original hot key mask\n");
2250 /* no short-circuit boolean operator below! */
2251 if ((hotkey_mask_set(hotkey_orig_mask) |
2252 hotkey_status_set(hotkey_orig_status)) != 0)
2253 printk(TPACPI_ERR
2254 "failed to restore hot key mask "
2255 "to BIOS defaults\n");
2259 static int __init hotkey_init(struct ibm_init_struct *iibm)
2261 /* Requirements for changing the default keymaps:
2263 * 1. Many of the keys are mapped to KEY_RESERVED for very
2264 * good reasons. Do not change them unless you have deep
2265 * knowledge on the IBM and Lenovo ThinkPad firmware for
2266 * the various ThinkPad models. The driver behaves
2267 * differently for KEY_RESERVED: such keys have their
2268 * hot key mask *unset* in mask_recommended, and also
2269 * in the initial hot key mask programmed into the
2270 * firmware at driver load time, which means the firm-
2271 * ware may react very differently if you change them to
2272 * something else;
2274 * 2. You must be subscribed to the linux-thinkpad and
2275 * ibm-acpi-devel mailing lists, and you should read the
2276 * list archives since 2007 if you want to change the
2277 * keymaps. This requirement exists so that you will
2278 * know the past history of problems with the thinkpad-
2279 * acpi driver keymaps, and also that you will be
2280 * listening to any bug reports;
2282 * 3. Do not send thinkpad-acpi specific patches directly to
2283 * for merging, *ever*. Send them to the linux-acpi
2284 * mailinglist for comments. Merging is to be done only
2285 * through acpi-test and the ACPI maintainer.
2287 * If the above is too much to ask, don't change the keymap.
2288 * Ask the thinkpad-acpi maintainer to do it, instead.
2290 static u16 ibm_keycode_map[] __initdata = {
2291 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2292 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2293 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2294 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2296 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2297 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2298 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2299 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2301 /* brightness: firmware always reacts to them, unless
2302 * X.org did some tricks in the radeon BIOS scratch
2303 * registers of *some* models */
2304 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2305 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2307 /* Thinklight: firmware always react to it */
2308 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2310 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2311 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2313 /* Volume: firmware always react to it and reprograms
2314 * the built-in *extra* mixer. Never map it to control
2315 * another mixer by default. */
2316 KEY_RESERVED, /* 0x14: VOLUME UP */
2317 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2318 KEY_RESERVED, /* 0x16: MUTE */
2320 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2322 /* (assignments unknown, please report if found) */
2323 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2324 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2326 static u16 lenovo_keycode_map[] __initdata = {
2327 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2328 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2329 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2330 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2332 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2333 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2334 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2335 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2337 /* These either have to go through ACPI video, or
2338 * act like in the IBM ThinkPads, so don't ever
2339 * enable them by default */
2340 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2341 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2343 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2345 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2346 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2348 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2349 * react to it and reprograms the built-in *extra* mixer.
2350 * Never map it to control another mixer by default.
2352 * T60?, T61, R60?, R61: firmware and EC tries to send
2353 * these over the regular keyboard, so these are no-ops,
2354 * but there are still weird bugs re. MUTE, so do not
2355 * change unless you get test reports from all Lenovo
2356 * models. May cause the BIOS to interfere with the
2357 * HDA mixer.
2359 KEY_RESERVED, /* 0x14: VOLUME UP */
2360 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2361 KEY_RESERVED, /* 0x16: MUTE */
2363 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2365 /* (assignments unknown, please report if found) */
2366 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2367 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2370 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2371 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2372 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2374 int res, i;
2375 int status;
2376 int hkeyv;
2378 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2380 BUG_ON(!tpacpi_inputdev);
2381 BUG_ON(tpacpi_inputdev->open != NULL ||
2382 tpacpi_inputdev->close != NULL);
2384 TPACPI_ACPIHANDLE_INIT(hkey);
2385 mutex_init(&hotkey_mutex);
2387 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2388 mutex_init(&hotkey_thread_mutex);
2389 mutex_init(&hotkey_thread_data_mutex);
2390 #endif
2392 /* hotkey not supported on 570 */
2393 tp_features.hotkey = hkey_handle != NULL;
2395 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2396 str_supported(tp_features.hotkey));
2398 if (!tp_features.hotkey)
2399 return 1;
2401 tpacpi_disable_brightness_delay();
2403 hotkey_dev_attributes = create_attr_set(13, NULL);
2404 if (!hotkey_dev_attributes)
2405 return -ENOMEM;
2406 res = add_many_to_attr_set(hotkey_dev_attributes,
2407 hotkey_attributes,
2408 ARRAY_SIZE(hotkey_attributes));
2409 if (res)
2410 goto err_exit;
2412 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2413 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2414 for HKEY interface version 0x100 */
2415 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2416 if ((hkeyv >> 8) != 1) {
2417 printk(TPACPI_ERR "unknown version of the "
2418 "HKEY interface: 0x%x\n", hkeyv);
2419 printk(TPACPI_ERR "please report this to %s\n",
2420 TPACPI_MAIL);
2421 } else {
2423 * MHKV 0x100 in A31, R40, R40e,
2424 * T4x, X31, and later
2426 tp_features.hotkey_mask = 1;
2430 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2431 str_supported(tp_features.hotkey_mask));
2433 if (tp_features.hotkey_mask) {
2434 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2435 "MHKA", "qd")) {
2436 printk(TPACPI_ERR
2437 "missing MHKA handler, "
2438 "please report this to %s\n",
2439 TPACPI_MAIL);
2440 /* FN+F12, FN+F4, FN+F3 */
2441 hotkey_all_mask = 0x080cU;
2445 /* hotkey_source_mask *must* be zero for
2446 * the first hotkey_mask_get */
2447 res = hotkey_status_get(&hotkey_orig_status);
2448 if (res)
2449 goto err_exit;
2451 if (tp_features.hotkey_mask) {
2452 res = hotkey_mask_get();
2453 if (res)
2454 goto err_exit;
2456 hotkey_orig_mask = hotkey_mask;
2457 res = add_many_to_attr_set(
2458 hotkey_dev_attributes,
2459 hotkey_mask_attributes,
2460 ARRAY_SIZE(hotkey_mask_attributes));
2461 if (res)
2462 goto err_exit;
2465 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2466 if (tp_features.hotkey_mask) {
2467 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2468 & ~hotkey_all_mask;
2469 } else {
2470 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2473 vdbg_printk(TPACPI_DBG_INIT,
2474 "hotkey source mask 0x%08x, polling freq %d\n",
2475 hotkey_source_mask, hotkey_poll_freq);
2476 #endif
2478 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2479 if (dbg_wlswemul) {
2480 tp_features.hotkey_wlsw = 1;
2481 printk(TPACPI_INFO
2482 "radio switch emulation enabled\n");
2483 } else
2484 #endif
2485 /* Not all thinkpads have a hardware radio switch */
2486 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2487 tp_features.hotkey_wlsw = 1;
2488 printk(TPACPI_INFO
2489 "radio switch found; radios are %s\n",
2490 enabled(status, 0));
2492 if (tp_features.hotkey_wlsw)
2493 res = add_to_attr_set(hotkey_dev_attributes,
2494 &dev_attr_hotkey_radio_sw.attr);
2496 /* For X41t, X60t, X61t Tablets... */
2497 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2498 tp_features.hotkey_tablet = 1;
2499 printk(TPACPI_INFO
2500 "possible tablet mode switch found; "
2501 "ThinkPad in %s mode\n",
2502 (status & TP_HOTKEY_TABLET_MASK)?
2503 "tablet" : "laptop");
2504 res = add_to_attr_set(hotkey_dev_attributes,
2505 &dev_attr_hotkey_tablet_mode.attr);
2508 if (!res)
2509 res = register_attr_set_with_sysfs(
2510 hotkey_dev_attributes,
2511 &tpacpi_pdev->dev.kobj);
2512 if (res)
2513 goto err_exit;
2515 /* Set up key map */
2517 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2518 GFP_KERNEL);
2519 if (!hotkey_keycode_map) {
2520 printk(TPACPI_ERR
2521 "failed to allocate memory for key map\n");
2522 res = -ENOMEM;
2523 goto err_exit;
2526 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2527 dbg_printk(TPACPI_DBG_INIT,
2528 "using Lenovo default hot key map\n");
2529 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2530 TPACPI_HOTKEY_MAP_SIZE);
2531 } else {
2532 dbg_printk(TPACPI_DBG_INIT,
2533 "using IBM default hot key map\n");
2534 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2535 TPACPI_HOTKEY_MAP_SIZE);
2538 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2539 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2540 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2541 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2542 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2543 tpacpi_inputdev->keycode = hotkey_keycode_map;
2544 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2545 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2546 set_bit(hotkey_keycode_map[i],
2547 tpacpi_inputdev->keybit);
2548 } else {
2549 if (i < sizeof(hotkey_reserved_mask)*8)
2550 hotkey_reserved_mask |= 1 << i;
2554 if (tp_features.hotkey_wlsw) {
2555 set_bit(EV_SW, tpacpi_inputdev->evbit);
2556 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2558 if (tp_features.hotkey_tablet) {
2559 set_bit(EV_SW, tpacpi_inputdev->evbit);
2560 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2563 /* Do not issue duplicate brightness change events to
2564 * userspace */
2565 if (!tp_features.bright_acpimode)
2566 /* update bright_acpimode... */
2567 tpacpi_check_std_acpi_brightness_support();
2569 if (tp_features.bright_acpimode) {
2570 printk(TPACPI_INFO
2571 "This ThinkPad has standard ACPI backlight "
2572 "brightness control, supported by the ACPI "
2573 "video driver\n");
2574 printk(TPACPI_NOTICE
2575 "Disabling thinkpad-acpi brightness events "
2576 "by default...\n");
2578 /* The hotkey_reserved_mask change below is not
2579 * necessary while the keys are at KEY_RESERVED in the
2580 * default map, but better safe than sorry, leave it
2581 * here as a marker of what we have to do, especially
2582 * when we finally become able to set this at runtime
2583 * on response to X.org requests */
2584 hotkey_reserved_mask |=
2585 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2586 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2589 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2590 res = hotkey_status_set(1);
2591 if (res) {
2592 hotkey_exit();
2593 return res;
2595 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2596 & ~hotkey_reserved_mask)
2597 | hotkey_orig_mask);
2598 if (res < 0 && res != -ENXIO) {
2599 hotkey_exit();
2600 return res;
2603 dbg_printk(TPACPI_DBG_INIT,
2604 "legacy hot key reporting over procfs %s\n",
2605 (hotkey_report_mode < 2) ?
2606 "enabled" : "disabled");
2608 tpacpi_inputdev->open = &hotkey_inputdev_open;
2609 tpacpi_inputdev->close = &hotkey_inputdev_close;
2611 hotkey_poll_setup_safe(1);
2612 tpacpi_send_radiosw_update();
2613 tpacpi_input_send_tabletsw();
2615 return 0;
2617 err_exit:
2618 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2619 hotkey_dev_attributes = NULL;
2621 return (res < 0)? res : 1;
2624 static bool hotkey_notify_hotkey(const u32 hkey,
2625 bool *send_acpi_ev,
2626 bool *ignore_acpi_ev)
2628 /* 0x1000-0x1FFF: key presses */
2629 unsigned int scancode = hkey & 0xfff;
2630 *send_acpi_ev = true;
2631 *ignore_acpi_ev = false;
2633 if (scancode > 0 && scancode < 0x21) {
2634 scancode--;
2635 if (!(hotkey_source_mask & (1 << scancode))) {
2636 tpacpi_input_send_key(scancode);
2637 *send_acpi_ev = false;
2638 } else {
2639 *ignore_acpi_ev = true;
2641 return true;
2643 return false;
2646 static bool hotkey_notify_wakeup(const u32 hkey,
2647 bool *send_acpi_ev,
2648 bool *ignore_acpi_ev)
2650 /* 0x2000-0x2FFF: Wakeup reason */
2651 *send_acpi_ev = true;
2652 *ignore_acpi_ev = false;
2654 switch (hkey) {
2655 case 0x2304: /* suspend, undock */
2656 case 0x2404: /* hibernation, undock */
2657 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2658 *ignore_acpi_ev = true;
2659 break;
2661 case 0x2305: /* suspend, bay eject */
2662 case 0x2405: /* hibernation, bay eject */
2663 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2664 *ignore_acpi_ev = true;
2665 break;
2667 case 0x2313: /* Battery on critical low level (S3) */
2668 case 0x2413: /* Battery on critical low level (S4) */
2669 printk(TPACPI_ALERT
2670 "EMERGENCY WAKEUP: battery almost empty\n");
2671 /* how to auto-heal: */
2672 /* 2313: woke up from S3, go to S4/S5 */
2673 /* 2413: woke up from S4, go to S5 */
2674 break;
2676 default:
2677 return false;
2680 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2681 printk(TPACPI_INFO
2682 "woke up due to a hot-unplug "
2683 "request...\n");
2684 hotkey_wakeup_reason_notify_change();
2686 return true;
2689 static bool hotkey_notify_usrevent(const u32 hkey,
2690 bool *send_acpi_ev,
2691 bool *ignore_acpi_ev)
2693 /* 0x5000-0x5FFF: human interface helpers */
2694 *send_acpi_ev = true;
2695 *ignore_acpi_ev = false;
2697 switch (hkey) {
2698 case 0x5010: /* Lenovo new BIOS: brightness changed */
2699 case 0x500b: /* X61t: tablet pen inserted into bay */
2700 case 0x500c: /* X61t: tablet pen removed from bay */
2701 return true;
2703 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2704 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2705 tpacpi_input_send_tabletsw();
2706 hotkey_tablet_mode_notify_change();
2707 *send_acpi_ev = false;
2708 return true;
2710 case 0x5001:
2711 case 0x5002:
2712 /* LID switch events. Do not propagate */
2713 *ignore_acpi_ev = true;
2714 return true;
2716 default:
2717 return false;
2721 static bool hotkey_notify_thermal(const u32 hkey,
2722 bool *send_acpi_ev,
2723 bool *ignore_acpi_ev)
2725 /* 0x6000-0x6FFF: thermal alarms */
2726 *send_acpi_ev = true;
2727 *ignore_acpi_ev = false;
2729 switch (hkey) {
2730 case 0x6011:
2731 printk(TPACPI_CRIT
2732 "THERMAL ALARM: battery is too hot!\n");
2733 /* recommended action: warn user through gui */
2734 return true;
2735 case 0x6012:
2736 printk(TPACPI_ALERT
2737 "THERMAL EMERGENCY: battery is extremely hot!\n");
2738 /* recommended action: immediate sleep/hibernate */
2739 return true;
2740 case 0x6021:
2741 printk(TPACPI_CRIT
2742 "THERMAL ALARM: "
2743 "a sensor reports something is too hot!\n");
2744 /* recommended action: warn user through gui, that */
2745 /* some internal component is too hot */
2746 return true;
2747 case 0x6022:
2748 printk(TPACPI_ALERT
2749 "THERMAL EMERGENCY: "
2750 "a sensor reports something is extremely hot!\n");
2751 /* recommended action: immediate sleep/hibernate */
2752 return true;
2753 case 0x6030:
2754 printk(TPACPI_INFO
2755 "EC reports that Thermal Table has changed\n");
2756 /* recommended action: do nothing, we don't have
2757 * Lenovo ATM information */
2758 return true;
2759 default:
2760 printk(TPACPI_ALERT
2761 "THERMAL ALERT: unknown thermal alarm received\n");
2762 return false;
2766 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2768 u32 hkey;
2769 bool send_acpi_ev;
2770 bool ignore_acpi_ev;
2771 bool known_ev;
2773 if (event != 0x80) {
2774 printk(TPACPI_ERR
2775 "unknown HKEY notification event %d\n", event);
2776 /* forward it to userspace, maybe it knows how to handle it */
2777 acpi_bus_generate_netlink_event(
2778 ibm->acpi->device->pnp.device_class,
2779 dev_name(&ibm->acpi->device->dev),
2780 event, 0);
2781 return;
2784 while (1) {
2785 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2786 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2787 return;
2790 if (hkey == 0) {
2791 /* queue empty */
2792 return;
2795 send_acpi_ev = true;
2796 ignore_acpi_ev = false;
2798 switch (hkey >> 12) {
2799 case 1:
2800 /* 0x1000-0x1FFF: key presses */
2801 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2802 &ignore_acpi_ev);
2803 break;
2804 case 2:
2805 /* 0x2000-0x2FFF: Wakeup reason */
2806 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2807 &ignore_acpi_ev);
2808 break;
2809 case 3:
2810 /* 0x3000-0x3FFF: bay-related wakeups */
2811 if (hkey == 0x3003) {
2812 hotkey_autosleep_ack = 1;
2813 printk(TPACPI_INFO
2814 "bay ejected\n");
2815 hotkey_wakeup_hotunplug_complete_notify_change();
2816 known_ev = true;
2817 } else {
2818 known_ev = false;
2820 break;
2821 case 4:
2822 /* 0x4000-0x4FFF: dock-related wakeups */
2823 if (hkey == 0x4003) {
2824 hotkey_autosleep_ack = 1;
2825 printk(TPACPI_INFO
2826 "undocked\n");
2827 hotkey_wakeup_hotunplug_complete_notify_change();
2828 known_ev = true;
2829 } else {
2830 known_ev = false;
2832 break;
2833 case 5:
2834 /* 0x5000-0x5FFF: human interface helpers */
2835 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2836 &ignore_acpi_ev);
2837 break;
2838 case 6:
2839 /* 0x6000-0x6FFF: thermal alarms */
2840 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2841 &ignore_acpi_ev);
2842 break;
2843 case 7:
2844 /* 0x7000-0x7FFF: misc */
2845 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2846 tpacpi_send_radiosw_update();
2847 send_acpi_ev = 0;
2848 known_ev = true;
2849 break;
2851 /* fallthrough to default */
2852 default:
2853 known_ev = false;
2855 if (!known_ev) {
2856 printk(TPACPI_NOTICE
2857 "unhandled HKEY event 0x%04x\n", hkey);
2858 printk(TPACPI_NOTICE
2859 "please report the conditions when this "
2860 "event happened to %s\n", TPACPI_MAIL);
2863 /* Legacy events */
2864 if (!ignore_acpi_ev &&
2865 (send_acpi_ev || hotkey_report_mode < 2)) {
2866 acpi_bus_generate_proc_event(ibm->acpi->device,
2867 event, hkey);
2870 /* netlink events */
2871 if (!ignore_acpi_ev && send_acpi_ev) {
2872 acpi_bus_generate_netlink_event(
2873 ibm->acpi->device->pnp.device_class,
2874 dev_name(&ibm->acpi->device->dev),
2875 event, hkey);
2880 static void hotkey_suspend(pm_message_t state)
2882 /* Do these on suspend, we get the events on early resume! */
2883 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2884 hotkey_autosleep_ack = 0;
2887 static void hotkey_resume(void)
2889 tpacpi_disable_brightness_delay();
2891 if (hotkey_mask_get())
2892 printk(TPACPI_ERR
2893 "error while trying to read hot key mask "
2894 "from firmware\n");
2895 tpacpi_send_radiosw_update();
2896 hotkey_tablet_mode_notify_change();
2897 hotkey_wakeup_reason_notify_change();
2898 hotkey_wakeup_hotunplug_complete_notify_change();
2899 hotkey_poll_setup_safe(0);
2902 /* procfs -------------------------------------------------------------- */
2903 static int hotkey_read(char *p)
2905 int res, status;
2906 int len = 0;
2908 if (!tp_features.hotkey) {
2909 len += sprintf(p + len, "status:\t\tnot supported\n");
2910 return len;
2913 if (mutex_lock_killable(&hotkey_mutex))
2914 return -ERESTARTSYS;
2915 res = hotkey_status_get(&status);
2916 if (!res)
2917 res = hotkey_mask_get();
2918 mutex_unlock(&hotkey_mutex);
2919 if (res)
2920 return res;
2922 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2923 if (tp_features.hotkey_mask) {
2924 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2925 len += sprintf(p + len,
2926 "commands:\tenable, disable, reset, <mask>\n");
2927 } else {
2928 len += sprintf(p + len, "mask:\t\tnot supported\n");
2929 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2932 return len;
2935 static int hotkey_write(char *buf)
2937 int res, status;
2938 u32 mask;
2939 char *cmd;
2941 if (!tp_features.hotkey)
2942 return -ENODEV;
2944 if (mutex_lock_killable(&hotkey_mutex))
2945 return -ERESTARTSYS;
2947 status = -1;
2948 mask = hotkey_mask;
2950 res = 0;
2951 while ((cmd = next_cmd(&buf))) {
2952 if (strlencmp(cmd, "enable") == 0) {
2953 status = 1;
2954 } else if (strlencmp(cmd, "disable") == 0) {
2955 status = 0;
2956 } else if (strlencmp(cmd, "reset") == 0) {
2957 status = hotkey_orig_status;
2958 mask = hotkey_orig_mask;
2959 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2960 /* mask set */
2961 } else if (sscanf(cmd, "%x", &mask) == 1) {
2962 /* mask set */
2963 } else {
2964 res = -EINVAL;
2965 goto errexit;
2968 if (status != -1)
2969 res = hotkey_status_set(status);
2971 if (!res && mask != hotkey_mask)
2972 res = hotkey_mask_set(mask);
2974 errexit:
2975 mutex_unlock(&hotkey_mutex);
2976 return res;
2979 static const struct acpi_device_id ibm_htk_device_ids[] = {
2980 {TPACPI_ACPI_HKEY_HID, 0},
2981 {"", 0},
2984 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2985 .hid = ibm_htk_device_ids,
2986 .notify = hotkey_notify,
2987 .handle = &hkey_handle,
2988 .type = ACPI_DEVICE_NOTIFY,
2991 static struct ibm_struct hotkey_driver_data = {
2992 .name = "hotkey",
2993 .read = hotkey_read,
2994 .write = hotkey_write,
2995 .exit = hotkey_exit,
2996 .resume = hotkey_resume,
2997 .suspend = hotkey_suspend,
2998 .acpi = &ibm_hotkey_acpidriver,
3001 /*************************************************************************
3002 * Bluetooth subdriver
3005 enum {
3006 /* ACPI GBDC/SBDC bits */
3007 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
3008 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
3009 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
3010 off / last state */
3013 enum {
3014 /* ACPI \BLTH commands */
3015 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
3016 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
3017 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
3018 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
3019 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
3022 static struct rfkill *tpacpi_bluetooth_rfkill;
3024 static void bluetooth_suspend(pm_message_t state)
3026 /* Try to make sure radio will resume powered off */
3027 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3028 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
3031 static int bluetooth_get_radiosw(void)
3033 int status;
3035 if (!tp_features.bluetooth)
3036 return -ENODEV;
3038 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3039 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3040 return RFKILL_STATE_HARD_BLOCKED;
3042 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3043 if (dbg_bluetoothemul)
3044 return (tpacpi_bluetooth_emulstate) ?
3045 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3046 #endif
3048 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3049 return -EIO;
3051 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3052 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3055 static void bluetooth_update_rfk(void)
3057 int status;
3059 if (!tpacpi_bluetooth_rfkill)
3060 return;
3062 status = bluetooth_get_radiosw();
3063 if (status < 0)
3064 return;
3065 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3068 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3070 int status;
3072 if (!tp_features.bluetooth)
3073 return -ENODEV;
3075 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3076 * reason to risk weird behaviour. */
3077 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3078 && radio_on)
3079 return -EPERM;
3081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3082 if (dbg_bluetoothemul) {
3083 tpacpi_bluetooth_emulstate = !!radio_on;
3084 if (update_rfk)
3085 bluetooth_update_rfk();
3086 return 0;
3088 #endif
3090 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3091 if (radio_on)
3092 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3093 else
3094 status = 0;
3095 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3096 return -EIO;
3098 if (update_rfk)
3099 bluetooth_update_rfk();
3101 return 0;
3104 /* sysfs bluetooth enable ---------------------------------------------- */
3105 static ssize_t bluetooth_enable_show(struct device *dev,
3106 struct device_attribute *attr,
3107 char *buf)
3109 int status;
3111 printk_deprecated_rfkill_attribute("bluetooth_enable");
3113 status = bluetooth_get_radiosw();
3114 if (status < 0)
3115 return status;
3117 return snprintf(buf, PAGE_SIZE, "%d\n",
3118 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3121 static ssize_t bluetooth_enable_store(struct device *dev,
3122 struct device_attribute *attr,
3123 const char *buf, size_t count)
3125 unsigned long t;
3126 int res;
3128 printk_deprecated_rfkill_attribute("bluetooth_enable");
3130 if (parse_strtoul(buf, 1, &t))
3131 return -EINVAL;
3133 res = bluetooth_set_radiosw(t, 1);
3135 return (res) ? res : count;
3138 static struct device_attribute dev_attr_bluetooth_enable =
3139 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3140 bluetooth_enable_show, bluetooth_enable_store);
3142 /* --------------------------------------------------------------------- */
3144 static struct attribute *bluetooth_attributes[] = {
3145 &dev_attr_bluetooth_enable.attr,
3146 NULL
3149 static const struct attribute_group bluetooth_attr_group = {
3150 .attrs = bluetooth_attributes,
3153 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3155 int bts = bluetooth_get_radiosw();
3157 if (bts < 0)
3158 return bts;
3160 *state = bts;
3161 return 0;
3164 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3166 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3169 static void bluetooth_shutdown(void)
3171 /* Order firmware to save current state to NVRAM */
3172 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3173 TP_ACPI_BLTH_SAVE_STATE))
3174 printk(TPACPI_NOTICE
3175 "failed to save bluetooth state to NVRAM\n");
3178 static void bluetooth_exit(void)
3180 bluetooth_shutdown();
3182 if (tpacpi_bluetooth_rfkill)
3183 rfkill_unregister(tpacpi_bluetooth_rfkill);
3185 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3186 &bluetooth_attr_group);
3189 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3191 int res;
3192 int status = 0;
3194 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3196 TPACPI_ACPIHANDLE_INIT(hkey);
3198 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3199 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3200 tp_features.bluetooth = hkey_handle &&
3201 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3203 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3204 str_supported(tp_features.bluetooth),
3205 status);
3207 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3208 if (dbg_bluetoothemul) {
3209 tp_features.bluetooth = 1;
3210 printk(TPACPI_INFO
3211 "bluetooth switch emulation enabled\n");
3212 } else
3213 #endif
3214 if (tp_features.bluetooth &&
3215 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3216 /* no bluetooth hardware present in system */
3217 tp_features.bluetooth = 0;
3218 dbg_printk(TPACPI_DBG_INIT,
3219 "bluetooth hardware not installed\n");
3222 if (!tp_features.bluetooth)
3223 return 1;
3225 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3226 &bluetooth_attr_group);
3227 if (res)
3228 return res;
3230 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3231 &tpacpi_bluetooth_rfkill,
3232 RFKILL_TYPE_BLUETOOTH,
3233 "tpacpi_bluetooth_sw",
3234 true,
3235 tpacpi_bluetooth_rfk_set,
3236 tpacpi_bluetooth_rfk_get);
3237 if (res) {
3238 bluetooth_exit();
3239 return res;
3242 return 0;
3245 /* procfs -------------------------------------------------------------- */
3246 static int bluetooth_read(char *p)
3248 int len = 0;
3249 int status = bluetooth_get_radiosw();
3251 if (!tp_features.bluetooth)
3252 len += sprintf(p + len, "status:\t\tnot supported\n");
3253 else {
3254 len += sprintf(p + len, "status:\t\t%s\n",
3255 (status == RFKILL_STATE_UNBLOCKED) ?
3256 "enabled" : "disabled");
3257 len += sprintf(p + len, "commands:\tenable, disable\n");
3260 return len;
3263 static int bluetooth_write(char *buf)
3265 char *cmd;
3267 if (!tp_features.bluetooth)
3268 return -ENODEV;
3270 while ((cmd = next_cmd(&buf))) {
3271 if (strlencmp(cmd, "enable") == 0) {
3272 bluetooth_set_radiosw(1, 1);
3273 } else if (strlencmp(cmd, "disable") == 0) {
3274 bluetooth_set_radiosw(0, 1);
3275 } else
3276 return -EINVAL;
3279 return 0;
3282 static struct ibm_struct bluetooth_driver_data = {
3283 .name = "bluetooth",
3284 .read = bluetooth_read,
3285 .write = bluetooth_write,
3286 .exit = bluetooth_exit,
3287 .suspend = bluetooth_suspend,
3288 .shutdown = bluetooth_shutdown,
3291 /*************************************************************************
3292 * Wan subdriver
3295 enum {
3296 /* ACPI GWAN/SWAN bits */
3297 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3298 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3299 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3300 off / last state */
3303 static struct rfkill *tpacpi_wan_rfkill;
3305 static void wan_suspend(pm_message_t state)
3307 /* Try to make sure radio will resume powered off */
3308 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3309 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3312 static int wan_get_radiosw(void)
3314 int status;
3316 if (!tp_features.wan)
3317 return -ENODEV;
3319 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3320 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3321 return RFKILL_STATE_HARD_BLOCKED;
3323 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3324 if (dbg_wwanemul)
3325 return (tpacpi_wwan_emulstate) ?
3326 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3327 #endif
3329 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3330 return -EIO;
3332 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3333 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3336 static void wan_update_rfk(void)
3338 int status;
3340 if (!tpacpi_wan_rfkill)
3341 return;
3343 status = wan_get_radiosw();
3344 if (status < 0)
3345 return;
3346 rfkill_force_state(tpacpi_wan_rfkill, status);
3349 static int wan_set_radiosw(int radio_on, int update_rfk)
3351 int status;
3353 if (!tp_features.wan)
3354 return -ENODEV;
3356 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3357 * reason to risk weird behaviour. */
3358 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3359 && radio_on)
3360 return -EPERM;
3362 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3363 if (dbg_wwanemul) {
3364 tpacpi_wwan_emulstate = !!radio_on;
3365 if (update_rfk)
3366 wan_update_rfk();
3367 return 0;
3369 #endif
3371 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3372 if (radio_on)
3373 status = TP_ACPI_WANCARD_RADIOSSW;
3374 else
3375 status = 0;
3376 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3377 return -EIO;
3379 if (update_rfk)
3380 wan_update_rfk();
3382 return 0;
3385 /* sysfs wan enable ---------------------------------------------------- */
3386 static ssize_t wan_enable_show(struct device *dev,
3387 struct device_attribute *attr,
3388 char *buf)
3390 int status;
3392 printk_deprecated_rfkill_attribute("wwan_enable");
3394 status = wan_get_radiosw();
3395 if (status < 0)
3396 return status;
3398 return snprintf(buf, PAGE_SIZE, "%d\n",
3399 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3402 static ssize_t wan_enable_store(struct device *dev,
3403 struct device_attribute *attr,
3404 const char *buf, size_t count)
3406 unsigned long t;
3407 int res;
3409 printk_deprecated_rfkill_attribute("wwan_enable");
3411 if (parse_strtoul(buf, 1, &t))
3412 return -EINVAL;
3414 res = wan_set_radiosw(t, 1);
3416 return (res) ? res : count;
3419 static struct device_attribute dev_attr_wan_enable =
3420 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3421 wan_enable_show, wan_enable_store);
3423 /* --------------------------------------------------------------------- */
3425 static struct attribute *wan_attributes[] = {
3426 &dev_attr_wan_enable.attr,
3427 NULL
3430 static const struct attribute_group wan_attr_group = {
3431 .attrs = wan_attributes,
3434 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3436 int wans = wan_get_radiosw();
3438 if (wans < 0)
3439 return wans;
3441 *state = wans;
3442 return 0;
3445 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3447 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3450 static void wan_shutdown(void)
3452 /* Order firmware to save current state to NVRAM */
3453 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3454 TP_ACPI_WGSV_SAVE_STATE))
3455 printk(TPACPI_NOTICE
3456 "failed to save WWAN state to NVRAM\n");
3459 static void wan_exit(void)
3461 wan_shutdown();
3463 if (tpacpi_wan_rfkill)
3464 rfkill_unregister(tpacpi_wan_rfkill);
3466 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3467 &wan_attr_group);
3470 static int __init wan_init(struct ibm_init_struct *iibm)
3472 int res;
3473 int status = 0;
3475 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3477 TPACPI_ACPIHANDLE_INIT(hkey);
3479 tp_features.wan = hkey_handle &&
3480 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3482 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3483 str_supported(tp_features.wan),
3484 status);
3486 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3487 if (dbg_wwanemul) {
3488 tp_features.wan = 1;
3489 printk(TPACPI_INFO
3490 "wwan switch emulation enabled\n");
3491 } else
3492 #endif
3493 if (tp_features.wan &&
3494 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3495 /* no wan hardware present in system */
3496 tp_features.wan = 0;
3497 dbg_printk(TPACPI_DBG_INIT,
3498 "wan hardware not installed\n");
3501 if (!tp_features.wan)
3502 return 1;
3504 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3505 &wan_attr_group);
3506 if (res)
3507 return res;
3509 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3510 &tpacpi_wan_rfkill,
3511 RFKILL_TYPE_WWAN,
3512 "tpacpi_wwan_sw",
3513 true,
3514 tpacpi_wan_rfk_set,
3515 tpacpi_wan_rfk_get);
3516 if (res) {
3517 wan_exit();
3518 return res;
3521 return 0;
3524 /* procfs -------------------------------------------------------------- */
3525 static int wan_read(char *p)
3527 int len = 0;
3528 int status = wan_get_radiosw();
3530 if (!tp_features.wan)
3531 len += sprintf(p + len, "status:\t\tnot supported\n");
3532 else {
3533 len += sprintf(p + len, "status:\t\t%s\n",
3534 (status == RFKILL_STATE_UNBLOCKED) ?
3535 "enabled" : "disabled");
3536 len += sprintf(p + len, "commands:\tenable, disable\n");
3539 return len;
3542 static int wan_write(char *buf)
3544 char *cmd;
3546 if (!tp_features.wan)
3547 return -ENODEV;
3549 while ((cmd = next_cmd(&buf))) {
3550 if (strlencmp(cmd, "enable") == 0) {
3551 wan_set_radiosw(1, 1);
3552 } else if (strlencmp(cmd, "disable") == 0) {
3553 wan_set_radiosw(0, 1);
3554 } else
3555 return -EINVAL;
3558 return 0;
3561 static struct ibm_struct wan_driver_data = {
3562 .name = "wan",
3563 .read = wan_read,
3564 .write = wan_write,
3565 .exit = wan_exit,
3566 .suspend = wan_suspend,
3567 .shutdown = wan_shutdown,
3570 /*************************************************************************
3571 * UWB subdriver
3574 enum {
3575 /* ACPI GUWB/SUWB bits */
3576 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3577 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3580 static struct rfkill *tpacpi_uwb_rfkill;
3582 static int uwb_get_radiosw(void)
3584 int status;
3586 if (!tp_features.uwb)
3587 return -ENODEV;
3589 /* WLSW overrides UWB in firmware/hardware, reflect that */
3590 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3591 return RFKILL_STATE_HARD_BLOCKED;
3593 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3594 if (dbg_uwbemul)
3595 return (tpacpi_uwb_emulstate) ?
3596 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3597 #endif
3599 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3600 return -EIO;
3602 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3603 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3606 static void uwb_update_rfk(void)
3608 int status;
3610 if (!tpacpi_uwb_rfkill)
3611 return;
3613 status = uwb_get_radiosw();
3614 if (status < 0)
3615 return;
3616 rfkill_force_state(tpacpi_uwb_rfkill, status);
3619 static int uwb_set_radiosw(int radio_on, int update_rfk)
3621 int status;
3623 if (!tp_features.uwb)
3624 return -ENODEV;
3626 /* WLSW overrides UWB in firmware/hardware, but there is no
3627 * reason to risk weird behaviour. */
3628 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3629 && radio_on)
3630 return -EPERM;
3632 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3633 if (dbg_uwbemul) {
3634 tpacpi_uwb_emulstate = !!radio_on;
3635 if (update_rfk)
3636 uwb_update_rfk();
3637 return 0;
3639 #endif
3641 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3642 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3643 return -EIO;
3645 if (update_rfk)
3646 uwb_update_rfk();
3648 return 0;
3651 /* --------------------------------------------------------------------- */
3653 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3655 int uwbs = uwb_get_radiosw();
3657 if (uwbs < 0)
3658 return uwbs;
3660 *state = uwbs;
3661 return 0;
3664 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3666 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3669 static void uwb_exit(void)
3671 if (tpacpi_uwb_rfkill)
3672 rfkill_unregister(tpacpi_uwb_rfkill);
3675 static int __init uwb_init(struct ibm_init_struct *iibm)
3677 int res;
3678 int status = 0;
3680 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3682 TPACPI_ACPIHANDLE_INIT(hkey);
3684 tp_features.uwb = hkey_handle &&
3685 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3687 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3688 str_supported(tp_features.uwb),
3689 status);
3691 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3692 if (dbg_uwbemul) {
3693 tp_features.uwb = 1;
3694 printk(TPACPI_INFO
3695 "uwb switch emulation enabled\n");
3696 } else
3697 #endif
3698 if (tp_features.uwb &&
3699 !(status & TP_ACPI_UWB_HWPRESENT)) {
3700 /* no uwb hardware present in system */
3701 tp_features.uwb = 0;
3702 dbg_printk(TPACPI_DBG_INIT,
3703 "uwb hardware not installed\n");
3706 if (!tp_features.uwb)
3707 return 1;
3709 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3710 &tpacpi_uwb_rfkill,
3711 RFKILL_TYPE_UWB,
3712 "tpacpi_uwb_sw",
3713 false,
3714 tpacpi_uwb_rfk_set,
3715 tpacpi_uwb_rfk_get);
3717 return res;
3720 static struct ibm_struct uwb_driver_data = {
3721 .name = "uwb",
3722 .exit = uwb_exit,
3723 .flags.experimental = 1,
3726 /*************************************************************************
3727 * Video subdriver
3730 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3732 enum video_access_mode {
3733 TPACPI_VIDEO_NONE = 0,
3734 TPACPI_VIDEO_570, /* 570 */
3735 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3736 TPACPI_VIDEO_NEW, /* all others */
3739 enum { /* video status flags, based on VIDEO_570 */
3740 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3741 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3742 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3745 enum { /* TPACPI_VIDEO_570 constants */
3746 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3747 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3748 * video_status_flags */
3749 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3750 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3753 static enum video_access_mode video_supported;
3754 static int video_orig_autosw;
3756 static int video_autosw_get(void);
3757 static int video_autosw_set(int enable);
3759 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3761 static int __init video_init(struct ibm_init_struct *iibm)
3763 int ivga;
3765 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3767 TPACPI_ACPIHANDLE_INIT(vid);
3768 TPACPI_ACPIHANDLE_INIT(vid2);
3770 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3771 /* G41, assume IVGA doesn't change */
3772 vid_handle = vid2_handle;
3774 if (!vid_handle)
3775 /* video switching not supported on R30, R31 */
3776 video_supported = TPACPI_VIDEO_NONE;
3777 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3778 /* 570 */
3779 video_supported = TPACPI_VIDEO_570;
3780 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3781 /* 600e/x, 770e, 770x */
3782 video_supported = TPACPI_VIDEO_770;
3783 else
3784 /* all others */
3785 video_supported = TPACPI_VIDEO_NEW;
3787 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3788 str_supported(video_supported != TPACPI_VIDEO_NONE),
3789 video_supported);
3791 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3794 static void video_exit(void)
3796 dbg_printk(TPACPI_DBG_EXIT,
3797 "restoring original video autoswitch mode\n");
3798 if (video_autosw_set(video_orig_autosw))
3799 printk(TPACPI_ERR "error while trying to restore original "
3800 "video autoswitch mode\n");
3803 static int video_outputsw_get(void)
3805 int status = 0;
3806 int i;
3808 switch (video_supported) {
3809 case TPACPI_VIDEO_570:
3810 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3811 TP_ACPI_VIDEO_570_PHSCMD))
3812 return -EIO;
3813 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3814 break;
3815 case TPACPI_VIDEO_770:
3816 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3817 return -EIO;
3818 if (i)
3819 status |= TP_ACPI_VIDEO_S_LCD;
3820 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3821 return -EIO;
3822 if (i)
3823 status |= TP_ACPI_VIDEO_S_CRT;
3824 break;
3825 case TPACPI_VIDEO_NEW:
3826 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3827 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3828 return -EIO;
3829 if (i)
3830 status |= TP_ACPI_VIDEO_S_CRT;
3832 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3833 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3834 return -EIO;
3835 if (i)
3836 status |= TP_ACPI_VIDEO_S_LCD;
3837 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3838 return -EIO;
3839 if (i)
3840 status |= TP_ACPI_VIDEO_S_DVI;
3841 break;
3842 default:
3843 return -ENOSYS;
3846 return status;
3849 static int video_outputsw_set(int status)
3851 int autosw;
3852 int res = 0;
3854 switch (video_supported) {
3855 case TPACPI_VIDEO_570:
3856 res = acpi_evalf(NULL, NULL,
3857 "\\_SB.PHS2", "vdd",
3858 TP_ACPI_VIDEO_570_PHS2CMD,
3859 status | TP_ACPI_VIDEO_570_PHS2SET);
3860 break;
3861 case TPACPI_VIDEO_770:
3862 autosw = video_autosw_get();
3863 if (autosw < 0)
3864 return autosw;
3866 res = video_autosw_set(1);
3867 if (res)
3868 return res;
3869 res = acpi_evalf(vid_handle, NULL,
3870 "ASWT", "vdd", status * 0x100, 0);
3871 if (!autosw && video_autosw_set(autosw)) {
3872 printk(TPACPI_ERR
3873 "video auto-switch left enabled due to error\n");
3874 return -EIO;
3876 break;
3877 case TPACPI_VIDEO_NEW:
3878 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3879 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3880 break;
3881 default:
3882 return -ENOSYS;
3885 return (res)? 0 : -EIO;
3888 static int video_autosw_get(void)
3890 int autosw = 0;
3892 switch (video_supported) {
3893 case TPACPI_VIDEO_570:
3894 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3895 return -EIO;
3896 break;
3897 case TPACPI_VIDEO_770:
3898 case TPACPI_VIDEO_NEW:
3899 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3900 return -EIO;
3901 break;
3902 default:
3903 return -ENOSYS;
3906 return autosw & 1;
3909 static int video_autosw_set(int enable)
3911 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3912 return -EIO;
3913 return 0;
3916 static int video_outputsw_cycle(void)
3918 int autosw = video_autosw_get();
3919 int res;
3921 if (autosw < 0)
3922 return autosw;
3924 switch (video_supported) {
3925 case TPACPI_VIDEO_570:
3926 res = video_autosw_set(1);
3927 if (res)
3928 return res;
3929 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3930 break;
3931 case TPACPI_VIDEO_770:
3932 case TPACPI_VIDEO_NEW:
3933 res = video_autosw_set(1);
3934 if (res)
3935 return res;
3936 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3937 break;
3938 default:
3939 return -ENOSYS;
3941 if (!autosw && video_autosw_set(autosw)) {
3942 printk(TPACPI_ERR
3943 "video auto-switch left enabled due to error\n");
3944 return -EIO;
3947 return (res)? 0 : -EIO;
3950 static int video_expand_toggle(void)
3952 switch (video_supported) {
3953 case TPACPI_VIDEO_570:
3954 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3955 0 : -EIO;
3956 case TPACPI_VIDEO_770:
3957 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3958 0 : -EIO;
3959 case TPACPI_VIDEO_NEW:
3960 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3961 0 : -EIO;
3962 default:
3963 return -ENOSYS;
3965 /* not reached */
3968 static int video_read(char *p)
3970 int status, autosw;
3971 int len = 0;
3973 if (video_supported == TPACPI_VIDEO_NONE) {
3974 len += sprintf(p + len, "status:\t\tnot supported\n");
3975 return len;
3978 status = video_outputsw_get();
3979 if (status < 0)
3980 return status;
3982 autosw = video_autosw_get();
3983 if (autosw < 0)
3984 return autosw;
3986 len += sprintf(p + len, "status:\t\tsupported\n");
3987 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3988 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3989 if (video_supported == TPACPI_VIDEO_NEW)
3990 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3991 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3992 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3993 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3994 if (video_supported == TPACPI_VIDEO_NEW)
3995 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3996 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3997 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3999 return len;
4002 static int video_write(char *buf)
4004 char *cmd;
4005 int enable, disable, status;
4006 int res;
4008 if (video_supported == TPACPI_VIDEO_NONE)
4009 return -ENODEV;
4011 enable = 0;
4012 disable = 0;
4014 while ((cmd = next_cmd(&buf))) {
4015 if (strlencmp(cmd, "lcd_enable") == 0) {
4016 enable |= TP_ACPI_VIDEO_S_LCD;
4017 } else if (strlencmp(cmd, "lcd_disable") == 0) {
4018 disable |= TP_ACPI_VIDEO_S_LCD;
4019 } else if (strlencmp(cmd, "crt_enable") == 0) {
4020 enable |= TP_ACPI_VIDEO_S_CRT;
4021 } else if (strlencmp(cmd, "crt_disable") == 0) {
4022 disable |= TP_ACPI_VIDEO_S_CRT;
4023 } else if (video_supported == TPACPI_VIDEO_NEW &&
4024 strlencmp(cmd, "dvi_enable") == 0) {
4025 enable |= TP_ACPI_VIDEO_S_DVI;
4026 } else if (video_supported == TPACPI_VIDEO_NEW &&
4027 strlencmp(cmd, "dvi_disable") == 0) {
4028 disable |= TP_ACPI_VIDEO_S_DVI;
4029 } else if (strlencmp(cmd, "auto_enable") == 0) {
4030 res = video_autosw_set(1);
4031 if (res)
4032 return res;
4033 } else if (strlencmp(cmd, "auto_disable") == 0) {
4034 res = video_autosw_set(0);
4035 if (res)
4036 return res;
4037 } else if (strlencmp(cmd, "video_switch") == 0) {
4038 res = video_outputsw_cycle();
4039 if (res)
4040 return res;
4041 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4042 res = video_expand_toggle();
4043 if (res)
4044 return res;
4045 } else
4046 return -EINVAL;
4049 if (enable || disable) {
4050 status = video_outputsw_get();
4051 if (status < 0)
4052 return status;
4053 res = video_outputsw_set((status & ~disable) | enable);
4054 if (res)
4055 return res;
4058 return 0;
4061 static struct ibm_struct video_driver_data = {
4062 .name = "video",
4063 .read = video_read,
4064 .write = video_write,
4065 .exit = video_exit,
4068 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4070 /*************************************************************************
4071 * Light (thinklight) subdriver
4074 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4075 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4077 static int light_get_status(void)
4079 int status = 0;
4081 if (tp_features.light_status) {
4082 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4083 return -EIO;
4084 return (!!status);
4087 return -ENXIO;
4090 static int light_set_status(int status)
4092 int rc;
4094 if (tp_features.light) {
4095 if (cmos_handle) {
4096 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4097 (status)?
4098 TP_CMOS_THINKLIGHT_ON :
4099 TP_CMOS_THINKLIGHT_OFF);
4100 } else {
4101 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4102 (status)? 1 : 0);
4104 return (rc)? 0 : -EIO;
4107 return -ENXIO;
4110 static void light_set_status_worker(struct work_struct *work)
4112 struct tpacpi_led_classdev *data =
4113 container_of(work, struct tpacpi_led_classdev, work);
4115 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4116 light_set_status((data->new_state != TPACPI_LED_OFF));
4119 static void light_sysfs_set(struct led_classdev *led_cdev,
4120 enum led_brightness brightness)
4122 struct tpacpi_led_classdev *data =
4123 container_of(led_cdev,
4124 struct tpacpi_led_classdev,
4125 led_classdev);
4126 data->new_state = (brightness != LED_OFF) ?
4127 TPACPI_LED_ON : TPACPI_LED_OFF;
4128 queue_work(tpacpi_wq, &data->work);
4131 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4133 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4136 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4137 .led_classdev = {
4138 .name = "tpacpi::thinklight",
4139 .brightness_set = &light_sysfs_set,
4140 .brightness_get = &light_sysfs_get,
4144 static int __init light_init(struct ibm_init_struct *iibm)
4146 int rc;
4148 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4150 TPACPI_ACPIHANDLE_INIT(ledb);
4151 TPACPI_ACPIHANDLE_INIT(lght);
4152 TPACPI_ACPIHANDLE_INIT(cmos);
4153 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4155 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4156 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4158 if (tp_features.light)
4159 /* light status not supported on
4160 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4161 tp_features.light_status =
4162 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4164 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4165 str_supported(tp_features.light),
4166 str_supported(tp_features.light_status));
4168 if (!tp_features.light)
4169 return 1;
4171 rc = led_classdev_register(&tpacpi_pdev->dev,
4172 &tpacpi_led_thinklight.led_classdev);
4174 if (rc < 0) {
4175 tp_features.light = 0;
4176 tp_features.light_status = 0;
4177 } else {
4178 rc = 0;
4181 return rc;
4184 static void light_exit(void)
4186 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4187 if (work_pending(&tpacpi_led_thinklight.work))
4188 flush_workqueue(tpacpi_wq);
4191 static int light_read(char *p)
4193 int len = 0;
4194 int status;
4196 if (!tp_features.light) {
4197 len += sprintf(p + len, "status:\t\tnot supported\n");
4198 } else if (!tp_features.light_status) {
4199 len += sprintf(p + len, "status:\t\tunknown\n");
4200 len += sprintf(p + len, "commands:\ton, off\n");
4201 } else {
4202 status = light_get_status();
4203 if (status < 0)
4204 return status;
4205 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4206 len += sprintf(p + len, "commands:\ton, off\n");
4209 return len;
4212 static int light_write(char *buf)
4214 char *cmd;
4215 int newstatus = 0;
4217 if (!tp_features.light)
4218 return -ENODEV;
4220 while ((cmd = next_cmd(&buf))) {
4221 if (strlencmp(cmd, "on") == 0) {
4222 newstatus = 1;
4223 } else if (strlencmp(cmd, "off") == 0) {
4224 newstatus = 0;
4225 } else
4226 return -EINVAL;
4229 return light_set_status(newstatus);
4232 static struct ibm_struct light_driver_data = {
4233 .name = "light",
4234 .read = light_read,
4235 .write = light_write,
4236 .exit = light_exit,
4239 /*************************************************************************
4240 * Dock subdriver
4243 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4245 static void dock_notify(struct ibm_struct *ibm, u32 event);
4246 static int dock_read(char *p);
4247 static int dock_write(char *buf);
4249 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4250 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4251 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4252 "\\_SB.PCI.ISA.SLCE", /* 570 */
4253 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4255 /* don't list other alternatives as we install a notify handler on the 570 */
4256 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4258 static const struct acpi_device_id ibm_pci_device_ids[] = {
4259 {PCI_ROOT_HID_STRING, 0},
4260 {"", 0},
4263 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4265 .notify = dock_notify,
4266 .handle = &dock_handle,
4267 .type = ACPI_SYSTEM_NOTIFY,
4270 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4271 * We just use it to get notifications of dock hotplug
4272 * in very old thinkpads */
4273 .hid = ibm_pci_device_ids,
4274 .notify = dock_notify,
4275 .handle = &pci_handle,
4276 .type = ACPI_SYSTEM_NOTIFY,
4280 static struct ibm_struct dock_driver_data[2] = {
4282 .name = "dock",
4283 .read = dock_read,
4284 .write = dock_write,
4285 .acpi = &ibm_dock_acpidriver[0],
4288 .name = "dock",
4289 .acpi = &ibm_dock_acpidriver[1],
4293 #define dock_docked() (_sta(dock_handle) & 1)
4295 static int __init dock_init(struct ibm_init_struct *iibm)
4297 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4299 TPACPI_ACPIHANDLE_INIT(dock);
4301 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4302 str_supported(dock_handle != NULL));
4304 return (dock_handle)? 0 : 1;
4307 static int __init dock_init2(struct ibm_init_struct *iibm)
4309 int dock2_needed;
4311 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4313 if (dock_driver_data[0].flags.acpi_driver_registered &&
4314 dock_driver_data[0].flags.acpi_notify_installed) {
4315 TPACPI_ACPIHANDLE_INIT(pci);
4316 dock2_needed = (pci_handle != NULL);
4317 vdbg_printk(TPACPI_DBG_INIT,
4318 "dock PCI handler for the TP 570 is %s\n",
4319 str_supported(dock2_needed));
4320 } else {
4321 vdbg_printk(TPACPI_DBG_INIT,
4322 "dock subdriver part 2 not required\n");
4323 dock2_needed = 0;
4326 return (dock2_needed)? 0 : 1;
4329 static void dock_notify(struct ibm_struct *ibm, u32 event)
4331 int docked = dock_docked();
4332 int pci = ibm->acpi->hid && ibm->acpi->device &&
4333 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4334 int data;
4336 if (event == 1 && !pci) /* 570 */
4337 data = 1; /* button */
4338 else if (event == 1 && pci) /* 570 */
4339 data = 3; /* dock */
4340 else if (event == 3 && docked)
4341 data = 1; /* button */
4342 else if (event == 3 && !docked)
4343 data = 2; /* undock */
4344 else if (event == 0 && docked)
4345 data = 3; /* dock */
4346 else {
4347 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4348 event, _sta(dock_handle));
4349 data = 0; /* unknown */
4351 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4352 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4353 dev_name(&ibm->acpi->device->dev),
4354 event, data);
4357 static int dock_read(char *p)
4359 int len = 0;
4360 int docked = dock_docked();
4362 if (!dock_handle)
4363 len += sprintf(p + len, "status:\t\tnot supported\n");
4364 else if (!docked)
4365 len += sprintf(p + len, "status:\t\tundocked\n");
4366 else {
4367 len += sprintf(p + len, "status:\t\tdocked\n");
4368 len += sprintf(p + len, "commands:\tdock, undock\n");
4371 return len;
4374 static int dock_write(char *buf)
4376 char *cmd;
4378 if (!dock_docked())
4379 return -ENODEV;
4381 while ((cmd = next_cmd(&buf))) {
4382 if (strlencmp(cmd, "undock") == 0) {
4383 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4384 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4385 return -EIO;
4386 } else if (strlencmp(cmd, "dock") == 0) {
4387 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4388 return -EIO;
4389 } else
4390 return -EINVAL;
4393 return 0;
4396 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4398 /*************************************************************************
4399 * Bay subdriver
4402 #ifdef CONFIG_THINKPAD_ACPI_BAY
4404 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4405 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4406 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4407 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4408 ); /* A21e, R30, R31 */
4409 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4410 "_EJ0", /* all others */
4411 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4412 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4413 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4414 ); /* all others */
4415 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4416 "_EJ0", /* 770x */
4417 ); /* all others */
4419 static int __init bay_init(struct ibm_init_struct *iibm)
4421 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4423 TPACPI_ACPIHANDLE_INIT(bay);
4424 if (bay_handle)
4425 TPACPI_ACPIHANDLE_INIT(bay_ej);
4426 TPACPI_ACPIHANDLE_INIT(bay2);
4427 if (bay2_handle)
4428 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4430 tp_features.bay_status = bay_handle &&
4431 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4432 tp_features.bay_status2 = bay2_handle &&
4433 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4435 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4436 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4437 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4438 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4440 vdbg_printk(TPACPI_DBG_INIT,
4441 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4442 str_supported(tp_features.bay_status),
4443 str_supported(tp_features.bay_eject),
4444 str_supported(tp_features.bay_status2),
4445 str_supported(tp_features.bay_eject2));
4447 return (tp_features.bay_status || tp_features.bay_eject ||
4448 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4451 static void bay_notify(struct ibm_struct *ibm, u32 event)
4453 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4454 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4455 dev_name(&ibm->acpi->device->dev),
4456 event, 0);
4459 #define bay_occupied(b) (_sta(b##_handle) & 1)
4461 static int bay_read(char *p)
4463 int len = 0;
4464 int occupied = bay_occupied(bay);
4465 int occupied2 = bay_occupied(bay2);
4466 int eject, eject2;
4468 len += sprintf(p + len, "status:\t\t%s\n",
4469 tp_features.bay_status ?
4470 (occupied ? "occupied" : "unoccupied") :
4471 "not supported");
4472 if (tp_features.bay_status2)
4473 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4474 "occupied" : "unoccupied");
4476 eject = tp_features.bay_eject && occupied;
4477 eject2 = tp_features.bay_eject2 && occupied2;
4479 if (eject && eject2)
4480 len += sprintf(p + len, "commands:\teject, eject2\n");
4481 else if (eject)
4482 len += sprintf(p + len, "commands:\teject\n");
4483 else if (eject2)
4484 len += sprintf(p + len, "commands:\teject2\n");
4486 return len;
4489 static int bay_write(char *buf)
4491 char *cmd;
4493 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4494 return -ENODEV;
4496 while ((cmd = next_cmd(&buf))) {
4497 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4498 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4499 return -EIO;
4500 } else if (tp_features.bay_eject2 &&
4501 strlencmp(cmd, "eject2") == 0) {
4502 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4503 return -EIO;
4504 } else
4505 return -EINVAL;
4508 return 0;
4511 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4512 .notify = bay_notify,
4513 .handle = &bay_handle,
4514 .type = ACPI_SYSTEM_NOTIFY,
4517 static struct ibm_struct bay_driver_data = {
4518 .name = "bay",
4519 .read = bay_read,
4520 .write = bay_write,
4521 .acpi = &ibm_bay_acpidriver,
4524 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4526 /*************************************************************************
4527 * CMOS subdriver
4530 /* sysfs cmos_command -------------------------------------------------- */
4531 static ssize_t cmos_command_store(struct device *dev,
4532 struct device_attribute *attr,
4533 const char *buf, size_t count)
4535 unsigned long cmos_cmd;
4536 int res;
4538 if (parse_strtoul(buf, 21, &cmos_cmd))
4539 return -EINVAL;
4541 res = issue_thinkpad_cmos_command(cmos_cmd);
4542 return (res)? res : count;
4545 static struct device_attribute dev_attr_cmos_command =
4546 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4548 /* --------------------------------------------------------------------- */
4550 static int __init cmos_init(struct ibm_init_struct *iibm)
4552 int res;
4554 vdbg_printk(TPACPI_DBG_INIT,
4555 "initializing cmos commands subdriver\n");
4557 TPACPI_ACPIHANDLE_INIT(cmos);
4559 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4560 str_supported(cmos_handle != NULL));
4562 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4563 if (res)
4564 return res;
4566 return (cmos_handle)? 0 : 1;
4569 static void cmos_exit(void)
4571 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4574 static int cmos_read(char *p)
4576 int len = 0;
4578 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4579 R30, R31, T20-22, X20-21 */
4580 if (!cmos_handle)
4581 len += sprintf(p + len, "status:\t\tnot supported\n");
4582 else {
4583 len += sprintf(p + len, "status:\t\tsupported\n");
4584 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4587 return len;
4590 static int cmos_write(char *buf)
4592 char *cmd;
4593 int cmos_cmd, res;
4595 while ((cmd = next_cmd(&buf))) {
4596 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4597 cmos_cmd >= 0 && cmos_cmd <= 21) {
4598 /* cmos_cmd set */
4599 } else
4600 return -EINVAL;
4602 res = issue_thinkpad_cmos_command(cmos_cmd);
4603 if (res)
4604 return res;
4607 return 0;
4610 static struct ibm_struct cmos_driver_data = {
4611 .name = "cmos",
4612 .read = cmos_read,
4613 .write = cmos_write,
4614 .exit = cmos_exit,
4617 /*************************************************************************
4618 * LED subdriver
4621 enum led_access_mode {
4622 TPACPI_LED_NONE = 0,
4623 TPACPI_LED_570, /* 570 */
4624 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4625 TPACPI_LED_NEW, /* all others */
4628 enum { /* For TPACPI_LED_OLD */
4629 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4630 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4631 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4634 static enum led_access_mode led_supported;
4636 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4637 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4638 /* T20-22, X20-21 */
4639 "LED", /* all others */
4640 ); /* R30, R31 */
4642 #define TPACPI_LED_NUMLEDS 8
4643 static struct tpacpi_led_classdev *tpacpi_leds;
4644 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4645 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4646 /* there's a limit of 19 chars + NULL before 2.6.26 */
4647 "tpacpi::power",
4648 "tpacpi:orange:batt",
4649 "tpacpi:green:batt",
4650 "tpacpi::dock_active",
4651 "tpacpi::bay_active",
4652 "tpacpi::dock_batt",
4653 "tpacpi::unknown_led",
4654 "tpacpi::standby",
4657 static int led_get_status(const unsigned int led)
4659 int status;
4660 enum led_status_t led_s;
4662 switch (led_supported) {
4663 case TPACPI_LED_570:
4664 if (!acpi_evalf(ec_handle,
4665 &status, "GLED", "dd", 1 << led))
4666 return -EIO;
4667 led_s = (status == 0)?
4668 TPACPI_LED_OFF :
4669 ((status == 1)?
4670 TPACPI_LED_ON :
4671 TPACPI_LED_BLINK);
4672 tpacpi_led_state_cache[led] = led_s;
4673 return led_s;
4674 default:
4675 return -ENXIO;
4678 /* not reached */
4681 static int led_set_status(const unsigned int led,
4682 const enum led_status_t ledstatus)
4684 /* off, on, blink. Index is led_status_t */
4685 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4686 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4688 int rc = 0;
4690 switch (led_supported) {
4691 case TPACPI_LED_570:
4692 /* 570 */
4693 if (led > 7)
4694 return -EINVAL;
4695 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4696 (1 << led), led_sled_arg1[ledstatus]))
4697 rc = -EIO;
4698 break;
4699 case TPACPI_LED_OLD:
4700 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4701 if (led > 7)
4702 return -EINVAL;
4703 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4704 if (rc >= 0)
4705 rc = ec_write(TPACPI_LED_EC_HLBL,
4706 (ledstatus == TPACPI_LED_BLINK) << led);
4707 if (rc >= 0)
4708 rc = ec_write(TPACPI_LED_EC_HLCL,
4709 (ledstatus != TPACPI_LED_OFF) << led);
4710 break;
4711 case TPACPI_LED_NEW:
4712 /* all others */
4713 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4714 led, led_led_arg1[ledstatus]))
4715 rc = -EIO;
4716 break;
4717 default:
4718 rc = -ENXIO;
4721 if (!rc)
4722 tpacpi_led_state_cache[led] = ledstatus;
4724 return rc;
4727 static void led_set_status_worker(struct work_struct *work)
4729 struct tpacpi_led_classdev *data =
4730 container_of(work, struct tpacpi_led_classdev, work);
4732 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4733 led_set_status(data->led, data->new_state);
4736 static void led_sysfs_set(struct led_classdev *led_cdev,
4737 enum led_brightness brightness)
4739 struct tpacpi_led_classdev *data = container_of(led_cdev,
4740 struct tpacpi_led_classdev, led_classdev);
4742 if (brightness == LED_OFF)
4743 data->new_state = TPACPI_LED_OFF;
4744 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4745 data->new_state = TPACPI_LED_ON;
4746 else
4747 data->new_state = TPACPI_LED_BLINK;
4749 queue_work(tpacpi_wq, &data->work);
4752 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4753 unsigned long *delay_on, unsigned long *delay_off)
4755 struct tpacpi_led_classdev *data = container_of(led_cdev,
4756 struct tpacpi_led_classdev, led_classdev);
4758 /* Can we choose the flash rate? */
4759 if (*delay_on == 0 && *delay_off == 0) {
4760 /* yes. set them to the hardware blink rate (1 Hz) */
4761 *delay_on = 500; /* ms */
4762 *delay_off = 500; /* ms */
4763 } else if ((*delay_on != 500) || (*delay_off != 500))
4764 return -EINVAL;
4766 data->new_state = TPACPI_LED_BLINK;
4767 queue_work(tpacpi_wq, &data->work);
4769 return 0;
4772 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4774 int rc;
4776 struct tpacpi_led_classdev *data = container_of(led_cdev,
4777 struct tpacpi_led_classdev, led_classdev);
4779 rc = led_get_status(data->led);
4781 if (rc == TPACPI_LED_OFF || rc < 0)
4782 rc = LED_OFF; /* no error handling in led class :( */
4783 else
4784 rc = LED_FULL;
4786 return rc;
4789 static void led_exit(void)
4791 unsigned int i;
4793 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4794 if (tpacpi_leds[i].led_classdev.name)
4795 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4798 kfree(tpacpi_leds);
4801 static int __init led_init(struct ibm_init_struct *iibm)
4803 unsigned int i;
4804 int rc;
4806 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4808 TPACPI_ACPIHANDLE_INIT(led);
4810 if (!led_handle)
4811 /* led not supported on R30, R31 */
4812 led_supported = TPACPI_LED_NONE;
4813 else if (strlencmp(led_path, "SLED") == 0)
4814 /* 570 */
4815 led_supported = TPACPI_LED_570;
4816 else if (strlencmp(led_path, "SYSL") == 0)
4817 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4818 led_supported = TPACPI_LED_OLD;
4819 else
4820 /* all others */
4821 led_supported = TPACPI_LED_NEW;
4823 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4824 str_supported(led_supported), led_supported);
4826 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4827 GFP_KERNEL);
4828 if (!tpacpi_leds) {
4829 printk(TPACPI_ERR "Out of memory for LED data\n");
4830 return -ENOMEM;
4833 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4834 tpacpi_leds[i].led = i;
4836 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4837 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4838 if (led_supported == TPACPI_LED_570)
4839 tpacpi_leds[i].led_classdev.brightness_get =
4840 &led_sysfs_get;
4842 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4844 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4846 rc = led_classdev_register(&tpacpi_pdev->dev,
4847 &tpacpi_leds[i].led_classdev);
4848 if (rc < 0) {
4849 tpacpi_leds[i].led_classdev.name = NULL;
4850 led_exit();
4851 return rc;
4855 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4858 #define str_led_status(s) \
4859 ((s) == TPACPI_LED_OFF ? "off" : \
4860 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4862 static int led_read(char *p)
4864 int len = 0;
4866 if (!led_supported) {
4867 len += sprintf(p + len, "status:\t\tnot supported\n");
4868 return len;
4870 len += sprintf(p + len, "status:\t\tsupported\n");
4872 if (led_supported == TPACPI_LED_570) {
4873 /* 570 */
4874 int i, status;
4875 for (i = 0; i < 8; i++) {
4876 status = led_get_status(i);
4877 if (status < 0)
4878 return -EIO;
4879 len += sprintf(p + len, "%d:\t\t%s\n",
4880 i, str_led_status(status));
4884 len += sprintf(p + len, "commands:\t"
4885 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4887 return len;
4890 static int led_write(char *buf)
4892 char *cmd;
4893 int led, rc;
4894 enum led_status_t s;
4896 if (!led_supported)
4897 return -ENODEV;
4899 while ((cmd = next_cmd(&buf))) {
4900 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4901 return -EINVAL;
4903 if (strstr(cmd, "off")) {
4904 s = TPACPI_LED_OFF;
4905 } else if (strstr(cmd, "on")) {
4906 s = TPACPI_LED_ON;
4907 } else if (strstr(cmd, "blink")) {
4908 s = TPACPI_LED_BLINK;
4909 } else {
4910 return -EINVAL;
4913 rc = led_set_status(led, s);
4914 if (rc < 0)
4915 return rc;
4918 return 0;
4921 static struct ibm_struct led_driver_data = {
4922 .name = "led",
4923 .read = led_read,
4924 .write = led_write,
4925 .exit = led_exit,
4928 /*************************************************************************
4929 * Beep subdriver
4932 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4934 static int __init beep_init(struct ibm_init_struct *iibm)
4936 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4938 TPACPI_ACPIHANDLE_INIT(beep);
4940 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4941 str_supported(beep_handle != NULL));
4943 return (beep_handle)? 0 : 1;
4946 static int beep_read(char *p)
4948 int len = 0;
4950 if (!beep_handle)
4951 len += sprintf(p + len, "status:\t\tnot supported\n");
4952 else {
4953 len += sprintf(p + len, "status:\t\tsupported\n");
4954 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4957 return len;
4960 static int beep_write(char *buf)
4962 char *cmd;
4963 int beep_cmd;
4965 if (!beep_handle)
4966 return -ENODEV;
4968 while ((cmd = next_cmd(&buf))) {
4969 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4970 beep_cmd >= 0 && beep_cmd <= 17) {
4971 /* beep_cmd set */
4972 } else
4973 return -EINVAL;
4974 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4975 return -EIO;
4978 return 0;
4981 static struct ibm_struct beep_driver_data = {
4982 .name = "beep",
4983 .read = beep_read,
4984 .write = beep_write,
4987 /*************************************************************************
4988 * Thermal subdriver
4991 enum thermal_access_mode {
4992 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4993 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4994 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4995 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4996 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4999 enum { /* TPACPI_THERMAL_TPEC_* */
5000 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5001 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5002 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5005 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5006 struct ibm_thermal_sensors_struct {
5007 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5010 static enum thermal_access_mode thermal_read_mode;
5012 /* idx is zero-based */
5013 static int thermal_get_sensor(int idx, s32 *value)
5015 int t;
5016 s8 tmp;
5017 char tmpi[5];
5019 t = TP_EC_THERMAL_TMP0;
5021 switch (thermal_read_mode) {
5022 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5023 case TPACPI_THERMAL_TPEC_16:
5024 if (idx >= 8 && idx <= 15) {
5025 t = TP_EC_THERMAL_TMP8;
5026 idx -= 8;
5028 /* fallthrough */
5029 #endif
5030 case TPACPI_THERMAL_TPEC_8:
5031 if (idx <= 7) {
5032 if (!acpi_ec_read(t + idx, &tmp))
5033 return -EIO;
5034 *value = tmp * 1000;
5035 return 0;
5037 break;
5039 case TPACPI_THERMAL_ACPI_UPDT:
5040 if (idx <= 7) {
5041 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5042 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5043 return -EIO;
5044 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5045 return -EIO;
5046 *value = (t - 2732) * 100;
5047 return 0;
5049 break;
5051 case TPACPI_THERMAL_ACPI_TMP07:
5052 if (idx <= 7) {
5053 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5054 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5055 return -EIO;
5056 if (t > 127 || t < -127)
5057 t = TP_EC_THERMAL_TMP_NA;
5058 *value = t * 1000;
5059 return 0;
5061 break;
5063 case TPACPI_THERMAL_NONE:
5064 default:
5065 return -ENOSYS;
5068 return -EINVAL;
5071 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5073 int res, i;
5074 int n;
5076 n = 8;
5077 i = 0;
5079 if (!s)
5080 return -EINVAL;
5082 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5083 n = 16;
5085 for (i = 0 ; i < n; i++) {
5086 res = thermal_get_sensor(i, &s->temp[i]);
5087 if (res)
5088 return res;
5091 return n;
5094 /* sysfs temp##_input -------------------------------------------------- */
5096 static ssize_t thermal_temp_input_show(struct device *dev,
5097 struct device_attribute *attr,
5098 char *buf)
5100 struct sensor_device_attribute *sensor_attr =
5101 to_sensor_dev_attr(attr);
5102 int idx = sensor_attr->index;
5103 s32 value;
5104 int res;
5106 res = thermal_get_sensor(idx, &value);
5107 if (res)
5108 return res;
5109 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5110 return -ENXIO;
5112 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5115 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5116 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5117 thermal_temp_input_show, NULL, _idxB)
5119 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5120 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5121 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5122 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5123 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5124 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5125 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5126 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5127 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5128 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5129 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5130 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5131 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5132 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5133 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5134 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5135 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5138 #define THERMAL_ATTRS(X) \
5139 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5141 static struct attribute *thermal_temp_input_attr[] = {
5142 THERMAL_ATTRS(8),
5143 THERMAL_ATTRS(9),
5144 THERMAL_ATTRS(10),
5145 THERMAL_ATTRS(11),
5146 THERMAL_ATTRS(12),
5147 THERMAL_ATTRS(13),
5148 THERMAL_ATTRS(14),
5149 THERMAL_ATTRS(15),
5150 THERMAL_ATTRS(0),
5151 THERMAL_ATTRS(1),
5152 THERMAL_ATTRS(2),
5153 THERMAL_ATTRS(3),
5154 THERMAL_ATTRS(4),
5155 THERMAL_ATTRS(5),
5156 THERMAL_ATTRS(6),
5157 THERMAL_ATTRS(7),
5158 NULL
5161 static const struct attribute_group thermal_temp_input16_group = {
5162 .attrs = thermal_temp_input_attr
5165 static const struct attribute_group thermal_temp_input8_group = {
5166 .attrs = &thermal_temp_input_attr[8]
5169 #undef THERMAL_SENSOR_ATTR_TEMP
5170 #undef THERMAL_ATTRS
5172 /* --------------------------------------------------------------------- */
5174 static int __init thermal_init(struct ibm_init_struct *iibm)
5176 u8 t, ta1, ta2;
5177 int i;
5178 int acpi_tmp7;
5179 int res;
5181 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5183 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5185 if (thinkpad_id.ec_model) {
5187 * Direct EC access mode: sensors at registers
5188 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5189 * non-implemented, thermal sensors return 0x80 when
5190 * not available
5193 ta1 = ta2 = 0;
5194 for (i = 0; i < 8; i++) {
5195 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5196 ta1 |= t;
5197 } else {
5198 ta1 = 0;
5199 break;
5201 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5202 ta2 |= t;
5203 } else {
5204 ta1 = 0;
5205 break;
5208 if (ta1 == 0) {
5209 /* This is sheer paranoia, but we handle it anyway */
5210 if (acpi_tmp7) {
5211 printk(TPACPI_ERR
5212 "ThinkPad ACPI EC access misbehaving, "
5213 "falling back to ACPI TMPx access "
5214 "mode\n");
5215 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5216 } else {
5217 printk(TPACPI_ERR
5218 "ThinkPad ACPI EC access misbehaving, "
5219 "disabling thermal sensors access\n");
5220 thermal_read_mode = TPACPI_THERMAL_NONE;
5222 } else {
5223 thermal_read_mode =
5224 (ta2 != 0) ?
5225 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5227 } else if (acpi_tmp7) {
5228 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5229 /* 600e/x, 770e, 770x */
5230 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5231 } else {
5232 /* Standard ACPI TMPx access, max 8 sensors */
5233 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5235 } else {
5236 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5237 thermal_read_mode = TPACPI_THERMAL_NONE;
5240 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5241 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5242 thermal_read_mode);
5244 switch (thermal_read_mode) {
5245 case TPACPI_THERMAL_TPEC_16:
5246 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5247 &thermal_temp_input16_group);
5248 if (res)
5249 return res;
5250 break;
5251 case TPACPI_THERMAL_TPEC_8:
5252 case TPACPI_THERMAL_ACPI_TMP07:
5253 case TPACPI_THERMAL_ACPI_UPDT:
5254 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5255 &thermal_temp_input8_group);
5256 if (res)
5257 return res;
5258 break;
5259 case TPACPI_THERMAL_NONE:
5260 default:
5261 return 1;
5264 return 0;
5267 static void thermal_exit(void)
5269 switch (thermal_read_mode) {
5270 case TPACPI_THERMAL_TPEC_16:
5271 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5272 &thermal_temp_input16_group);
5273 break;
5274 case TPACPI_THERMAL_TPEC_8:
5275 case TPACPI_THERMAL_ACPI_TMP07:
5276 case TPACPI_THERMAL_ACPI_UPDT:
5277 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5278 &thermal_temp_input16_group);
5279 break;
5280 case TPACPI_THERMAL_NONE:
5281 default:
5282 break;
5286 static int thermal_read(char *p)
5288 int len = 0;
5289 int n, i;
5290 struct ibm_thermal_sensors_struct t;
5292 n = thermal_get_sensors(&t);
5293 if (unlikely(n < 0))
5294 return n;
5296 len += sprintf(p + len, "temperatures:\t");
5298 if (n > 0) {
5299 for (i = 0; i < (n - 1); i++)
5300 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5301 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5302 } else
5303 len += sprintf(p + len, "not supported\n");
5305 return len;
5308 static struct ibm_struct thermal_driver_data = {
5309 .name = "thermal",
5310 .read = thermal_read,
5311 .exit = thermal_exit,
5314 /*************************************************************************
5315 * EC Dump subdriver
5318 static u8 ecdump_regs[256];
5320 static int ecdump_read(char *p)
5322 int len = 0;
5323 int i, j;
5324 u8 v;
5326 len += sprintf(p + len, "EC "
5327 " +00 +01 +02 +03 +04 +05 +06 +07"
5328 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5329 for (i = 0; i < 256; i += 16) {
5330 len += sprintf(p + len, "EC 0x%02x:", i);
5331 for (j = 0; j < 16; j++) {
5332 if (!acpi_ec_read(i + j, &v))
5333 break;
5334 if (v != ecdump_regs[i + j])
5335 len += sprintf(p + len, " *%02x", v);
5336 else
5337 len += sprintf(p + len, " %02x", v);
5338 ecdump_regs[i + j] = v;
5340 len += sprintf(p + len, "\n");
5341 if (j != 16)
5342 break;
5345 /* These are way too dangerous to advertise openly... */
5346 #if 0
5347 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5348 " (<offset> is 00-ff, <value> is 00-ff)\n");
5349 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5350 " (<offset> is 00-ff, <value> is 0-255)\n");
5351 #endif
5352 return len;
5355 static int ecdump_write(char *buf)
5357 char *cmd;
5358 int i, v;
5360 while ((cmd = next_cmd(&buf))) {
5361 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5362 /* i and v set */
5363 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5364 /* i and v set */
5365 } else
5366 return -EINVAL;
5367 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5368 if (!acpi_ec_write(i, v))
5369 return -EIO;
5370 } else
5371 return -EINVAL;
5374 return 0;
5377 static struct ibm_struct ecdump_driver_data = {
5378 .name = "ecdump",
5379 .read = ecdump_read,
5380 .write = ecdump_write,
5381 .flags.experimental = 1,
5384 /*************************************************************************
5385 * Backlight/brightness subdriver
5388 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5390 enum {
5391 TP_EC_BACKLIGHT = 0x31,
5393 /* TP_EC_BACKLIGHT bitmasks */
5394 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5395 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5396 TP_EC_BACKLIGHT_MAPSW = 0x20,
5399 static struct backlight_device *ibm_backlight_device;
5400 static int brightness_mode;
5401 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5403 static struct mutex brightness_mutex;
5406 * ThinkPads can read brightness from two places: EC 0x31, or
5407 * CMOS NVRAM byte 0x5E, bits 0-3.
5409 * EC 0x31 has the following layout
5410 * Bit 7: unknown function
5411 * Bit 6: unknown function
5412 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5413 * Bit 4: must be set to zero to avoid problems
5414 * Bit 3-0: backlight brightness level
5416 * brightness_get_raw returns status data in the EC 0x31 layout
5418 static int brightness_get_raw(int *status)
5420 u8 lec = 0, lcmos = 0, level = 0;
5422 if (brightness_mode & 1) {
5423 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5424 return -EIO;
5425 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5427 if (brightness_mode & 2) {
5428 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5429 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5430 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5431 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5432 level = lcmos;
5435 if (brightness_mode == 3) {
5436 *status = lec; /* Prefer EC, CMOS is just a backing store */
5437 lec &= TP_EC_BACKLIGHT_LVLMSK;
5438 if (lec == lcmos)
5439 tp_warned.bright_cmos_ec_unsync = 0;
5440 else {
5441 if (!tp_warned.bright_cmos_ec_unsync) {
5442 printk(TPACPI_ERR
5443 "CMOS NVRAM (%u) and EC (%u) do not "
5444 "agree on display brightness level\n",
5445 (unsigned int) lcmos,
5446 (unsigned int) lec);
5447 tp_warned.bright_cmos_ec_unsync = 1;
5449 return -EIO;
5451 } else {
5452 *status = level;
5455 return 0;
5458 /* May return EINTR which can always be mapped to ERESTARTSYS */
5459 static int brightness_set(int value)
5461 int cmos_cmd, inc, i, res;
5462 int current_value;
5463 int command_bits;
5465 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5466 value < 0)
5467 return -EINVAL;
5469 res = mutex_lock_killable(&brightness_mutex);
5470 if (res < 0)
5471 return res;
5473 res = brightness_get_raw(&current_value);
5474 if (res < 0)
5475 goto errout;
5477 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5478 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5480 cmos_cmd = value > current_value ?
5481 TP_CMOS_BRIGHTNESS_UP :
5482 TP_CMOS_BRIGHTNESS_DOWN;
5483 inc = (value > current_value)? 1 : -1;
5485 res = 0;
5486 for (i = current_value; i != value; i += inc) {
5487 if ((brightness_mode & 2) &&
5488 issue_thinkpad_cmos_command(cmos_cmd)) {
5489 res = -EIO;
5490 goto errout;
5492 if ((brightness_mode & 1) &&
5493 !acpi_ec_write(TP_EC_BACKLIGHT,
5494 (i + inc) | command_bits)) {
5495 res = -EIO;
5496 goto errout;;
5500 errout:
5501 mutex_unlock(&brightness_mutex);
5502 return res;
5505 /* sysfs backlight class ----------------------------------------------- */
5507 static int brightness_update_status(struct backlight_device *bd)
5509 /* it is the backlight class's job (caller) to handle
5510 * EINTR and other errors properly */
5511 return brightness_set(
5512 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5513 bd->props.power == FB_BLANK_UNBLANK) ?
5514 bd->props.brightness : 0);
5517 static int brightness_get(struct backlight_device *bd)
5519 int status, res;
5521 res = brightness_get_raw(&status);
5522 if (res < 0)
5523 return 0; /* FIXME: teach backlight about error handling */
5525 return status & TP_EC_BACKLIGHT_LVLMSK;
5528 static struct backlight_ops ibm_backlight_data = {
5529 .get_brightness = brightness_get,
5530 .update_status = brightness_update_status,
5533 /* --------------------------------------------------------------------- */
5535 static int __init brightness_init(struct ibm_init_struct *iibm)
5537 int b;
5539 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5541 mutex_init(&brightness_mutex);
5544 * We always attempt to detect acpi support, so as to switch
5545 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5546 * going to publish a backlight interface
5548 b = tpacpi_check_std_acpi_brightness_support();
5549 if (b > 0) {
5550 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
5551 printk(TPACPI_NOTICE
5552 "Lenovo BIOS switched to ACPI backlight "
5553 "control mode\n");
5555 if (brightness_enable > 1) {
5556 printk(TPACPI_NOTICE
5557 "standard ACPI backlight interface "
5558 "available, not loading native one...\n");
5559 return 1;
5563 if (!brightness_enable) {
5564 dbg_printk(TPACPI_DBG_INIT,
5565 "brightness support disabled by "
5566 "module parameter\n");
5567 return 1;
5570 if (b > 16) {
5571 printk(TPACPI_ERR
5572 "Unsupported brightness interface, "
5573 "please contact %s\n", TPACPI_MAIL);
5574 return 1;
5576 if (b == 16)
5577 tp_features.bright_16levels = 1;
5579 if (!brightness_mode) {
5580 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5581 brightness_mode = 2;
5582 else
5583 brightness_mode = 3;
5585 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5586 brightness_mode);
5589 if (brightness_mode > 3)
5590 return -EINVAL;
5592 if (brightness_get_raw(&b) < 0)
5593 return 1;
5595 if (tp_features.bright_16levels)
5596 printk(TPACPI_INFO
5597 "detected a 16-level brightness capable ThinkPad\n");
5599 ibm_backlight_device = backlight_device_register(
5600 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5601 &ibm_backlight_data);
5602 if (IS_ERR(ibm_backlight_device)) {
5603 printk(TPACPI_ERR "Could not register backlight device\n");
5604 return PTR_ERR(ibm_backlight_device);
5606 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5608 ibm_backlight_device->props.max_brightness =
5609 (tp_features.bright_16levels)? 15 : 7;
5610 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5611 backlight_update_status(ibm_backlight_device);
5613 return 0;
5616 static void brightness_exit(void)
5618 if (ibm_backlight_device) {
5619 vdbg_printk(TPACPI_DBG_EXIT,
5620 "calling backlight_device_unregister()\n");
5621 backlight_device_unregister(ibm_backlight_device);
5625 static int brightness_read(char *p)
5627 int len = 0;
5628 int level;
5630 level = brightness_get(NULL);
5631 if (level < 0) {
5632 len += sprintf(p + len, "level:\t\tunreadable\n");
5633 } else {
5634 len += sprintf(p + len, "level:\t\t%d\n", level);
5635 len += sprintf(p + len, "commands:\tup, down\n");
5636 len += sprintf(p + len, "commands:\tlevel <level>"
5637 " (<level> is 0-%d)\n",
5638 (tp_features.bright_16levels) ? 15 : 7);
5641 return len;
5644 static int brightness_write(char *buf)
5646 int level;
5647 int rc;
5648 char *cmd;
5649 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5651 level = brightness_get(NULL);
5652 if (level < 0)
5653 return level;
5655 while ((cmd = next_cmd(&buf))) {
5656 if (strlencmp(cmd, "up") == 0) {
5657 if (level < max_level)
5658 level++;
5659 } else if (strlencmp(cmd, "down") == 0) {
5660 if (level > 0)
5661 level--;
5662 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5663 level >= 0 && level <= max_level) {
5664 /* new level set */
5665 } else
5666 return -EINVAL;
5670 * Now we know what the final level should be, so we try to set it.
5671 * Doing it this way makes the syscall restartable in case of EINTR
5673 rc = brightness_set(level);
5674 return (rc == -EINTR)? -ERESTARTSYS : rc;
5677 static struct ibm_struct brightness_driver_data = {
5678 .name = "brightness",
5679 .read = brightness_read,
5680 .write = brightness_write,
5681 .exit = brightness_exit,
5684 /*************************************************************************
5685 * Volume subdriver
5688 static int volume_offset = 0x30;
5690 static int volume_read(char *p)
5692 int len = 0;
5693 u8 level;
5695 if (!acpi_ec_read(volume_offset, &level)) {
5696 len += sprintf(p + len, "level:\t\tunreadable\n");
5697 } else {
5698 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5699 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5700 len += sprintf(p + len, "commands:\tup, down, mute\n");
5701 len += sprintf(p + len, "commands:\tlevel <level>"
5702 " (<level> is 0-15)\n");
5705 return len;
5708 static int volume_write(char *buf)
5710 int cmos_cmd, inc, i;
5711 u8 level, mute;
5712 int new_level, new_mute;
5713 char *cmd;
5715 while ((cmd = next_cmd(&buf))) {
5716 if (!acpi_ec_read(volume_offset, &level))
5717 return -EIO;
5718 new_mute = mute = level & 0x40;
5719 new_level = level = level & 0xf;
5721 if (strlencmp(cmd, "up") == 0) {
5722 if (mute)
5723 new_mute = 0;
5724 else
5725 new_level = level == 15 ? 15 : level + 1;
5726 } else if (strlencmp(cmd, "down") == 0) {
5727 if (mute)
5728 new_mute = 0;
5729 else
5730 new_level = level == 0 ? 0 : level - 1;
5731 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5732 new_level >= 0 && new_level <= 15) {
5733 /* new_level set */
5734 } else if (strlencmp(cmd, "mute") == 0) {
5735 new_mute = 0x40;
5736 } else
5737 return -EINVAL;
5739 if (new_level != level) {
5740 /* mute doesn't change */
5742 cmos_cmd = (new_level > level) ?
5743 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5744 inc = new_level > level ? 1 : -1;
5746 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5747 !acpi_ec_write(volume_offset, level)))
5748 return -EIO;
5750 for (i = level; i != new_level; i += inc)
5751 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5752 !acpi_ec_write(volume_offset, i + inc))
5753 return -EIO;
5755 if (mute &&
5756 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5757 !acpi_ec_write(volume_offset, new_level + mute))) {
5758 return -EIO;
5762 if (new_mute != mute) {
5763 /* level doesn't change */
5765 cmos_cmd = (new_mute) ?
5766 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5768 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5769 !acpi_ec_write(volume_offset, level + new_mute))
5770 return -EIO;
5774 return 0;
5777 static struct ibm_struct volume_driver_data = {
5778 .name = "volume",
5779 .read = volume_read,
5780 .write = volume_write,
5783 /*************************************************************************
5784 * Fan subdriver
5788 * FAN ACCESS MODES
5790 * TPACPI_FAN_RD_ACPI_GFAN:
5791 * ACPI GFAN method: returns fan level
5793 * see TPACPI_FAN_WR_ACPI_SFAN
5794 * EC 0x2f (HFSP) not available if GFAN exists
5796 * TPACPI_FAN_WR_ACPI_SFAN:
5797 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5799 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5800 * it for writing.
5802 * TPACPI_FAN_WR_TPEC:
5803 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5804 * Supported on almost all ThinkPads
5806 * Fan speed changes of any sort (including those caused by the
5807 * disengaged mode) are usually done slowly by the firmware as the
5808 * maximum ammount of fan duty cycle change per second seems to be
5809 * limited.
5811 * Reading is not available if GFAN exists.
5812 * Writing is not available if SFAN exists.
5814 * Bits
5815 * 7 automatic mode engaged;
5816 * (default operation mode of the ThinkPad)
5817 * fan level is ignored in this mode.
5818 * 6 full speed mode (takes precedence over bit 7);
5819 * not available on all thinkpads. May disable
5820 * the tachometer while the fan controller ramps up
5821 * the speed (which can take up to a few *minutes*).
5822 * Speeds up fan to 100% duty-cycle, which is far above
5823 * the standard RPM levels. It is not impossible that
5824 * it could cause hardware damage.
5825 * 5-3 unused in some models. Extra bits for fan level
5826 * in others, but still useless as all values above
5827 * 7 map to the same speed as level 7 in these models.
5828 * 2-0 fan level (0..7 usually)
5829 * 0x00 = stop
5830 * 0x07 = max (set when temperatures critical)
5831 * Some ThinkPads may have other levels, see
5832 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5834 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5835 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5836 * does so, its initial value is meaningless (0x07).
5838 * For firmware bugs, refer to:
5839 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5841 * ----
5843 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5844 * Main fan tachometer reading (in RPM)
5846 * This register is present on all ThinkPads with a new-style EC, and
5847 * it is known not to be present on the A21m/e, and T22, as there is
5848 * something else in offset 0x84 according to the ACPI DSDT. Other
5849 * ThinkPads from this same time period (and earlier) probably lack the
5850 * tachometer as well.
5852 * Unfortunately a lot of ThinkPads with new-style ECs but whose firmware
5853 * was never fixed by IBM to report the EC firmware version string
5854 * probably support the tachometer (like the early X models), so
5855 * detecting it is quite hard. We need more data to know for sure.
5857 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5858 * might result.
5860 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5861 * mode.
5863 * For firmware bugs, refer to:
5864 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5866 * TPACPI_FAN_WR_ACPI_FANS:
5867 * ThinkPad X31, X40, X41. Not available in the X60.
5869 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5870 * high speed. ACPI DSDT seems to map these three speeds to levels
5871 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5872 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5874 * The speeds are stored on handles
5875 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5877 * There are three default speed sets, acessible as handles:
5878 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5880 * ACPI DSDT switches which set is in use depending on various
5881 * factors.
5883 * TPACPI_FAN_WR_TPEC is also available and should be used to
5884 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5885 * but the ACPI tables just mention level 7.
5888 enum { /* Fan control constants */
5889 fan_status_offset = 0x2f, /* EC register 0x2f */
5890 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5891 * 0x84 must be read before 0x85 */
5893 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5894 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5896 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5899 enum fan_status_access_mode {
5900 TPACPI_FAN_NONE = 0, /* No fan status or control */
5901 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5902 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5905 enum fan_control_access_mode {
5906 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5907 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5908 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5909 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5912 enum fan_control_commands {
5913 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5914 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5915 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5916 * and also watchdog cmd */
5919 static int fan_control_allowed;
5921 static enum fan_status_access_mode fan_status_access_mode;
5922 static enum fan_control_access_mode fan_control_access_mode;
5923 static enum fan_control_commands fan_control_commands;
5925 static u8 fan_control_initial_status;
5926 static u8 fan_control_desired_level;
5927 static u8 fan_control_resume_level;
5928 static int fan_watchdog_maxinterval;
5930 static struct mutex fan_mutex;
5932 static void fan_watchdog_fire(struct work_struct *ignored);
5933 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5935 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5936 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5937 "\\FSPD", /* 600e/x, 770e, 770x */
5938 ); /* all others */
5939 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5940 "JFNS", /* 770x-JL */
5941 ); /* all others */
5944 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5945 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5946 * be in auto mode (0x80).
5948 * This is corrected by any write to HFSP either by the driver, or
5949 * by the firmware.
5951 * We assume 0x07 really means auto mode while this quirk is active,
5952 * as this is far more likely than the ThinkPad being in level 7,
5953 * which is only used by the firmware during thermal emergencies.
5956 static void fan_quirk1_detect(void)
5958 /* In some ThinkPads, neither the EC nor the ACPI
5959 * DSDT initialize the HFSP register, and it ends up
5960 * being initially set to 0x07 when it *could* be
5961 * either 0x07 or 0x80.
5963 * Enable for TP-1Y (T43), TP-78 (R51e),
5964 * TP-76 (R52), TP-70 (T43, R52), which are known
5965 * to be buggy. */
5966 if (fan_control_initial_status == 0x07) {
5967 switch (thinkpad_id.ec_model) {
5968 case 0x5931: /* TP-1Y */
5969 case 0x3837: /* TP-78 */
5970 case 0x3637: /* TP-76 */
5971 case 0x3037: /* TP-70 */
5972 printk(TPACPI_NOTICE
5973 "fan_init: initial fan status is unknown, "
5974 "assuming it is in auto mode\n");
5975 tp_features.fan_ctrl_status_undef = 1;
5981 static void fan_quirk1_handle(u8 *fan_status)
5983 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5984 if (*fan_status != fan_control_initial_status) {
5985 /* something changed the HFSP regisnter since
5986 * driver init time, so it is not undefined
5987 * anymore */
5988 tp_features.fan_ctrl_status_undef = 0;
5989 } else {
5990 /* Return most likely status. In fact, it
5991 * might be the only possible status */
5992 *fan_status = TP_EC_FAN_AUTO;
5998 * Call with fan_mutex held
6000 static void fan_update_desired_level(u8 status)
6002 if ((status &
6003 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6004 if (status > 7)
6005 fan_control_desired_level = 7;
6006 else
6007 fan_control_desired_level = status;
6011 static int fan_get_status(u8 *status)
6013 u8 s;
6015 /* TODO:
6016 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6018 switch (fan_status_access_mode) {
6019 case TPACPI_FAN_RD_ACPI_GFAN:
6020 /* 570, 600e/x, 770e, 770x */
6022 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6023 return -EIO;
6025 if (likely(status))
6026 *status = s & 0x07;
6028 break;
6030 case TPACPI_FAN_RD_TPEC:
6031 /* all except 570, 600e/x, 770e, 770x */
6032 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6033 return -EIO;
6035 if (likely(status)) {
6036 *status = s;
6037 fan_quirk1_handle(status);
6040 break;
6042 default:
6043 return -ENXIO;
6046 return 0;
6049 static int fan_get_status_safe(u8 *status)
6051 int rc;
6052 u8 s;
6054 if (mutex_lock_killable(&fan_mutex))
6055 return -ERESTARTSYS;
6056 rc = fan_get_status(&s);
6057 if (!rc)
6058 fan_update_desired_level(s);
6059 mutex_unlock(&fan_mutex);
6061 if (status)
6062 *status = s;
6064 return rc;
6067 static int fan_get_speed(unsigned int *speed)
6069 u8 hi, lo;
6071 switch (fan_status_access_mode) {
6072 case TPACPI_FAN_RD_TPEC:
6073 /* all except 570, 600e/x, 770e, 770x */
6074 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6075 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6076 return -EIO;
6078 if (likely(speed))
6079 *speed = (hi << 8) | lo;
6081 break;
6083 default:
6084 return -ENXIO;
6087 return 0;
6090 static int fan_set_level(int level)
6092 if (!fan_control_allowed)
6093 return -EPERM;
6095 switch (fan_control_access_mode) {
6096 case TPACPI_FAN_WR_ACPI_SFAN:
6097 if (level >= 0 && level <= 7) {
6098 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6099 return -EIO;
6100 } else
6101 return -EINVAL;
6102 break;
6104 case TPACPI_FAN_WR_ACPI_FANS:
6105 case TPACPI_FAN_WR_TPEC:
6106 if (!(level & TP_EC_FAN_AUTO) &&
6107 !(level & TP_EC_FAN_FULLSPEED) &&
6108 ((level < 0) || (level > 7)))
6109 return -EINVAL;
6111 /* safety net should the EC not support AUTO
6112 * or FULLSPEED mode bits and just ignore them */
6113 if (level & TP_EC_FAN_FULLSPEED)
6114 level |= 7; /* safety min speed 7 */
6115 else if (level & TP_EC_FAN_AUTO)
6116 level |= 4; /* safety min speed 4 */
6118 if (!acpi_ec_write(fan_status_offset, level))
6119 return -EIO;
6120 else
6121 tp_features.fan_ctrl_status_undef = 0;
6122 break;
6124 default:
6125 return -ENXIO;
6127 return 0;
6130 static int fan_set_level_safe(int level)
6132 int rc;
6134 if (!fan_control_allowed)
6135 return -EPERM;
6137 if (mutex_lock_killable(&fan_mutex))
6138 return -ERESTARTSYS;
6140 if (level == TPACPI_FAN_LAST_LEVEL)
6141 level = fan_control_desired_level;
6143 rc = fan_set_level(level);
6144 if (!rc)
6145 fan_update_desired_level(level);
6147 mutex_unlock(&fan_mutex);
6148 return rc;
6151 static int fan_set_enable(void)
6153 u8 s;
6154 int rc;
6156 if (!fan_control_allowed)
6157 return -EPERM;
6159 if (mutex_lock_killable(&fan_mutex))
6160 return -ERESTARTSYS;
6162 switch (fan_control_access_mode) {
6163 case TPACPI_FAN_WR_ACPI_FANS:
6164 case TPACPI_FAN_WR_TPEC:
6165 rc = fan_get_status(&s);
6166 if (rc < 0)
6167 break;
6169 /* Don't go out of emergency fan mode */
6170 if (s != 7) {
6171 s &= 0x07;
6172 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6175 if (!acpi_ec_write(fan_status_offset, s))
6176 rc = -EIO;
6177 else {
6178 tp_features.fan_ctrl_status_undef = 0;
6179 rc = 0;
6181 break;
6183 case TPACPI_FAN_WR_ACPI_SFAN:
6184 rc = fan_get_status(&s);
6185 if (rc < 0)
6186 break;
6188 s &= 0x07;
6190 /* Set fan to at least level 4 */
6191 s |= 4;
6193 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6194 rc = -EIO;
6195 else
6196 rc = 0;
6197 break;
6199 default:
6200 rc = -ENXIO;
6203 mutex_unlock(&fan_mutex);
6204 return rc;
6207 static int fan_set_disable(void)
6209 int rc;
6211 if (!fan_control_allowed)
6212 return -EPERM;
6214 if (mutex_lock_killable(&fan_mutex))
6215 return -ERESTARTSYS;
6217 rc = 0;
6218 switch (fan_control_access_mode) {
6219 case TPACPI_FAN_WR_ACPI_FANS:
6220 case TPACPI_FAN_WR_TPEC:
6221 if (!acpi_ec_write(fan_status_offset, 0x00))
6222 rc = -EIO;
6223 else {
6224 fan_control_desired_level = 0;
6225 tp_features.fan_ctrl_status_undef = 0;
6227 break;
6229 case TPACPI_FAN_WR_ACPI_SFAN:
6230 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6231 rc = -EIO;
6232 else
6233 fan_control_desired_level = 0;
6234 break;
6236 default:
6237 rc = -ENXIO;
6241 mutex_unlock(&fan_mutex);
6242 return rc;
6245 static int fan_set_speed(int speed)
6247 int rc;
6249 if (!fan_control_allowed)
6250 return -EPERM;
6252 if (mutex_lock_killable(&fan_mutex))
6253 return -ERESTARTSYS;
6255 rc = 0;
6256 switch (fan_control_access_mode) {
6257 case TPACPI_FAN_WR_ACPI_FANS:
6258 if (speed >= 0 && speed <= 65535) {
6259 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6260 speed, speed, speed))
6261 rc = -EIO;
6262 } else
6263 rc = -EINVAL;
6264 break;
6266 default:
6267 rc = -ENXIO;
6270 mutex_unlock(&fan_mutex);
6271 return rc;
6274 static void fan_watchdog_reset(void)
6276 static int fan_watchdog_active;
6278 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6279 return;
6281 if (fan_watchdog_active)
6282 cancel_delayed_work(&fan_watchdog_task);
6284 if (fan_watchdog_maxinterval > 0 &&
6285 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6286 fan_watchdog_active = 1;
6287 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6288 msecs_to_jiffies(fan_watchdog_maxinterval
6289 * 1000))) {
6290 printk(TPACPI_ERR
6291 "failed to queue the fan watchdog, "
6292 "watchdog will not trigger\n");
6294 } else
6295 fan_watchdog_active = 0;
6298 static void fan_watchdog_fire(struct work_struct *ignored)
6300 int rc;
6302 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6303 return;
6305 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6306 rc = fan_set_enable();
6307 if (rc < 0) {
6308 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6309 "will try again later...\n", -rc);
6310 /* reschedule for later */
6311 fan_watchdog_reset();
6316 * SYSFS fan layout: hwmon compatible (device)
6318 * pwm*_enable:
6319 * 0: "disengaged" mode
6320 * 1: manual mode
6321 * 2: native EC "auto" mode (recommended, hardware default)
6323 * pwm*: set speed in manual mode, ignored otherwise.
6324 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6325 * interpolation.
6327 * fan*_input: tachometer reading, RPM
6330 * SYSFS fan layout: extensions
6332 * fan_watchdog (driver):
6333 * fan watchdog interval in seconds, 0 disables (default), max 120
6336 /* sysfs fan pwm1_enable ----------------------------------------------- */
6337 static ssize_t fan_pwm1_enable_show(struct device *dev,
6338 struct device_attribute *attr,
6339 char *buf)
6341 int res, mode;
6342 u8 status;
6344 res = fan_get_status_safe(&status);
6345 if (res)
6346 return res;
6348 if (status & TP_EC_FAN_FULLSPEED) {
6349 mode = 0;
6350 } else if (status & TP_EC_FAN_AUTO) {
6351 mode = 2;
6352 } else
6353 mode = 1;
6355 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6358 static ssize_t fan_pwm1_enable_store(struct device *dev,
6359 struct device_attribute *attr,
6360 const char *buf, size_t count)
6362 unsigned long t;
6363 int res, level;
6365 if (parse_strtoul(buf, 2, &t))
6366 return -EINVAL;
6368 switch (t) {
6369 case 0:
6370 level = TP_EC_FAN_FULLSPEED;
6371 break;
6372 case 1:
6373 level = TPACPI_FAN_LAST_LEVEL;
6374 break;
6375 case 2:
6376 level = TP_EC_FAN_AUTO;
6377 break;
6378 case 3:
6379 /* reserved for software-controlled auto mode */
6380 return -ENOSYS;
6381 default:
6382 return -EINVAL;
6385 res = fan_set_level_safe(level);
6386 if (res == -ENXIO)
6387 return -EINVAL;
6388 else if (res < 0)
6389 return res;
6391 fan_watchdog_reset();
6393 return count;
6396 static struct device_attribute dev_attr_fan_pwm1_enable =
6397 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6398 fan_pwm1_enable_show, fan_pwm1_enable_store);
6400 /* sysfs fan pwm1 ------------------------------------------------------ */
6401 static ssize_t fan_pwm1_show(struct device *dev,
6402 struct device_attribute *attr,
6403 char *buf)
6405 int res;
6406 u8 status;
6408 res = fan_get_status_safe(&status);
6409 if (res)
6410 return res;
6412 if ((status &
6413 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6414 status = fan_control_desired_level;
6416 if (status > 7)
6417 status = 7;
6419 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6422 static ssize_t fan_pwm1_store(struct device *dev,
6423 struct device_attribute *attr,
6424 const char *buf, size_t count)
6426 unsigned long s;
6427 int rc;
6428 u8 status, newlevel;
6430 if (parse_strtoul(buf, 255, &s))
6431 return -EINVAL;
6433 /* scale down from 0-255 to 0-7 */
6434 newlevel = (s >> 5) & 0x07;
6436 if (mutex_lock_killable(&fan_mutex))
6437 return -ERESTARTSYS;
6439 rc = fan_get_status(&status);
6440 if (!rc && (status &
6441 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6442 rc = fan_set_level(newlevel);
6443 if (rc == -ENXIO)
6444 rc = -EINVAL;
6445 else if (!rc) {
6446 fan_update_desired_level(newlevel);
6447 fan_watchdog_reset();
6451 mutex_unlock(&fan_mutex);
6452 return (rc)? rc : count;
6455 static struct device_attribute dev_attr_fan_pwm1 =
6456 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6457 fan_pwm1_show, fan_pwm1_store);
6459 /* sysfs fan fan1_input ------------------------------------------------ */
6460 static ssize_t fan_fan1_input_show(struct device *dev,
6461 struct device_attribute *attr,
6462 char *buf)
6464 int res;
6465 unsigned int speed;
6467 res = fan_get_speed(&speed);
6468 if (res < 0)
6469 return res;
6471 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6474 static struct device_attribute dev_attr_fan_fan1_input =
6475 __ATTR(fan1_input, S_IRUGO,
6476 fan_fan1_input_show, NULL);
6478 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6479 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6480 char *buf)
6482 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6485 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6486 const char *buf, size_t count)
6488 unsigned long t;
6490 if (parse_strtoul(buf, 120, &t))
6491 return -EINVAL;
6493 if (!fan_control_allowed)
6494 return -EPERM;
6496 fan_watchdog_maxinterval = t;
6497 fan_watchdog_reset();
6499 return count;
6502 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6503 fan_fan_watchdog_show, fan_fan_watchdog_store);
6505 /* --------------------------------------------------------------------- */
6506 static struct attribute *fan_attributes[] = {
6507 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6508 &dev_attr_fan_fan1_input.attr,
6509 NULL
6512 static const struct attribute_group fan_attr_group = {
6513 .attrs = fan_attributes,
6516 static int __init fan_init(struct ibm_init_struct *iibm)
6518 int rc;
6520 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6522 mutex_init(&fan_mutex);
6523 fan_status_access_mode = TPACPI_FAN_NONE;
6524 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6525 fan_control_commands = 0;
6526 fan_watchdog_maxinterval = 0;
6527 tp_features.fan_ctrl_status_undef = 0;
6528 fan_control_desired_level = 7;
6530 TPACPI_ACPIHANDLE_INIT(fans);
6531 TPACPI_ACPIHANDLE_INIT(gfan);
6532 TPACPI_ACPIHANDLE_INIT(sfan);
6534 if (gfan_handle) {
6535 /* 570, 600e/x, 770e, 770x */
6536 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6537 } else {
6538 /* all other ThinkPads: note that even old-style
6539 * ThinkPad ECs supports the fan control register */
6540 if (likely(acpi_ec_read(fan_status_offset,
6541 &fan_control_initial_status))) {
6542 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6543 fan_quirk1_detect();
6544 } else {
6545 printk(TPACPI_ERR
6546 "ThinkPad ACPI EC access misbehaving, "
6547 "fan status and control unavailable\n");
6548 return 1;
6552 if (sfan_handle) {
6553 /* 570, 770x-JL */
6554 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6555 fan_control_commands |=
6556 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6557 } else {
6558 if (!gfan_handle) {
6559 /* gfan without sfan means no fan control */
6560 /* all other models implement TP EC 0x2f control */
6562 if (fans_handle) {
6563 /* X31, X40, X41 */
6564 fan_control_access_mode =
6565 TPACPI_FAN_WR_ACPI_FANS;
6566 fan_control_commands |=
6567 TPACPI_FAN_CMD_SPEED |
6568 TPACPI_FAN_CMD_LEVEL |
6569 TPACPI_FAN_CMD_ENABLE;
6570 } else {
6571 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6572 fan_control_commands |=
6573 TPACPI_FAN_CMD_LEVEL |
6574 TPACPI_FAN_CMD_ENABLE;
6579 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6580 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6581 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6582 fan_status_access_mode, fan_control_access_mode);
6584 /* fan control master switch */
6585 if (!fan_control_allowed) {
6586 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6587 fan_control_commands = 0;
6588 dbg_printk(TPACPI_DBG_INIT,
6589 "fan control features disabled by parameter\n");
6592 /* update fan_control_desired_level */
6593 if (fan_status_access_mode != TPACPI_FAN_NONE)
6594 fan_get_status_safe(NULL);
6596 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6597 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6598 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6599 &fan_attr_group);
6600 if (rc < 0)
6601 return rc;
6603 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6604 &driver_attr_fan_watchdog);
6605 if (rc < 0) {
6606 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6607 &fan_attr_group);
6608 return rc;
6610 return 0;
6611 } else
6612 return 1;
6615 static void fan_exit(void)
6617 vdbg_printk(TPACPI_DBG_EXIT,
6618 "cancelling any pending fan watchdog tasks\n");
6620 /* FIXME: can we really do this unconditionally? */
6621 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6622 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6623 &driver_attr_fan_watchdog);
6625 cancel_delayed_work(&fan_watchdog_task);
6626 flush_workqueue(tpacpi_wq);
6629 static void fan_suspend(pm_message_t state)
6631 int rc;
6633 if (!fan_control_allowed)
6634 return;
6636 /* Store fan status in cache */
6637 fan_control_resume_level = 0;
6638 rc = fan_get_status_safe(&fan_control_resume_level);
6639 if (rc < 0)
6640 printk(TPACPI_NOTICE
6641 "failed to read fan level for later "
6642 "restore during resume: %d\n", rc);
6644 /* if it is undefined, don't attempt to restore it.
6645 * KEEP THIS LAST */
6646 if (tp_features.fan_ctrl_status_undef)
6647 fan_control_resume_level = 0;
6650 static void fan_resume(void)
6652 u8 current_level = 7;
6653 bool do_set = false;
6654 int rc;
6656 /* DSDT *always* updates status on resume */
6657 tp_features.fan_ctrl_status_undef = 0;
6659 if (!fan_control_allowed ||
6660 !fan_control_resume_level ||
6661 (fan_get_status_safe(&current_level) < 0))
6662 return;
6664 switch (fan_control_access_mode) {
6665 case TPACPI_FAN_WR_ACPI_SFAN:
6666 /* never decrease fan level */
6667 do_set = (fan_control_resume_level > current_level);
6668 break;
6669 case TPACPI_FAN_WR_ACPI_FANS:
6670 case TPACPI_FAN_WR_TPEC:
6671 /* never decrease fan level, scale is:
6672 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6674 * We expect the firmware to set either 7 or AUTO, but we
6675 * handle FULLSPEED out of paranoia.
6677 * So, we can safely only restore FULLSPEED or 7, anything
6678 * else could slow the fan. Restoring AUTO is useless, at
6679 * best that's exactly what the DSDT already set (it is the
6680 * slower it uses).
6682 * Always keep in mind that the DSDT *will* have set the
6683 * fans to what the vendor supposes is the best level. We
6684 * muck with it only to speed the fan up.
6686 if (fan_control_resume_level != 7 &&
6687 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6688 return;
6689 else
6690 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6691 (current_level != fan_control_resume_level);
6692 break;
6693 default:
6694 return;
6696 if (do_set) {
6697 printk(TPACPI_NOTICE
6698 "restoring fan level to 0x%02x\n",
6699 fan_control_resume_level);
6700 rc = fan_set_level_safe(fan_control_resume_level);
6701 if (rc < 0)
6702 printk(TPACPI_NOTICE
6703 "failed to restore fan level: %d\n", rc);
6707 static int fan_read(char *p)
6709 int len = 0;
6710 int rc;
6711 u8 status;
6712 unsigned int speed = 0;
6714 switch (fan_status_access_mode) {
6715 case TPACPI_FAN_RD_ACPI_GFAN:
6716 /* 570, 600e/x, 770e, 770x */
6717 rc = fan_get_status_safe(&status);
6718 if (rc < 0)
6719 return rc;
6721 len += sprintf(p + len, "status:\t\t%s\n"
6722 "level:\t\t%d\n",
6723 (status != 0) ? "enabled" : "disabled", status);
6724 break;
6726 case TPACPI_FAN_RD_TPEC:
6727 /* all except 570, 600e/x, 770e, 770x */
6728 rc = fan_get_status_safe(&status);
6729 if (rc < 0)
6730 return rc;
6732 len += sprintf(p + len, "status:\t\t%s\n",
6733 (status != 0) ? "enabled" : "disabled");
6735 rc = fan_get_speed(&speed);
6736 if (rc < 0)
6737 return rc;
6739 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6741 if (status & TP_EC_FAN_FULLSPEED)
6742 /* Disengaged mode takes precedence */
6743 len += sprintf(p + len, "level:\t\tdisengaged\n");
6744 else if (status & TP_EC_FAN_AUTO)
6745 len += sprintf(p + len, "level:\t\tauto\n");
6746 else
6747 len += sprintf(p + len, "level:\t\t%d\n", status);
6748 break;
6750 case TPACPI_FAN_NONE:
6751 default:
6752 len += sprintf(p + len, "status:\t\tnot supported\n");
6755 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6756 len += sprintf(p + len, "commands:\tlevel <level>");
6758 switch (fan_control_access_mode) {
6759 case TPACPI_FAN_WR_ACPI_SFAN:
6760 len += sprintf(p + len, " (<level> is 0-7)\n");
6761 break;
6763 default:
6764 len += sprintf(p + len, " (<level> is 0-7, "
6765 "auto, disengaged, full-speed)\n");
6766 break;
6770 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6771 len += sprintf(p + len, "commands:\tenable, disable\n"
6772 "commands:\twatchdog <timeout> (<timeout> "
6773 "is 0 (off), 1-120 (seconds))\n");
6775 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6776 len += sprintf(p + len, "commands:\tspeed <speed>"
6777 " (<speed> is 0-65535)\n");
6779 return len;
6782 static int fan_write_cmd_level(const char *cmd, int *rc)
6784 int level;
6786 if (strlencmp(cmd, "level auto") == 0)
6787 level = TP_EC_FAN_AUTO;
6788 else if ((strlencmp(cmd, "level disengaged") == 0) |
6789 (strlencmp(cmd, "level full-speed") == 0))
6790 level = TP_EC_FAN_FULLSPEED;
6791 else if (sscanf(cmd, "level %d", &level) != 1)
6792 return 0;
6794 *rc = fan_set_level_safe(level);
6795 if (*rc == -ENXIO)
6796 printk(TPACPI_ERR "level command accepted for unsupported "
6797 "access mode %d", fan_control_access_mode);
6799 return 1;
6802 static int fan_write_cmd_enable(const char *cmd, int *rc)
6804 if (strlencmp(cmd, "enable") != 0)
6805 return 0;
6807 *rc = fan_set_enable();
6808 if (*rc == -ENXIO)
6809 printk(TPACPI_ERR "enable command accepted for unsupported "
6810 "access mode %d", fan_control_access_mode);
6812 return 1;
6815 static int fan_write_cmd_disable(const char *cmd, int *rc)
6817 if (strlencmp(cmd, "disable") != 0)
6818 return 0;
6820 *rc = fan_set_disable();
6821 if (*rc == -ENXIO)
6822 printk(TPACPI_ERR "disable command accepted for unsupported "
6823 "access mode %d", fan_control_access_mode);
6825 return 1;
6828 static int fan_write_cmd_speed(const char *cmd, int *rc)
6830 int speed;
6832 /* TODO:
6833 * Support speed <low> <medium> <high> ? */
6835 if (sscanf(cmd, "speed %d", &speed) != 1)
6836 return 0;
6838 *rc = fan_set_speed(speed);
6839 if (*rc == -ENXIO)
6840 printk(TPACPI_ERR "speed command accepted for unsupported "
6841 "access mode %d", fan_control_access_mode);
6843 return 1;
6846 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6848 int interval;
6850 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6851 return 0;
6853 if (interval < 0 || interval > 120)
6854 *rc = -EINVAL;
6855 else
6856 fan_watchdog_maxinterval = interval;
6858 return 1;
6861 static int fan_write(char *buf)
6863 char *cmd;
6864 int rc = 0;
6866 while (!rc && (cmd = next_cmd(&buf))) {
6867 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6868 fan_write_cmd_level(cmd, &rc)) &&
6869 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6870 (fan_write_cmd_enable(cmd, &rc) ||
6871 fan_write_cmd_disable(cmd, &rc) ||
6872 fan_write_cmd_watchdog(cmd, &rc))) &&
6873 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6874 fan_write_cmd_speed(cmd, &rc))
6876 rc = -EINVAL;
6877 else if (!rc)
6878 fan_watchdog_reset();
6881 return rc;
6884 static struct ibm_struct fan_driver_data = {
6885 .name = "fan",
6886 .read = fan_read,
6887 .write = fan_write,
6888 .exit = fan_exit,
6889 .suspend = fan_suspend,
6890 .resume = fan_resume,
6893 /****************************************************************************
6894 ****************************************************************************
6896 * Infrastructure
6898 ****************************************************************************
6899 ****************************************************************************/
6901 /* sysfs name ---------------------------------------------------------- */
6902 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6903 struct device_attribute *attr,
6904 char *buf)
6906 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6909 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6910 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6912 /* --------------------------------------------------------------------- */
6914 /* /proc support */
6915 static struct proc_dir_entry *proc_dir;
6918 * Module and infrastructure proble, init and exit handling
6921 static int force_load;
6923 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6924 static const char * __init str_supported(int is_supported)
6926 static char text_unsupported[] __initdata = "not supported";
6928 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6930 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6932 static void ibm_exit(struct ibm_struct *ibm)
6934 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6936 list_del_init(&ibm->all_drivers);
6938 if (ibm->flags.acpi_notify_installed) {
6939 dbg_printk(TPACPI_DBG_EXIT,
6940 "%s: acpi_remove_notify_handler\n", ibm->name);
6941 BUG_ON(!ibm->acpi);
6942 acpi_remove_notify_handler(*ibm->acpi->handle,
6943 ibm->acpi->type,
6944 dispatch_acpi_notify);
6945 ibm->flags.acpi_notify_installed = 0;
6946 ibm->flags.acpi_notify_installed = 0;
6949 if (ibm->flags.proc_created) {
6950 dbg_printk(TPACPI_DBG_EXIT,
6951 "%s: remove_proc_entry\n", ibm->name);
6952 remove_proc_entry(ibm->name, proc_dir);
6953 ibm->flags.proc_created = 0;
6956 if (ibm->flags.acpi_driver_registered) {
6957 dbg_printk(TPACPI_DBG_EXIT,
6958 "%s: acpi_bus_unregister_driver\n", ibm->name);
6959 BUG_ON(!ibm->acpi);
6960 acpi_bus_unregister_driver(ibm->acpi->driver);
6961 kfree(ibm->acpi->driver);
6962 ibm->acpi->driver = NULL;
6963 ibm->flags.acpi_driver_registered = 0;
6966 if (ibm->flags.init_called && ibm->exit) {
6967 ibm->exit();
6968 ibm->flags.init_called = 0;
6971 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6974 static int __init ibm_init(struct ibm_init_struct *iibm)
6976 int ret;
6977 struct ibm_struct *ibm = iibm->data;
6978 struct proc_dir_entry *entry;
6980 BUG_ON(ibm == NULL);
6982 INIT_LIST_HEAD(&ibm->all_drivers);
6984 if (ibm->flags.experimental && !experimental)
6985 return 0;
6987 dbg_printk(TPACPI_DBG_INIT,
6988 "probing for %s\n", ibm->name);
6990 if (iibm->init) {
6991 ret = iibm->init(iibm);
6992 if (ret > 0)
6993 return 0; /* probe failed */
6994 if (ret)
6995 return ret;
6997 ibm->flags.init_called = 1;
7000 if (ibm->acpi) {
7001 if (ibm->acpi->hid) {
7002 ret = register_tpacpi_subdriver(ibm);
7003 if (ret)
7004 goto err_out;
7007 if (ibm->acpi->notify) {
7008 ret = setup_acpi_notify(ibm);
7009 if (ret == -ENODEV) {
7010 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7011 ibm->name);
7012 ret = 0;
7013 goto err_out;
7015 if (ret < 0)
7016 goto err_out;
7020 dbg_printk(TPACPI_DBG_INIT,
7021 "%s installed\n", ibm->name);
7023 if (ibm->read) {
7024 entry = create_proc_entry(ibm->name,
7025 S_IFREG | S_IRUGO | S_IWUSR,
7026 proc_dir);
7027 if (!entry) {
7028 printk(TPACPI_ERR "unable to create proc entry %s\n",
7029 ibm->name);
7030 ret = -ENODEV;
7031 goto err_out;
7033 entry->owner = THIS_MODULE;
7034 entry->data = ibm;
7035 entry->read_proc = &dispatch_procfs_read;
7036 if (ibm->write)
7037 entry->write_proc = &dispatch_procfs_write;
7038 ibm->flags.proc_created = 1;
7041 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7043 return 0;
7045 err_out:
7046 dbg_printk(TPACPI_DBG_INIT,
7047 "%s: at error exit path with result %d\n",
7048 ibm->name, ret);
7050 ibm_exit(ibm);
7051 return (ret < 0)? ret : 0;
7054 /* Probing */
7056 /* returns 0 - probe ok, or < 0 - probe error.
7057 * Probe ok doesn't mean thinkpad found.
7058 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7059 static int __must_check __init get_thinkpad_model_data(
7060 struct thinkpad_id_data *tp)
7062 const struct dmi_device *dev = NULL;
7063 char ec_fw_string[18];
7064 char const *s;
7066 if (!tp)
7067 return -EINVAL;
7069 memset(tp, 0, sizeof(*tp));
7071 if (dmi_name_in_vendors("IBM"))
7072 tp->vendor = PCI_VENDOR_ID_IBM;
7073 else if (dmi_name_in_vendors("LENOVO"))
7074 tp->vendor = PCI_VENDOR_ID_LENOVO;
7075 else
7076 return 0;
7078 s = dmi_get_system_info(DMI_BIOS_VERSION);
7079 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7080 if (s && !tp->bios_version_str)
7081 return -ENOMEM;
7082 if (!tp->bios_version_str)
7083 return 0;
7084 tp->bios_model = tp->bios_version_str[0]
7085 | (tp->bios_version_str[1] << 8);
7088 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7089 * X32 or newer, all Z series; Some models must have an
7090 * up-to-date BIOS or they will not be detected.
7092 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7094 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7095 if (sscanf(dev->name,
7096 "IBM ThinkPad Embedded Controller -[%17c",
7097 ec_fw_string) == 1) {
7098 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7099 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7101 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7102 if (!tp->ec_version_str)
7103 return -ENOMEM;
7104 tp->ec_model = ec_fw_string[0]
7105 | (ec_fw_string[1] << 8);
7106 break;
7110 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7111 if (s && !strnicmp(s, "ThinkPad", 8)) {
7112 tp->model_str = kstrdup(s, GFP_KERNEL);
7113 if (!tp->model_str)
7114 return -ENOMEM;
7117 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7118 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7119 if (s && !tp->nummodel_str)
7120 return -ENOMEM;
7122 return 0;
7125 static int __init probe_for_thinkpad(void)
7127 int is_thinkpad;
7129 if (acpi_disabled)
7130 return -ENODEV;
7133 * Non-ancient models have better DMI tagging, but very old models
7134 * don't.
7136 is_thinkpad = (thinkpad_id.model_str != NULL);
7138 /* ec is required because many other handles are relative to it */
7139 TPACPI_ACPIHANDLE_INIT(ec);
7140 if (!ec_handle) {
7141 if (is_thinkpad)
7142 printk(TPACPI_ERR
7143 "Not yet supported ThinkPad detected!\n");
7144 return -ENODEV;
7148 * Risks a regression on very old machines, but reduces potential
7149 * false positives a damn great deal
7151 if (!is_thinkpad)
7152 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7154 if (!is_thinkpad && !force_load)
7155 return -ENODEV;
7157 return 0;
7161 /* Module init, exit, parameters */
7163 static struct ibm_init_struct ibms_init[] __initdata = {
7165 .init = thinkpad_acpi_driver_init,
7166 .data = &thinkpad_acpi_driver_data,
7169 .init = hotkey_init,
7170 .data = &hotkey_driver_data,
7173 .init = bluetooth_init,
7174 .data = &bluetooth_driver_data,
7177 .init = wan_init,
7178 .data = &wan_driver_data,
7181 .init = uwb_init,
7182 .data = &uwb_driver_data,
7184 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7186 .init = video_init,
7187 .data = &video_driver_data,
7189 #endif
7191 .init = light_init,
7192 .data = &light_driver_data,
7194 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7196 .init = dock_init,
7197 .data = &dock_driver_data[0],
7200 .init = dock_init2,
7201 .data = &dock_driver_data[1],
7203 #endif
7204 #ifdef CONFIG_THINKPAD_ACPI_BAY
7206 .init = bay_init,
7207 .data = &bay_driver_data,
7209 #endif
7211 .init = cmos_init,
7212 .data = &cmos_driver_data,
7215 .init = led_init,
7216 .data = &led_driver_data,
7219 .init = beep_init,
7220 .data = &beep_driver_data,
7223 .init = thermal_init,
7224 .data = &thermal_driver_data,
7227 .data = &ecdump_driver_data,
7230 .init = brightness_init,
7231 .data = &brightness_driver_data,
7234 .data = &volume_driver_data,
7237 .init = fan_init,
7238 .data = &fan_driver_data,
7242 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7244 unsigned int i;
7245 struct ibm_struct *ibm;
7247 if (!kp || !kp->name || !val)
7248 return -EINVAL;
7250 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7251 ibm = ibms_init[i].data;
7252 WARN_ON(ibm == NULL);
7254 if (!ibm || !ibm->name)
7255 continue;
7257 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7258 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7259 return -ENOSPC;
7260 strcpy(ibms_init[i].param, val);
7261 strcat(ibms_init[i].param, ",");
7262 return 0;
7266 return -EINVAL;
7269 module_param(experimental, int, 0);
7270 MODULE_PARM_DESC(experimental,
7271 "Enables experimental features when non-zero");
7273 module_param_named(debug, dbg_level, uint, 0);
7274 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7276 module_param(force_load, bool, 0);
7277 MODULE_PARM_DESC(force_load,
7278 "Attempts to load the driver even on a "
7279 "mis-identified ThinkPad when true");
7281 module_param_named(fan_control, fan_control_allowed, bool, 0);
7282 MODULE_PARM_DESC(fan_control,
7283 "Enables setting fan parameters features when true");
7285 module_param_named(brightness_mode, brightness_mode, int, 0);
7286 MODULE_PARM_DESC(brightness_mode,
7287 "Selects brightness control strategy: "
7288 "0=auto, 1=EC, 2=CMOS, 3=both");
7290 module_param(brightness_enable, uint, 0);
7291 MODULE_PARM_DESC(brightness_enable,
7292 "Enables backlight control when 1, disables when 0");
7294 module_param(hotkey_report_mode, uint, 0);
7295 MODULE_PARM_DESC(hotkey_report_mode,
7296 "used for backwards compatibility with userspace, "
7297 "see documentation");
7299 #define TPACPI_PARAM(feature) \
7300 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7301 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7302 "at module load, see documentation")
7304 TPACPI_PARAM(hotkey);
7305 TPACPI_PARAM(bluetooth);
7306 TPACPI_PARAM(video);
7307 TPACPI_PARAM(light);
7308 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7309 TPACPI_PARAM(dock);
7310 #endif
7311 #ifdef CONFIG_THINKPAD_ACPI_BAY
7312 TPACPI_PARAM(bay);
7313 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7314 TPACPI_PARAM(cmos);
7315 TPACPI_PARAM(led);
7316 TPACPI_PARAM(beep);
7317 TPACPI_PARAM(ecdump);
7318 TPACPI_PARAM(brightness);
7319 TPACPI_PARAM(volume);
7320 TPACPI_PARAM(fan);
7322 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7323 module_param(dbg_wlswemul, uint, 0);
7324 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7325 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7326 MODULE_PARM_DESC(wlsw_state,
7327 "Initial state of the emulated WLSW switch");
7329 module_param(dbg_bluetoothemul, uint, 0);
7330 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7331 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7332 MODULE_PARM_DESC(bluetooth_state,
7333 "Initial state of the emulated bluetooth switch");
7335 module_param(dbg_wwanemul, uint, 0);
7336 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7337 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7338 MODULE_PARM_DESC(wwan_state,
7339 "Initial state of the emulated WWAN switch");
7341 module_param(dbg_uwbemul, uint, 0);
7342 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7343 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7344 MODULE_PARM_DESC(uwb_state,
7345 "Initial state of the emulated UWB switch");
7346 #endif
7348 static void thinkpad_acpi_module_exit(void)
7350 struct ibm_struct *ibm, *itmp;
7352 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7354 list_for_each_entry_safe_reverse(ibm, itmp,
7355 &tpacpi_all_drivers,
7356 all_drivers) {
7357 ibm_exit(ibm);
7360 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7362 if (tpacpi_inputdev) {
7363 if (tp_features.input_device_registered)
7364 input_unregister_device(tpacpi_inputdev);
7365 else
7366 input_free_device(tpacpi_inputdev);
7369 if (tpacpi_hwmon)
7370 hwmon_device_unregister(tpacpi_hwmon);
7372 if (tp_features.sensors_pdev_attrs_registered)
7373 device_remove_file(&tpacpi_sensors_pdev->dev,
7374 &dev_attr_thinkpad_acpi_pdev_name);
7375 if (tpacpi_sensors_pdev)
7376 platform_device_unregister(tpacpi_sensors_pdev);
7377 if (tpacpi_pdev)
7378 platform_device_unregister(tpacpi_pdev);
7380 if (tp_features.sensors_pdrv_attrs_registered)
7381 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7382 if (tp_features.platform_drv_attrs_registered)
7383 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7385 if (tp_features.sensors_pdrv_registered)
7386 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7388 if (tp_features.platform_drv_registered)
7389 platform_driver_unregister(&tpacpi_pdriver);
7391 if (proc_dir)
7392 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7394 if (tpacpi_wq)
7395 destroy_workqueue(tpacpi_wq);
7397 kfree(thinkpad_id.bios_version_str);
7398 kfree(thinkpad_id.ec_version_str);
7399 kfree(thinkpad_id.model_str);
7403 static int __init thinkpad_acpi_module_init(void)
7405 int ret, i;
7407 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7409 /* Parameter checking */
7410 if (hotkey_report_mode > 2)
7411 return -EINVAL;
7413 /* Driver-level probe */
7415 ret = get_thinkpad_model_data(&thinkpad_id);
7416 if (ret) {
7417 printk(TPACPI_ERR
7418 "unable to get DMI data: %d\n", ret);
7419 thinkpad_acpi_module_exit();
7420 return ret;
7422 ret = probe_for_thinkpad();
7423 if (ret) {
7424 thinkpad_acpi_module_exit();
7425 return ret;
7428 /* Driver initialization */
7430 TPACPI_ACPIHANDLE_INIT(ecrd);
7431 TPACPI_ACPIHANDLE_INIT(ecwr);
7433 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7434 if (!tpacpi_wq) {
7435 thinkpad_acpi_module_exit();
7436 return -ENOMEM;
7439 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7440 if (!proc_dir) {
7441 printk(TPACPI_ERR
7442 "unable to create proc dir " TPACPI_PROC_DIR);
7443 thinkpad_acpi_module_exit();
7444 return -ENODEV;
7446 proc_dir->owner = THIS_MODULE;
7448 ret = platform_driver_register(&tpacpi_pdriver);
7449 if (ret) {
7450 printk(TPACPI_ERR
7451 "unable to register main platform driver\n");
7452 thinkpad_acpi_module_exit();
7453 return ret;
7455 tp_features.platform_drv_registered = 1;
7457 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7458 if (ret) {
7459 printk(TPACPI_ERR
7460 "unable to register hwmon platform driver\n");
7461 thinkpad_acpi_module_exit();
7462 return ret;
7464 tp_features.sensors_pdrv_registered = 1;
7466 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7467 if (!ret) {
7468 tp_features.platform_drv_attrs_registered = 1;
7469 ret = tpacpi_create_driver_attributes(
7470 &tpacpi_hwmon_pdriver.driver);
7472 if (ret) {
7473 printk(TPACPI_ERR
7474 "unable to create sysfs driver attributes\n");
7475 thinkpad_acpi_module_exit();
7476 return ret;
7478 tp_features.sensors_pdrv_attrs_registered = 1;
7481 /* Device initialization */
7482 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7483 NULL, 0);
7484 if (IS_ERR(tpacpi_pdev)) {
7485 ret = PTR_ERR(tpacpi_pdev);
7486 tpacpi_pdev = NULL;
7487 printk(TPACPI_ERR "unable to register platform device\n");
7488 thinkpad_acpi_module_exit();
7489 return ret;
7491 tpacpi_sensors_pdev = platform_device_register_simple(
7492 TPACPI_HWMON_DRVR_NAME,
7493 -1, NULL, 0);
7494 if (IS_ERR(tpacpi_sensors_pdev)) {
7495 ret = PTR_ERR(tpacpi_sensors_pdev);
7496 tpacpi_sensors_pdev = NULL;
7497 printk(TPACPI_ERR
7498 "unable to register hwmon platform device\n");
7499 thinkpad_acpi_module_exit();
7500 return ret;
7502 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7503 &dev_attr_thinkpad_acpi_pdev_name);
7504 if (ret) {
7505 printk(TPACPI_ERR
7506 "unable to create sysfs hwmon device attributes\n");
7507 thinkpad_acpi_module_exit();
7508 return ret;
7510 tp_features.sensors_pdev_attrs_registered = 1;
7511 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7512 if (IS_ERR(tpacpi_hwmon)) {
7513 ret = PTR_ERR(tpacpi_hwmon);
7514 tpacpi_hwmon = NULL;
7515 printk(TPACPI_ERR "unable to register hwmon device\n");
7516 thinkpad_acpi_module_exit();
7517 return ret;
7519 mutex_init(&tpacpi_inputdev_send_mutex);
7520 tpacpi_inputdev = input_allocate_device();
7521 if (!tpacpi_inputdev) {
7522 printk(TPACPI_ERR "unable to allocate input device\n");
7523 thinkpad_acpi_module_exit();
7524 return -ENOMEM;
7525 } else {
7526 /* Prepare input device, but don't register */
7527 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7528 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7529 tpacpi_inputdev->id.bustype = BUS_HOST;
7530 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7531 thinkpad_id.vendor :
7532 PCI_VENDOR_ID_IBM;
7533 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7534 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7536 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7537 ret = ibm_init(&ibms_init[i]);
7538 if (ret >= 0 && *ibms_init[i].param)
7539 ret = ibms_init[i].data->write(ibms_init[i].param);
7540 if (ret < 0) {
7541 thinkpad_acpi_module_exit();
7542 return ret;
7545 ret = input_register_device(tpacpi_inputdev);
7546 if (ret < 0) {
7547 printk(TPACPI_ERR "unable to register input device\n");
7548 thinkpad_acpi_module_exit();
7549 return ret;
7550 } else {
7551 tp_features.input_device_registered = 1;
7554 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7555 return 0;
7558 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7561 * DMI matching for module autoloading
7563 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7564 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7566 * Only models listed in thinkwiki will be supported, so add yours
7567 * if it is not there yet.
7569 #define IBM_BIOS_MODULE_ALIAS(__type) \
7570 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7572 /* Non-ancient thinkpads */
7573 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7574 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7576 /* Ancient thinkpad BIOSes have to be identified by
7577 * BIOS type or model number, and there are far less
7578 * BIOS types than model numbers... */
7579 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7580 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7581 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7583 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7584 MODULE_DESCRIPTION(TPACPI_DESC);
7585 MODULE_VERSION(TPACPI_VERSION);
7586 MODULE_LICENSE("GPL");
7588 module_init(thinkpad_acpi_module_init);
7589 module_exit(thinkpad_acpi_module_exit);