ACPI: thinkpad-acpi: fix fan sleep/resume path
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / misc / thinkpad_acpi.c
blob24c55e7bf34b8dc1d90a7e8ed15cc20849b9f664
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.21"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/kthread.h>
58 #include <linux/freezer.h>
59 #include <linux/delay.h>
61 #include <linux/nvram.h>
62 #include <linux/proc_fs.h>
63 #include <linux/sysfs.h>
64 #include <linux/backlight.h>
65 #include <linux/fb.h>
66 #include <linux/platform_device.h>
67 #include <linux/hwmon.h>
68 #include <linux/hwmon-sysfs.h>
69 #include <linux/input.h>
70 #include <linux/leds.h>
71 #include <linux/rfkill.h>
72 #include <asm/uaccess.h>
74 #include <linux/dmi.h>
75 #include <linux/jiffies.h>
76 #include <linux/workqueue.h>
78 #include <acpi/acpi_drivers.h>
79 #include <acpi/acnamesp.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
127 /****************************************************************************
128 * Main driver
131 #define TPACPI_NAME "thinkpad"
132 #define TPACPI_DESC "ThinkPad ACPI Extras"
133 #define TPACPI_FILE TPACPI_NAME "_acpi"
134 #define TPACPI_URL "http://ibm-acpi.sf.net/"
135 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
137 #define TPACPI_PROC_DIR "ibm"
138 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
139 #define TPACPI_DRVR_NAME TPACPI_FILE
140 #define TPACPI_DRVR_SHORTNAME "tpacpi"
141 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
143 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
144 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
146 #define TPACPI_MAX_ACPI_ARGS 3
148 /* rfkill switches */
149 enum {
150 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
151 TPACPI_RFK_WWAN_SW_ID,
154 /* Debugging */
155 #define TPACPI_LOG TPACPI_FILE ": "
156 #define TPACPI_ERR KERN_ERR TPACPI_LOG
157 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
158 #define TPACPI_INFO KERN_INFO TPACPI_LOG
159 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
161 #define TPACPI_DBG_ALL 0xffff
162 #define TPACPI_DBG_INIT 0x0001
163 #define TPACPI_DBG_EXIT 0x0002
164 #define dbg_printk(a_dbg_level, format, arg...) \
165 do { if (dbg_level & a_dbg_level) \
166 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
167 } while (0)
168 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
169 #define vdbg_printk(a_dbg_level, format, arg...) \
170 dbg_printk(a_dbg_level, format, ## arg)
171 static const char *str_supported(int is_supported);
172 #else
173 #define vdbg_printk(a_dbg_level, format, arg...)
174 #endif
176 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
177 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
178 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
181 /****************************************************************************
182 * Driver-wide structs and misc. variables
185 struct ibm_struct;
187 struct tp_acpi_drv_struct {
188 const struct acpi_device_id *hid;
189 struct acpi_driver *driver;
191 void (*notify) (struct ibm_struct *, u32);
192 acpi_handle *handle;
193 u32 type;
194 struct acpi_device *device;
197 struct ibm_struct {
198 char *name;
200 int (*read) (char *);
201 int (*write) (char *);
202 void (*exit) (void);
203 void (*resume) (void);
204 void (*suspend) (pm_message_t state);
206 struct list_head all_drivers;
208 struct tp_acpi_drv_struct *acpi;
210 struct {
211 u8 acpi_driver_registered:1;
212 u8 acpi_notify_installed:1;
213 u8 proc_created:1;
214 u8 init_called:1;
215 u8 experimental:1;
216 } flags;
219 struct ibm_init_struct {
220 char param[32];
222 int (*init) (struct ibm_init_struct *);
223 struct ibm_struct *data;
226 static struct {
227 #ifdef CONFIG_THINKPAD_ACPI_BAY
228 u32 bay_status:1;
229 u32 bay_eject:1;
230 u32 bay_status2:1;
231 u32 bay_eject2:1;
232 #endif
233 u32 bluetooth:1;
234 u32 hotkey:1;
235 u32 hotkey_mask:1;
236 u32 hotkey_wlsw:1;
237 u32 hotkey_tablet:1;
238 u32 light:1;
239 u32 light_status:1;
240 u32 bright_16levels:1;
241 u32 bright_acpimode:1;
242 u32 wan:1;
243 u32 fan_ctrl_status_undef:1;
244 u32 input_device_registered:1;
245 u32 platform_drv_registered:1;
246 u32 platform_drv_attrs_registered:1;
247 u32 sensors_pdrv_registered:1;
248 u32 sensors_pdrv_attrs_registered:1;
249 u32 sensors_pdev_attrs_registered:1;
250 u32 hotkey_poll_active:1;
251 } tp_features;
253 static struct {
254 u16 hotkey_mask_ff:1;
255 u16 bright_cmos_ec_unsync:1;
256 } tp_warned;
258 struct thinkpad_id_data {
259 unsigned int vendor; /* ThinkPad vendor:
260 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
262 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
263 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
265 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
266 u16 ec_model;
268 char *model_str; /* ThinkPad T43 */
269 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
271 static struct thinkpad_id_data thinkpad_id;
273 static enum {
274 TPACPI_LIFE_INIT = 0,
275 TPACPI_LIFE_RUNNING,
276 TPACPI_LIFE_EXITING,
277 } tpacpi_lifecycle;
279 static int experimental;
280 static u32 dbg_level;
282 static struct workqueue_struct *tpacpi_wq;
284 enum led_status_t {
285 TPACPI_LED_OFF = 0,
286 TPACPI_LED_ON,
287 TPACPI_LED_BLINK,
290 /* Special LED class that can defer work */
291 struct tpacpi_led_classdev {
292 struct led_classdev led_classdev;
293 struct work_struct work;
294 enum led_status_t new_state;
295 unsigned int led;
298 /****************************************************************************
299 ****************************************************************************
301 * ACPI Helpers and device model
303 ****************************************************************************
304 ****************************************************************************/
306 /*************************************************************************
307 * ACPI basic handles
310 static acpi_handle root_handle;
312 #define TPACPI_HANDLE(object, parent, paths...) \
313 static acpi_handle object##_handle; \
314 static acpi_handle *object##_parent = &parent##_handle; \
315 static char *object##_path; \
316 static char *object##_paths[] = { paths }
318 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
319 "\\_SB.PCI.ISA.EC", /* 570 */
320 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
321 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
322 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
323 "\\_SB.PCI0.ICH3.EC0", /* R31 */
324 "\\_SB.PCI0.LPC.EC", /* all others */
327 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
328 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
330 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
331 /* T4x, X31, X40 */
332 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
333 "\\CMS", /* R40, R40e */
334 ); /* all others */
336 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
337 "^HKEY", /* R30, R31 */
338 "HKEY", /* all others */
339 ); /* 570 */
341 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
342 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
343 "\\_SB.PCI0.VID0", /* 770e */
344 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
345 "\\_SB.PCI0.AGP.VID", /* all others */
346 ); /* R30, R31 */
349 /*************************************************************************
350 * ACPI helpers
353 static int acpi_evalf(acpi_handle handle,
354 void *res, char *method, char *fmt, ...)
356 char *fmt0 = fmt;
357 struct acpi_object_list params;
358 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
359 struct acpi_buffer result, *resultp;
360 union acpi_object out_obj;
361 acpi_status status;
362 va_list ap;
363 char res_type;
364 int success;
365 int quiet;
367 if (!*fmt) {
368 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
369 return 0;
372 if (*fmt == 'q') {
373 quiet = 1;
374 fmt++;
375 } else
376 quiet = 0;
378 res_type = *(fmt++);
380 params.count = 0;
381 params.pointer = &in_objs[0];
383 va_start(ap, fmt);
384 while (*fmt) {
385 char c = *(fmt++);
386 switch (c) {
387 case 'd': /* int */
388 in_objs[params.count].integer.value = va_arg(ap, int);
389 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
390 break;
391 /* add more types as needed */
392 default:
393 printk(TPACPI_ERR "acpi_evalf() called "
394 "with invalid format character '%c'\n", c);
395 return 0;
398 va_end(ap);
400 if (res_type != 'v') {
401 result.length = sizeof(out_obj);
402 result.pointer = &out_obj;
403 resultp = &result;
404 } else
405 resultp = NULL;
407 status = acpi_evaluate_object(handle, method, &params, resultp);
409 switch (res_type) {
410 case 'd': /* int */
411 if (res)
412 *(int *)res = out_obj.integer.value;
413 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
414 break;
415 case 'v': /* void */
416 success = status == AE_OK;
417 break;
418 /* add more types as needed */
419 default:
420 printk(TPACPI_ERR "acpi_evalf() called "
421 "with invalid format character '%c'\n", res_type);
422 return 0;
425 if (!success && !quiet)
426 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
427 method, fmt0, status);
429 return success;
432 static int acpi_ec_read(int i, u8 *p)
434 int v;
436 if (ecrd_handle) {
437 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
438 return 0;
439 *p = v;
440 } else {
441 if (ec_read(i, p) < 0)
442 return 0;
445 return 1;
448 static int acpi_ec_write(int i, u8 v)
450 if (ecwr_handle) {
451 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
452 return 0;
453 } else {
454 if (ec_write(i, v) < 0)
455 return 0;
458 return 1;
461 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
462 static int _sta(acpi_handle handle)
464 int status;
466 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
467 status = 0;
469 return status;
471 #endif
473 static int issue_thinkpad_cmos_command(int cmos_cmd)
475 if (!cmos_handle)
476 return -ENXIO;
478 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
479 return -EIO;
481 return 0;
484 /*************************************************************************
485 * ACPI device model
488 #define TPACPI_ACPIHANDLE_INIT(object) \
489 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
490 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
492 static void drv_acpi_handle_init(char *name,
493 acpi_handle *handle, acpi_handle parent,
494 char **paths, int num_paths, char **path)
496 int i;
497 acpi_status status;
499 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
500 name);
502 for (i = 0; i < num_paths; i++) {
503 status = acpi_get_handle(parent, paths[i], handle);
504 if (ACPI_SUCCESS(status)) {
505 *path = paths[i];
506 dbg_printk(TPACPI_DBG_INIT,
507 "Found ACPI handle %s for %s\n",
508 *path, name);
509 return;
513 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
514 name);
515 *handle = NULL;
518 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
520 struct ibm_struct *ibm = data;
522 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
523 return;
525 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
526 return;
528 ibm->acpi->notify(ibm, event);
531 static int __init setup_acpi_notify(struct ibm_struct *ibm)
533 acpi_status status;
534 int rc;
536 BUG_ON(!ibm->acpi);
538 if (!*ibm->acpi->handle)
539 return 0;
541 vdbg_printk(TPACPI_DBG_INIT,
542 "setting up ACPI notify for %s\n", ibm->name);
544 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
545 if (rc < 0) {
546 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
547 ibm->name, rc);
548 return -ENODEV;
551 acpi_driver_data(ibm->acpi->device) = ibm;
552 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
553 TPACPI_ACPI_EVENT_PREFIX,
554 ibm->name);
556 status = acpi_install_notify_handler(*ibm->acpi->handle,
557 ibm->acpi->type, dispatch_acpi_notify, ibm);
558 if (ACPI_FAILURE(status)) {
559 if (status == AE_ALREADY_EXISTS) {
560 printk(TPACPI_NOTICE
561 "another device driver is already "
562 "handling %s events\n", ibm->name);
563 } else {
564 printk(TPACPI_ERR
565 "acpi_install_notify_handler(%s) failed: %d\n",
566 ibm->name, status);
568 return -ENODEV;
570 ibm->flags.acpi_notify_installed = 1;
571 return 0;
574 static int __init tpacpi_device_add(struct acpi_device *device)
576 return 0;
579 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
581 int rc;
583 dbg_printk(TPACPI_DBG_INIT,
584 "registering %s as an ACPI driver\n", ibm->name);
586 BUG_ON(!ibm->acpi);
588 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
589 if (!ibm->acpi->driver) {
590 printk(TPACPI_ERR
591 "failed to allocate memory for ibm->acpi->driver\n");
592 return -ENOMEM;
595 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
596 ibm->acpi->driver->ids = ibm->acpi->hid;
598 ibm->acpi->driver->ops.add = &tpacpi_device_add;
600 rc = acpi_bus_register_driver(ibm->acpi->driver);
601 if (rc < 0) {
602 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
603 ibm->name, rc);
604 kfree(ibm->acpi->driver);
605 ibm->acpi->driver = NULL;
606 } else if (!rc)
607 ibm->flags.acpi_driver_registered = 1;
609 return rc;
613 /****************************************************************************
614 ****************************************************************************
616 * Procfs Helpers
618 ****************************************************************************
619 ****************************************************************************/
621 static int dispatch_procfs_read(char *page, char **start, off_t off,
622 int count, int *eof, void *data)
624 struct ibm_struct *ibm = data;
625 int len;
627 if (!ibm || !ibm->read)
628 return -EINVAL;
630 len = ibm->read(page);
631 if (len < 0)
632 return len;
634 if (len <= off + count)
635 *eof = 1;
636 *start = page + off;
637 len -= off;
638 if (len > count)
639 len = count;
640 if (len < 0)
641 len = 0;
643 return len;
646 static int dispatch_procfs_write(struct file *file,
647 const char __user *userbuf,
648 unsigned long count, void *data)
650 struct ibm_struct *ibm = data;
651 char *kernbuf;
652 int ret;
654 if (!ibm || !ibm->write)
655 return -EINVAL;
657 kernbuf = kmalloc(count + 2, GFP_KERNEL);
658 if (!kernbuf)
659 return -ENOMEM;
661 if (copy_from_user(kernbuf, userbuf, count)) {
662 kfree(kernbuf);
663 return -EFAULT;
666 kernbuf[count] = 0;
667 strcat(kernbuf, ",");
668 ret = ibm->write(kernbuf);
669 if (ret == 0)
670 ret = count;
672 kfree(kernbuf);
674 return ret;
677 static char *next_cmd(char **cmds)
679 char *start = *cmds;
680 char *end;
682 while ((end = strchr(start, ',')) && end == start)
683 start = end + 1;
685 if (!end)
686 return NULL;
688 *end = 0;
689 *cmds = end + 1;
690 return start;
694 /****************************************************************************
695 ****************************************************************************
697 * Device model: input, hwmon and platform
699 ****************************************************************************
700 ****************************************************************************/
702 static struct platform_device *tpacpi_pdev;
703 static struct platform_device *tpacpi_sensors_pdev;
704 static struct device *tpacpi_hwmon;
705 static struct input_dev *tpacpi_inputdev;
706 static struct mutex tpacpi_inputdev_send_mutex;
707 static LIST_HEAD(tpacpi_all_drivers);
709 static int tpacpi_suspend_handler(struct platform_device *pdev,
710 pm_message_t state)
712 struct ibm_struct *ibm, *itmp;
714 list_for_each_entry_safe(ibm, itmp,
715 &tpacpi_all_drivers,
716 all_drivers) {
717 if (ibm->suspend)
718 (ibm->suspend)(state);
721 return 0;
724 static int tpacpi_resume_handler(struct platform_device *pdev)
726 struct ibm_struct *ibm, *itmp;
728 list_for_each_entry_safe(ibm, itmp,
729 &tpacpi_all_drivers,
730 all_drivers) {
731 if (ibm->resume)
732 (ibm->resume)();
735 return 0;
738 static struct platform_driver tpacpi_pdriver = {
739 .driver = {
740 .name = TPACPI_DRVR_NAME,
741 .owner = THIS_MODULE,
743 .suspend = tpacpi_suspend_handler,
744 .resume = tpacpi_resume_handler,
747 static struct platform_driver tpacpi_hwmon_pdriver = {
748 .driver = {
749 .name = TPACPI_HWMON_DRVR_NAME,
750 .owner = THIS_MODULE,
754 /*************************************************************************
755 * sysfs support helpers
758 struct attribute_set {
759 unsigned int members, max_members;
760 struct attribute_group group;
763 struct attribute_set_obj {
764 struct attribute_set s;
765 struct attribute *a;
766 } __attribute__((packed));
768 static struct attribute_set *create_attr_set(unsigned int max_members,
769 const char *name)
771 struct attribute_set_obj *sobj;
773 if (max_members == 0)
774 return NULL;
776 /* Allocates space for implicit NULL at the end too */
777 sobj = kzalloc(sizeof(struct attribute_set_obj) +
778 max_members * sizeof(struct attribute *),
779 GFP_KERNEL);
780 if (!sobj)
781 return NULL;
782 sobj->s.max_members = max_members;
783 sobj->s.group.attrs = &sobj->a;
784 sobj->s.group.name = name;
786 return &sobj->s;
789 #define destroy_attr_set(_set) \
790 kfree(_set);
792 /* not multi-threaded safe, use it in a single thread per set */
793 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
795 if (!s || !attr)
796 return -EINVAL;
798 if (s->members >= s->max_members)
799 return -ENOMEM;
801 s->group.attrs[s->members] = attr;
802 s->members++;
804 return 0;
807 static int add_many_to_attr_set(struct attribute_set *s,
808 struct attribute **attr,
809 unsigned int count)
811 int i, res;
813 for (i = 0; i < count; i++) {
814 res = add_to_attr_set(s, attr[i]);
815 if (res)
816 return res;
819 return 0;
822 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
824 sysfs_remove_group(kobj, &s->group);
825 destroy_attr_set(s);
828 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
829 sysfs_create_group(_kobj, &_attr_set->group)
831 static int parse_strtoul(const char *buf,
832 unsigned long max, unsigned long *value)
834 char *endp;
836 while (*buf && isspace(*buf))
837 buf++;
838 *value = simple_strtoul(buf, &endp, 0);
839 while (*endp && isspace(*endp))
840 endp++;
841 if (*endp || *value > max)
842 return -EINVAL;
844 return 0;
847 static void tpacpi_disable_brightness_delay(void)
849 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
850 printk(TPACPI_NOTICE
851 "ACPI backlight control delay disabled\n");
854 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
856 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
857 union acpi_object *obj;
858 int rc;
860 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
861 obj = (union acpi_object *)buffer.pointer;
862 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
863 printk(TPACPI_ERR "Unknown _BCL data, "
864 "please report this to %s\n", TPACPI_MAIL);
865 rc = 0;
866 } else {
867 rc = obj->package.count;
869 } else {
870 return 0;
873 kfree(buffer.pointer);
874 return rc;
877 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
878 u32 lvl, void *context, void **rv)
880 char name[ACPI_PATH_SEGMENT_LENGTH];
881 struct acpi_buffer buffer = { sizeof(name), &name };
883 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
884 !strncmp("_BCL", name, sizeof(name) - 1)) {
885 BUG_ON(!rv || !*rv);
886 **(int **)rv = tpacpi_query_bcl_levels(handle);
887 return AE_CTRL_TERMINATE;
888 } else {
889 return AE_OK;
894 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
896 static int __init tpacpi_check_std_acpi_brightness_support(void)
898 int status;
899 int bcl_levels = 0;
900 void *bcl_ptr = &bcl_levels;
902 if (!vid_handle) {
903 TPACPI_ACPIHANDLE_INIT(vid);
905 if (!vid_handle)
906 return 0;
909 * Search for a _BCL method, and execute it. This is safe on all
910 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
911 * BIOS in ACPI backlight control mode. We do NOT have to care
912 * about calling the _BCL method in an enabled video device, any
913 * will do for our purposes.
916 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
917 tpacpi_acpi_walk_find_bcl, NULL,
918 &bcl_ptr);
920 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
921 tp_features.bright_acpimode = 1;
922 return (bcl_levels - 2);
925 return 0;
928 static int __init tpacpi_new_rfkill(const unsigned int id,
929 struct rfkill **rfk,
930 const enum rfkill_type rfktype,
931 const char *name,
932 int (*toggle_radio)(void *, enum rfkill_state),
933 int (*get_state)(void *, enum rfkill_state *))
935 int res;
936 enum rfkill_state initial_state;
938 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
939 if (!*rfk) {
940 printk(TPACPI_ERR
941 "failed to allocate memory for rfkill class\n");
942 return -ENOMEM;
945 (*rfk)->name = name;
946 (*rfk)->get_state = get_state;
947 (*rfk)->toggle_radio = toggle_radio;
949 if (!get_state(NULL, &initial_state))
950 (*rfk)->state = initial_state;
952 res = rfkill_register(*rfk);
953 if (res < 0) {
954 printk(TPACPI_ERR
955 "failed to register %s rfkill switch: %d\n",
956 name, res);
957 rfkill_free(*rfk);
958 *rfk = NULL;
959 return res;
962 return 0;
965 /*************************************************************************
966 * thinkpad-acpi driver attributes
969 /* interface_version --------------------------------------------------- */
970 static ssize_t tpacpi_driver_interface_version_show(
971 struct device_driver *drv,
972 char *buf)
974 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
977 static DRIVER_ATTR(interface_version, S_IRUGO,
978 tpacpi_driver_interface_version_show, NULL);
980 /* debug_level --------------------------------------------------------- */
981 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
982 char *buf)
984 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
987 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
988 const char *buf, size_t count)
990 unsigned long t;
992 if (parse_strtoul(buf, 0xffff, &t))
993 return -EINVAL;
995 dbg_level = t;
997 return count;
1000 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1001 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1003 /* version ------------------------------------------------------------- */
1004 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1005 char *buf)
1007 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1008 TPACPI_DESC, TPACPI_VERSION);
1011 static DRIVER_ATTR(version, S_IRUGO,
1012 tpacpi_driver_version_show, NULL);
1014 /* --------------------------------------------------------------------- */
1016 static struct driver_attribute *tpacpi_driver_attributes[] = {
1017 &driver_attr_debug_level, &driver_attr_version,
1018 &driver_attr_interface_version,
1021 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1023 int i, res;
1025 i = 0;
1026 res = 0;
1027 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1028 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1029 i++;
1032 return res;
1035 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1037 int i;
1039 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1040 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1043 /****************************************************************************
1044 ****************************************************************************
1046 * Subdrivers
1048 ****************************************************************************
1049 ****************************************************************************/
1051 /*************************************************************************
1052 * thinkpad-acpi init subdriver
1055 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1057 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1058 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1060 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1061 (thinkpad_id.bios_version_str) ?
1062 thinkpad_id.bios_version_str : "unknown",
1063 (thinkpad_id.ec_version_str) ?
1064 thinkpad_id.ec_version_str : "unknown");
1066 if (thinkpad_id.vendor && thinkpad_id.model_str)
1067 printk(TPACPI_INFO "%s %s, model %s\n",
1068 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1069 "IBM" : ((thinkpad_id.vendor ==
1070 PCI_VENDOR_ID_LENOVO) ?
1071 "Lenovo" : "Unknown vendor"),
1072 thinkpad_id.model_str,
1073 (thinkpad_id.nummodel_str) ?
1074 thinkpad_id.nummodel_str : "unknown");
1076 return 0;
1079 static int thinkpad_acpi_driver_read(char *p)
1081 int len = 0;
1083 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1084 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1086 return len;
1089 static struct ibm_struct thinkpad_acpi_driver_data = {
1090 .name = "driver",
1091 .read = thinkpad_acpi_driver_read,
1094 /*************************************************************************
1095 * Hotkey subdriver
1098 enum { /* hot key scan codes (derived from ACPI DSDT) */
1099 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1100 TP_ACPI_HOTKEYSCAN_FNF2,
1101 TP_ACPI_HOTKEYSCAN_FNF3,
1102 TP_ACPI_HOTKEYSCAN_FNF4,
1103 TP_ACPI_HOTKEYSCAN_FNF5,
1104 TP_ACPI_HOTKEYSCAN_FNF6,
1105 TP_ACPI_HOTKEYSCAN_FNF7,
1106 TP_ACPI_HOTKEYSCAN_FNF8,
1107 TP_ACPI_HOTKEYSCAN_FNF9,
1108 TP_ACPI_HOTKEYSCAN_FNF10,
1109 TP_ACPI_HOTKEYSCAN_FNF11,
1110 TP_ACPI_HOTKEYSCAN_FNF12,
1111 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1112 TP_ACPI_HOTKEYSCAN_FNINSERT,
1113 TP_ACPI_HOTKEYSCAN_FNDELETE,
1114 TP_ACPI_HOTKEYSCAN_FNHOME,
1115 TP_ACPI_HOTKEYSCAN_FNEND,
1116 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1117 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1118 TP_ACPI_HOTKEYSCAN_FNSPACE,
1119 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1120 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1121 TP_ACPI_HOTKEYSCAN_MUTE,
1122 TP_ACPI_HOTKEYSCAN_THINKPAD,
1125 enum { /* Keys available through NVRAM polling */
1126 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1127 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1130 enum { /* Positions of some of the keys in hotkey masks */
1131 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1132 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1133 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1134 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1135 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1136 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1137 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1138 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1139 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1140 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1141 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1144 enum { /* NVRAM to ACPI HKEY group map */
1145 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1146 TP_ACPI_HKEY_ZOOM_MASK |
1147 TP_ACPI_HKEY_DISPSWTCH_MASK |
1148 TP_ACPI_HKEY_HIBERNATE_MASK,
1149 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1150 TP_ACPI_HKEY_BRGHTDWN_MASK,
1151 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1152 TP_ACPI_HKEY_VOLDWN_MASK |
1153 TP_ACPI_HKEY_MUTE_MASK,
1156 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1157 struct tp_nvram_state {
1158 u16 thinkpad_toggle:1;
1159 u16 zoom_toggle:1;
1160 u16 display_toggle:1;
1161 u16 thinklight_toggle:1;
1162 u16 hibernate_toggle:1;
1163 u16 displayexp_toggle:1;
1164 u16 display_state:1;
1165 u16 brightness_toggle:1;
1166 u16 volume_toggle:1;
1167 u16 mute:1;
1169 u8 brightness_level;
1170 u8 volume_level;
1173 static struct task_struct *tpacpi_hotkey_task;
1174 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1175 static int hotkey_poll_freq = 10; /* Hz */
1176 static struct mutex hotkey_thread_mutex;
1177 static struct mutex hotkey_thread_data_mutex;
1178 static unsigned int hotkey_config_change;
1180 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1182 #define hotkey_source_mask 0U
1184 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1186 static struct mutex hotkey_mutex;
1188 static enum { /* Reasons for waking up */
1189 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1190 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1191 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1192 } hotkey_wakeup_reason;
1194 static int hotkey_autosleep_ack;
1196 static int hotkey_orig_status;
1197 static u32 hotkey_orig_mask;
1198 static u32 hotkey_all_mask;
1199 static u32 hotkey_reserved_mask;
1200 static u32 hotkey_mask;
1202 static unsigned int hotkey_report_mode;
1204 static u16 *hotkey_keycode_map;
1206 static struct attribute_set *hotkey_dev_attributes;
1208 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1209 #define HOTKEY_CONFIG_CRITICAL_START \
1210 do { \
1211 mutex_lock(&hotkey_thread_data_mutex); \
1212 hotkey_config_change++; \
1213 } while (0);
1214 #define HOTKEY_CONFIG_CRITICAL_END \
1215 mutex_unlock(&hotkey_thread_data_mutex);
1216 #else
1217 #define HOTKEY_CONFIG_CRITICAL_START
1218 #define HOTKEY_CONFIG_CRITICAL_END
1219 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1221 /* HKEY.MHKG() return bits */
1222 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1224 static int hotkey_get_wlsw(int *status)
1226 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1227 return -EIO;
1228 return 0;
1231 static int hotkey_get_tablet_mode(int *status)
1233 int s;
1235 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1236 return -EIO;
1238 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1239 return 0;
1243 * Call with hotkey_mutex held
1245 static int hotkey_mask_get(void)
1247 u32 m = 0;
1249 if (tp_features.hotkey_mask) {
1250 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1251 return -EIO;
1253 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1255 return 0;
1259 * Call with hotkey_mutex held
1261 static int hotkey_mask_set(u32 mask)
1263 int i;
1264 int rc = 0;
1266 if (tp_features.hotkey_mask) {
1267 if (!tp_warned.hotkey_mask_ff &&
1268 (mask == 0xffff || mask == 0xffffff ||
1269 mask == 0xffffffff)) {
1270 tp_warned.hotkey_mask_ff = 1;
1271 printk(TPACPI_NOTICE
1272 "setting the hotkey mask to 0x%08x is likely "
1273 "not the best way to go about it\n", mask);
1274 printk(TPACPI_NOTICE
1275 "please consider using the driver defaults, "
1276 "and refer to up-to-date thinkpad-acpi "
1277 "documentation\n");
1280 HOTKEY_CONFIG_CRITICAL_START
1281 for (i = 0; i < 32; i++) {
1282 u32 m = 1 << i;
1283 /* enable in firmware mask only keys not in NVRAM
1284 * mode, but enable the key in the cached hotkey_mask
1285 * regardless of mode, or the key will end up
1286 * disabled by hotkey_mask_get() */
1287 if (!acpi_evalf(hkey_handle,
1288 NULL, "MHKM", "vdd", i + 1,
1289 !!((mask & ~hotkey_source_mask) & m))) {
1290 rc = -EIO;
1291 break;
1292 } else {
1293 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1296 HOTKEY_CONFIG_CRITICAL_END
1298 /* hotkey_mask_get must be called unconditionally below */
1299 if (!hotkey_mask_get() && !rc &&
1300 (hotkey_mask & ~hotkey_source_mask) !=
1301 (mask & ~hotkey_source_mask)) {
1302 printk(TPACPI_NOTICE
1303 "requested hot key mask 0x%08x, but "
1304 "firmware forced it to 0x%08x\n",
1305 mask, hotkey_mask);
1307 } else {
1308 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1309 HOTKEY_CONFIG_CRITICAL_START
1310 hotkey_mask = mask & hotkey_source_mask;
1311 HOTKEY_CONFIG_CRITICAL_END
1312 hotkey_mask_get();
1313 if (hotkey_mask != mask) {
1314 printk(TPACPI_NOTICE
1315 "requested hot key mask 0x%08x, "
1316 "forced to 0x%08x (NVRAM poll mask is "
1317 "0x%08x): no firmware mask support\n",
1318 mask, hotkey_mask, hotkey_source_mask);
1320 #else
1321 hotkey_mask_get();
1322 rc = -ENXIO;
1323 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1326 return rc;
1329 static int hotkey_status_get(int *status)
1331 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1332 return -EIO;
1334 return 0;
1337 static int hotkey_status_set(int status)
1339 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1340 return -EIO;
1342 return 0;
1345 static void tpacpi_input_send_tabletsw(void)
1347 int state;
1349 if (tp_features.hotkey_tablet &&
1350 !hotkey_get_tablet_mode(&state)) {
1351 mutex_lock(&tpacpi_inputdev_send_mutex);
1353 input_report_switch(tpacpi_inputdev,
1354 SW_TABLET_MODE, !!state);
1355 input_sync(tpacpi_inputdev);
1357 mutex_unlock(&tpacpi_inputdev_send_mutex);
1361 static void tpacpi_input_send_key(unsigned int scancode)
1363 unsigned int keycode;
1365 keycode = hotkey_keycode_map[scancode];
1367 if (keycode != KEY_RESERVED) {
1368 mutex_lock(&tpacpi_inputdev_send_mutex);
1370 input_report_key(tpacpi_inputdev, keycode, 1);
1371 if (keycode == KEY_UNKNOWN)
1372 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1373 scancode);
1374 input_sync(tpacpi_inputdev);
1376 input_report_key(tpacpi_inputdev, keycode, 0);
1377 if (keycode == KEY_UNKNOWN)
1378 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1379 scancode);
1380 input_sync(tpacpi_inputdev);
1382 mutex_unlock(&tpacpi_inputdev_send_mutex);
1386 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1387 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1389 static void tpacpi_hotkey_send_key(unsigned int scancode)
1391 tpacpi_input_send_key(scancode);
1392 if (hotkey_report_mode < 2) {
1393 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1394 0x80, 0x1001 + scancode);
1398 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1400 u8 d;
1402 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1403 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1404 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1405 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1406 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1407 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1409 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1410 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1411 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1413 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1414 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1415 n->displayexp_toggle =
1416 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1418 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1419 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1420 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1421 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1422 n->brightness_toggle =
1423 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1425 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1426 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1427 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1428 >> TP_NVRAM_POS_LEVEL_VOLUME;
1429 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1430 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1434 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1435 do { \
1436 if ((mask & (1 << __scancode)) && \
1437 oldn->__member != newn->__member) \
1438 tpacpi_hotkey_send_key(__scancode); \
1439 } while (0)
1441 #define TPACPI_MAY_SEND_KEY(__scancode) \
1442 do { if (mask & (1 << __scancode)) \
1443 tpacpi_hotkey_send_key(__scancode); } while (0)
1445 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1446 struct tp_nvram_state *newn,
1447 u32 mask)
1449 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1450 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1451 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1452 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1454 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1456 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1458 /* handle volume */
1459 if (oldn->volume_toggle != newn->volume_toggle) {
1460 if (oldn->mute != newn->mute) {
1461 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1463 if (oldn->volume_level > newn->volume_level) {
1464 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1465 } else if (oldn->volume_level < newn->volume_level) {
1466 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1467 } else if (oldn->mute == newn->mute) {
1468 /* repeated key presses that didn't change state */
1469 if (newn->mute) {
1470 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1471 } else if (newn->volume_level != 0) {
1472 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1473 } else {
1474 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1479 /* handle brightness */
1480 if (oldn->brightness_toggle != newn->brightness_toggle) {
1481 if (oldn->brightness_level < newn->brightness_level) {
1482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1483 } else if (oldn->brightness_level > newn->brightness_level) {
1484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1485 } else {
1486 /* repeated key presses that didn't change state */
1487 if (newn->brightness_level != 0) {
1488 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1489 } else {
1490 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1496 #undef TPACPI_COMPARE_KEY
1497 #undef TPACPI_MAY_SEND_KEY
1499 static int hotkey_kthread(void *data)
1501 struct tp_nvram_state s[2];
1502 u32 mask;
1503 unsigned int si, so;
1504 unsigned long t;
1505 unsigned int change_detector, must_reset;
1507 mutex_lock(&hotkey_thread_mutex);
1509 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1510 goto exit;
1512 set_freezable();
1514 so = 0;
1515 si = 1;
1516 t = 0;
1518 /* Initial state for compares */
1519 mutex_lock(&hotkey_thread_data_mutex);
1520 change_detector = hotkey_config_change;
1521 mask = hotkey_source_mask & hotkey_mask;
1522 mutex_unlock(&hotkey_thread_data_mutex);
1523 hotkey_read_nvram(&s[so], mask);
1525 while (!kthread_should_stop() && hotkey_poll_freq) {
1526 if (t == 0)
1527 t = 1000/hotkey_poll_freq;
1528 t = msleep_interruptible(t);
1529 if (unlikely(kthread_should_stop()))
1530 break;
1531 must_reset = try_to_freeze();
1532 if (t > 0 && !must_reset)
1533 continue;
1535 mutex_lock(&hotkey_thread_data_mutex);
1536 if (must_reset || hotkey_config_change != change_detector) {
1537 /* forget old state on thaw or config change */
1538 si = so;
1539 t = 0;
1540 change_detector = hotkey_config_change;
1542 mask = hotkey_source_mask & hotkey_mask;
1543 mutex_unlock(&hotkey_thread_data_mutex);
1545 if (likely(mask)) {
1546 hotkey_read_nvram(&s[si], mask);
1547 if (likely(si != so)) {
1548 hotkey_compare_and_issue_event(&s[so], &s[si],
1549 mask);
1553 so = si;
1554 si ^= 1;
1557 exit:
1558 mutex_unlock(&hotkey_thread_mutex);
1559 return 0;
1562 static void hotkey_poll_stop_sync(void)
1564 if (tpacpi_hotkey_task) {
1565 if (frozen(tpacpi_hotkey_task) ||
1566 freezing(tpacpi_hotkey_task))
1567 thaw_process(tpacpi_hotkey_task);
1569 kthread_stop(tpacpi_hotkey_task);
1570 tpacpi_hotkey_task = NULL;
1571 mutex_lock(&hotkey_thread_mutex);
1572 /* at this point, the thread did exit */
1573 mutex_unlock(&hotkey_thread_mutex);
1577 /* call with hotkey_mutex held */
1578 static void hotkey_poll_setup(int may_warn)
1580 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1581 hotkey_poll_freq > 0 &&
1582 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1583 if (!tpacpi_hotkey_task) {
1584 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1585 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1586 if (IS_ERR(tpacpi_hotkey_task)) {
1587 tpacpi_hotkey_task = NULL;
1588 printk(TPACPI_ERR
1589 "could not create kernel thread "
1590 "for hotkey polling\n");
1593 } else {
1594 hotkey_poll_stop_sync();
1595 if (may_warn &&
1596 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1597 printk(TPACPI_NOTICE
1598 "hot keys 0x%08x require polling, "
1599 "which is currently disabled\n",
1600 hotkey_source_mask);
1605 static void hotkey_poll_setup_safe(int may_warn)
1607 mutex_lock(&hotkey_mutex);
1608 hotkey_poll_setup(may_warn);
1609 mutex_unlock(&hotkey_mutex);
1612 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1614 static void hotkey_poll_setup_safe(int __unused)
1618 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1620 static int hotkey_inputdev_open(struct input_dev *dev)
1622 switch (tpacpi_lifecycle) {
1623 case TPACPI_LIFE_INIT:
1625 * hotkey_init will call hotkey_poll_setup_safe
1626 * at the appropriate moment
1628 return 0;
1629 case TPACPI_LIFE_EXITING:
1630 return -EBUSY;
1631 case TPACPI_LIFE_RUNNING:
1632 hotkey_poll_setup_safe(0);
1633 return 0;
1636 /* Should only happen if tpacpi_lifecycle is corrupt */
1637 BUG();
1638 return -EBUSY;
1641 static void hotkey_inputdev_close(struct input_dev *dev)
1643 /* disable hotkey polling when possible */
1644 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1645 hotkey_poll_setup_safe(0);
1648 /* sysfs hotkey enable ------------------------------------------------- */
1649 static ssize_t hotkey_enable_show(struct device *dev,
1650 struct device_attribute *attr,
1651 char *buf)
1653 int res, status;
1655 res = hotkey_status_get(&status);
1656 if (res)
1657 return res;
1659 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1662 static ssize_t hotkey_enable_store(struct device *dev,
1663 struct device_attribute *attr,
1664 const char *buf, size_t count)
1666 unsigned long t;
1667 int res;
1669 if (parse_strtoul(buf, 1, &t))
1670 return -EINVAL;
1672 res = hotkey_status_set(t);
1674 return (res) ? res : count;
1677 static struct device_attribute dev_attr_hotkey_enable =
1678 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1679 hotkey_enable_show, hotkey_enable_store);
1681 /* sysfs hotkey mask --------------------------------------------------- */
1682 static ssize_t hotkey_mask_show(struct device *dev,
1683 struct device_attribute *attr,
1684 char *buf)
1686 int res;
1688 if (mutex_lock_interruptible(&hotkey_mutex))
1689 return -ERESTARTSYS;
1690 res = hotkey_mask_get();
1691 mutex_unlock(&hotkey_mutex);
1693 return (res)?
1694 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1697 static ssize_t hotkey_mask_store(struct device *dev,
1698 struct device_attribute *attr,
1699 const char *buf, size_t count)
1701 unsigned long t;
1702 int res;
1704 if (parse_strtoul(buf, 0xffffffffUL, &t))
1705 return -EINVAL;
1707 if (mutex_lock_interruptible(&hotkey_mutex))
1708 return -ERESTARTSYS;
1710 res = hotkey_mask_set(t);
1712 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1713 hotkey_poll_setup(1);
1714 #endif
1716 mutex_unlock(&hotkey_mutex);
1718 return (res) ? res : count;
1721 static struct device_attribute dev_attr_hotkey_mask =
1722 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1723 hotkey_mask_show, hotkey_mask_store);
1725 /* sysfs hotkey bios_enabled ------------------------------------------- */
1726 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1727 struct device_attribute *attr,
1728 char *buf)
1730 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1733 static struct device_attribute dev_attr_hotkey_bios_enabled =
1734 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1736 /* sysfs hotkey bios_mask ---------------------------------------------- */
1737 static ssize_t hotkey_bios_mask_show(struct device *dev,
1738 struct device_attribute *attr,
1739 char *buf)
1741 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1744 static struct device_attribute dev_attr_hotkey_bios_mask =
1745 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1747 /* sysfs hotkey all_mask ----------------------------------------------- */
1748 static ssize_t hotkey_all_mask_show(struct device *dev,
1749 struct device_attribute *attr,
1750 char *buf)
1752 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1753 hotkey_all_mask | hotkey_source_mask);
1756 static struct device_attribute dev_attr_hotkey_all_mask =
1757 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1759 /* sysfs hotkey recommended_mask --------------------------------------- */
1760 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1761 struct device_attribute *attr,
1762 char *buf)
1764 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1765 (hotkey_all_mask | hotkey_source_mask)
1766 & ~hotkey_reserved_mask);
1769 static struct device_attribute dev_attr_hotkey_recommended_mask =
1770 __ATTR(hotkey_recommended_mask, S_IRUGO,
1771 hotkey_recommended_mask_show, NULL);
1773 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1775 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1776 static ssize_t hotkey_source_mask_show(struct device *dev,
1777 struct device_attribute *attr,
1778 char *buf)
1780 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1783 static ssize_t hotkey_source_mask_store(struct device *dev,
1784 struct device_attribute *attr,
1785 const char *buf, size_t count)
1787 unsigned long t;
1789 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1790 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1791 return -EINVAL;
1793 if (mutex_lock_interruptible(&hotkey_mutex))
1794 return -ERESTARTSYS;
1796 HOTKEY_CONFIG_CRITICAL_START
1797 hotkey_source_mask = t;
1798 HOTKEY_CONFIG_CRITICAL_END
1800 hotkey_poll_setup(1);
1802 mutex_unlock(&hotkey_mutex);
1804 return count;
1807 static struct device_attribute dev_attr_hotkey_source_mask =
1808 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1809 hotkey_source_mask_show, hotkey_source_mask_store);
1811 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1812 static ssize_t hotkey_poll_freq_show(struct device *dev,
1813 struct device_attribute *attr,
1814 char *buf)
1816 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1819 static ssize_t hotkey_poll_freq_store(struct device *dev,
1820 struct device_attribute *attr,
1821 const char *buf, size_t count)
1823 unsigned long t;
1825 if (parse_strtoul(buf, 25, &t))
1826 return -EINVAL;
1828 if (mutex_lock_interruptible(&hotkey_mutex))
1829 return -ERESTARTSYS;
1831 hotkey_poll_freq = t;
1833 hotkey_poll_setup(1);
1834 mutex_unlock(&hotkey_mutex);
1836 return count;
1839 static struct device_attribute dev_attr_hotkey_poll_freq =
1840 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1841 hotkey_poll_freq_show, hotkey_poll_freq_store);
1843 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1845 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1846 static ssize_t hotkey_radio_sw_show(struct device *dev,
1847 struct device_attribute *attr,
1848 char *buf)
1850 int res, s;
1851 res = hotkey_get_wlsw(&s);
1852 if (res < 0)
1853 return res;
1855 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1858 static struct device_attribute dev_attr_hotkey_radio_sw =
1859 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1861 static void hotkey_radio_sw_notify_change(void)
1863 if (tp_features.hotkey_wlsw)
1864 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1865 "hotkey_radio_sw");
1868 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1869 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1870 struct device_attribute *attr,
1871 char *buf)
1873 int res, s;
1874 res = hotkey_get_tablet_mode(&s);
1875 if (res < 0)
1876 return res;
1878 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1881 static struct device_attribute dev_attr_hotkey_tablet_mode =
1882 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1884 static void hotkey_tablet_mode_notify_change(void)
1886 if (tp_features.hotkey_tablet)
1887 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1888 "hotkey_tablet_mode");
1891 /* sysfs hotkey report_mode -------------------------------------------- */
1892 static ssize_t hotkey_report_mode_show(struct device *dev,
1893 struct device_attribute *attr,
1894 char *buf)
1896 return snprintf(buf, PAGE_SIZE, "%d\n",
1897 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1900 static struct device_attribute dev_attr_hotkey_report_mode =
1901 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1903 /* sysfs wakeup reason (pollable) -------------------------------------- */
1904 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1905 struct device_attribute *attr,
1906 char *buf)
1908 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1911 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1912 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1914 static void hotkey_wakeup_reason_notify_change(void)
1916 if (tp_features.hotkey_mask)
1917 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1918 "wakeup_reason");
1921 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1922 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1923 struct device_attribute *attr,
1924 char *buf)
1926 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1929 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1930 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1931 hotkey_wakeup_hotunplug_complete_show, NULL);
1933 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1935 if (tp_features.hotkey_mask)
1936 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1937 "wakeup_hotunplug_complete");
1940 /* --------------------------------------------------------------------- */
1942 static struct attribute *hotkey_attributes[] __initdata = {
1943 &dev_attr_hotkey_enable.attr,
1944 &dev_attr_hotkey_bios_enabled.attr,
1945 &dev_attr_hotkey_report_mode.attr,
1946 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1947 &dev_attr_hotkey_mask.attr,
1948 &dev_attr_hotkey_all_mask.attr,
1949 &dev_attr_hotkey_recommended_mask.attr,
1950 &dev_attr_hotkey_source_mask.attr,
1951 &dev_attr_hotkey_poll_freq.attr,
1952 #endif
1955 static struct attribute *hotkey_mask_attributes[] __initdata = {
1956 &dev_attr_hotkey_bios_mask.attr,
1957 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1958 &dev_attr_hotkey_mask.attr,
1959 &dev_attr_hotkey_all_mask.attr,
1960 &dev_attr_hotkey_recommended_mask.attr,
1961 #endif
1962 &dev_attr_hotkey_wakeup_reason.attr,
1963 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1966 static void bluetooth_update_rfk(void);
1967 static void wan_update_rfk(void);
1968 static void tpacpi_send_radiosw_update(void)
1970 int wlsw;
1972 /* Sync these BEFORE sending any rfkill events */
1973 if (tp_features.bluetooth)
1974 bluetooth_update_rfk();
1975 if (tp_features.wan)
1976 wan_update_rfk();
1978 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1979 mutex_lock(&tpacpi_inputdev_send_mutex);
1981 input_report_switch(tpacpi_inputdev,
1982 SW_RFKILL_ALL, !!wlsw);
1983 input_sync(tpacpi_inputdev);
1985 mutex_unlock(&tpacpi_inputdev_send_mutex);
1987 hotkey_radio_sw_notify_change();
1990 static void hotkey_exit(void)
1992 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1993 hotkey_poll_stop_sync();
1994 #endif
1996 if (hotkey_dev_attributes)
1997 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1999 kfree(hotkey_keycode_map);
2001 if (tp_features.hotkey) {
2002 dbg_printk(TPACPI_DBG_EXIT,
2003 "restoring original hot key mask\n");
2004 /* no short-circuit boolean operator below! */
2005 if ((hotkey_mask_set(hotkey_orig_mask) |
2006 hotkey_status_set(hotkey_orig_status)) != 0)
2007 printk(TPACPI_ERR
2008 "failed to restore hot key mask "
2009 "to BIOS defaults\n");
2013 static int __init hotkey_init(struct ibm_init_struct *iibm)
2015 /* Requirements for changing the default keymaps:
2017 * 1. Many of the keys are mapped to KEY_RESERVED for very
2018 * good reasons. Do not change them unless you have deep
2019 * knowledge on the IBM and Lenovo ThinkPad firmware for
2020 * the various ThinkPad models. The driver behaves
2021 * differently for KEY_RESERVED: such keys have their
2022 * hot key mask *unset* in mask_recommended, and also
2023 * in the initial hot key mask programmed into the
2024 * firmware at driver load time, which means the firm-
2025 * ware may react very differently if you change them to
2026 * something else;
2028 * 2. You must be subscribed to the linux-thinkpad and
2029 * ibm-acpi-devel mailing lists, and you should read the
2030 * list archives since 2007 if you want to change the
2031 * keymaps. This requirement exists so that you will
2032 * know the past history of problems with the thinkpad-
2033 * acpi driver keymaps, and also that you will be
2034 * listening to any bug reports;
2036 * 3. Do not send thinkpad-acpi specific patches directly to
2037 * for merging, *ever*. Send them to the linux-acpi
2038 * mailinglist for comments. Merging is to be done only
2039 * through acpi-test and the ACPI maintainer.
2041 * If the above is too much to ask, don't change the keymap.
2042 * Ask the thinkpad-acpi maintainer to do it, instead.
2044 static u16 ibm_keycode_map[] __initdata = {
2045 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2046 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2047 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2048 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2050 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2051 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2052 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2053 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2055 /* brightness: firmware always reacts to them, unless
2056 * X.org did some tricks in the radeon BIOS scratch
2057 * registers of *some* models */
2058 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2059 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2061 /* Thinklight: firmware always react to it */
2062 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2064 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2065 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2067 /* Volume: firmware always react to it and reprograms
2068 * the built-in *extra* mixer. Never map it to control
2069 * another mixer by default. */
2070 KEY_RESERVED, /* 0x14: VOLUME UP */
2071 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2072 KEY_RESERVED, /* 0x16: MUTE */
2074 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2076 /* (assignments unknown, please report if found) */
2077 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2078 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2080 static u16 lenovo_keycode_map[] __initdata = {
2081 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2082 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2083 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2084 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2086 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2087 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2088 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2089 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2091 /* These either have to go through ACPI video, or
2092 * act like in the IBM ThinkPads, so don't ever
2093 * enable them by default */
2094 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2095 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2097 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2099 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2100 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2102 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2103 * react to it and reprograms the built-in *extra* mixer.
2104 * Never map it to control another mixer by default.
2106 * T60?, T61, R60?, R61: firmware and EC tries to send
2107 * these over the regular keyboard, so these are no-ops,
2108 * but there are still weird bugs re. MUTE, so do not
2109 * change unless you get test reports from all Lenovo
2110 * models. May cause the BIOS to interfere with the
2111 * HDA mixer.
2113 KEY_RESERVED, /* 0x14: VOLUME UP */
2114 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2115 KEY_RESERVED, /* 0x16: MUTE */
2117 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2119 /* (assignments unknown, please report if found) */
2120 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2121 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2124 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2125 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2126 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2128 int res, i;
2129 int status;
2130 int hkeyv;
2132 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2134 BUG_ON(!tpacpi_inputdev);
2135 BUG_ON(tpacpi_inputdev->open != NULL ||
2136 tpacpi_inputdev->close != NULL);
2138 TPACPI_ACPIHANDLE_INIT(hkey);
2139 mutex_init(&hotkey_mutex);
2141 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2142 mutex_init(&hotkey_thread_mutex);
2143 mutex_init(&hotkey_thread_data_mutex);
2144 #endif
2146 /* hotkey not supported on 570 */
2147 tp_features.hotkey = hkey_handle != NULL;
2149 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2150 str_supported(tp_features.hotkey));
2152 if (!tp_features.hotkey)
2153 return 1;
2155 tpacpi_disable_brightness_delay();
2157 hotkey_dev_attributes = create_attr_set(13, NULL);
2158 if (!hotkey_dev_attributes)
2159 return -ENOMEM;
2160 res = add_many_to_attr_set(hotkey_dev_attributes,
2161 hotkey_attributes,
2162 ARRAY_SIZE(hotkey_attributes));
2163 if (res)
2164 goto err_exit;
2166 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2167 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2168 for HKEY interface version 0x100 */
2169 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2170 if ((hkeyv >> 8) != 1) {
2171 printk(TPACPI_ERR "unknown version of the "
2172 "HKEY interface: 0x%x\n", hkeyv);
2173 printk(TPACPI_ERR "please report this to %s\n",
2174 TPACPI_MAIL);
2175 } else {
2177 * MHKV 0x100 in A31, R40, R40e,
2178 * T4x, X31, and later
2180 tp_features.hotkey_mask = 1;
2184 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2185 str_supported(tp_features.hotkey_mask));
2187 if (tp_features.hotkey_mask) {
2188 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2189 "MHKA", "qd")) {
2190 printk(TPACPI_ERR
2191 "missing MHKA handler, "
2192 "please report this to %s\n",
2193 TPACPI_MAIL);
2194 /* FN+F12, FN+F4, FN+F3 */
2195 hotkey_all_mask = 0x080cU;
2199 /* hotkey_source_mask *must* be zero for
2200 * the first hotkey_mask_get */
2201 res = hotkey_status_get(&hotkey_orig_status);
2202 if (res)
2203 goto err_exit;
2205 if (tp_features.hotkey_mask) {
2206 res = hotkey_mask_get();
2207 if (res)
2208 goto err_exit;
2210 hotkey_orig_mask = hotkey_mask;
2211 res = add_many_to_attr_set(
2212 hotkey_dev_attributes,
2213 hotkey_mask_attributes,
2214 ARRAY_SIZE(hotkey_mask_attributes));
2215 if (res)
2216 goto err_exit;
2219 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2220 if (tp_features.hotkey_mask) {
2221 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2222 & ~hotkey_all_mask;
2223 } else {
2224 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2227 vdbg_printk(TPACPI_DBG_INIT,
2228 "hotkey source mask 0x%08x, polling freq %d\n",
2229 hotkey_source_mask, hotkey_poll_freq);
2230 #endif
2232 /* Not all thinkpads have a hardware radio switch */
2233 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2234 tp_features.hotkey_wlsw = 1;
2235 printk(TPACPI_INFO
2236 "radio switch found; radios are %s\n",
2237 enabled(status, 0));
2239 if (tp_features.hotkey_wlsw)
2240 res = add_to_attr_set(hotkey_dev_attributes,
2241 &dev_attr_hotkey_radio_sw.attr);
2243 /* For X41t, X60t, X61t Tablets... */
2244 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2245 tp_features.hotkey_tablet = 1;
2246 printk(TPACPI_INFO
2247 "possible tablet mode switch found; "
2248 "ThinkPad in %s mode\n",
2249 (status & TP_HOTKEY_TABLET_MASK)?
2250 "tablet" : "laptop");
2251 res = add_to_attr_set(hotkey_dev_attributes,
2252 &dev_attr_hotkey_tablet_mode.attr);
2255 if (!res)
2256 res = register_attr_set_with_sysfs(
2257 hotkey_dev_attributes,
2258 &tpacpi_pdev->dev.kobj);
2259 if (res)
2260 goto err_exit;
2262 /* Set up key map */
2264 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2265 GFP_KERNEL);
2266 if (!hotkey_keycode_map) {
2267 printk(TPACPI_ERR
2268 "failed to allocate memory for key map\n");
2269 res = -ENOMEM;
2270 goto err_exit;
2273 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2274 dbg_printk(TPACPI_DBG_INIT,
2275 "using Lenovo default hot key map\n");
2276 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2277 TPACPI_HOTKEY_MAP_SIZE);
2278 } else {
2279 dbg_printk(TPACPI_DBG_INIT,
2280 "using IBM default hot key map\n");
2281 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2282 TPACPI_HOTKEY_MAP_SIZE);
2285 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2286 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2287 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2288 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2289 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2290 tpacpi_inputdev->keycode = hotkey_keycode_map;
2291 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2292 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2293 set_bit(hotkey_keycode_map[i],
2294 tpacpi_inputdev->keybit);
2295 } else {
2296 if (i < sizeof(hotkey_reserved_mask)*8)
2297 hotkey_reserved_mask |= 1 << i;
2301 if (tp_features.hotkey_wlsw) {
2302 set_bit(EV_SW, tpacpi_inputdev->evbit);
2303 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2305 if (tp_features.hotkey_tablet) {
2306 set_bit(EV_SW, tpacpi_inputdev->evbit);
2307 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2310 /* Do not issue duplicate brightness change events to
2311 * userspace */
2312 if (!tp_features.bright_acpimode)
2313 /* update bright_acpimode... */
2314 tpacpi_check_std_acpi_brightness_support();
2316 if (tp_features.bright_acpimode) {
2317 printk(TPACPI_INFO
2318 "This ThinkPad has standard ACPI backlight "
2319 "brightness control, supported by the ACPI "
2320 "video driver\n");
2321 printk(TPACPI_NOTICE
2322 "Disabling thinkpad-acpi brightness events "
2323 "by default...\n");
2325 /* The hotkey_reserved_mask change below is not
2326 * necessary while the keys are at KEY_RESERVED in the
2327 * default map, but better safe than sorry, leave it
2328 * here as a marker of what we have to do, especially
2329 * when we finally become able to set this at runtime
2330 * on response to X.org requests */
2331 hotkey_reserved_mask |=
2332 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2333 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2336 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2337 res = hotkey_status_set(1);
2338 if (res) {
2339 hotkey_exit();
2340 return res;
2342 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2343 & ~hotkey_reserved_mask)
2344 | hotkey_orig_mask);
2345 if (res < 0 && res != -ENXIO) {
2346 hotkey_exit();
2347 return res;
2350 dbg_printk(TPACPI_DBG_INIT,
2351 "legacy hot key reporting over procfs %s\n",
2352 (hotkey_report_mode < 2) ?
2353 "enabled" : "disabled");
2355 tpacpi_inputdev->open = &hotkey_inputdev_open;
2356 tpacpi_inputdev->close = &hotkey_inputdev_close;
2358 hotkey_poll_setup_safe(1);
2359 tpacpi_send_radiosw_update();
2360 tpacpi_input_send_tabletsw();
2362 return 0;
2364 err_exit:
2365 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2366 hotkey_dev_attributes = NULL;
2368 return (res < 0)? res : 1;
2371 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2373 u32 hkey;
2374 unsigned int scancode;
2375 int send_acpi_ev;
2376 int ignore_acpi_ev;
2377 int unk_ev;
2379 if (event != 0x80) {
2380 printk(TPACPI_ERR
2381 "unknown HKEY notification event %d\n", event);
2382 /* forward it to userspace, maybe it knows how to handle it */
2383 acpi_bus_generate_netlink_event(
2384 ibm->acpi->device->pnp.device_class,
2385 ibm->acpi->device->dev.bus_id,
2386 event, 0);
2387 return;
2390 while (1) {
2391 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2392 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2393 return;
2396 if (hkey == 0) {
2397 /* queue empty */
2398 return;
2401 send_acpi_ev = 1;
2402 ignore_acpi_ev = 0;
2403 unk_ev = 0;
2405 switch (hkey >> 12) {
2406 case 1:
2407 /* 0x1000-0x1FFF: key presses */
2408 scancode = hkey & 0xfff;
2409 if (scancode > 0 && scancode < 0x21) {
2410 scancode--;
2411 if (!(hotkey_source_mask & (1 << scancode))) {
2412 tpacpi_input_send_key(scancode);
2413 send_acpi_ev = 0;
2414 } else {
2415 ignore_acpi_ev = 1;
2417 } else {
2418 unk_ev = 1;
2420 break;
2421 case 2:
2422 /* Wakeup reason */
2423 switch (hkey) {
2424 case 0x2304: /* suspend, undock */
2425 case 0x2404: /* hibernation, undock */
2426 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2427 ignore_acpi_ev = 1;
2428 break;
2429 case 0x2305: /* suspend, bay eject */
2430 case 0x2405: /* hibernation, bay eject */
2431 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2432 ignore_acpi_ev = 1;
2433 break;
2434 default:
2435 unk_ev = 1;
2437 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2438 printk(TPACPI_INFO
2439 "woke up due to a hot-unplug "
2440 "request...\n");
2441 hotkey_wakeup_reason_notify_change();
2443 break;
2444 case 3:
2445 /* bay-related wakeups */
2446 if (hkey == 0x3003) {
2447 hotkey_autosleep_ack = 1;
2448 printk(TPACPI_INFO
2449 "bay ejected\n");
2450 hotkey_wakeup_hotunplug_complete_notify_change();
2451 } else {
2452 unk_ev = 1;
2454 break;
2455 case 4:
2456 /* dock-related wakeups */
2457 if (hkey == 0x4003) {
2458 hotkey_autosleep_ack = 1;
2459 printk(TPACPI_INFO
2460 "undocked\n");
2461 hotkey_wakeup_hotunplug_complete_notify_change();
2462 } else {
2463 unk_ev = 1;
2465 break;
2466 case 5:
2467 /* 0x5000-0x5FFF: human interface helpers */
2468 switch (hkey) {
2469 case 0x5010: /* Lenovo new BIOS: brightness changed */
2470 case 0x500b: /* X61t: tablet pen inserted into bay */
2471 case 0x500c: /* X61t: tablet pen removed from bay */
2472 break;
2473 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2474 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2475 tpacpi_input_send_tabletsw();
2476 hotkey_tablet_mode_notify_change();
2477 send_acpi_ev = 0;
2478 break;
2479 case 0x5001:
2480 case 0x5002:
2481 /* LID switch events. Do not propagate */
2482 ignore_acpi_ev = 1;
2483 break;
2484 default:
2485 unk_ev = 1;
2487 break;
2488 case 7:
2489 /* 0x7000-0x7FFF: misc */
2490 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2491 tpacpi_send_radiosw_update();
2492 send_acpi_ev = 0;
2493 break;
2495 /* fallthrough to default */
2496 default:
2497 unk_ev = 1;
2499 if (unk_ev) {
2500 printk(TPACPI_NOTICE
2501 "unhandled HKEY event 0x%04x\n", hkey);
2504 /* Legacy events */
2505 if (!ignore_acpi_ev &&
2506 (send_acpi_ev || hotkey_report_mode < 2)) {
2507 acpi_bus_generate_proc_event(ibm->acpi->device,
2508 event, hkey);
2511 /* netlink events */
2512 if (!ignore_acpi_ev && send_acpi_ev) {
2513 acpi_bus_generate_netlink_event(
2514 ibm->acpi->device->pnp.device_class,
2515 ibm->acpi->device->dev.bus_id,
2516 event, hkey);
2521 static void hotkey_suspend(pm_message_t state)
2523 /* Do these on suspend, we get the events on early resume! */
2524 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2525 hotkey_autosleep_ack = 0;
2528 static void hotkey_resume(void)
2530 tpacpi_disable_brightness_delay();
2532 if (hotkey_mask_get())
2533 printk(TPACPI_ERR
2534 "error while trying to read hot key mask "
2535 "from firmware\n");
2536 tpacpi_send_radiosw_update();
2537 hotkey_tablet_mode_notify_change();
2538 hotkey_wakeup_reason_notify_change();
2539 hotkey_wakeup_hotunplug_complete_notify_change();
2540 hotkey_poll_setup_safe(0);
2543 /* procfs -------------------------------------------------------------- */
2544 static int hotkey_read(char *p)
2546 int res, status;
2547 int len = 0;
2549 if (!tp_features.hotkey) {
2550 len += sprintf(p + len, "status:\t\tnot supported\n");
2551 return len;
2554 if (mutex_lock_interruptible(&hotkey_mutex))
2555 return -ERESTARTSYS;
2556 res = hotkey_status_get(&status);
2557 if (!res)
2558 res = hotkey_mask_get();
2559 mutex_unlock(&hotkey_mutex);
2560 if (res)
2561 return res;
2563 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2564 if (tp_features.hotkey_mask) {
2565 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2566 len += sprintf(p + len,
2567 "commands:\tenable, disable, reset, <mask>\n");
2568 } else {
2569 len += sprintf(p + len, "mask:\t\tnot supported\n");
2570 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2573 return len;
2576 static int hotkey_write(char *buf)
2578 int res, status;
2579 u32 mask;
2580 char *cmd;
2582 if (!tp_features.hotkey)
2583 return -ENODEV;
2585 if (mutex_lock_interruptible(&hotkey_mutex))
2586 return -ERESTARTSYS;
2588 status = -1;
2589 mask = hotkey_mask;
2591 res = 0;
2592 while ((cmd = next_cmd(&buf))) {
2593 if (strlencmp(cmd, "enable") == 0) {
2594 status = 1;
2595 } else if (strlencmp(cmd, "disable") == 0) {
2596 status = 0;
2597 } else if (strlencmp(cmd, "reset") == 0) {
2598 status = hotkey_orig_status;
2599 mask = hotkey_orig_mask;
2600 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2601 /* mask set */
2602 } else if (sscanf(cmd, "%x", &mask) == 1) {
2603 /* mask set */
2604 } else {
2605 res = -EINVAL;
2606 goto errexit;
2609 if (status != -1)
2610 res = hotkey_status_set(status);
2612 if (!res && mask != hotkey_mask)
2613 res = hotkey_mask_set(mask);
2615 errexit:
2616 mutex_unlock(&hotkey_mutex);
2617 return res;
2620 static const struct acpi_device_id ibm_htk_device_ids[] = {
2621 {TPACPI_ACPI_HKEY_HID, 0},
2622 {"", 0},
2625 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2626 .hid = ibm_htk_device_ids,
2627 .notify = hotkey_notify,
2628 .handle = &hkey_handle,
2629 .type = ACPI_DEVICE_NOTIFY,
2632 static struct ibm_struct hotkey_driver_data = {
2633 .name = "hotkey",
2634 .read = hotkey_read,
2635 .write = hotkey_write,
2636 .exit = hotkey_exit,
2637 .resume = hotkey_resume,
2638 .suspend = hotkey_suspend,
2639 .acpi = &ibm_hotkey_acpidriver,
2642 /*************************************************************************
2643 * Bluetooth subdriver
2646 enum {
2647 /* ACPI GBDC/SBDC bits */
2648 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2649 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2650 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2653 static struct rfkill *tpacpi_bluetooth_rfkill;
2655 static int bluetooth_get_radiosw(void)
2657 int status;
2659 if (!tp_features.bluetooth)
2660 return -ENODEV;
2662 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2663 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2664 return RFKILL_STATE_HARD_BLOCKED;
2666 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2667 return -EIO;
2669 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2670 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2673 static void bluetooth_update_rfk(void)
2675 int status;
2677 if (!tpacpi_bluetooth_rfkill)
2678 return;
2680 status = bluetooth_get_radiosw();
2681 if (status < 0)
2682 return;
2683 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2686 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2688 int status;
2690 if (!tp_features.bluetooth)
2691 return -ENODEV;
2693 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2694 * reason to risk weird behaviour. */
2695 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2696 && radio_on)
2697 return -EPERM;
2699 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2700 return -EIO;
2701 if (radio_on)
2702 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2703 else
2704 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2705 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2706 return -EIO;
2708 if (update_rfk)
2709 bluetooth_update_rfk();
2711 return 0;
2714 /* sysfs bluetooth enable ---------------------------------------------- */
2715 static ssize_t bluetooth_enable_show(struct device *dev,
2716 struct device_attribute *attr,
2717 char *buf)
2719 int status;
2721 status = bluetooth_get_radiosw();
2722 if (status < 0)
2723 return status;
2725 return snprintf(buf, PAGE_SIZE, "%d\n",
2726 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2729 static ssize_t bluetooth_enable_store(struct device *dev,
2730 struct device_attribute *attr,
2731 const char *buf, size_t count)
2733 unsigned long t;
2734 int res;
2736 if (parse_strtoul(buf, 1, &t))
2737 return -EINVAL;
2739 res = bluetooth_set_radiosw(t, 1);
2741 return (res) ? res : count;
2744 static struct device_attribute dev_attr_bluetooth_enable =
2745 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2746 bluetooth_enable_show, bluetooth_enable_store);
2748 /* --------------------------------------------------------------------- */
2750 static struct attribute *bluetooth_attributes[] = {
2751 &dev_attr_bluetooth_enable.attr,
2752 NULL
2755 static const struct attribute_group bluetooth_attr_group = {
2756 .attrs = bluetooth_attributes,
2759 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2761 int bts = bluetooth_get_radiosw();
2763 if (bts < 0)
2764 return bts;
2766 *state = bts;
2767 return 0;
2770 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2772 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2775 static void bluetooth_exit(void)
2777 if (tpacpi_bluetooth_rfkill)
2778 rfkill_unregister(tpacpi_bluetooth_rfkill);
2780 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2781 &bluetooth_attr_group);
2784 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2786 int res;
2787 int status = 0;
2789 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2791 TPACPI_ACPIHANDLE_INIT(hkey);
2793 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2794 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2795 tp_features.bluetooth = hkey_handle &&
2796 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2798 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2799 str_supported(tp_features.bluetooth),
2800 status);
2802 if (tp_features.bluetooth &&
2803 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2804 /* no bluetooth hardware present in system */
2805 tp_features.bluetooth = 0;
2806 dbg_printk(TPACPI_DBG_INIT,
2807 "bluetooth hardware not installed\n");
2810 if (!tp_features.bluetooth)
2811 return 1;
2813 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2814 &bluetooth_attr_group);
2815 if (res)
2816 return res;
2818 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2819 &tpacpi_bluetooth_rfkill,
2820 RFKILL_TYPE_BLUETOOTH,
2821 "tpacpi_bluetooth_sw",
2822 tpacpi_bluetooth_rfk_set,
2823 tpacpi_bluetooth_rfk_get);
2824 if (res) {
2825 bluetooth_exit();
2826 return res;
2829 return 0;
2832 /* procfs -------------------------------------------------------------- */
2833 static int bluetooth_read(char *p)
2835 int len = 0;
2836 int status = bluetooth_get_radiosw();
2838 if (!tp_features.bluetooth)
2839 len += sprintf(p + len, "status:\t\tnot supported\n");
2840 else {
2841 len += sprintf(p + len, "status:\t\t%s\n",
2842 (status == RFKILL_STATE_UNBLOCKED) ?
2843 "enabled" : "disabled");
2844 len += sprintf(p + len, "commands:\tenable, disable\n");
2847 return len;
2850 static int bluetooth_write(char *buf)
2852 char *cmd;
2854 if (!tp_features.bluetooth)
2855 return -ENODEV;
2857 while ((cmd = next_cmd(&buf))) {
2858 if (strlencmp(cmd, "enable") == 0) {
2859 bluetooth_set_radiosw(1, 1);
2860 } else if (strlencmp(cmd, "disable") == 0) {
2861 bluetooth_set_radiosw(0, 1);
2862 } else
2863 return -EINVAL;
2866 return 0;
2869 static struct ibm_struct bluetooth_driver_data = {
2870 .name = "bluetooth",
2871 .read = bluetooth_read,
2872 .write = bluetooth_write,
2873 .exit = bluetooth_exit,
2876 /*************************************************************************
2877 * Wan subdriver
2880 enum {
2881 /* ACPI GWAN/SWAN bits */
2882 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2883 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2884 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2887 static struct rfkill *tpacpi_wan_rfkill;
2889 static int wan_get_radiosw(void)
2891 int status;
2893 if (!tp_features.wan)
2894 return -ENODEV;
2896 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2897 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2898 return RFKILL_STATE_HARD_BLOCKED;
2900 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2901 return -EIO;
2903 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2904 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2907 static void wan_update_rfk(void)
2909 int status;
2911 if (!tpacpi_wan_rfkill)
2912 return;
2914 status = wan_get_radiosw();
2915 if (status < 0)
2916 return;
2917 rfkill_force_state(tpacpi_wan_rfkill, status);
2920 static int wan_set_radiosw(int radio_on, int update_rfk)
2922 int status;
2924 if (!tp_features.wan)
2925 return -ENODEV;
2927 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2928 * reason to risk weird behaviour. */
2929 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2930 && radio_on)
2931 return -EPERM;
2933 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2934 return -EIO;
2935 if (radio_on)
2936 status |= TP_ACPI_WANCARD_RADIOSSW;
2937 else
2938 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2939 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2940 return -EIO;
2942 if (update_rfk)
2943 wan_update_rfk();
2945 return 0;
2948 /* sysfs wan enable ---------------------------------------------------- */
2949 static ssize_t wan_enable_show(struct device *dev,
2950 struct device_attribute *attr,
2951 char *buf)
2953 int status;
2955 status = wan_get_radiosw();
2956 if (status < 0)
2957 return status;
2959 return snprintf(buf, PAGE_SIZE, "%d\n",
2960 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2963 static ssize_t wan_enable_store(struct device *dev,
2964 struct device_attribute *attr,
2965 const char *buf, size_t count)
2967 unsigned long t;
2968 int res;
2970 if (parse_strtoul(buf, 1, &t))
2971 return -EINVAL;
2973 res = wan_set_radiosw(t, 1);
2975 return (res) ? res : count;
2978 static struct device_attribute dev_attr_wan_enable =
2979 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2980 wan_enable_show, wan_enable_store);
2982 /* --------------------------------------------------------------------- */
2984 static struct attribute *wan_attributes[] = {
2985 &dev_attr_wan_enable.attr,
2986 NULL
2989 static const struct attribute_group wan_attr_group = {
2990 .attrs = wan_attributes,
2993 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2995 int wans = wan_get_radiosw();
2997 if (wans < 0)
2998 return wans;
3000 *state = wans;
3001 return 0;
3004 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3006 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3009 static void wan_exit(void)
3011 if (tpacpi_wan_rfkill)
3012 rfkill_unregister(tpacpi_wan_rfkill);
3014 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3015 &wan_attr_group);
3018 static int __init wan_init(struct ibm_init_struct *iibm)
3020 int res;
3021 int status = 0;
3023 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3025 TPACPI_ACPIHANDLE_INIT(hkey);
3027 tp_features.wan = hkey_handle &&
3028 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3030 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3031 str_supported(tp_features.wan),
3032 status);
3034 if (tp_features.wan &&
3035 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3036 /* no wan hardware present in system */
3037 tp_features.wan = 0;
3038 dbg_printk(TPACPI_DBG_INIT,
3039 "wan hardware not installed\n");
3042 if (!tp_features.wan)
3043 return 1;
3045 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3046 &wan_attr_group);
3047 if (res)
3048 return res;
3050 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3051 &tpacpi_wan_rfkill,
3052 RFKILL_TYPE_WWAN,
3053 "tpacpi_wwan_sw",
3054 tpacpi_wan_rfk_set,
3055 tpacpi_wan_rfk_get);
3056 if (res) {
3057 wan_exit();
3058 return res;
3061 return 0;
3064 /* procfs -------------------------------------------------------------- */
3065 static int wan_read(char *p)
3067 int len = 0;
3068 int status = wan_get_radiosw();
3070 if (!tp_features.wan)
3071 len += sprintf(p + len, "status:\t\tnot supported\n");
3072 else {
3073 len += sprintf(p + len, "status:\t\t%s\n",
3074 (status == RFKILL_STATE_UNBLOCKED) ?
3075 "enabled" : "disabled");
3076 len += sprintf(p + len, "commands:\tenable, disable\n");
3079 return len;
3082 static int wan_write(char *buf)
3084 char *cmd;
3086 if (!tp_features.wan)
3087 return -ENODEV;
3089 while ((cmd = next_cmd(&buf))) {
3090 if (strlencmp(cmd, "enable") == 0) {
3091 wan_set_radiosw(1, 1);
3092 } else if (strlencmp(cmd, "disable") == 0) {
3093 wan_set_radiosw(0, 1);
3094 } else
3095 return -EINVAL;
3098 return 0;
3101 static struct ibm_struct wan_driver_data = {
3102 .name = "wan",
3103 .read = wan_read,
3104 .write = wan_write,
3105 .exit = wan_exit,
3108 /*************************************************************************
3109 * Video subdriver
3112 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3114 enum video_access_mode {
3115 TPACPI_VIDEO_NONE = 0,
3116 TPACPI_VIDEO_570, /* 570 */
3117 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3118 TPACPI_VIDEO_NEW, /* all others */
3121 enum { /* video status flags, based on VIDEO_570 */
3122 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3123 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3124 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3127 enum { /* TPACPI_VIDEO_570 constants */
3128 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3129 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3130 * video_status_flags */
3131 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3132 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3135 static enum video_access_mode video_supported;
3136 static int video_orig_autosw;
3138 static int video_autosw_get(void);
3139 static int video_autosw_set(int enable);
3141 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3143 static int __init video_init(struct ibm_init_struct *iibm)
3145 int ivga;
3147 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3149 TPACPI_ACPIHANDLE_INIT(vid);
3150 TPACPI_ACPIHANDLE_INIT(vid2);
3152 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3153 /* G41, assume IVGA doesn't change */
3154 vid_handle = vid2_handle;
3156 if (!vid_handle)
3157 /* video switching not supported on R30, R31 */
3158 video_supported = TPACPI_VIDEO_NONE;
3159 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3160 /* 570 */
3161 video_supported = TPACPI_VIDEO_570;
3162 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3163 /* 600e/x, 770e, 770x */
3164 video_supported = TPACPI_VIDEO_770;
3165 else
3166 /* all others */
3167 video_supported = TPACPI_VIDEO_NEW;
3169 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3170 str_supported(video_supported != TPACPI_VIDEO_NONE),
3171 video_supported);
3173 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3176 static void video_exit(void)
3178 dbg_printk(TPACPI_DBG_EXIT,
3179 "restoring original video autoswitch mode\n");
3180 if (video_autosw_set(video_orig_autosw))
3181 printk(TPACPI_ERR "error while trying to restore original "
3182 "video autoswitch mode\n");
3185 static int video_outputsw_get(void)
3187 int status = 0;
3188 int i;
3190 switch (video_supported) {
3191 case TPACPI_VIDEO_570:
3192 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3193 TP_ACPI_VIDEO_570_PHSCMD))
3194 return -EIO;
3195 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3196 break;
3197 case TPACPI_VIDEO_770:
3198 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3199 return -EIO;
3200 if (i)
3201 status |= TP_ACPI_VIDEO_S_LCD;
3202 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3203 return -EIO;
3204 if (i)
3205 status |= TP_ACPI_VIDEO_S_CRT;
3206 break;
3207 case TPACPI_VIDEO_NEW:
3208 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3209 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3210 return -EIO;
3211 if (i)
3212 status |= TP_ACPI_VIDEO_S_CRT;
3214 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3215 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3216 return -EIO;
3217 if (i)
3218 status |= TP_ACPI_VIDEO_S_LCD;
3219 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3220 return -EIO;
3221 if (i)
3222 status |= TP_ACPI_VIDEO_S_DVI;
3223 break;
3224 default:
3225 return -ENOSYS;
3228 return status;
3231 static int video_outputsw_set(int status)
3233 int autosw;
3234 int res = 0;
3236 switch (video_supported) {
3237 case TPACPI_VIDEO_570:
3238 res = acpi_evalf(NULL, NULL,
3239 "\\_SB.PHS2", "vdd",
3240 TP_ACPI_VIDEO_570_PHS2CMD,
3241 status | TP_ACPI_VIDEO_570_PHS2SET);
3242 break;
3243 case TPACPI_VIDEO_770:
3244 autosw = video_autosw_get();
3245 if (autosw < 0)
3246 return autosw;
3248 res = video_autosw_set(1);
3249 if (res)
3250 return res;
3251 res = acpi_evalf(vid_handle, NULL,
3252 "ASWT", "vdd", status * 0x100, 0);
3253 if (!autosw && video_autosw_set(autosw)) {
3254 printk(TPACPI_ERR
3255 "video auto-switch left enabled due to error\n");
3256 return -EIO;
3258 break;
3259 case TPACPI_VIDEO_NEW:
3260 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3261 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3262 break;
3263 default:
3264 return -ENOSYS;
3267 return (res)? 0 : -EIO;
3270 static int video_autosw_get(void)
3272 int autosw = 0;
3274 switch (video_supported) {
3275 case TPACPI_VIDEO_570:
3276 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3277 return -EIO;
3278 break;
3279 case TPACPI_VIDEO_770:
3280 case TPACPI_VIDEO_NEW:
3281 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3282 return -EIO;
3283 break;
3284 default:
3285 return -ENOSYS;
3288 return autosw & 1;
3291 static int video_autosw_set(int enable)
3293 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3294 return -EIO;
3295 return 0;
3298 static int video_outputsw_cycle(void)
3300 int autosw = video_autosw_get();
3301 int res;
3303 if (autosw < 0)
3304 return autosw;
3306 switch (video_supported) {
3307 case TPACPI_VIDEO_570:
3308 res = video_autosw_set(1);
3309 if (res)
3310 return res;
3311 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3312 break;
3313 case TPACPI_VIDEO_770:
3314 case TPACPI_VIDEO_NEW:
3315 res = video_autosw_set(1);
3316 if (res)
3317 return res;
3318 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3319 break;
3320 default:
3321 return -ENOSYS;
3323 if (!autosw && video_autosw_set(autosw)) {
3324 printk(TPACPI_ERR
3325 "video auto-switch left enabled due to error\n");
3326 return -EIO;
3329 return (res)? 0 : -EIO;
3332 static int video_expand_toggle(void)
3334 switch (video_supported) {
3335 case TPACPI_VIDEO_570:
3336 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3337 0 : -EIO;
3338 case TPACPI_VIDEO_770:
3339 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3340 0 : -EIO;
3341 case TPACPI_VIDEO_NEW:
3342 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3343 0 : -EIO;
3344 default:
3345 return -ENOSYS;
3347 /* not reached */
3350 static int video_read(char *p)
3352 int status, autosw;
3353 int len = 0;
3355 if (video_supported == TPACPI_VIDEO_NONE) {
3356 len += sprintf(p + len, "status:\t\tnot supported\n");
3357 return len;
3360 status = video_outputsw_get();
3361 if (status < 0)
3362 return status;
3364 autosw = video_autosw_get();
3365 if (autosw < 0)
3366 return autosw;
3368 len += sprintf(p + len, "status:\t\tsupported\n");
3369 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3370 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3371 if (video_supported == TPACPI_VIDEO_NEW)
3372 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3373 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3374 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3375 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3376 if (video_supported == TPACPI_VIDEO_NEW)
3377 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3378 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3379 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3381 return len;
3384 static int video_write(char *buf)
3386 char *cmd;
3387 int enable, disable, status;
3388 int res;
3390 if (video_supported == TPACPI_VIDEO_NONE)
3391 return -ENODEV;
3393 enable = 0;
3394 disable = 0;
3396 while ((cmd = next_cmd(&buf))) {
3397 if (strlencmp(cmd, "lcd_enable") == 0) {
3398 enable |= TP_ACPI_VIDEO_S_LCD;
3399 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3400 disable |= TP_ACPI_VIDEO_S_LCD;
3401 } else if (strlencmp(cmd, "crt_enable") == 0) {
3402 enable |= TP_ACPI_VIDEO_S_CRT;
3403 } else if (strlencmp(cmd, "crt_disable") == 0) {
3404 disable |= TP_ACPI_VIDEO_S_CRT;
3405 } else if (video_supported == TPACPI_VIDEO_NEW &&
3406 strlencmp(cmd, "dvi_enable") == 0) {
3407 enable |= TP_ACPI_VIDEO_S_DVI;
3408 } else if (video_supported == TPACPI_VIDEO_NEW &&
3409 strlencmp(cmd, "dvi_disable") == 0) {
3410 disable |= TP_ACPI_VIDEO_S_DVI;
3411 } else if (strlencmp(cmd, "auto_enable") == 0) {
3412 res = video_autosw_set(1);
3413 if (res)
3414 return res;
3415 } else if (strlencmp(cmd, "auto_disable") == 0) {
3416 res = video_autosw_set(0);
3417 if (res)
3418 return res;
3419 } else if (strlencmp(cmd, "video_switch") == 0) {
3420 res = video_outputsw_cycle();
3421 if (res)
3422 return res;
3423 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3424 res = video_expand_toggle();
3425 if (res)
3426 return res;
3427 } else
3428 return -EINVAL;
3431 if (enable || disable) {
3432 status = video_outputsw_get();
3433 if (status < 0)
3434 return status;
3435 res = video_outputsw_set((status & ~disable) | enable);
3436 if (res)
3437 return res;
3440 return 0;
3443 static struct ibm_struct video_driver_data = {
3444 .name = "video",
3445 .read = video_read,
3446 .write = video_write,
3447 .exit = video_exit,
3450 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3452 /*************************************************************************
3453 * Light (thinklight) subdriver
3456 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3457 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3459 static int light_get_status(void)
3461 int status = 0;
3463 if (tp_features.light_status) {
3464 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3465 return -EIO;
3466 return (!!status);
3469 return -ENXIO;
3472 static int light_set_status(int status)
3474 int rc;
3476 if (tp_features.light) {
3477 if (cmos_handle) {
3478 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3479 (status)?
3480 TP_CMOS_THINKLIGHT_ON :
3481 TP_CMOS_THINKLIGHT_OFF);
3482 } else {
3483 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3484 (status)? 1 : 0);
3486 return (rc)? 0 : -EIO;
3489 return -ENXIO;
3492 static void light_set_status_worker(struct work_struct *work)
3494 struct tpacpi_led_classdev *data =
3495 container_of(work, struct tpacpi_led_classdev, work);
3497 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3498 light_set_status((data->new_state != TPACPI_LED_OFF));
3501 static void light_sysfs_set(struct led_classdev *led_cdev,
3502 enum led_brightness brightness)
3504 struct tpacpi_led_classdev *data =
3505 container_of(led_cdev,
3506 struct tpacpi_led_classdev,
3507 led_classdev);
3508 data->new_state = (brightness != LED_OFF) ?
3509 TPACPI_LED_ON : TPACPI_LED_OFF;
3510 queue_work(tpacpi_wq, &data->work);
3513 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3515 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3518 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3519 .led_classdev = {
3520 .name = "tpacpi::thinklight",
3521 .brightness_set = &light_sysfs_set,
3522 .brightness_get = &light_sysfs_get,
3526 static int __init light_init(struct ibm_init_struct *iibm)
3528 int rc;
3530 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3532 TPACPI_ACPIHANDLE_INIT(ledb);
3533 TPACPI_ACPIHANDLE_INIT(lght);
3534 TPACPI_ACPIHANDLE_INIT(cmos);
3535 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3537 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3538 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3540 if (tp_features.light)
3541 /* light status not supported on
3542 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3543 tp_features.light_status =
3544 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3546 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3547 str_supported(tp_features.light),
3548 str_supported(tp_features.light_status));
3550 if (!tp_features.light)
3551 return 1;
3553 rc = led_classdev_register(&tpacpi_pdev->dev,
3554 &tpacpi_led_thinklight.led_classdev);
3556 if (rc < 0) {
3557 tp_features.light = 0;
3558 tp_features.light_status = 0;
3559 } else {
3560 rc = 0;
3563 return rc;
3566 static void light_exit(void)
3568 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3569 if (work_pending(&tpacpi_led_thinklight.work))
3570 flush_workqueue(tpacpi_wq);
3573 static int light_read(char *p)
3575 int len = 0;
3576 int status;
3578 if (!tp_features.light) {
3579 len += sprintf(p + len, "status:\t\tnot supported\n");
3580 } else if (!tp_features.light_status) {
3581 len += sprintf(p + len, "status:\t\tunknown\n");
3582 len += sprintf(p + len, "commands:\ton, off\n");
3583 } else {
3584 status = light_get_status();
3585 if (status < 0)
3586 return status;
3587 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3588 len += sprintf(p + len, "commands:\ton, off\n");
3591 return len;
3594 static int light_write(char *buf)
3596 char *cmd;
3597 int newstatus = 0;
3599 if (!tp_features.light)
3600 return -ENODEV;
3602 while ((cmd = next_cmd(&buf))) {
3603 if (strlencmp(cmd, "on") == 0) {
3604 newstatus = 1;
3605 } else if (strlencmp(cmd, "off") == 0) {
3606 newstatus = 0;
3607 } else
3608 return -EINVAL;
3611 return light_set_status(newstatus);
3614 static struct ibm_struct light_driver_data = {
3615 .name = "light",
3616 .read = light_read,
3617 .write = light_write,
3618 .exit = light_exit,
3621 /*************************************************************************
3622 * Dock subdriver
3625 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3627 static void dock_notify(struct ibm_struct *ibm, u32 event);
3628 static int dock_read(char *p);
3629 static int dock_write(char *buf);
3631 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3632 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3633 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3634 "\\_SB.PCI.ISA.SLCE", /* 570 */
3635 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3637 /* don't list other alternatives as we install a notify handler on the 570 */
3638 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3640 static const struct acpi_device_id ibm_pci_device_ids[] = {
3641 {PCI_ROOT_HID_STRING, 0},
3642 {"", 0},
3645 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3647 .notify = dock_notify,
3648 .handle = &dock_handle,
3649 .type = ACPI_SYSTEM_NOTIFY,
3652 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3653 * We just use it to get notifications of dock hotplug
3654 * in very old thinkpads */
3655 .hid = ibm_pci_device_ids,
3656 .notify = dock_notify,
3657 .handle = &pci_handle,
3658 .type = ACPI_SYSTEM_NOTIFY,
3662 static struct ibm_struct dock_driver_data[2] = {
3664 .name = "dock",
3665 .read = dock_read,
3666 .write = dock_write,
3667 .acpi = &ibm_dock_acpidriver[0],
3670 .name = "dock",
3671 .acpi = &ibm_dock_acpidriver[1],
3675 #define dock_docked() (_sta(dock_handle) & 1)
3677 static int __init dock_init(struct ibm_init_struct *iibm)
3679 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3681 TPACPI_ACPIHANDLE_INIT(dock);
3683 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3684 str_supported(dock_handle != NULL));
3686 return (dock_handle)? 0 : 1;
3689 static int __init dock_init2(struct ibm_init_struct *iibm)
3691 int dock2_needed;
3693 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3695 if (dock_driver_data[0].flags.acpi_driver_registered &&
3696 dock_driver_data[0].flags.acpi_notify_installed) {
3697 TPACPI_ACPIHANDLE_INIT(pci);
3698 dock2_needed = (pci_handle != NULL);
3699 vdbg_printk(TPACPI_DBG_INIT,
3700 "dock PCI handler for the TP 570 is %s\n",
3701 str_supported(dock2_needed));
3702 } else {
3703 vdbg_printk(TPACPI_DBG_INIT,
3704 "dock subdriver part 2 not required\n");
3705 dock2_needed = 0;
3708 return (dock2_needed)? 0 : 1;
3711 static void dock_notify(struct ibm_struct *ibm, u32 event)
3713 int docked = dock_docked();
3714 int pci = ibm->acpi->hid && ibm->acpi->device &&
3715 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3716 int data;
3718 if (event == 1 && !pci) /* 570 */
3719 data = 1; /* button */
3720 else if (event == 1 && pci) /* 570 */
3721 data = 3; /* dock */
3722 else if (event == 3 && docked)
3723 data = 1; /* button */
3724 else if (event == 3 && !docked)
3725 data = 2; /* undock */
3726 else if (event == 0 && docked)
3727 data = 3; /* dock */
3728 else {
3729 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3730 event, _sta(dock_handle));
3731 data = 0; /* unknown */
3733 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3734 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3735 ibm->acpi->device->dev.bus_id,
3736 event, data);
3739 static int dock_read(char *p)
3741 int len = 0;
3742 int docked = dock_docked();
3744 if (!dock_handle)
3745 len += sprintf(p + len, "status:\t\tnot supported\n");
3746 else if (!docked)
3747 len += sprintf(p + len, "status:\t\tundocked\n");
3748 else {
3749 len += sprintf(p + len, "status:\t\tdocked\n");
3750 len += sprintf(p + len, "commands:\tdock, undock\n");
3753 return len;
3756 static int dock_write(char *buf)
3758 char *cmd;
3760 if (!dock_docked())
3761 return -ENODEV;
3763 while ((cmd = next_cmd(&buf))) {
3764 if (strlencmp(cmd, "undock") == 0) {
3765 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3766 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3767 return -EIO;
3768 } else if (strlencmp(cmd, "dock") == 0) {
3769 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3770 return -EIO;
3771 } else
3772 return -EINVAL;
3775 return 0;
3778 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3780 /*************************************************************************
3781 * Bay subdriver
3784 #ifdef CONFIG_THINKPAD_ACPI_BAY
3786 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3787 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3788 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3789 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3790 ); /* A21e, R30, R31 */
3791 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3792 "_EJ0", /* all others */
3793 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3794 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3795 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3796 ); /* all others */
3797 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3798 "_EJ0", /* 770x */
3799 ); /* all others */
3801 static int __init bay_init(struct ibm_init_struct *iibm)
3803 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3805 TPACPI_ACPIHANDLE_INIT(bay);
3806 if (bay_handle)
3807 TPACPI_ACPIHANDLE_INIT(bay_ej);
3808 TPACPI_ACPIHANDLE_INIT(bay2);
3809 if (bay2_handle)
3810 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3812 tp_features.bay_status = bay_handle &&
3813 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3814 tp_features.bay_status2 = bay2_handle &&
3815 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3817 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3818 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3819 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3820 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3822 vdbg_printk(TPACPI_DBG_INIT,
3823 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3824 str_supported(tp_features.bay_status),
3825 str_supported(tp_features.bay_eject),
3826 str_supported(tp_features.bay_status2),
3827 str_supported(tp_features.bay_eject2));
3829 return (tp_features.bay_status || tp_features.bay_eject ||
3830 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3833 static void bay_notify(struct ibm_struct *ibm, u32 event)
3835 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3836 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3837 ibm->acpi->device->dev.bus_id,
3838 event, 0);
3841 #define bay_occupied(b) (_sta(b##_handle) & 1)
3843 static int bay_read(char *p)
3845 int len = 0;
3846 int occupied = bay_occupied(bay);
3847 int occupied2 = bay_occupied(bay2);
3848 int eject, eject2;
3850 len += sprintf(p + len, "status:\t\t%s\n",
3851 tp_features.bay_status ?
3852 (occupied ? "occupied" : "unoccupied") :
3853 "not supported");
3854 if (tp_features.bay_status2)
3855 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3856 "occupied" : "unoccupied");
3858 eject = tp_features.bay_eject && occupied;
3859 eject2 = tp_features.bay_eject2 && occupied2;
3861 if (eject && eject2)
3862 len += sprintf(p + len, "commands:\teject, eject2\n");
3863 else if (eject)
3864 len += sprintf(p + len, "commands:\teject\n");
3865 else if (eject2)
3866 len += sprintf(p + len, "commands:\teject2\n");
3868 return len;
3871 static int bay_write(char *buf)
3873 char *cmd;
3875 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3876 return -ENODEV;
3878 while ((cmd = next_cmd(&buf))) {
3879 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3880 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3881 return -EIO;
3882 } else if (tp_features.bay_eject2 &&
3883 strlencmp(cmd, "eject2") == 0) {
3884 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3885 return -EIO;
3886 } else
3887 return -EINVAL;
3890 return 0;
3893 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3894 .notify = bay_notify,
3895 .handle = &bay_handle,
3896 .type = ACPI_SYSTEM_NOTIFY,
3899 static struct ibm_struct bay_driver_data = {
3900 .name = "bay",
3901 .read = bay_read,
3902 .write = bay_write,
3903 .acpi = &ibm_bay_acpidriver,
3906 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3908 /*************************************************************************
3909 * CMOS subdriver
3912 /* sysfs cmos_command -------------------------------------------------- */
3913 static ssize_t cmos_command_store(struct device *dev,
3914 struct device_attribute *attr,
3915 const char *buf, size_t count)
3917 unsigned long cmos_cmd;
3918 int res;
3920 if (parse_strtoul(buf, 21, &cmos_cmd))
3921 return -EINVAL;
3923 res = issue_thinkpad_cmos_command(cmos_cmd);
3924 return (res)? res : count;
3927 static struct device_attribute dev_attr_cmos_command =
3928 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3930 /* --------------------------------------------------------------------- */
3932 static int __init cmos_init(struct ibm_init_struct *iibm)
3934 int res;
3936 vdbg_printk(TPACPI_DBG_INIT,
3937 "initializing cmos commands subdriver\n");
3939 TPACPI_ACPIHANDLE_INIT(cmos);
3941 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3942 str_supported(cmos_handle != NULL));
3944 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3945 if (res)
3946 return res;
3948 return (cmos_handle)? 0 : 1;
3951 static void cmos_exit(void)
3953 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3956 static int cmos_read(char *p)
3958 int len = 0;
3960 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3961 R30, R31, T20-22, X20-21 */
3962 if (!cmos_handle)
3963 len += sprintf(p + len, "status:\t\tnot supported\n");
3964 else {
3965 len += sprintf(p + len, "status:\t\tsupported\n");
3966 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3969 return len;
3972 static int cmos_write(char *buf)
3974 char *cmd;
3975 int cmos_cmd, res;
3977 while ((cmd = next_cmd(&buf))) {
3978 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3979 cmos_cmd >= 0 && cmos_cmd <= 21) {
3980 /* cmos_cmd set */
3981 } else
3982 return -EINVAL;
3984 res = issue_thinkpad_cmos_command(cmos_cmd);
3985 if (res)
3986 return res;
3989 return 0;
3992 static struct ibm_struct cmos_driver_data = {
3993 .name = "cmos",
3994 .read = cmos_read,
3995 .write = cmos_write,
3996 .exit = cmos_exit,
3999 /*************************************************************************
4000 * LED subdriver
4003 enum led_access_mode {
4004 TPACPI_LED_NONE = 0,
4005 TPACPI_LED_570, /* 570 */
4006 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4007 TPACPI_LED_NEW, /* all others */
4010 enum { /* For TPACPI_LED_OLD */
4011 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4012 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4013 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4016 static enum led_access_mode led_supported;
4018 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4019 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4020 /* T20-22, X20-21 */
4021 "LED", /* all others */
4022 ); /* R30, R31 */
4024 #define TPACPI_LED_NUMLEDS 8
4025 static struct tpacpi_led_classdev *tpacpi_leds;
4026 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4027 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4028 /* there's a limit of 19 chars + NULL before 2.6.26 */
4029 "tpacpi::power",
4030 "tpacpi:orange:batt",
4031 "tpacpi:green:batt",
4032 "tpacpi::dock_active",
4033 "tpacpi::bay_active",
4034 "tpacpi::dock_batt",
4035 "tpacpi::unknown_led",
4036 "tpacpi::standby",
4039 static int led_get_status(const unsigned int led)
4041 int status;
4042 enum led_status_t led_s;
4044 switch (led_supported) {
4045 case TPACPI_LED_570:
4046 if (!acpi_evalf(ec_handle,
4047 &status, "GLED", "dd", 1 << led))
4048 return -EIO;
4049 led_s = (status == 0)?
4050 TPACPI_LED_OFF :
4051 ((status == 1)?
4052 TPACPI_LED_ON :
4053 TPACPI_LED_BLINK);
4054 tpacpi_led_state_cache[led] = led_s;
4055 return led_s;
4056 default:
4057 return -ENXIO;
4060 /* not reached */
4063 static int led_set_status(const unsigned int led,
4064 const enum led_status_t ledstatus)
4066 /* off, on, blink. Index is led_status_t */
4067 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4068 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4070 int rc = 0;
4072 switch (led_supported) {
4073 case TPACPI_LED_570:
4074 /* 570 */
4075 if (led > 7)
4076 return -EINVAL;
4077 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4078 (1 << led), led_sled_arg1[ledstatus]))
4079 rc = -EIO;
4080 break;
4081 case TPACPI_LED_OLD:
4082 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4083 if (led > 7)
4084 return -EINVAL;
4085 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4086 if (rc >= 0)
4087 rc = ec_write(TPACPI_LED_EC_HLBL,
4088 (ledstatus == TPACPI_LED_BLINK) << led);
4089 if (rc >= 0)
4090 rc = ec_write(TPACPI_LED_EC_HLCL,
4091 (ledstatus != TPACPI_LED_OFF) << led);
4092 break;
4093 case TPACPI_LED_NEW:
4094 /* all others */
4095 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4096 led, led_led_arg1[ledstatus]))
4097 rc = -EIO;
4098 break;
4099 default:
4100 rc = -ENXIO;
4103 if (!rc)
4104 tpacpi_led_state_cache[led] = ledstatus;
4106 return rc;
4109 static void led_set_status_worker(struct work_struct *work)
4111 struct tpacpi_led_classdev *data =
4112 container_of(work, struct tpacpi_led_classdev, work);
4114 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4115 led_set_status(data->led, data->new_state);
4118 static void led_sysfs_set(struct led_classdev *led_cdev,
4119 enum led_brightness brightness)
4121 struct tpacpi_led_classdev *data = container_of(led_cdev,
4122 struct tpacpi_led_classdev, led_classdev);
4124 if (brightness == LED_OFF)
4125 data->new_state = TPACPI_LED_OFF;
4126 else if (tpacpi_led_state_cache[data->led] != TPACPI_LED_BLINK)
4127 data->new_state = TPACPI_LED_ON;
4128 else
4129 data->new_state = TPACPI_LED_BLINK;
4131 queue_work(tpacpi_wq, &data->work);
4134 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4135 unsigned long *delay_on, unsigned long *delay_off)
4137 struct tpacpi_led_classdev *data = container_of(led_cdev,
4138 struct tpacpi_led_classdev, led_classdev);
4140 /* Can we choose the flash rate? */
4141 if (*delay_on == 0 && *delay_off == 0) {
4142 /* yes. set them to the hardware blink rate (1 Hz) */
4143 *delay_on = 500; /* ms */
4144 *delay_off = 500; /* ms */
4145 } else if ((*delay_on != 500) || (*delay_off != 500))
4146 return -EINVAL;
4148 data->new_state = TPACPI_LED_BLINK;
4149 queue_work(tpacpi_wq, &data->work);
4151 return 0;
4154 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4156 int rc;
4158 struct tpacpi_led_classdev *data = container_of(led_cdev,
4159 struct tpacpi_led_classdev, led_classdev);
4161 rc = led_get_status(data->led);
4163 if (rc == TPACPI_LED_OFF || rc < 0)
4164 rc = LED_OFF; /* no error handling in led class :( */
4165 else
4166 rc = LED_FULL;
4168 return rc;
4171 static void led_exit(void)
4173 unsigned int i;
4175 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4176 if (tpacpi_leds[i].led_classdev.name)
4177 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4180 kfree(tpacpi_leds);
4183 static int __init led_init(struct ibm_init_struct *iibm)
4185 unsigned int i;
4186 int rc;
4188 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4190 TPACPI_ACPIHANDLE_INIT(led);
4192 if (!led_handle)
4193 /* led not supported on R30, R31 */
4194 led_supported = TPACPI_LED_NONE;
4195 else if (strlencmp(led_path, "SLED") == 0)
4196 /* 570 */
4197 led_supported = TPACPI_LED_570;
4198 else if (strlencmp(led_path, "SYSL") == 0)
4199 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4200 led_supported = TPACPI_LED_OLD;
4201 else
4202 /* all others */
4203 led_supported = TPACPI_LED_NEW;
4205 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4206 str_supported(led_supported), led_supported);
4208 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4209 GFP_KERNEL);
4210 if (!tpacpi_leds) {
4211 printk(TPACPI_ERR "Out of memory for LED data\n");
4212 return -ENOMEM;
4215 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4216 tpacpi_leds[i].led = i;
4218 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4219 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4220 if (led_supported == TPACPI_LED_570)
4221 tpacpi_leds[i].led_classdev.brightness_get =
4222 &led_sysfs_get;
4224 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4226 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4228 rc = led_classdev_register(&tpacpi_pdev->dev,
4229 &tpacpi_leds[i].led_classdev);
4230 if (rc < 0) {
4231 tpacpi_leds[i].led_classdev.name = NULL;
4232 led_exit();
4233 return rc;
4237 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4240 #define str_led_status(s) \
4241 ((s) == TPACPI_LED_OFF ? "off" : \
4242 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4244 static int led_read(char *p)
4246 int len = 0;
4248 if (!led_supported) {
4249 len += sprintf(p + len, "status:\t\tnot supported\n");
4250 return len;
4252 len += sprintf(p + len, "status:\t\tsupported\n");
4254 if (led_supported == TPACPI_LED_570) {
4255 /* 570 */
4256 int i, status;
4257 for (i = 0; i < 8; i++) {
4258 status = led_get_status(i);
4259 if (status < 0)
4260 return -EIO;
4261 len += sprintf(p + len, "%d:\t\t%s\n",
4262 i, str_led_status(status));
4266 len += sprintf(p + len, "commands:\t"
4267 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4269 return len;
4272 static int led_write(char *buf)
4274 char *cmd;
4275 int led, rc;
4276 enum led_status_t s;
4278 if (!led_supported)
4279 return -ENODEV;
4281 while ((cmd = next_cmd(&buf))) {
4282 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4283 return -EINVAL;
4285 if (strstr(cmd, "off")) {
4286 s = TPACPI_LED_OFF;
4287 } else if (strstr(cmd, "on")) {
4288 s = TPACPI_LED_ON;
4289 } else if (strstr(cmd, "blink")) {
4290 s = TPACPI_LED_BLINK;
4291 } else {
4292 return -EINVAL;
4295 rc = led_set_status(led, s);
4296 if (rc < 0)
4297 return rc;
4300 return 0;
4303 static struct ibm_struct led_driver_data = {
4304 .name = "led",
4305 .read = led_read,
4306 .write = led_write,
4307 .exit = led_exit,
4310 /*************************************************************************
4311 * Beep subdriver
4314 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4316 static int __init beep_init(struct ibm_init_struct *iibm)
4318 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4320 TPACPI_ACPIHANDLE_INIT(beep);
4322 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4323 str_supported(beep_handle != NULL));
4325 return (beep_handle)? 0 : 1;
4328 static int beep_read(char *p)
4330 int len = 0;
4332 if (!beep_handle)
4333 len += sprintf(p + len, "status:\t\tnot supported\n");
4334 else {
4335 len += sprintf(p + len, "status:\t\tsupported\n");
4336 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4339 return len;
4342 static int beep_write(char *buf)
4344 char *cmd;
4345 int beep_cmd;
4347 if (!beep_handle)
4348 return -ENODEV;
4350 while ((cmd = next_cmd(&buf))) {
4351 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4352 beep_cmd >= 0 && beep_cmd <= 17) {
4353 /* beep_cmd set */
4354 } else
4355 return -EINVAL;
4356 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4357 return -EIO;
4360 return 0;
4363 static struct ibm_struct beep_driver_data = {
4364 .name = "beep",
4365 .read = beep_read,
4366 .write = beep_write,
4369 /*************************************************************************
4370 * Thermal subdriver
4373 enum thermal_access_mode {
4374 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4375 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4376 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4377 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4378 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4381 enum { /* TPACPI_THERMAL_TPEC_* */
4382 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4383 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4384 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4387 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4388 struct ibm_thermal_sensors_struct {
4389 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4392 static enum thermal_access_mode thermal_read_mode;
4394 /* idx is zero-based */
4395 static int thermal_get_sensor(int idx, s32 *value)
4397 int t;
4398 s8 tmp;
4399 char tmpi[5];
4401 t = TP_EC_THERMAL_TMP0;
4403 switch (thermal_read_mode) {
4404 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4405 case TPACPI_THERMAL_TPEC_16:
4406 if (idx >= 8 && idx <= 15) {
4407 t = TP_EC_THERMAL_TMP8;
4408 idx -= 8;
4410 /* fallthrough */
4411 #endif
4412 case TPACPI_THERMAL_TPEC_8:
4413 if (idx <= 7) {
4414 if (!acpi_ec_read(t + idx, &tmp))
4415 return -EIO;
4416 *value = tmp * 1000;
4417 return 0;
4419 break;
4421 case TPACPI_THERMAL_ACPI_UPDT:
4422 if (idx <= 7) {
4423 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4424 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4425 return -EIO;
4426 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4427 return -EIO;
4428 *value = (t - 2732) * 100;
4429 return 0;
4431 break;
4433 case TPACPI_THERMAL_ACPI_TMP07:
4434 if (idx <= 7) {
4435 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4436 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4437 return -EIO;
4438 if (t > 127 || t < -127)
4439 t = TP_EC_THERMAL_TMP_NA;
4440 *value = t * 1000;
4441 return 0;
4443 break;
4445 case TPACPI_THERMAL_NONE:
4446 default:
4447 return -ENOSYS;
4450 return -EINVAL;
4453 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4455 int res, i;
4456 int n;
4458 n = 8;
4459 i = 0;
4461 if (!s)
4462 return -EINVAL;
4464 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4465 n = 16;
4467 for (i = 0 ; i < n; i++) {
4468 res = thermal_get_sensor(i, &s->temp[i]);
4469 if (res)
4470 return res;
4473 return n;
4476 /* sysfs temp##_input -------------------------------------------------- */
4478 static ssize_t thermal_temp_input_show(struct device *dev,
4479 struct device_attribute *attr,
4480 char *buf)
4482 struct sensor_device_attribute *sensor_attr =
4483 to_sensor_dev_attr(attr);
4484 int idx = sensor_attr->index;
4485 s32 value;
4486 int res;
4488 res = thermal_get_sensor(idx, &value);
4489 if (res)
4490 return res;
4491 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4492 return -ENXIO;
4494 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4497 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4498 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4499 thermal_temp_input_show, NULL, _idxB)
4501 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4502 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4503 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4504 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4505 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4506 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4507 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4508 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4509 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4510 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4511 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4512 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4513 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4514 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4515 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4516 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4517 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4520 #define THERMAL_ATTRS(X) \
4521 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4523 static struct attribute *thermal_temp_input_attr[] = {
4524 THERMAL_ATTRS(8),
4525 THERMAL_ATTRS(9),
4526 THERMAL_ATTRS(10),
4527 THERMAL_ATTRS(11),
4528 THERMAL_ATTRS(12),
4529 THERMAL_ATTRS(13),
4530 THERMAL_ATTRS(14),
4531 THERMAL_ATTRS(15),
4532 THERMAL_ATTRS(0),
4533 THERMAL_ATTRS(1),
4534 THERMAL_ATTRS(2),
4535 THERMAL_ATTRS(3),
4536 THERMAL_ATTRS(4),
4537 THERMAL_ATTRS(5),
4538 THERMAL_ATTRS(6),
4539 THERMAL_ATTRS(7),
4540 NULL
4543 static const struct attribute_group thermal_temp_input16_group = {
4544 .attrs = thermal_temp_input_attr
4547 static const struct attribute_group thermal_temp_input8_group = {
4548 .attrs = &thermal_temp_input_attr[8]
4551 #undef THERMAL_SENSOR_ATTR_TEMP
4552 #undef THERMAL_ATTRS
4554 /* --------------------------------------------------------------------- */
4556 static int __init thermal_init(struct ibm_init_struct *iibm)
4558 u8 t, ta1, ta2;
4559 int i;
4560 int acpi_tmp7;
4561 int res;
4563 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4565 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4567 if (thinkpad_id.ec_model) {
4569 * Direct EC access mode: sensors at registers
4570 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4571 * non-implemented, thermal sensors return 0x80 when
4572 * not available
4575 ta1 = ta2 = 0;
4576 for (i = 0; i < 8; i++) {
4577 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4578 ta1 |= t;
4579 } else {
4580 ta1 = 0;
4581 break;
4583 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4584 ta2 |= t;
4585 } else {
4586 ta1 = 0;
4587 break;
4590 if (ta1 == 0) {
4591 /* This is sheer paranoia, but we handle it anyway */
4592 if (acpi_tmp7) {
4593 printk(TPACPI_ERR
4594 "ThinkPad ACPI EC access misbehaving, "
4595 "falling back to ACPI TMPx access "
4596 "mode\n");
4597 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4598 } else {
4599 printk(TPACPI_ERR
4600 "ThinkPad ACPI EC access misbehaving, "
4601 "disabling thermal sensors access\n");
4602 thermal_read_mode = TPACPI_THERMAL_NONE;
4604 } else {
4605 thermal_read_mode =
4606 (ta2 != 0) ?
4607 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4609 } else if (acpi_tmp7) {
4610 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4611 /* 600e/x, 770e, 770x */
4612 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4613 } else {
4614 /* Standard ACPI TMPx access, max 8 sensors */
4615 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4617 } else {
4618 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4619 thermal_read_mode = TPACPI_THERMAL_NONE;
4622 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4623 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4624 thermal_read_mode);
4626 switch (thermal_read_mode) {
4627 case TPACPI_THERMAL_TPEC_16:
4628 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4629 &thermal_temp_input16_group);
4630 if (res)
4631 return res;
4632 break;
4633 case TPACPI_THERMAL_TPEC_8:
4634 case TPACPI_THERMAL_ACPI_TMP07:
4635 case TPACPI_THERMAL_ACPI_UPDT:
4636 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4637 &thermal_temp_input8_group);
4638 if (res)
4639 return res;
4640 break;
4641 case TPACPI_THERMAL_NONE:
4642 default:
4643 return 1;
4646 return 0;
4649 static void thermal_exit(void)
4651 switch (thermal_read_mode) {
4652 case TPACPI_THERMAL_TPEC_16:
4653 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4654 &thermal_temp_input16_group);
4655 break;
4656 case TPACPI_THERMAL_TPEC_8:
4657 case TPACPI_THERMAL_ACPI_TMP07:
4658 case TPACPI_THERMAL_ACPI_UPDT:
4659 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4660 &thermal_temp_input16_group);
4661 break;
4662 case TPACPI_THERMAL_NONE:
4663 default:
4664 break;
4668 static int thermal_read(char *p)
4670 int len = 0;
4671 int n, i;
4672 struct ibm_thermal_sensors_struct t;
4674 n = thermal_get_sensors(&t);
4675 if (unlikely(n < 0))
4676 return n;
4678 len += sprintf(p + len, "temperatures:\t");
4680 if (n > 0) {
4681 for (i = 0; i < (n - 1); i++)
4682 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4683 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4684 } else
4685 len += sprintf(p + len, "not supported\n");
4687 return len;
4690 static struct ibm_struct thermal_driver_data = {
4691 .name = "thermal",
4692 .read = thermal_read,
4693 .exit = thermal_exit,
4696 /*************************************************************************
4697 * EC Dump subdriver
4700 static u8 ecdump_regs[256];
4702 static int ecdump_read(char *p)
4704 int len = 0;
4705 int i, j;
4706 u8 v;
4708 len += sprintf(p + len, "EC "
4709 " +00 +01 +02 +03 +04 +05 +06 +07"
4710 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4711 for (i = 0; i < 256; i += 16) {
4712 len += sprintf(p + len, "EC 0x%02x:", i);
4713 for (j = 0; j < 16; j++) {
4714 if (!acpi_ec_read(i + j, &v))
4715 break;
4716 if (v != ecdump_regs[i + j])
4717 len += sprintf(p + len, " *%02x", v);
4718 else
4719 len += sprintf(p + len, " %02x", v);
4720 ecdump_regs[i + j] = v;
4722 len += sprintf(p + len, "\n");
4723 if (j != 16)
4724 break;
4727 /* These are way too dangerous to advertise openly... */
4728 #if 0
4729 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4730 " (<offset> is 00-ff, <value> is 00-ff)\n");
4731 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4732 " (<offset> is 00-ff, <value> is 0-255)\n");
4733 #endif
4734 return len;
4737 static int ecdump_write(char *buf)
4739 char *cmd;
4740 int i, v;
4742 while ((cmd = next_cmd(&buf))) {
4743 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4744 /* i and v set */
4745 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4746 /* i and v set */
4747 } else
4748 return -EINVAL;
4749 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4750 if (!acpi_ec_write(i, v))
4751 return -EIO;
4752 } else
4753 return -EINVAL;
4756 return 0;
4759 static struct ibm_struct ecdump_driver_data = {
4760 .name = "ecdump",
4761 .read = ecdump_read,
4762 .write = ecdump_write,
4763 .flags.experimental = 1,
4766 /*************************************************************************
4767 * Backlight/brightness subdriver
4770 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4772 enum {
4773 TP_EC_BACKLIGHT = 0x31,
4775 /* TP_EC_BACKLIGHT bitmasks */
4776 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4777 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4778 TP_EC_BACKLIGHT_MAPSW = 0x20,
4781 static struct backlight_device *ibm_backlight_device;
4782 static int brightness_mode;
4783 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4785 static struct mutex brightness_mutex;
4788 * ThinkPads can read brightness from two places: EC 0x31, or
4789 * CMOS NVRAM byte 0x5E, bits 0-3.
4791 * EC 0x31 has the following layout
4792 * Bit 7: unknown function
4793 * Bit 6: unknown function
4794 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4795 * Bit 4: must be set to zero to avoid problems
4796 * Bit 3-0: backlight brightness level
4798 * brightness_get_raw returns status data in the EC 0x31 layout
4800 static int brightness_get_raw(int *status)
4802 u8 lec = 0, lcmos = 0, level = 0;
4804 if (brightness_mode & 1) {
4805 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4806 return -EIO;
4807 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4809 if (brightness_mode & 2) {
4810 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4811 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4812 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4813 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4814 level = lcmos;
4817 if (brightness_mode == 3) {
4818 *status = lec; /* Prefer EC, CMOS is just a backing store */
4819 lec &= TP_EC_BACKLIGHT_LVLMSK;
4820 if (lec == lcmos)
4821 tp_warned.bright_cmos_ec_unsync = 0;
4822 else {
4823 if (!tp_warned.bright_cmos_ec_unsync) {
4824 printk(TPACPI_ERR
4825 "CMOS NVRAM (%u) and EC (%u) do not "
4826 "agree on display brightness level\n",
4827 (unsigned int) lcmos,
4828 (unsigned int) lec);
4829 tp_warned.bright_cmos_ec_unsync = 1;
4831 return -EIO;
4833 } else {
4834 *status = level;
4837 return 0;
4840 /* May return EINTR which can always be mapped to ERESTARTSYS */
4841 static int brightness_set(int value)
4843 int cmos_cmd, inc, i, res;
4844 int current_value;
4845 int command_bits;
4847 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4848 value < 0)
4849 return -EINVAL;
4851 res = mutex_lock_interruptible(&brightness_mutex);
4852 if (res < 0)
4853 return res;
4855 res = brightness_get_raw(&current_value);
4856 if (res < 0)
4857 goto errout;
4859 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4860 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4862 cmos_cmd = value > current_value ?
4863 TP_CMOS_BRIGHTNESS_UP :
4864 TP_CMOS_BRIGHTNESS_DOWN;
4865 inc = (value > current_value)? 1 : -1;
4867 res = 0;
4868 for (i = current_value; i != value; i += inc) {
4869 if ((brightness_mode & 2) &&
4870 issue_thinkpad_cmos_command(cmos_cmd)) {
4871 res = -EIO;
4872 goto errout;
4874 if ((brightness_mode & 1) &&
4875 !acpi_ec_write(TP_EC_BACKLIGHT,
4876 (i + inc) | command_bits)) {
4877 res = -EIO;
4878 goto errout;;
4882 errout:
4883 mutex_unlock(&brightness_mutex);
4884 return res;
4887 /* sysfs backlight class ----------------------------------------------- */
4889 static int brightness_update_status(struct backlight_device *bd)
4891 /* it is the backlight class's job (caller) to handle
4892 * EINTR and other errors properly */
4893 return brightness_set(
4894 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4895 bd->props.power == FB_BLANK_UNBLANK) ?
4896 bd->props.brightness : 0);
4899 static int brightness_get(struct backlight_device *bd)
4901 int status, res;
4903 res = brightness_get_raw(&status);
4904 if (res < 0)
4905 return 0; /* FIXME: teach backlight about error handling */
4907 return status & TP_EC_BACKLIGHT_LVLMSK;
4910 static struct backlight_ops ibm_backlight_data = {
4911 .get_brightness = brightness_get,
4912 .update_status = brightness_update_status,
4915 /* --------------------------------------------------------------------- */
4917 static int __init brightness_init(struct ibm_init_struct *iibm)
4919 int b;
4921 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4923 mutex_init(&brightness_mutex);
4926 * We always attempt to detect acpi support, so as to switch
4927 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4928 * going to publish a backlight interface
4930 b = tpacpi_check_std_acpi_brightness_support();
4931 if (b > 0) {
4932 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4933 printk(TPACPI_NOTICE
4934 "Lenovo BIOS switched to ACPI backlight "
4935 "control mode\n");
4937 if (brightness_enable > 1) {
4938 printk(TPACPI_NOTICE
4939 "standard ACPI backlight interface "
4940 "available, not loading native one...\n");
4941 return 1;
4945 if (!brightness_enable) {
4946 dbg_printk(TPACPI_DBG_INIT,
4947 "brightness support disabled by "
4948 "module parameter\n");
4949 return 1;
4952 if (b > 16) {
4953 printk(TPACPI_ERR
4954 "Unsupported brightness interface, "
4955 "please contact %s\n", TPACPI_MAIL);
4956 return 1;
4958 if (b == 16)
4959 tp_features.bright_16levels = 1;
4961 if (!brightness_mode) {
4962 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4963 brightness_mode = 2;
4964 else
4965 brightness_mode = 3;
4967 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4968 brightness_mode);
4971 if (brightness_mode > 3)
4972 return -EINVAL;
4974 if (brightness_get_raw(&b) < 0)
4975 return 1;
4977 if (tp_features.bright_16levels)
4978 printk(TPACPI_INFO
4979 "detected a 16-level brightness capable ThinkPad\n");
4981 ibm_backlight_device = backlight_device_register(
4982 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4983 &ibm_backlight_data);
4984 if (IS_ERR(ibm_backlight_device)) {
4985 printk(TPACPI_ERR "Could not register backlight device\n");
4986 return PTR_ERR(ibm_backlight_device);
4988 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
4990 ibm_backlight_device->props.max_brightness =
4991 (tp_features.bright_16levels)? 15 : 7;
4992 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
4993 backlight_update_status(ibm_backlight_device);
4995 return 0;
4998 static void brightness_exit(void)
5000 if (ibm_backlight_device) {
5001 vdbg_printk(TPACPI_DBG_EXIT,
5002 "calling backlight_device_unregister()\n");
5003 backlight_device_unregister(ibm_backlight_device);
5007 static int brightness_read(char *p)
5009 int len = 0;
5010 int level;
5012 level = brightness_get(NULL);
5013 if (level < 0) {
5014 len += sprintf(p + len, "level:\t\tunreadable\n");
5015 } else {
5016 len += sprintf(p + len, "level:\t\t%d\n", level);
5017 len += sprintf(p + len, "commands:\tup, down\n");
5018 len += sprintf(p + len, "commands:\tlevel <level>"
5019 " (<level> is 0-%d)\n",
5020 (tp_features.bright_16levels) ? 15 : 7);
5023 return len;
5026 static int brightness_write(char *buf)
5028 int level;
5029 int rc;
5030 char *cmd;
5031 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5033 level = brightness_get(NULL);
5034 if (level < 0)
5035 return level;
5037 while ((cmd = next_cmd(&buf))) {
5038 if (strlencmp(cmd, "up") == 0) {
5039 if (level < max_level)
5040 level++;
5041 } else if (strlencmp(cmd, "down") == 0) {
5042 if (level > 0)
5043 level--;
5044 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5045 level >= 0 && level <= max_level) {
5046 /* new level set */
5047 } else
5048 return -EINVAL;
5052 * Now we know what the final level should be, so we try to set it.
5053 * Doing it this way makes the syscall restartable in case of EINTR
5055 rc = brightness_set(level);
5056 return (rc == -EINTR)? -ERESTARTSYS : rc;
5059 static struct ibm_struct brightness_driver_data = {
5060 .name = "brightness",
5061 .read = brightness_read,
5062 .write = brightness_write,
5063 .exit = brightness_exit,
5066 /*************************************************************************
5067 * Volume subdriver
5070 static int volume_offset = 0x30;
5072 static int volume_read(char *p)
5074 int len = 0;
5075 u8 level;
5077 if (!acpi_ec_read(volume_offset, &level)) {
5078 len += sprintf(p + len, "level:\t\tunreadable\n");
5079 } else {
5080 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5081 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5082 len += sprintf(p + len, "commands:\tup, down, mute\n");
5083 len += sprintf(p + len, "commands:\tlevel <level>"
5084 " (<level> is 0-15)\n");
5087 return len;
5090 static int volume_write(char *buf)
5092 int cmos_cmd, inc, i;
5093 u8 level, mute;
5094 int new_level, new_mute;
5095 char *cmd;
5097 while ((cmd = next_cmd(&buf))) {
5098 if (!acpi_ec_read(volume_offset, &level))
5099 return -EIO;
5100 new_mute = mute = level & 0x40;
5101 new_level = level = level & 0xf;
5103 if (strlencmp(cmd, "up") == 0) {
5104 if (mute)
5105 new_mute = 0;
5106 else
5107 new_level = level == 15 ? 15 : level + 1;
5108 } else if (strlencmp(cmd, "down") == 0) {
5109 if (mute)
5110 new_mute = 0;
5111 else
5112 new_level = level == 0 ? 0 : level - 1;
5113 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5114 new_level >= 0 && new_level <= 15) {
5115 /* new_level set */
5116 } else if (strlencmp(cmd, "mute") == 0) {
5117 new_mute = 0x40;
5118 } else
5119 return -EINVAL;
5121 if (new_level != level) {
5122 /* mute doesn't change */
5124 cmos_cmd = (new_level > level) ?
5125 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5126 inc = new_level > level ? 1 : -1;
5128 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5129 !acpi_ec_write(volume_offset, level)))
5130 return -EIO;
5132 for (i = level; i != new_level; i += inc)
5133 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5134 !acpi_ec_write(volume_offset, i + inc))
5135 return -EIO;
5137 if (mute &&
5138 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5139 !acpi_ec_write(volume_offset, new_level + mute))) {
5140 return -EIO;
5144 if (new_mute != mute) {
5145 /* level doesn't change */
5147 cmos_cmd = (new_mute) ?
5148 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5150 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5151 !acpi_ec_write(volume_offset, level + new_mute))
5152 return -EIO;
5156 return 0;
5159 static struct ibm_struct volume_driver_data = {
5160 .name = "volume",
5161 .read = volume_read,
5162 .write = volume_write,
5165 /*************************************************************************
5166 * Fan subdriver
5170 * FAN ACCESS MODES
5172 * TPACPI_FAN_RD_ACPI_GFAN:
5173 * ACPI GFAN method: returns fan level
5175 * see TPACPI_FAN_WR_ACPI_SFAN
5176 * EC 0x2f (HFSP) not available if GFAN exists
5178 * TPACPI_FAN_WR_ACPI_SFAN:
5179 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5181 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5182 * it for writing.
5184 * TPACPI_FAN_WR_TPEC:
5185 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5186 * Supported on almost all ThinkPads
5188 * Fan speed changes of any sort (including those caused by the
5189 * disengaged mode) are usually done slowly by the firmware as the
5190 * maximum ammount of fan duty cycle change per second seems to be
5191 * limited.
5193 * Reading is not available if GFAN exists.
5194 * Writing is not available if SFAN exists.
5196 * Bits
5197 * 7 automatic mode engaged;
5198 * (default operation mode of the ThinkPad)
5199 * fan level is ignored in this mode.
5200 * 6 full speed mode (takes precedence over bit 7);
5201 * not available on all thinkpads. May disable
5202 * the tachometer while the fan controller ramps up
5203 * the speed (which can take up to a few *minutes*).
5204 * Speeds up fan to 100% duty-cycle, which is far above
5205 * the standard RPM levels. It is not impossible that
5206 * it could cause hardware damage.
5207 * 5-3 unused in some models. Extra bits for fan level
5208 * in others, but still useless as all values above
5209 * 7 map to the same speed as level 7 in these models.
5210 * 2-0 fan level (0..7 usually)
5211 * 0x00 = stop
5212 * 0x07 = max (set when temperatures critical)
5213 * Some ThinkPads may have other levels, see
5214 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5216 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5217 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5218 * does so, its initial value is meaningless (0x07).
5220 * For firmware bugs, refer to:
5221 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5223 * ----
5225 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5226 * Main fan tachometer reading (in RPM)
5228 * This register is present on all ThinkPads with a new-style EC, and
5229 * it is known not to be present on the A21m/e, and T22, as there is
5230 * something else in offset 0x84 according to the ACPI DSDT. Other
5231 * ThinkPads from this same time period (and earlier) probably lack the
5232 * tachometer as well.
5234 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5235 * was never fixed by IBM to report the EC firmware version string
5236 * probably support the tachometer (like the early X models), so
5237 * detecting it is quite hard. We need more data to know for sure.
5239 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5240 * might result.
5242 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5243 * mode.
5245 * For firmware bugs, refer to:
5246 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5248 * TPACPI_FAN_WR_ACPI_FANS:
5249 * ThinkPad X31, X40, X41. Not available in the X60.
5251 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5252 * high speed. ACPI DSDT seems to map these three speeds to levels
5253 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5254 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5256 * The speeds are stored on handles
5257 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5259 * There are three default speed sets, acessible as handles:
5260 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5262 * ACPI DSDT switches which set is in use depending on various
5263 * factors.
5265 * TPACPI_FAN_WR_TPEC is also available and should be used to
5266 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5267 * but the ACPI tables just mention level 7.
5270 enum { /* Fan control constants */
5271 fan_status_offset = 0x2f, /* EC register 0x2f */
5272 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5273 * 0x84 must be read before 0x85 */
5275 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5276 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5278 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5281 enum fan_status_access_mode {
5282 TPACPI_FAN_NONE = 0, /* No fan status or control */
5283 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5284 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5287 enum fan_control_access_mode {
5288 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5289 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5290 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5291 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5294 enum fan_control_commands {
5295 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5296 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5297 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5298 * and also watchdog cmd */
5301 static int fan_control_allowed;
5303 static enum fan_status_access_mode fan_status_access_mode;
5304 static enum fan_control_access_mode fan_control_access_mode;
5305 static enum fan_control_commands fan_control_commands;
5307 static u8 fan_control_initial_status;
5308 static u8 fan_control_desired_level;
5309 static u8 fan_control_resume_level;
5310 static int fan_watchdog_maxinterval;
5312 static struct mutex fan_mutex;
5314 static void fan_watchdog_fire(struct work_struct *ignored);
5315 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5317 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5318 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5319 "\\FSPD", /* 600e/x, 770e, 770x */
5320 ); /* all others */
5321 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5322 "JFNS", /* 770x-JL */
5323 ); /* all others */
5326 * Call with fan_mutex held
5328 static void fan_update_desired_level(u8 status)
5330 if ((status &
5331 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5332 if (status > 7)
5333 fan_control_desired_level = 7;
5334 else
5335 fan_control_desired_level = status;
5339 static int fan_get_status(u8 *status)
5341 u8 s;
5343 /* TODO:
5344 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5346 switch (fan_status_access_mode) {
5347 case TPACPI_FAN_RD_ACPI_GFAN:
5348 /* 570, 600e/x, 770e, 770x */
5350 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5351 return -EIO;
5353 if (likely(status))
5354 *status = s & 0x07;
5356 break;
5358 case TPACPI_FAN_RD_TPEC:
5359 /* all except 570, 600e/x, 770e, 770x */
5360 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5361 return -EIO;
5363 if (likely(status))
5364 *status = s;
5366 break;
5368 default:
5369 return -ENXIO;
5372 return 0;
5375 static int fan_get_status_safe(u8 *status)
5377 int rc;
5378 u8 s;
5380 if (mutex_lock_interruptible(&fan_mutex))
5381 return -ERESTARTSYS;
5382 rc = fan_get_status(&s);
5383 if (!rc)
5384 fan_update_desired_level(s);
5385 mutex_unlock(&fan_mutex);
5387 if (status)
5388 *status = s;
5390 return rc;
5393 static int fan_get_speed(unsigned int *speed)
5395 u8 hi, lo;
5397 switch (fan_status_access_mode) {
5398 case TPACPI_FAN_RD_TPEC:
5399 /* all except 570, 600e/x, 770e, 770x */
5400 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5401 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5402 return -EIO;
5404 if (likely(speed))
5405 *speed = (hi << 8) | lo;
5407 break;
5409 default:
5410 return -ENXIO;
5413 return 0;
5416 static int fan_set_level(int level)
5418 if (!fan_control_allowed)
5419 return -EPERM;
5421 switch (fan_control_access_mode) {
5422 case TPACPI_FAN_WR_ACPI_SFAN:
5423 if (level >= 0 && level <= 7) {
5424 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5425 return -EIO;
5426 } else
5427 return -EINVAL;
5428 break;
5430 case TPACPI_FAN_WR_ACPI_FANS:
5431 case TPACPI_FAN_WR_TPEC:
5432 if (!(level & TP_EC_FAN_AUTO) &&
5433 !(level & TP_EC_FAN_FULLSPEED) &&
5434 ((level < 0) || (level > 7)))
5435 return -EINVAL;
5437 /* safety net should the EC not support AUTO
5438 * or FULLSPEED mode bits and just ignore them */
5439 if (level & TP_EC_FAN_FULLSPEED)
5440 level |= 7; /* safety min speed 7 */
5441 else if (level & TP_EC_FAN_AUTO)
5442 level |= 4; /* safety min speed 4 */
5444 if (!acpi_ec_write(fan_status_offset, level))
5445 return -EIO;
5446 else
5447 tp_features.fan_ctrl_status_undef = 0;
5448 break;
5450 default:
5451 return -ENXIO;
5453 return 0;
5456 static int fan_set_level_safe(int level)
5458 int rc;
5460 if (!fan_control_allowed)
5461 return -EPERM;
5463 if (mutex_lock_interruptible(&fan_mutex))
5464 return -ERESTARTSYS;
5466 if (level == TPACPI_FAN_LAST_LEVEL)
5467 level = fan_control_desired_level;
5469 rc = fan_set_level(level);
5470 if (!rc)
5471 fan_update_desired_level(level);
5473 mutex_unlock(&fan_mutex);
5474 return rc;
5477 static int fan_set_enable(void)
5479 u8 s;
5480 int rc;
5482 if (!fan_control_allowed)
5483 return -EPERM;
5485 if (mutex_lock_interruptible(&fan_mutex))
5486 return -ERESTARTSYS;
5488 switch (fan_control_access_mode) {
5489 case TPACPI_FAN_WR_ACPI_FANS:
5490 case TPACPI_FAN_WR_TPEC:
5491 rc = fan_get_status(&s);
5492 if (rc < 0)
5493 break;
5495 /* Don't go out of emergency fan mode */
5496 if (s != 7) {
5497 s &= 0x07;
5498 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5501 if (!acpi_ec_write(fan_status_offset, s))
5502 rc = -EIO;
5503 else {
5504 tp_features.fan_ctrl_status_undef = 0;
5505 rc = 0;
5507 break;
5509 case TPACPI_FAN_WR_ACPI_SFAN:
5510 rc = fan_get_status(&s);
5511 if (rc < 0)
5512 break;
5514 s &= 0x07;
5516 /* Set fan to at least level 4 */
5517 s |= 4;
5519 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5520 rc = -EIO;
5521 else
5522 rc = 0;
5523 break;
5525 default:
5526 rc = -ENXIO;
5529 mutex_unlock(&fan_mutex);
5530 return rc;
5533 static int fan_set_disable(void)
5535 int rc;
5537 if (!fan_control_allowed)
5538 return -EPERM;
5540 if (mutex_lock_interruptible(&fan_mutex))
5541 return -ERESTARTSYS;
5543 rc = 0;
5544 switch (fan_control_access_mode) {
5545 case TPACPI_FAN_WR_ACPI_FANS:
5546 case TPACPI_FAN_WR_TPEC:
5547 if (!acpi_ec_write(fan_status_offset, 0x00))
5548 rc = -EIO;
5549 else {
5550 fan_control_desired_level = 0;
5551 tp_features.fan_ctrl_status_undef = 0;
5553 break;
5555 case TPACPI_FAN_WR_ACPI_SFAN:
5556 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5557 rc = -EIO;
5558 else
5559 fan_control_desired_level = 0;
5560 break;
5562 default:
5563 rc = -ENXIO;
5567 mutex_unlock(&fan_mutex);
5568 return rc;
5571 static int fan_set_speed(int speed)
5573 int rc;
5575 if (!fan_control_allowed)
5576 return -EPERM;
5578 if (mutex_lock_interruptible(&fan_mutex))
5579 return -ERESTARTSYS;
5581 rc = 0;
5582 switch (fan_control_access_mode) {
5583 case TPACPI_FAN_WR_ACPI_FANS:
5584 if (speed >= 0 && speed <= 65535) {
5585 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5586 speed, speed, speed))
5587 rc = -EIO;
5588 } else
5589 rc = -EINVAL;
5590 break;
5592 default:
5593 rc = -ENXIO;
5596 mutex_unlock(&fan_mutex);
5597 return rc;
5600 static void fan_watchdog_reset(void)
5602 static int fan_watchdog_active;
5604 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5605 return;
5607 if (fan_watchdog_active)
5608 cancel_delayed_work(&fan_watchdog_task);
5610 if (fan_watchdog_maxinterval > 0 &&
5611 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5612 fan_watchdog_active = 1;
5613 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5614 msecs_to_jiffies(fan_watchdog_maxinterval
5615 * 1000))) {
5616 printk(TPACPI_ERR
5617 "failed to queue the fan watchdog, "
5618 "watchdog will not trigger\n");
5620 } else
5621 fan_watchdog_active = 0;
5624 static void fan_watchdog_fire(struct work_struct *ignored)
5626 int rc;
5628 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5629 return;
5631 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5632 rc = fan_set_enable();
5633 if (rc < 0) {
5634 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5635 "will try again later...\n", -rc);
5636 /* reschedule for later */
5637 fan_watchdog_reset();
5642 * SYSFS fan layout: hwmon compatible (device)
5644 * pwm*_enable:
5645 * 0: "disengaged" mode
5646 * 1: manual mode
5647 * 2: native EC "auto" mode (recommended, hardware default)
5649 * pwm*: set speed in manual mode, ignored otherwise.
5650 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5651 * interpolation.
5653 * fan*_input: tachometer reading, RPM
5656 * SYSFS fan layout: extensions
5658 * fan_watchdog (driver):
5659 * fan watchdog interval in seconds, 0 disables (default), max 120
5662 /* sysfs fan pwm1_enable ----------------------------------------------- */
5663 static ssize_t fan_pwm1_enable_show(struct device *dev,
5664 struct device_attribute *attr,
5665 char *buf)
5667 int res, mode;
5668 u8 status;
5670 res = fan_get_status_safe(&status);
5671 if (res)
5672 return res;
5674 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5675 if (status != fan_control_initial_status) {
5676 tp_features.fan_ctrl_status_undef = 0;
5677 } else {
5678 /* Return most likely status. In fact, it
5679 * might be the only possible status */
5680 status = TP_EC_FAN_AUTO;
5684 if (status & TP_EC_FAN_FULLSPEED) {
5685 mode = 0;
5686 } else if (status & TP_EC_FAN_AUTO) {
5687 mode = 2;
5688 } else
5689 mode = 1;
5691 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5694 static ssize_t fan_pwm1_enable_store(struct device *dev,
5695 struct device_attribute *attr,
5696 const char *buf, size_t count)
5698 unsigned long t;
5699 int res, level;
5701 if (parse_strtoul(buf, 2, &t))
5702 return -EINVAL;
5704 switch (t) {
5705 case 0:
5706 level = TP_EC_FAN_FULLSPEED;
5707 break;
5708 case 1:
5709 level = TPACPI_FAN_LAST_LEVEL;
5710 break;
5711 case 2:
5712 level = TP_EC_FAN_AUTO;
5713 break;
5714 case 3:
5715 /* reserved for software-controlled auto mode */
5716 return -ENOSYS;
5717 default:
5718 return -EINVAL;
5721 res = fan_set_level_safe(level);
5722 if (res == -ENXIO)
5723 return -EINVAL;
5724 else if (res < 0)
5725 return res;
5727 fan_watchdog_reset();
5729 return count;
5732 static struct device_attribute dev_attr_fan_pwm1_enable =
5733 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5734 fan_pwm1_enable_show, fan_pwm1_enable_store);
5736 /* sysfs fan pwm1 ------------------------------------------------------ */
5737 static ssize_t fan_pwm1_show(struct device *dev,
5738 struct device_attribute *attr,
5739 char *buf)
5741 int res;
5742 u8 status;
5744 res = fan_get_status_safe(&status);
5745 if (res)
5746 return res;
5748 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5749 if (status != fan_control_initial_status) {
5750 tp_features.fan_ctrl_status_undef = 0;
5751 } else {
5752 status = TP_EC_FAN_AUTO;
5756 if ((status &
5757 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5758 status = fan_control_desired_level;
5760 if (status > 7)
5761 status = 7;
5763 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5766 static ssize_t fan_pwm1_store(struct device *dev,
5767 struct device_attribute *attr,
5768 const char *buf, size_t count)
5770 unsigned long s;
5771 int rc;
5772 u8 status, newlevel;
5774 if (parse_strtoul(buf, 255, &s))
5775 return -EINVAL;
5777 /* scale down from 0-255 to 0-7 */
5778 newlevel = (s >> 5) & 0x07;
5780 if (mutex_lock_interruptible(&fan_mutex))
5781 return -ERESTARTSYS;
5783 rc = fan_get_status(&status);
5784 if (!rc && (status &
5785 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5786 rc = fan_set_level(newlevel);
5787 if (rc == -ENXIO)
5788 rc = -EINVAL;
5789 else if (!rc) {
5790 fan_update_desired_level(newlevel);
5791 fan_watchdog_reset();
5795 mutex_unlock(&fan_mutex);
5796 return (rc)? rc : count;
5799 static struct device_attribute dev_attr_fan_pwm1 =
5800 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5801 fan_pwm1_show, fan_pwm1_store);
5803 /* sysfs fan fan1_input ------------------------------------------------ */
5804 static ssize_t fan_fan1_input_show(struct device *dev,
5805 struct device_attribute *attr,
5806 char *buf)
5808 int res;
5809 unsigned int speed;
5811 res = fan_get_speed(&speed);
5812 if (res < 0)
5813 return res;
5815 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5818 static struct device_attribute dev_attr_fan_fan1_input =
5819 __ATTR(fan1_input, S_IRUGO,
5820 fan_fan1_input_show, NULL);
5822 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5823 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5824 char *buf)
5826 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5829 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5830 const char *buf, size_t count)
5832 unsigned long t;
5834 if (parse_strtoul(buf, 120, &t))
5835 return -EINVAL;
5837 if (!fan_control_allowed)
5838 return -EPERM;
5840 fan_watchdog_maxinterval = t;
5841 fan_watchdog_reset();
5843 return count;
5846 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5847 fan_fan_watchdog_show, fan_fan_watchdog_store);
5849 /* --------------------------------------------------------------------- */
5850 static struct attribute *fan_attributes[] = {
5851 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5852 &dev_attr_fan_fan1_input.attr,
5853 NULL
5856 static const struct attribute_group fan_attr_group = {
5857 .attrs = fan_attributes,
5860 static int __init fan_init(struct ibm_init_struct *iibm)
5862 int rc;
5864 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5866 mutex_init(&fan_mutex);
5867 fan_status_access_mode = TPACPI_FAN_NONE;
5868 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5869 fan_control_commands = 0;
5870 fan_watchdog_maxinterval = 0;
5871 tp_features.fan_ctrl_status_undef = 0;
5872 fan_control_desired_level = 7;
5874 TPACPI_ACPIHANDLE_INIT(fans);
5875 TPACPI_ACPIHANDLE_INIT(gfan);
5876 TPACPI_ACPIHANDLE_INIT(sfan);
5878 if (gfan_handle) {
5879 /* 570, 600e/x, 770e, 770x */
5880 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5881 } else {
5882 /* all other ThinkPads: note that even old-style
5883 * ThinkPad ECs supports the fan control register */
5884 if (likely(acpi_ec_read(fan_status_offset,
5885 &fan_control_initial_status))) {
5886 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5888 /* In some ThinkPads, neither the EC nor the ACPI
5889 * DSDT initialize the fan status, and it ends up
5890 * being set to 0x07 when it *could* be either
5891 * 0x07 or 0x80.
5893 * Enable for TP-1Y (T43), TP-78 (R51e),
5894 * TP-76 (R52), TP-70 (T43, R52), which are known
5895 * to be buggy. */
5896 if (fan_control_initial_status == 0x07) {
5897 switch (thinkpad_id.ec_model) {
5898 case 0x5931: /* TP-1Y */
5899 case 0x3837: /* TP-78 */
5900 case 0x3637: /* TP-76 */
5901 case 0x3037: /* TP-70 */
5902 printk(TPACPI_NOTICE
5903 "fan_init: initial fan status "
5904 "is unknown, assuming it is "
5905 "in auto mode\n");
5906 tp_features.fan_ctrl_status_undef = 1;
5910 } else {
5911 printk(TPACPI_ERR
5912 "ThinkPad ACPI EC access misbehaving, "
5913 "fan status and control unavailable\n");
5914 return 1;
5918 if (sfan_handle) {
5919 /* 570, 770x-JL */
5920 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5921 fan_control_commands |=
5922 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5923 } else {
5924 if (!gfan_handle) {
5925 /* gfan without sfan means no fan control */
5926 /* all other models implement TP EC 0x2f control */
5928 if (fans_handle) {
5929 /* X31, X40, X41 */
5930 fan_control_access_mode =
5931 TPACPI_FAN_WR_ACPI_FANS;
5932 fan_control_commands |=
5933 TPACPI_FAN_CMD_SPEED |
5934 TPACPI_FAN_CMD_LEVEL |
5935 TPACPI_FAN_CMD_ENABLE;
5936 } else {
5937 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5938 fan_control_commands |=
5939 TPACPI_FAN_CMD_LEVEL |
5940 TPACPI_FAN_CMD_ENABLE;
5945 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5946 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5947 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5948 fan_status_access_mode, fan_control_access_mode);
5950 /* fan control master switch */
5951 if (!fan_control_allowed) {
5952 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5953 fan_control_commands = 0;
5954 dbg_printk(TPACPI_DBG_INIT,
5955 "fan control features disabled by parameter\n");
5958 /* update fan_control_desired_level */
5959 if (fan_status_access_mode != TPACPI_FAN_NONE)
5960 fan_get_status_safe(NULL);
5962 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5963 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5964 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5965 &fan_attr_group);
5966 if (rc < 0)
5967 return rc;
5969 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5970 &driver_attr_fan_watchdog);
5971 if (rc < 0) {
5972 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5973 &fan_attr_group);
5974 return rc;
5976 return 0;
5977 } else
5978 return 1;
5981 static void fan_exit(void)
5983 vdbg_printk(TPACPI_DBG_EXIT,
5984 "cancelling any pending fan watchdog tasks\n");
5986 /* FIXME: can we really do this unconditionally? */
5987 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
5988 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5989 &driver_attr_fan_watchdog);
5991 cancel_delayed_work(&fan_watchdog_task);
5992 flush_workqueue(tpacpi_wq);
5995 static void fan_suspend(pm_message_t state)
5997 int rc;
5999 if (!fan_control_allowed)
6000 return;
6002 /* Store fan status in cache */
6003 fan_control_resume_level = 0;
6004 rc = fan_get_status_safe(&fan_control_resume_level);
6005 if (rc < 0)
6006 printk(TPACPI_NOTICE
6007 "failed to read fan level for later "
6008 "restore during resume: %d\n", rc);
6010 /* if it is undefined, don't attempt to restore it.
6011 * KEEP THIS LAST */
6012 if (tp_features.fan_ctrl_status_undef)
6013 fan_control_resume_level = 0;
6016 static void fan_resume(void)
6018 u8 current_level = 7;
6019 bool do_set = false;
6020 int rc;
6022 /* DSDT *always* updates status on resume */
6023 tp_features.fan_ctrl_status_undef = 0;
6025 if (!fan_control_allowed ||
6026 !fan_control_resume_level ||
6027 (fan_get_status_safe(&current_level) < 0))
6028 return;
6030 switch (fan_control_access_mode) {
6031 case TPACPI_FAN_WR_ACPI_SFAN:
6032 /* never decrease fan level */
6033 do_set = (fan_control_resume_level > current_level);
6034 break;
6035 case TPACPI_FAN_WR_ACPI_FANS:
6036 case TPACPI_FAN_WR_TPEC:
6037 /* never decrease fan level, scale is:
6038 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6040 * We expect the firmware to set either 7 or AUTO, but we
6041 * handle FULLSPEED out of paranoia.
6043 * So, we can safely only restore FULLSPEED or 7, anything
6044 * else could slow the fan. Restoring AUTO is useless, at
6045 * best that's exactly what the DSDT already set (it is the
6046 * slower it uses).
6048 * Always keep in mind that the DSDT *will* have set the
6049 * fans to what the vendor supposes is the best level. We
6050 * muck with it only to speed the fan up.
6052 if (fan_control_resume_level != 7 &&
6053 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6054 return;
6055 else
6056 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6057 (current_level != fan_control_resume_level);
6058 break;
6059 default:
6060 return;
6062 if (do_set) {
6063 printk(TPACPI_NOTICE
6064 "restoring fan level to 0x%02x\n",
6065 fan_control_resume_level);
6066 rc = fan_set_level_safe(fan_control_resume_level);
6067 if (rc < 0)
6068 printk(TPACPI_NOTICE
6069 "failed to restore fan level: %d\n", rc);
6073 static int fan_read(char *p)
6075 int len = 0;
6076 int rc;
6077 u8 status;
6078 unsigned int speed = 0;
6080 switch (fan_status_access_mode) {
6081 case TPACPI_FAN_RD_ACPI_GFAN:
6082 /* 570, 600e/x, 770e, 770x */
6083 rc = fan_get_status_safe(&status);
6084 if (rc < 0)
6085 return rc;
6087 len += sprintf(p + len, "status:\t\t%s\n"
6088 "level:\t\t%d\n",
6089 (status != 0) ? "enabled" : "disabled", status);
6090 break;
6092 case TPACPI_FAN_RD_TPEC:
6093 /* all except 570, 600e/x, 770e, 770x */
6094 rc = fan_get_status_safe(&status);
6095 if (rc < 0)
6096 return rc;
6098 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6099 if (status != fan_control_initial_status)
6100 tp_features.fan_ctrl_status_undef = 0;
6101 else
6102 /* Return most likely status. In fact, it
6103 * might be the only possible status */
6104 status = TP_EC_FAN_AUTO;
6107 len += sprintf(p + len, "status:\t\t%s\n",
6108 (status != 0) ? "enabled" : "disabled");
6110 rc = fan_get_speed(&speed);
6111 if (rc < 0)
6112 return rc;
6114 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6116 if (status & TP_EC_FAN_FULLSPEED)
6117 /* Disengaged mode takes precedence */
6118 len += sprintf(p + len, "level:\t\tdisengaged\n");
6119 else if (status & TP_EC_FAN_AUTO)
6120 len += sprintf(p + len, "level:\t\tauto\n");
6121 else
6122 len += sprintf(p + len, "level:\t\t%d\n", status);
6123 break;
6125 case TPACPI_FAN_NONE:
6126 default:
6127 len += sprintf(p + len, "status:\t\tnot supported\n");
6130 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6131 len += sprintf(p + len, "commands:\tlevel <level>");
6133 switch (fan_control_access_mode) {
6134 case TPACPI_FAN_WR_ACPI_SFAN:
6135 len += sprintf(p + len, " (<level> is 0-7)\n");
6136 break;
6138 default:
6139 len += sprintf(p + len, " (<level> is 0-7, "
6140 "auto, disengaged, full-speed)\n");
6141 break;
6145 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6146 len += sprintf(p + len, "commands:\tenable, disable\n"
6147 "commands:\twatchdog <timeout> (<timeout> "
6148 "is 0 (off), 1-120 (seconds))\n");
6150 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6151 len += sprintf(p + len, "commands:\tspeed <speed>"
6152 " (<speed> is 0-65535)\n");
6154 return len;
6157 static int fan_write_cmd_level(const char *cmd, int *rc)
6159 int level;
6161 if (strlencmp(cmd, "level auto") == 0)
6162 level = TP_EC_FAN_AUTO;
6163 else if ((strlencmp(cmd, "level disengaged") == 0) |
6164 (strlencmp(cmd, "level full-speed") == 0))
6165 level = TP_EC_FAN_FULLSPEED;
6166 else if (sscanf(cmd, "level %d", &level) != 1)
6167 return 0;
6169 *rc = fan_set_level_safe(level);
6170 if (*rc == -ENXIO)
6171 printk(TPACPI_ERR "level command accepted for unsupported "
6172 "access mode %d", fan_control_access_mode);
6174 return 1;
6177 static int fan_write_cmd_enable(const char *cmd, int *rc)
6179 if (strlencmp(cmd, "enable") != 0)
6180 return 0;
6182 *rc = fan_set_enable();
6183 if (*rc == -ENXIO)
6184 printk(TPACPI_ERR "enable command accepted for unsupported "
6185 "access mode %d", fan_control_access_mode);
6187 return 1;
6190 static int fan_write_cmd_disable(const char *cmd, int *rc)
6192 if (strlencmp(cmd, "disable") != 0)
6193 return 0;
6195 *rc = fan_set_disable();
6196 if (*rc == -ENXIO)
6197 printk(TPACPI_ERR "disable command accepted for unsupported "
6198 "access mode %d", fan_control_access_mode);
6200 return 1;
6203 static int fan_write_cmd_speed(const char *cmd, int *rc)
6205 int speed;
6207 /* TODO:
6208 * Support speed <low> <medium> <high> ? */
6210 if (sscanf(cmd, "speed %d", &speed) != 1)
6211 return 0;
6213 *rc = fan_set_speed(speed);
6214 if (*rc == -ENXIO)
6215 printk(TPACPI_ERR "speed command accepted for unsupported "
6216 "access mode %d", fan_control_access_mode);
6218 return 1;
6221 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6223 int interval;
6225 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6226 return 0;
6228 if (interval < 0 || interval > 120)
6229 *rc = -EINVAL;
6230 else
6231 fan_watchdog_maxinterval = interval;
6233 return 1;
6236 static int fan_write(char *buf)
6238 char *cmd;
6239 int rc = 0;
6241 while (!rc && (cmd = next_cmd(&buf))) {
6242 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6243 fan_write_cmd_level(cmd, &rc)) &&
6244 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6245 (fan_write_cmd_enable(cmd, &rc) ||
6246 fan_write_cmd_disable(cmd, &rc) ||
6247 fan_write_cmd_watchdog(cmd, &rc))) &&
6248 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6249 fan_write_cmd_speed(cmd, &rc))
6251 rc = -EINVAL;
6252 else if (!rc)
6253 fan_watchdog_reset();
6256 return rc;
6259 static struct ibm_struct fan_driver_data = {
6260 .name = "fan",
6261 .read = fan_read,
6262 .write = fan_write,
6263 .exit = fan_exit,
6264 .suspend = fan_suspend,
6265 .resume = fan_resume,
6268 /****************************************************************************
6269 ****************************************************************************
6271 * Infrastructure
6273 ****************************************************************************
6274 ****************************************************************************/
6276 /* sysfs name ---------------------------------------------------------- */
6277 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6278 struct device_attribute *attr,
6279 char *buf)
6281 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6284 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6285 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6287 /* --------------------------------------------------------------------- */
6289 /* /proc support */
6290 static struct proc_dir_entry *proc_dir;
6293 * Module and infrastructure proble, init and exit handling
6296 static int force_load;
6298 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6299 static const char * __init str_supported(int is_supported)
6301 static char text_unsupported[] __initdata = "not supported";
6303 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6305 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6307 static void ibm_exit(struct ibm_struct *ibm)
6309 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6311 list_del_init(&ibm->all_drivers);
6313 if (ibm->flags.acpi_notify_installed) {
6314 dbg_printk(TPACPI_DBG_EXIT,
6315 "%s: acpi_remove_notify_handler\n", ibm->name);
6316 BUG_ON(!ibm->acpi);
6317 acpi_remove_notify_handler(*ibm->acpi->handle,
6318 ibm->acpi->type,
6319 dispatch_acpi_notify);
6320 ibm->flags.acpi_notify_installed = 0;
6321 ibm->flags.acpi_notify_installed = 0;
6324 if (ibm->flags.proc_created) {
6325 dbg_printk(TPACPI_DBG_EXIT,
6326 "%s: remove_proc_entry\n", ibm->name);
6327 remove_proc_entry(ibm->name, proc_dir);
6328 ibm->flags.proc_created = 0;
6331 if (ibm->flags.acpi_driver_registered) {
6332 dbg_printk(TPACPI_DBG_EXIT,
6333 "%s: acpi_bus_unregister_driver\n", ibm->name);
6334 BUG_ON(!ibm->acpi);
6335 acpi_bus_unregister_driver(ibm->acpi->driver);
6336 kfree(ibm->acpi->driver);
6337 ibm->acpi->driver = NULL;
6338 ibm->flags.acpi_driver_registered = 0;
6341 if (ibm->flags.init_called && ibm->exit) {
6342 ibm->exit();
6343 ibm->flags.init_called = 0;
6346 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6349 static int __init ibm_init(struct ibm_init_struct *iibm)
6351 int ret;
6352 struct ibm_struct *ibm = iibm->data;
6353 struct proc_dir_entry *entry;
6355 BUG_ON(ibm == NULL);
6357 INIT_LIST_HEAD(&ibm->all_drivers);
6359 if (ibm->flags.experimental && !experimental)
6360 return 0;
6362 dbg_printk(TPACPI_DBG_INIT,
6363 "probing for %s\n", ibm->name);
6365 if (iibm->init) {
6366 ret = iibm->init(iibm);
6367 if (ret > 0)
6368 return 0; /* probe failed */
6369 if (ret)
6370 return ret;
6372 ibm->flags.init_called = 1;
6375 if (ibm->acpi) {
6376 if (ibm->acpi->hid) {
6377 ret = register_tpacpi_subdriver(ibm);
6378 if (ret)
6379 goto err_out;
6382 if (ibm->acpi->notify) {
6383 ret = setup_acpi_notify(ibm);
6384 if (ret == -ENODEV) {
6385 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6386 ibm->name);
6387 ret = 0;
6388 goto err_out;
6390 if (ret < 0)
6391 goto err_out;
6395 dbg_printk(TPACPI_DBG_INIT,
6396 "%s installed\n", ibm->name);
6398 if (ibm->read) {
6399 entry = create_proc_entry(ibm->name,
6400 S_IFREG | S_IRUGO | S_IWUSR,
6401 proc_dir);
6402 if (!entry) {
6403 printk(TPACPI_ERR "unable to create proc entry %s\n",
6404 ibm->name);
6405 ret = -ENODEV;
6406 goto err_out;
6408 entry->owner = THIS_MODULE;
6409 entry->data = ibm;
6410 entry->read_proc = &dispatch_procfs_read;
6411 if (ibm->write)
6412 entry->write_proc = &dispatch_procfs_write;
6413 ibm->flags.proc_created = 1;
6416 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6418 return 0;
6420 err_out:
6421 dbg_printk(TPACPI_DBG_INIT,
6422 "%s: at error exit path with result %d\n",
6423 ibm->name, ret);
6425 ibm_exit(ibm);
6426 return (ret < 0)? ret : 0;
6429 /* Probing */
6431 /* returns 0 - probe ok, or < 0 - probe error.
6432 * Probe ok doesn't mean thinkpad found.
6433 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6434 static int __must_check __init get_thinkpad_model_data(
6435 struct thinkpad_id_data *tp)
6437 const struct dmi_device *dev = NULL;
6438 char ec_fw_string[18];
6439 char const *s;
6441 if (!tp)
6442 return -EINVAL;
6444 memset(tp, 0, sizeof(*tp));
6446 if (dmi_name_in_vendors("IBM"))
6447 tp->vendor = PCI_VENDOR_ID_IBM;
6448 else if (dmi_name_in_vendors("LENOVO"))
6449 tp->vendor = PCI_VENDOR_ID_LENOVO;
6450 else
6451 return 0;
6453 s = dmi_get_system_info(DMI_BIOS_VERSION);
6454 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6455 if (s && !tp->bios_version_str)
6456 return -ENOMEM;
6457 if (!tp->bios_version_str)
6458 return 0;
6459 tp->bios_model = tp->bios_version_str[0]
6460 | (tp->bios_version_str[1] << 8);
6463 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6464 * X32 or newer, all Z series; Some models must have an
6465 * up-to-date BIOS or they will not be detected.
6467 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6469 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6470 if (sscanf(dev->name,
6471 "IBM ThinkPad Embedded Controller -[%17c",
6472 ec_fw_string) == 1) {
6473 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6474 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6476 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6477 if (!tp->ec_version_str)
6478 return -ENOMEM;
6479 tp->ec_model = ec_fw_string[0]
6480 | (ec_fw_string[1] << 8);
6481 break;
6485 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6486 if (s && !strnicmp(s, "ThinkPad", 8)) {
6487 tp->model_str = kstrdup(s, GFP_KERNEL);
6488 if (!tp->model_str)
6489 return -ENOMEM;
6492 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6493 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6494 if (s && !tp->nummodel_str)
6495 return -ENOMEM;
6497 return 0;
6500 static int __init probe_for_thinkpad(void)
6502 int is_thinkpad;
6504 if (acpi_disabled)
6505 return -ENODEV;
6508 * Non-ancient models have better DMI tagging, but very old models
6509 * don't.
6511 is_thinkpad = (thinkpad_id.model_str != NULL);
6513 /* ec is required because many other handles are relative to it */
6514 TPACPI_ACPIHANDLE_INIT(ec);
6515 if (!ec_handle) {
6516 if (is_thinkpad)
6517 printk(TPACPI_ERR
6518 "Not yet supported ThinkPad detected!\n");
6519 return -ENODEV;
6523 * Risks a regression on very old machines, but reduces potential
6524 * false positives a damn great deal
6526 if (!is_thinkpad)
6527 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6529 if (!is_thinkpad && !force_load)
6530 return -ENODEV;
6532 return 0;
6536 /* Module init, exit, parameters */
6538 static struct ibm_init_struct ibms_init[] __initdata = {
6540 .init = thinkpad_acpi_driver_init,
6541 .data = &thinkpad_acpi_driver_data,
6544 .init = hotkey_init,
6545 .data = &hotkey_driver_data,
6548 .init = bluetooth_init,
6549 .data = &bluetooth_driver_data,
6552 .init = wan_init,
6553 .data = &wan_driver_data,
6555 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6557 .init = video_init,
6558 .data = &video_driver_data,
6560 #endif
6562 .init = light_init,
6563 .data = &light_driver_data,
6565 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6567 .init = dock_init,
6568 .data = &dock_driver_data[0],
6571 .init = dock_init2,
6572 .data = &dock_driver_data[1],
6574 #endif
6575 #ifdef CONFIG_THINKPAD_ACPI_BAY
6577 .init = bay_init,
6578 .data = &bay_driver_data,
6580 #endif
6582 .init = cmos_init,
6583 .data = &cmos_driver_data,
6586 .init = led_init,
6587 .data = &led_driver_data,
6590 .init = beep_init,
6591 .data = &beep_driver_data,
6594 .init = thermal_init,
6595 .data = &thermal_driver_data,
6598 .data = &ecdump_driver_data,
6601 .init = brightness_init,
6602 .data = &brightness_driver_data,
6605 .data = &volume_driver_data,
6608 .init = fan_init,
6609 .data = &fan_driver_data,
6613 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6615 unsigned int i;
6616 struct ibm_struct *ibm;
6618 if (!kp || !kp->name || !val)
6619 return -EINVAL;
6621 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6622 ibm = ibms_init[i].data;
6623 WARN_ON(ibm == NULL);
6625 if (!ibm || !ibm->name)
6626 continue;
6628 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6629 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6630 return -ENOSPC;
6631 strcpy(ibms_init[i].param, val);
6632 strcat(ibms_init[i].param, ",");
6633 return 0;
6637 return -EINVAL;
6640 module_param(experimental, int, 0);
6641 MODULE_PARM_DESC(experimental,
6642 "Enables experimental features when non-zero");
6644 module_param_named(debug, dbg_level, uint, 0);
6645 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6647 module_param(force_load, bool, 0);
6648 MODULE_PARM_DESC(force_load,
6649 "Attempts to load the driver even on a "
6650 "mis-identified ThinkPad when true");
6652 module_param_named(fan_control, fan_control_allowed, bool, 0);
6653 MODULE_PARM_DESC(fan_control,
6654 "Enables setting fan parameters features when true");
6656 module_param_named(brightness_mode, brightness_mode, int, 0);
6657 MODULE_PARM_DESC(brightness_mode,
6658 "Selects brightness control strategy: "
6659 "0=auto, 1=EC, 2=CMOS, 3=both");
6661 module_param(brightness_enable, uint, 0);
6662 MODULE_PARM_DESC(brightness_enable,
6663 "Enables backlight control when 1, disables when 0");
6665 module_param(hotkey_report_mode, uint, 0);
6666 MODULE_PARM_DESC(hotkey_report_mode,
6667 "used for backwards compatibility with userspace, "
6668 "see documentation");
6670 #define TPACPI_PARAM(feature) \
6671 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6672 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6673 "at module load, see documentation")
6675 TPACPI_PARAM(hotkey);
6676 TPACPI_PARAM(bluetooth);
6677 TPACPI_PARAM(video);
6678 TPACPI_PARAM(light);
6679 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6680 TPACPI_PARAM(dock);
6681 #endif
6682 #ifdef CONFIG_THINKPAD_ACPI_BAY
6683 TPACPI_PARAM(bay);
6684 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6685 TPACPI_PARAM(cmos);
6686 TPACPI_PARAM(led);
6687 TPACPI_PARAM(beep);
6688 TPACPI_PARAM(ecdump);
6689 TPACPI_PARAM(brightness);
6690 TPACPI_PARAM(volume);
6691 TPACPI_PARAM(fan);
6693 static void thinkpad_acpi_module_exit(void)
6695 struct ibm_struct *ibm, *itmp;
6697 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6699 list_for_each_entry_safe_reverse(ibm, itmp,
6700 &tpacpi_all_drivers,
6701 all_drivers) {
6702 ibm_exit(ibm);
6705 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6707 if (tpacpi_inputdev) {
6708 if (tp_features.input_device_registered)
6709 input_unregister_device(tpacpi_inputdev);
6710 else
6711 input_free_device(tpacpi_inputdev);
6714 if (tpacpi_hwmon)
6715 hwmon_device_unregister(tpacpi_hwmon);
6717 if (tp_features.sensors_pdev_attrs_registered)
6718 device_remove_file(&tpacpi_sensors_pdev->dev,
6719 &dev_attr_thinkpad_acpi_pdev_name);
6720 if (tpacpi_sensors_pdev)
6721 platform_device_unregister(tpacpi_sensors_pdev);
6722 if (tpacpi_pdev)
6723 platform_device_unregister(tpacpi_pdev);
6725 if (tp_features.sensors_pdrv_attrs_registered)
6726 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6727 if (tp_features.platform_drv_attrs_registered)
6728 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6730 if (tp_features.sensors_pdrv_registered)
6731 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6733 if (tp_features.platform_drv_registered)
6734 platform_driver_unregister(&tpacpi_pdriver);
6736 if (proc_dir)
6737 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6739 if (tpacpi_wq)
6740 destroy_workqueue(tpacpi_wq);
6742 kfree(thinkpad_id.bios_version_str);
6743 kfree(thinkpad_id.ec_version_str);
6744 kfree(thinkpad_id.model_str);
6748 static int __init thinkpad_acpi_module_init(void)
6750 int ret, i;
6752 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6754 /* Parameter checking */
6755 if (hotkey_report_mode > 2)
6756 return -EINVAL;
6758 /* Driver-level probe */
6760 ret = get_thinkpad_model_data(&thinkpad_id);
6761 if (ret) {
6762 printk(TPACPI_ERR
6763 "unable to get DMI data: %d\n", ret);
6764 thinkpad_acpi_module_exit();
6765 return ret;
6767 ret = probe_for_thinkpad();
6768 if (ret) {
6769 thinkpad_acpi_module_exit();
6770 return ret;
6773 /* Driver initialization */
6775 TPACPI_ACPIHANDLE_INIT(ecrd);
6776 TPACPI_ACPIHANDLE_INIT(ecwr);
6778 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6779 if (!tpacpi_wq) {
6780 thinkpad_acpi_module_exit();
6781 return -ENOMEM;
6784 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6785 if (!proc_dir) {
6786 printk(TPACPI_ERR
6787 "unable to create proc dir " TPACPI_PROC_DIR);
6788 thinkpad_acpi_module_exit();
6789 return -ENODEV;
6791 proc_dir->owner = THIS_MODULE;
6793 ret = platform_driver_register(&tpacpi_pdriver);
6794 if (ret) {
6795 printk(TPACPI_ERR
6796 "unable to register main platform driver\n");
6797 thinkpad_acpi_module_exit();
6798 return ret;
6800 tp_features.platform_drv_registered = 1;
6802 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6803 if (ret) {
6804 printk(TPACPI_ERR
6805 "unable to register hwmon platform driver\n");
6806 thinkpad_acpi_module_exit();
6807 return ret;
6809 tp_features.sensors_pdrv_registered = 1;
6811 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6812 if (!ret) {
6813 tp_features.platform_drv_attrs_registered = 1;
6814 ret = tpacpi_create_driver_attributes(
6815 &tpacpi_hwmon_pdriver.driver);
6817 if (ret) {
6818 printk(TPACPI_ERR
6819 "unable to create sysfs driver attributes\n");
6820 thinkpad_acpi_module_exit();
6821 return ret;
6823 tp_features.sensors_pdrv_attrs_registered = 1;
6826 /* Device initialization */
6827 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6828 NULL, 0);
6829 if (IS_ERR(tpacpi_pdev)) {
6830 ret = PTR_ERR(tpacpi_pdev);
6831 tpacpi_pdev = NULL;
6832 printk(TPACPI_ERR "unable to register platform device\n");
6833 thinkpad_acpi_module_exit();
6834 return ret;
6836 tpacpi_sensors_pdev = platform_device_register_simple(
6837 TPACPI_HWMON_DRVR_NAME,
6838 -1, NULL, 0);
6839 if (IS_ERR(tpacpi_sensors_pdev)) {
6840 ret = PTR_ERR(tpacpi_sensors_pdev);
6841 tpacpi_sensors_pdev = NULL;
6842 printk(TPACPI_ERR
6843 "unable to register hwmon platform device\n");
6844 thinkpad_acpi_module_exit();
6845 return ret;
6847 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6848 &dev_attr_thinkpad_acpi_pdev_name);
6849 if (ret) {
6850 printk(TPACPI_ERR
6851 "unable to create sysfs hwmon device attributes\n");
6852 thinkpad_acpi_module_exit();
6853 return ret;
6855 tp_features.sensors_pdev_attrs_registered = 1;
6856 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6857 if (IS_ERR(tpacpi_hwmon)) {
6858 ret = PTR_ERR(tpacpi_hwmon);
6859 tpacpi_hwmon = NULL;
6860 printk(TPACPI_ERR "unable to register hwmon device\n");
6861 thinkpad_acpi_module_exit();
6862 return ret;
6864 mutex_init(&tpacpi_inputdev_send_mutex);
6865 tpacpi_inputdev = input_allocate_device();
6866 if (!tpacpi_inputdev) {
6867 printk(TPACPI_ERR "unable to allocate input device\n");
6868 thinkpad_acpi_module_exit();
6869 return -ENOMEM;
6870 } else {
6871 /* Prepare input device, but don't register */
6872 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6873 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6874 tpacpi_inputdev->id.bustype = BUS_HOST;
6875 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6876 thinkpad_id.vendor :
6877 PCI_VENDOR_ID_IBM;
6878 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6879 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6881 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6882 ret = ibm_init(&ibms_init[i]);
6883 if (ret >= 0 && *ibms_init[i].param)
6884 ret = ibms_init[i].data->write(ibms_init[i].param);
6885 if (ret < 0) {
6886 thinkpad_acpi_module_exit();
6887 return ret;
6890 ret = input_register_device(tpacpi_inputdev);
6891 if (ret < 0) {
6892 printk(TPACPI_ERR "unable to register input device\n");
6893 thinkpad_acpi_module_exit();
6894 return ret;
6895 } else {
6896 tp_features.input_device_registered = 1;
6899 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6900 return 0;
6903 /* Please remove this in year 2009 */
6904 MODULE_ALIAS("ibm_acpi");
6906 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6909 * DMI matching for module autoloading
6911 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6912 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6914 * Only models listed in thinkwiki will be supported, so add yours
6915 * if it is not there yet.
6917 #define IBM_BIOS_MODULE_ALIAS(__type) \
6918 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
6920 /* Non-ancient thinkpads */
6921 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6922 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6924 /* Ancient thinkpad BIOSes have to be identified by
6925 * BIOS type or model number, and there are far less
6926 * BIOS types than model numbers... */
6927 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
6928 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
6929 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
6931 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6932 MODULE_DESCRIPTION(TPACPI_DESC);
6933 MODULE_VERSION(TPACPI_VERSION);
6934 MODULE_LICENSE("GPL");
6936 module_init(thinkpad_acpi_module_init);
6937 module_exit(thinkpad_acpi_module_exit);