Staging: rt2870: fix up netdev->priv usage
[linux-2.6/mini2440.git] / drivers / misc / thinkpad_acpi.c
blob899766e16fa822cdfd7abc240e2ce0ee7ea329e4
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 /* Special LED class that can defer work */
285 struct tpacpi_led_classdev {
286 struct led_classdev led_classdev;
287 struct work_struct work;
288 enum led_brightness new_brightness;
289 unsigned int led;
292 /****************************************************************************
293 ****************************************************************************
295 * ACPI Helpers and device model
297 ****************************************************************************
298 ****************************************************************************/
300 /*************************************************************************
301 * ACPI basic handles
304 static acpi_handle root_handle;
306 #define TPACPI_HANDLE(object, parent, paths...) \
307 static acpi_handle object##_handle; \
308 static acpi_handle *object##_parent = &parent##_handle; \
309 static char *object##_path; \
310 static char *object##_paths[] = { paths }
312 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
313 "\\_SB.PCI.ISA.EC", /* 570 */
314 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
315 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
316 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
317 "\\_SB.PCI0.ICH3.EC0", /* R31 */
318 "\\_SB.PCI0.LPC.EC", /* all others */
321 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
322 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
324 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
325 /* T4x, X31, X40 */
326 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
327 "\\CMS", /* R40, R40e */
328 ); /* all others */
330 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
331 "^HKEY", /* R30, R31 */
332 "HKEY", /* all others */
333 ); /* 570 */
335 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
336 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
337 "\\_SB.PCI0.VID0", /* 770e */
338 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
339 "\\_SB.PCI0.AGP.VID", /* all others */
340 ); /* R30, R31 */
343 /*************************************************************************
344 * ACPI helpers
347 static int acpi_evalf(acpi_handle handle,
348 void *res, char *method, char *fmt, ...)
350 char *fmt0 = fmt;
351 struct acpi_object_list params;
352 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
353 struct acpi_buffer result, *resultp;
354 union acpi_object out_obj;
355 acpi_status status;
356 va_list ap;
357 char res_type;
358 int success;
359 int quiet;
361 if (!*fmt) {
362 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
363 return 0;
366 if (*fmt == 'q') {
367 quiet = 1;
368 fmt++;
369 } else
370 quiet = 0;
372 res_type = *(fmt++);
374 params.count = 0;
375 params.pointer = &in_objs[0];
377 va_start(ap, fmt);
378 while (*fmt) {
379 char c = *(fmt++);
380 switch (c) {
381 case 'd': /* int */
382 in_objs[params.count].integer.value = va_arg(ap, int);
383 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
384 break;
385 /* add more types as needed */
386 default:
387 printk(TPACPI_ERR "acpi_evalf() called "
388 "with invalid format character '%c'\n", c);
389 return 0;
392 va_end(ap);
394 if (res_type != 'v') {
395 result.length = sizeof(out_obj);
396 result.pointer = &out_obj;
397 resultp = &result;
398 } else
399 resultp = NULL;
401 status = acpi_evaluate_object(handle, method, &params, resultp);
403 switch (res_type) {
404 case 'd': /* int */
405 if (res)
406 *(int *)res = out_obj.integer.value;
407 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
408 break;
409 case 'v': /* void */
410 success = status == AE_OK;
411 break;
412 /* add more types as needed */
413 default:
414 printk(TPACPI_ERR "acpi_evalf() called "
415 "with invalid format character '%c'\n", res_type);
416 return 0;
419 if (!success && !quiet)
420 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
421 method, fmt0, status);
423 return success;
426 static int acpi_ec_read(int i, u8 *p)
428 int v;
430 if (ecrd_handle) {
431 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
432 return 0;
433 *p = v;
434 } else {
435 if (ec_read(i, p) < 0)
436 return 0;
439 return 1;
442 static int acpi_ec_write(int i, u8 v)
444 if (ecwr_handle) {
445 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
446 return 0;
447 } else {
448 if (ec_write(i, v) < 0)
449 return 0;
452 return 1;
455 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
456 static int _sta(acpi_handle handle)
458 int status;
460 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
461 status = 0;
463 return status;
465 #endif
467 static int issue_thinkpad_cmos_command(int cmos_cmd)
469 if (!cmos_handle)
470 return -ENXIO;
472 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
473 return -EIO;
475 return 0;
478 /*************************************************************************
479 * ACPI device model
482 #define TPACPI_ACPIHANDLE_INIT(object) \
483 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
484 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
486 static void drv_acpi_handle_init(char *name,
487 acpi_handle *handle, acpi_handle parent,
488 char **paths, int num_paths, char **path)
490 int i;
491 acpi_status status;
493 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
494 name);
496 for (i = 0; i < num_paths; i++) {
497 status = acpi_get_handle(parent, paths[i], handle);
498 if (ACPI_SUCCESS(status)) {
499 *path = paths[i];
500 dbg_printk(TPACPI_DBG_INIT,
501 "Found ACPI handle %s for %s\n",
502 *path, name);
503 return;
507 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
508 name);
509 *handle = NULL;
512 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
514 struct ibm_struct *ibm = data;
516 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
517 return;
519 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
520 return;
522 ibm->acpi->notify(ibm, event);
525 static int __init setup_acpi_notify(struct ibm_struct *ibm)
527 acpi_status status;
528 int rc;
530 BUG_ON(!ibm->acpi);
532 if (!*ibm->acpi->handle)
533 return 0;
535 vdbg_printk(TPACPI_DBG_INIT,
536 "setting up ACPI notify for %s\n", ibm->name);
538 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
539 if (rc < 0) {
540 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
541 ibm->name, rc);
542 return -ENODEV;
545 ibm->acpi->device->driver_data = ibm;
546 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
547 TPACPI_ACPI_EVENT_PREFIX,
548 ibm->name);
550 status = acpi_install_notify_handler(*ibm->acpi->handle,
551 ibm->acpi->type, dispatch_acpi_notify, ibm);
552 if (ACPI_FAILURE(status)) {
553 if (status == AE_ALREADY_EXISTS) {
554 printk(TPACPI_NOTICE
555 "another device driver is already "
556 "handling %s events\n", ibm->name);
557 } else {
558 printk(TPACPI_ERR
559 "acpi_install_notify_handler(%s) failed: %d\n",
560 ibm->name, status);
562 return -ENODEV;
564 ibm->flags.acpi_notify_installed = 1;
565 return 0;
568 static int __init tpacpi_device_add(struct acpi_device *device)
570 return 0;
573 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
575 int rc;
577 dbg_printk(TPACPI_DBG_INIT,
578 "registering %s as an ACPI driver\n", ibm->name);
580 BUG_ON(!ibm->acpi);
582 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
583 if (!ibm->acpi->driver) {
584 printk(TPACPI_ERR
585 "failed to allocate memory for ibm->acpi->driver\n");
586 return -ENOMEM;
589 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
590 ibm->acpi->driver->ids = ibm->acpi->hid;
592 ibm->acpi->driver->ops.add = &tpacpi_device_add;
594 rc = acpi_bus_register_driver(ibm->acpi->driver);
595 if (rc < 0) {
596 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
597 ibm->name, rc);
598 kfree(ibm->acpi->driver);
599 ibm->acpi->driver = NULL;
600 } else if (!rc)
601 ibm->flags.acpi_driver_registered = 1;
603 return rc;
607 /****************************************************************************
608 ****************************************************************************
610 * Procfs Helpers
612 ****************************************************************************
613 ****************************************************************************/
615 static int dispatch_procfs_read(char *page, char **start, off_t off,
616 int count, int *eof, void *data)
618 struct ibm_struct *ibm = data;
619 int len;
621 if (!ibm || !ibm->read)
622 return -EINVAL;
624 len = ibm->read(page);
625 if (len < 0)
626 return len;
628 if (len <= off + count)
629 *eof = 1;
630 *start = page + off;
631 len -= off;
632 if (len > count)
633 len = count;
634 if (len < 0)
635 len = 0;
637 return len;
640 static int dispatch_procfs_write(struct file *file,
641 const char __user *userbuf,
642 unsigned long count, void *data)
644 struct ibm_struct *ibm = data;
645 char *kernbuf;
646 int ret;
648 if (!ibm || !ibm->write)
649 return -EINVAL;
651 kernbuf = kmalloc(count + 2, GFP_KERNEL);
652 if (!kernbuf)
653 return -ENOMEM;
655 if (copy_from_user(kernbuf, userbuf, count)) {
656 kfree(kernbuf);
657 return -EFAULT;
660 kernbuf[count] = 0;
661 strcat(kernbuf, ",");
662 ret = ibm->write(kernbuf);
663 if (ret == 0)
664 ret = count;
666 kfree(kernbuf);
668 return ret;
671 static char *next_cmd(char **cmds)
673 char *start = *cmds;
674 char *end;
676 while ((end = strchr(start, ',')) && end == start)
677 start = end + 1;
679 if (!end)
680 return NULL;
682 *end = 0;
683 *cmds = end + 1;
684 return start;
688 /****************************************************************************
689 ****************************************************************************
691 * Device model: input, hwmon and platform
693 ****************************************************************************
694 ****************************************************************************/
696 static struct platform_device *tpacpi_pdev;
697 static struct platform_device *tpacpi_sensors_pdev;
698 static struct device *tpacpi_hwmon;
699 static struct input_dev *tpacpi_inputdev;
700 static struct mutex tpacpi_inputdev_send_mutex;
701 static LIST_HEAD(tpacpi_all_drivers);
703 static int tpacpi_suspend_handler(struct platform_device *pdev,
704 pm_message_t state)
706 struct ibm_struct *ibm, *itmp;
708 list_for_each_entry_safe(ibm, itmp,
709 &tpacpi_all_drivers,
710 all_drivers) {
711 if (ibm->suspend)
712 (ibm->suspend)(state);
715 return 0;
718 static int tpacpi_resume_handler(struct platform_device *pdev)
720 struct ibm_struct *ibm, *itmp;
722 list_for_each_entry_safe(ibm, itmp,
723 &tpacpi_all_drivers,
724 all_drivers) {
725 if (ibm->resume)
726 (ibm->resume)();
729 return 0;
732 static struct platform_driver tpacpi_pdriver = {
733 .driver = {
734 .name = TPACPI_DRVR_NAME,
735 .owner = THIS_MODULE,
737 .suspend = tpacpi_suspend_handler,
738 .resume = tpacpi_resume_handler,
741 static struct platform_driver tpacpi_hwmon_pdriver = {
742 .driver = {
743 .name = TPACPI_HWMON_DRVR_NAME,
744 .owner = THIS_MODULE,
748 /*************************************************************************
749 * sysfs support helpers
752 struct attribute_set {
753 unsigned int members, max_members;
754 struct attribute_group group;
757 struct attribute_set_obj {
758 struct attribute_set s;
759 struct attribute *a;
760 } __attribute__((packed));
762 static struct attribute_set *create_attr_set(unsigned int max_members,
763 const char *name)
765 struct attribute_set_obj *sobj;
767 if (max_members == 0)
768 return NULL;
770 /* Allocates space for implicit NULL at the end too */
771 sobj = kzalloc(sizeof(struct attribute_set_obj) +
772 max_members * sizeof(struct attribute *),
773 GFP_KERNEL);
774 if (!sobj)
775 return NULL;
776 sobj->s.max_members = max_members;
777 sobj->s.group.attrs = &sobj->a;
778 sobj->s.group.name = name;
780 return &sobj->s;
783 #define destroy_attr_set(_set) \
784 kfree(_set);
786 /* not multi-threaded safe, use it in a single thread per set */
787 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
789 if (!s || !attr)
790 return -EINVAL;
792 if (s->members >= s->max_members)
793 return -ENOMEM;
795 s->group.attrs[s->members] = attr;
796 s->members++;
798 return 0;
801 static int add_many_to_attr_set(struct attribute_set *s,
802 struct attribute **attr,
803 unsigned int count)
805 int i, res;
807 for (i = 0; i < count; i++) {
808 res = add_to_attr_set(s, attr[i]);
809 if (res)
810 return res;
813 return 0;
816 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
818 sysfs_remove_group(kobj, &s->group);
819 destroy_attr_set(s);
822 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
823 sysfs_create_group(_kobj, &_attr_set->group)
825 static int parse_strtoul(const char *buf,
826 unsigned long max, unsigned long *value)
828 char *endp;
830 while (*buf && isspace(*buf))
831 buf++;
832 *value = simple_strtoul(buf, &endp, 0);
833 while (*endp && isspace(*endp))
834 endp++;
835 if (*endp || *value > max)
836 return -EINVAL;
838 return 0;
841 static void tpacpi_disable_brightness_delay(void)
843 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
844 printk(TPACPI_NOTICE
845 "ACPI backlight control delay disabled\n");
848 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
850 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
851 union acpi_object *obj;
852 int rc;
854 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
855 obj = (union acpi_object *)buffer.pointer;
856 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
857 printk(TPACPI_ERR "Unknown _BCL data, "
858 "please report this to %s\n", TPACPI_MAIL);
859 rc = 0;
860 } else {
861 rc = obj->package.count;
863 } else {
864 return 0;
867 kfree(buffer.pointer);
868 return rc;
871 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
872 u32 lvl, void *context, void **rv)
874 char name[ACPI_PATH_SEGMENT_LENGTH];
875 struct acpi_buffer buffer = { sizeof(name), &name };
877 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
878 !strncmp("_BCL", name, sizeof(name) - 1)) {
879 BUG_ON(!rv || !*rv);
880 **(int **)rv = tpacpi_query_bcl_levels(handle);
881 return AE_CTRL_TERMINATE;
882 } else {
883 return AE_OK;
888 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
890 static int __init tpacpi_check_std_acpi_brightness_support(void)
892 int status;
893 int bcl_levels = 0;
894 void *bcl_ptr = &bcl_levels;
896 if (!vid_handle) {
897 TPACPI_ACPIHANDLE_INIT(vid);
899 if (!vid_handle)
900 return 0;
903 * Search for a _BCL method, and execute it. This is safe on all
904 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
905 * BIOS in ACPI backlight control mode. We do NOT have to care
906 * about calling the _BCL method in an enabled video device, any
907 * will do for our purposes.
910 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
911 tpacpi_acpi_walk_find_bcl, NULL,
912 &bcl_ptr);
914 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
915 tp_features.bright_acpimode = 1;
916 return (bcl_levels - 2);
919 return 0;
922 static int __init tpacpi_new_rfkill(const unsigned int id,
923 struct rfkill **rfk,
924 const enum rfkill_type rfktype,
925 const char *name,
926 int (*toggle_radio)(void *, enum rfkill_state),
927 int (*get_state)(void *, enum rfkill_state *))
929 int res;
930 enum rfkill_state initial_state;
932 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
933 if (!*rfk) {
934 printk(TPACPI_ERR
935 "failed to allocate memory for rfkill class\n");
936 return -ENOMEM;
939 (*rfk)->name = name;
940 (*rfk)->get_state = get_state;
941 (*rfk)->toggle_radio = toggle_radio;
943 if (!get_state(NULL, &initial_state))
944 (*rfk)->state = initial_state;
946 res = rfkill_register(*rfk);
947 if (res < 0) {
948 printk(TPACPI_ERR
949 "failed to register %s rfkill switch: %d\n",
950 name, res);
951 rfkill_free(*rfk);
952 *rfk = NULL;
953 return res;
956 return 0;
959 /*************************************************************************
960 * thinkpad-acpi driver attributes
963 /* interface_version --------------------------------------------------- */
964 static ssize_t tpacpi_driver_interface_version_show(
965 struct device_driver *drv,
966 char *buf)
968 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
971 static DRIVER_ATTR(interface_version, S_IRUGO,
972 tpacpi_driver_interface_version_show, NULL);
974 /* debug_level --------------------------------------------------------- */
975 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
976 char *buf)
978 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
981 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
982 const char *buf, size_t count)
984 unsigned long t;
986 if (parse_strtoul(buf, 0xffff, &t))
987 return -EINVAL;
989 dbg_level = t;
991 return count;
994 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
995 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
997 /* version ------------------------------------------------------------- */
998 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
999 char *buf)
1001 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1002 TPACPI_DESC, TPACPI_VERSION);
1005 static DRIVER_ATTR(version, S_IRUGO,
1006 tpacpi_driver_version_show, NULL);
1008 /* --------------------------------------------------------------------- */
1010 static struct driver_attribute *tpacpi_driver_attributes[] = {
1011 &driver_attr_debug_level, &driver_attr_version,
1012 &driver_attr_interface_version,
1015 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1017 int i, res;
1019 i = 0;
1020 res = 0;
1021 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1022 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1023 i++;
1026 return res;
1029 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1031 int i;
1033 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1034 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1037 /****************************************************************************
1038 ****************************************************************************
1040 * Subdrivers
1042 ****************************************************************************
1043 ****************************************************************************/
1045 /*************************************************************************
1046 * thinkpad-acpi init subdriver
1049 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1051 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1052 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1054 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1055 (thinkpad_id.bios_version_str) ?
1056 thinkpad_id.bios_version_str : "unknown",
1057 (thinkpad_id.ec_version_str) ?
1058 thinkpad_id.ec_version_str : "unknown");
1060 if (thinkpad_id.vendor && thinkpad_id.model_str)
1061 printk(TPACPI_INFO "%s %s, model %s\n",
1062 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1063 "IBM" : ((thinkpad_id.vendor ==
1064 PCI_VENDOR_ID_LENOVO) ?
1065 "Lenovo" : "Unknown vendor"),
1066 thinkpad_id.model_str,
1067 (thinkpad_id.nummodel_str) ?
1068 thinkpad_id.nummodel_str : "unknown");
1070 return 0;
1073 static int thinkpad_acpi_driver_read(char *p)
1075 int len = 0;
1077 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1078 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1080 return len;
1083 static struct ibm_struct thinkpad_acpi_driver_data = {
1084 .name = "driver",
1085 .read = thinkpad_acpi_driver_read,
1088 /*************************************************************************
1089 * Hotkey subdriver
1092 enum { /* hot key scan codes (derived from ACPI DSDT) */
1093 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1094 TP_ACPI_HOTKEYSCAN_FNF2,
1095 TP_ACPI_HOTKEYSCAN_FNF3,
1096 TP_ACPI_HOTKEYSCAN_FNF4,
1097 TP_ACPI_HOTKEYSCAN_FNF5,
1098 TP_ACPI_HOTKEYSCAN_FNF6,
1099 TP_ACPI_HOTKEYSCAN_FNF7,
1100 TP_ACPI_HOTKEYSCAN_FNF8,
1101 TP_ACPI_HOTKEYSCAN_FNF9,
1102 TP_ACPI_HOTKEYSCAN_FNF10,
1103 TP_ACPI_HOTKEYSCAN_FNF11,
1104 TP_ACPI_HOTKEYSCAN_FNF12,
1105 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1106 TP_ACPI_HOTKEYSCAN_FNINSERT,
1107 TP_ACPI_HOTKEYSCAN_FNDELETE,
1108 TP_ACPI_HOTKEYSCAN_FNHOME,
1109 TP_ACPI_HOTKEYSCAN_FNEND,
1110 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1111 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1112 TP_ACPI_HOTKEYSCAN_FNSPACE,
1113 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1114 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1115 TP_ACPI_HOTKEYSCAN_MUTE,
1116 TP_ACPI_HOTKEYSCAN_THINKPAD,
1119 enum { /* Keys available through NVRAM polling */
1120 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1121 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1124 enum { /* Positions of some of the keys in hotkey masks */
1125 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1126 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1127 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1128 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1129 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1130 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1131 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1132 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1133 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1134 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1135 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1138 enum { /* NVRAM to ACPI HKEY group map */
1139 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1140 TP_ACPI_HKEY_ZOOM_MASK |
1141 TP_ACPI_HKEY_DISPSWTCH_MASK |
1142 TP_ACPI_HKEY_HIBERNATE_MASK,
1143 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1144 TP_ACPI_HKEY_BRGHTDWN_MASK,
1145 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1146 TP_ACPI_HKEY_VOLDWN_MASK |
1147 TP_ACPI_HKEY_MUTE_MASK,
1150 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1151 struct tp_nvram_state {
1152 u16 thinkpad_toggle:1;
1153 u16 zoom_toggle:1;
1154 u16 display_toggle:1;
1155 u16 thinklight_toggle:1;
1156 u16 hibernate_toggle:1;
1157 u16 displayexp_toggle:1;
1158 u16 display_state:1;
1159 u16 brightness_toggle:1;
1160 u16 volume_toggle:1;
1161 u16 mute:1;
1163 u8 brightness_level;
1164 u8 volume_level;
1167 static struct task_struct *tpacpi_hotkey_task;
1168 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1169 static int hotkey_poll_freq = 10; /* Hz */
1170 static struct mutex hotkey_thread_mutex;
1171 static struct mutex hotkey_thread_data_mutex;
1172 static unsigned int hotkey_config_change;
1174 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1176 #define hotkey_source_mask 0U
1178 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1180 static struct mutex hotkey_mutex;
1182 static enum { /* Reasons for waking up */
1183 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1184 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1185 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1186 } hotkey_wakeup_reason;
1188 static int hotkey_autosleep_ack;
1190 static int hotkey_orig_status;
1191 static u32 hotkey_orig_mask;
1192 static u32 hotkey_all_mask;
1193 static u32 hotkey_reserved_mask;
1194 static u32 hotkey_mask;
1196 static unsigned int hotkey_report_mode;
1198 static u16 *hotkey_keycode_map;
1200 static struct attribute_set *hotkey_dev_attributes;
1202 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1203 #define HOTKEY_CONFIG_CRITICAL_START \
1204 do { \
1205 mutex_lock(&hotkey_thread_data_mutex); \
1206 hotkey_config_change++; \
1207 } while (0);
1208 #define HOTKEY_CONFIG_CRITICAL_END \
1209 mutex_unlock(&hotkey_thread_data_mutex);
1210 #else
1211 #define HOTKEY_CONFIG_CRITICAL_START
1212 #define HOTKEY_CONFIG_CRITICAL_END
1213 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1215 /* HKEY.MHKG() return bits */
1216 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1218 static int hotkey_get_wlsw(int *status)
1220 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1221 return -EIO;
1222 return 0;
1225 static int hotkey_get_tablet_mode(int *status)
1227 int s;
1229 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1230 return -EIO;
1232 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1233 return 0;
1237 * Call with hotkey_mutex held
1239 static int hotkey_mask_get(void)
1241 u32 m = 0;
1243 if (tp_features.hotkey_mask) {
1244 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1245 return -EIO;
1247 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1249 return 0;
1253 * Call with hotkey_mutex held
1255 static int hotkey_mask_set(u32 mask)
1257 int i;
1258 int rc = 0;
1260 if (tp_features.hotkey_mask) {
1261 if (!tp_warned.hotkey_mask_ff &&
1262 (mask == 0xffff || mask == 0xffffff ||
1263 mask == 0xffffffff)) {
1264 tp_warned.hotkey_mask_ff = 1;
1265 printk(TPACPI_NOTICE
1266 "setting the hotkey mask to 0x%08x is likely "
1267 "not the best way to go about it\n", mask);
1268 printk(TPACPI_NOTICE
1269 "please consider using the driver defaults, "
1270 "and refer to up-to-date thinkpad-acpi "
1271 "documentation\n");
1274 HOTKEY_CONFIG_CRITICAL_START
1275 for (i = 0; i < 32; i++) {
1276 u32 m = 1 << i;
1277 /* enable in firmware mask only keys not in NVRAM
1278 * mode, but enable the key in the cached hotkey_mask
1279 * regardless of mode, or the key will end up
1280 * disabled by hotkey_mask_get() */
1281 if (!acpi_evalf(hkey_handle,
1282 NULL, "MHKM", "vdd", i + 1,
1283 !!((mask & ~hotkey_source_mask) & m))) {
1284 rc = -EIO;
1285 break;
1286 } else {
1287 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1290 HOTKEY_CONFIG_CRITICAL_END
1292 /* hotkey_mask_get must be called unconditionally below */
1293 if (!hotkey_mask_get() && !rc &&
1294 (hotkey_mask & ~hotkey_source_mask) !=
1295 (mask & ~hotkey_source_mask)) {
1296 printk(TPACPI_NOTICE
1297 "requested hot key mask 0x%08x, but "
1298 "firmware forced it to 0x%08x\n",
1299 mask, hotkey_mask);
1301 } else {
1302 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1303 HOTKEY_CONFIG_CRITICAL_START
1304 hotkey_mask = mask & hotkey_source_mask;
1305 HOTKEY_CONFIG_CRITICAL_END
1306 hotkey_mask_get();
1307 if (hotkey_mask != mask) {
1308 printk(TPACPI_NOTICE
1309 "requested hot key mask 0x%08x, "
1310 "forced to 0x%08x (NVRAM poll mask is "
1311 "0x%08x): no firmware mask support\n",
1312 mask, hotkey_mask, hotkey_source_mask);
1314 #else
1315 hotkey_mask_get();
1316 rc = -ENXIO;
1317 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1320 return rc;
1323 static int hotkey_status_get(int *status)
1325 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1326 return -EIO;
1328 return 0;
1331 static int hotkey_status_set(int status)
1333 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1334 return -EIO;
1336 return 0;
1339 static void tpacpi_input_send_tabletsw(void)
1341 int state;
1343 if (tp_features.hotkey_tablet &&
1344 !hotkey_get_tablet_mode(&state)) {
1345 mutex_lock(&tpacpi_inputdev_send_mutex);
1347 input_report_switch(tpacpi_inputdev,
1348 SW_TABLET_MODE, !!state);
1349 input_sync(tpacpi_inputdev);
1351 mutex_unlock(&tpacpi_inputdev_send_mutex);
1355 static void tpacpi_input_send_key(unsigned int scancode)
1357 unsigned int keycode;
1359 keycode = hotkey_keycode_map[scancode];
1361 if (keycode != KEY_RESERVED) {
1362 mutex_lock(&tpacpi_inputdev_send_mutex);
1364 input_report_key(tpacpi_inputdev, keycode, 1);
1365 if (keycode == KEY_UNKNOWN)
1366 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1367 scancode);
1368 input_sync(tpacpi_inputdev);
1370 input_report_key(tpacpi_inputdev, keycode, 0);
1371 if (keycode == KEY_UNKNOWN)
1372 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1373 scancode);
1374 input_sync(tpacpi_inputdev);
1376 mutex_unlock(&tpacpi_inputdev_send_mutex);
1380 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1381 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1383 static void tpacpi_hotkey_send_key(unsigned int scancode)
1385 tpacpi_input_send_key(scancode);
1386 if (hotkey_report_mode < 2) {
1387 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1388 0x80, 0x1001 + scancode);
1392 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1394 u8 d;
1396 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1397 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1398 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1399 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1400 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1401 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1403 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1404 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1405 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1407 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1408 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1409 n->displayexp_toggle =
1410 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1412 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1413 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1414 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1415 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1416 n->brightness_toggle =
1417 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1419 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1420 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1421 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1422 >> TP_NVRAM_POS_LEVEL_VOLUME;
1423 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1424 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1428 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1429 do { \
1430 if ((mask & (1 << __scancode)) && \
1431 oldn->__member != newn->__member) \
1432 tpacpi_hotkey_send_key(__scancode); \
1433 } while (0)
1435 #define TPACPI_MAY_SEND_KEY(__scancode) \
1436 do { if (mask & (1 << __scancode)) \
1437 tpacpi_hotkey_send_key(__scancode); } while (0)
1439 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1440 struct tp_nvram_state *newn,
1441 u32 mask)
1443 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1444 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1445 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1446 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1448 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1450 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1452 /* handle volume */
1453 if (oldn->volume_toggle != newn->volume_toggle) {
1454 if (oldn->mute != newn->mute) {
1455 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1457 if (oldn->volume_level > newn->volume_level) {
1458 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1459 } else if (oldn->volume_level < newn->volume_level) {
1460 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1461 } else if (oldn->mute == newn->mute) {
1462 /* repeated key presses that didn't change state */
1463 if (newn->mute) {
1464 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1465 } else if (newn->volume_level != 0) {
1466 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1467 } else {
1468 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1473 /* handle brightness */
1474 if (oldn->brightness_toggle != newn->brightness_toggle) {
1475 if (oldn->brightness_level < newn->brightness_level) {
1476 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1477 } else if (oldn->brightness_level > newn->brightness_level) {
1478 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1479 } else {
1480 /* repeated key presses that didn't change state */
1481 if (newn->brightness_level != 0) {
1482 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1483 } else {
1484 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1490 #undef TPACPI_COMPARE_KEY
1491 #undef TPACPI_MAY_SEND_KEY
1493 static int hotkey_kthread(void *data)
1495 struct tp_nvram_state s[2];
1496 u32 mask;
1497 unsigned int si, so;
1498 unsigned long t;
1499 unsigned int change_detector, must_reset;
1501 mutex_lock(&hotkey_thread_mutex);
1503 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1504 goto exit;
1506 set_freezable();
1508 so = 0;
1509 si = 1;
1510 t = 0;
1512 /* Initial state for compares */
1513 mutex_lock(&hotkey_thread_data_mutex);
1514 change_detector = hotkey_config_change;
1515 mask = hotkey_source_mask & hotkey_mask;
1516 mutex_unlock(&hotkey_thread_data_mutex);
1517 hotkey_read_nvram(&s[so], mask);
1519 while (!kthread_should_stop() && hotkey_poll_freq) {
1520 if (t == 0)
1521 t = 1000/hotkey_poll_freq;
1522 t = msleep_interruptible(t);
1523 if (unlikely(kthread_should_stop()))
1524 break;
1525 must_reset = try_to_freeze();
1526 if (t > 0 && !must_reset)
1527 continue;
1529 mutex_lock(&hotkey_thread_data_mutex);
1530 if (must_reset || hotkey_config_change != change_detector) {
1531 /* forget old state on thaw or config change */
1532 si = so;
1533 t = 0;
1534 change_detector = hotkey_config_change;
1536 mask = hotkey_source_mask & hotkey_mask;
1537 mutex_unlock(&hotkey_thread_data_mutex);
1539 if (likely(mask)) {
1540 hotkey_read_nvram(&s[si], mask);
1541 if (likely(si != so)) {
1542 hotkey_compare_and_issue_event(&s[so], &s[si],
1543 mask);
1547 so = si;
1548 si ^= 1;
1551 exit:
1552 mutex_unlock(&hotkey_thread_mutex);
1553 return 0;
1556 static void hotkey_poll_stop_sync(void)
1558 if (tpacpi_hotkey_task) {
1559 if (frozen(tpacpi_hotkey_task) ||
1560 freezing(tpacpi_hotkey_task))
1561 thaw_process(tpacpi_hotkey_task);
1563 kthread_stop(tpacpi_hotkey_task);
1564 tpacpi_hotkey_task = NULL;
1565 mutex_lock(&hotkey_thread_mutex);
1566 /* at this point, the thread did exit */
1567 mutex_unlock(&hotkey_thread_mutex);
1571 /* call with hotkey_mutex held */
1572 static void hotkey_poll_setup(int may_warn)
1574 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1575 hotkey_poll_freq > 0 &&
1576 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1577 if (!tpacpi_hotkey_task) {
1578 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1579 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1580 if (IS_ERR(tpacpi_hotkey_task)) {
1581 tpacpi_hotkey_task = NULL;
1582 printk(TPACPI_ERR
1583 "could not create kernel thread "
1584 "for hotkey polling\n");
1587 } else {
1588 hotkey_poll_stop_sync();
1589 if (may_warn &&
1590 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1591 printk(TPACPI_NOTICE
1592 "hot keys 0x%08x require polling, "
1593 "which is currently disabled\n",
1594 hotkey_source_mask);
1599 static void hotkey_poll_setup_safe(int may_warn)
1601 mutex_lock(&hotkey_mutex);
1602 hotkey_poll_setup(may_warn);
1603 mutex_unlock(&hotkey_mutex);
1606 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1608 static void hotkey_poll_setup_safe(int __unused)
1612 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1614 static int hotkey_inputdev_open(struct input_dev *dev)
1616 switch (tpacpi_lifecycle) {
1617 case TPACPI_LIFE_INIT:
1619 * hotkey_init will call hotkey_poll_setup_safe
1620 * at the appropriate moment
1622 return 0;
1623 case TPACPI_LIFE_EXITING:
1624 return -EBUSY;
1625 case TPACPI_LIFE_RUNNING:
1626 hotkey_poll_setup_safe(0);
1627 return 0;
1630 /* Should only happen if tpacpi_lifecycle is corrupt */
1631 BUG();
1632 return -EBUSY;
1635 static void hotkey_inputdev_close(struct input_dev *dev)
1637 /* disable hotkey polling when possible */
1638 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1639 hotkey_poll_setup_safe(0);
1642 /* sysfs hotkey enable ------------------------------------------------- */
1643 static ssize_t hotkey_enable_show(struct device *dev,
1644 struct device_attribute *attr,
1645 char *buf)
1647 int res, status;
1649 res = hotkey_status_get(&status);
1650 if (res)
1651 return res;
1653 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1656 static ssize_t hotkey_enable_store(struct device *dev,
1657 struct device_attribute *attr,
1658 const char *buf, size_t count)
1660 unsigned long t;
1661 int res;
1663 if (parse_strtoul(buf, 1, &t))
1664 return -EINVAL;
1666 res = hotkey_status_set(t);
1668 return (res) ? res : count;
1671 static struct device_attribute dev_attr_hotkey_enable =
1672 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1673 hotkey_enable_show, hotkey_enable_store);
1675 /* sysfs hotkey mask --------------------------------------------------- */
1676 static ssize_t hotkey_mask_show(struct device *dev,
1677 struct device_attribute *attr,
1678 char *buf)
1680 int res;
1682 if (mutex_lock_interruptible(&hotkey_mutex))
1683 return -ERESTARTSYS;
1684 res = hotkey_mask_get();
1685 mutex_unlock(&hotkey_mutex);
1687 return (res)?
1688 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1691 static ssize_t hotkey_mask_store(struct device *dev,
1692 struct device_attribute *attr,
1693 const char *buf, size_t count)
1695 unsigned long t;
1696 int res;
1698 if (parse_strtoul(buf, 0xffffffffUL, &t))
1699 return -EINVAL;
1701 if (mutex_lock_interruptible(&hotkey_mutex))
1702 return -ERESTARTSYS;
1704 res = hotkey_mask_set(t);
1706 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1707 hotkey_poll_setup(1);
1708 #endif
1710 mutex_unlock(&hotkey_mutex);
1712 return (res) ? res : count;
1715 static struct device_attribute dev_attr_hotkey_mask =
1716 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1717 hotkey_mask_show, hotkey_mask_store);
1719 /* sysfs hotkey bios_enabled ------------------------------------------- */
1720 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1721 struct device_attribute *attr,
1722 char *buf)
1724 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1727 static struct device_attribute dev_attr_hotkey_bios_enabled =
1728 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1730 /* sysfs hotkey bios_mask ---------------------------------------------- */
1731 static ssize_t hotkey_bios_mask_show(struct device *dev,
1732 struct device_attribute *attr,
1733 char *buf)
1735 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1738 static struct device_attribute dev_attr_hotkey_bios_mask =
1739 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1741 /* sysfs hotkey all_mask ----------------------------------------------- */
1742 static ssize_t hotkey_all_mask_show(struct device *dev,
1743 struct device_attribute *attr,
1744 char *buf)
1746 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1747 hotkey_all_mask | hotkey_source_mask);
1750 static struct device_attribute dev_attr_hotkey_all_mask =
1751 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1753 /* sysfs hotkey recommended_mask --------------------------------------- */
1754 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1755 struct device_attribute *attr,
1756 char *buf)
1758 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1759 (hotkey_all_mask | hotkey_source_mask)
1760 & ~hotkey_reserved_mask);
1763 static struct device_attribute dev_attr_hotkey_recommended_mask =
1764 __ATTR(hotkey_recommended_mask, S_IRUGO,
1765 hotkey_recommended_mask_show, NULL);
1767 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1769 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1770 static ssize_t hotkey_source_mask_show(struct device *dev,
1771 struct device_attribute *attr,
1772 char *buf)
1774 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1777 static ssize_t hotkey_source_mask_store(struct device *dev,
1778 struct device_attribute *attr,
1779 const char *buf, size_t count)
1781 unsigned long t;
1783 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1784 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1785 return -EINVAL;
1787 if (mutex_lock_interruptible(&hotkey_mutex))
1788 return -ERESTARTSYS;
1790 HOTKEY_CONFIG_CRITICAL_START
1791 hotkey_source_mask = t;
1792 HOTKEY_CONFIG_CRITICAL_END
1794 hotkey_poll_setup(1);
1796 mutex_unlock(&hotkey_mutex);
1798 return count;
1801 static struct device_attribute dev_attr_hotkey_source_mask =
1802 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1803 hotkey_source_mask_show, hotkey_source_mask_store);
1805 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1806 static ssize_t hotkey_poll_freq_show(struct device *dev,
1807 struct device_attribute *attr,
1808 char *buf)
1810 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1813 static ssize_t hotkey_poll_freq_store(struct device *dev,
1814 struct device_attribute *attr,
1815 const char *buf, size_t count)
1817 unsigned long t;
1819 if (parse_strtoul(buf, 25, &t))
1820 return -EINVAL;
1822 if (mutex_lock_interruptible(&hotkey_mutex))
1823 return -ERESTARTSYS;
1825 hotkey_poll_freq = t;
1827 hotkey_poll_setup(1);
1828 mutex_unlock(&hotkey_mutex);
1830 return count;
1833 static struct device_attribute dev_attr_hotkey_poll_freq =
1834 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1835 hotkey_poll_freq_show, hotkey_poll_freq_store);
1837 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1839 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
1840 static ssize_t hotkey_radio_sw_show(struct device *dev,
1841 struct device_attribute *attr,
1842 char *buf)
1844 int res, s;
1845 res = hotkey_get_wlsw(&s);
1846 if (res < 0)
1847 return res;
1849 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1852 static struct device_attribute dev_attr_hotkey_radio_sw =
1853 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1855 static void hotkey_radio_sw_notify_change(void)
1857 if (tp_features.hotkey_wlsw)
1858 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1859 "hotkey_radio_sw");
1862 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
1863 static ssize_t hotkey_tablet_mode_show(struct device *dev,
1864 struct device_attribute *attr,
1865 char *buf)
1867 int res, s;
1868 res = hotkey_get_tablet_mode(&s);
1869 if (res < 0)
1870 return res;
1872 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1875 static struct device_attribute dev_attr_hotkey_tablet_mode =
1876 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1878 static void hotkey_tablet_mode_notify_change(void)
1880 if (tp_features.hotkey_tablet)
1881 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1882 "hotkey_tablet_mode");
1885 /* sysfs hotkey report_mode -------------------------------------------- */
1886 static ssize_t hotkey_report_mode_show(struct device *dev,
1887 struct device_attribute *attr,
1888 char *buf)
1890 return snprintf(buf, PAGE_SIZE, "%d\n",
1891 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1894 static struct device_attribute dev_attr_hotkey_report_mode =
1895 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1897 /* sysfs wakeup reason (pollable) -------------------------------------- */
1898 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1899 struct device_attribute *attr,
1900 char *buf)
1902 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1905 static struct device_attribute dev_attr_hotkey_wakeup_reason =
1906 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1908 static void hotkey_wakeup_reason_notify_change(void)
1910 if (tp_features.hotkey_mask)
1911 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1912 "wakeup_reason");
1915 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
1916 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1917 struct device_attribute *attr,
1918 char *buf)
1920 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1923 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1924 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1925 hotkey_wakeup_hotunplug_complete_show, NULL);
1927 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
1929 if (tp_features.hotkey_mask)
1930 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1931 "wakeup_hotunplug_complete");
1934 /* --------------------------------------------------------------------- */
1936 static struct attribute *hotkey_attributes[] __initdata = {
1937 &dev_attr_hotkey_enable.attr,
1938 &dev_attr_hotkey_bios_enabled.attr,
1939 &dev_attr_hotkey_report_mode.attr,
1940 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1941 &dev_attr_hotkey_mask.attr,
1942 &dev_attr_hotkey_all_mask.attr,
1943 &dev_attr_hotkey_recommended_mask.attr,
1944 &dev_attr_hotkey_source_mask.attr,
1945 &dev_attr_hotkey_poll_freq.attr,
1946 #endif
1949 static struct attribute *hotkey_mask_attributes[] __initdata = {
1950 &dev_attr_hotkey_bios_mask.attr,
1951 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1952 &dev_attr_hotkey_mask.attr,
1953 &dev_attr_hotkey_all_mask.attr,
1954 &dev_attr_hotkey_recommended_mask.attr,
1955 #endif
1956 &dev_attr_hotkey_wakeup_reason.attr,
1957 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
1960 static void bluetooth_update_rfk(void);
1961 static void wan_update_rfk(void);
1962 static void tpacpi_send_radiosw_update(void)
1964 int wlsw;
1966 /* Sync these BEFORE sending any rfkill events */
1967 if (tp_features.bluetooth)
1968 bluetooth_update_rfk();
1969 if (tp_features.wan)
1970 wan_update_rfk();
1972 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
1973 mutex_lock(&tpacpi_inputdev_send_mutex);
1975 input_report_switch(tpacpi_inputdev,
1976 SW_RFKILL_ALL, !!wlsw);
1977 input_sync(tpacpi_inputdev);
1979 mutex_unlock(&tpacpi_inputdev_send_mutex);
1981 hotkey_radio_sw_notify_change();
1984 static void hotkey_exit(void)
1986 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1987 hotkey_poll_stop_sync();
1988 #endif
1990 if (hotkey_dev_attributes)
1991 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
1993 kfree(hotkey_keycode_map);
1995 if (tp_features.hotkey) {
1996 dbg_printk(TPACPI_DBG_EXIT,
1997 "restoring original hot key mask\n");
1998 /* no short-circuit boolean operator below! */
1999 if ((hotkey_mask_set(hotkey_orig_mask) |
2000 hotkey_status_set(hotkey_orig_status)) != 0)
2001 printk(TPACPI_ERR
2002 "failed to restore hot key mask "
2003 "to BIOS defaults\n");
2007 static int __init hotkey_init(struct ibm_init_struct *iibm)
2009 /* Requirements for changing the default keymaps:
2011 * 1. Many of the keys are mapped to KEY_RESERVED for very
2012 * good reasons. Do not change them unless you have deep
2013 * knowledge on the IBM and Lenovo ThinkPad firmware for
2014 * the various ThinkPad models. The driver behaves
2015 * differently for KEY_RESERVED: such keys have their
2016 * hot key mask *unset* in mask_recommended, and also
2017 * in the initial hot key mask programmed into the
2018 * firmware at driver load time, which means the firm-
2019 * ware may react very differently if you change them to
2020 * something else;
2022 * 2. You must be subscribed to the linux-thinkpad and
2023 * ibm-acpi-devel mailing lists, and you should read the
2024 * list archives since 2007 if you want to change the
2025 * keymaps. This requirement exists so that you will
2026 * know the past history of problems with the thinkpad-
2027 * acpi driver keymaps, and also that you will be
2028 * listening to any bug reports;
2030 * 3. Do not send thinkpad-acpi specific patches directly to
2031 * for merging, *ever*. Send them to the linux-acpi
2032 * mailinglist for comments. Merging is to be done only
2033 * through acpi-test and the ACPI maintainer.
2035 * If the above is too much to ask, don't change the keymap.
2036 * Ask the thinkpad-acpi maintainer to do it, instead.
2038 static u16 ibm_keycode_map[] __initdata = {
2039 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2040 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2041 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2042 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2044 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2045 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2046 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2047 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2049 /* brightness: firmware always reacts to them, unless
2050 * X.org did some tricks in the radeon BIOS scratch
2051 * registers of *some* models */
2052 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2053 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2055 /* Thinklight: firmware always react to it */
2056 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2058 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2059 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2061 /* Volume: firmware always react to it and reprograms
2062 * the built-in *extra* mixer. Never map it to control
2063 * another mixer by default. */
2064 KEY_RESERVED, /* 0x14: VOLUME UP */
2065 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2066 KEY_RESERVED, /* 0x16: MUTE */
2068 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2070 /* (assignments unknown, please report if found) */
2071 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2072 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2074 static u16 lenovo_keycode_map[] __initdata = {
2075 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2076 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2077 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2078 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2080 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2081 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2082 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2083 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2085 /* These either have to go through ACPI video, or
2086 * act like in the IBM ThinkPads, so don't ever
2087 * enable them by default */
2088 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2089 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2091 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2093 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2094 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2096 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2097 * react to it and reprograms the built-in *extra* mixer.
2098 * Never map it to control another mixer by default.
2100 * T60?, T61, R60?, R61: firmware and EC tries to send
2101 * these over the regular keyboard, so these are no-ops,
2102 * but there are still weird bugs re. MUTE, so do not
2103 * change unless you get test reports from all Lenovo
2104 * models. May cause the BIOS to interfere with the
2105 * HDA mixer.
2107 KEY_RESERVED, /* 0x14: VOLUME UP */
2108 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2109 KEY_RESERVED, /* 0x16: MUTE */
2111 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2113 /* (assignments unknown, please report if found) */
2114 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2115 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2118 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2119 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2120 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2122 int res, i;
2123 int status;
2124 int hkeyv;
2126 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2128 BUG_ON(!tpacpi_inputdev);
2129 BUG_ON(tpacpi_inputdev->open != NULL ||
2130 tpacpi_inputdev->close != NULL);
2132 TPACPI_ACPIHANDLE_INIT(hkey);
2133 mutex_init(&hotkey_mutex);
2135 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2136 mutex_init(&hotkey_thread_mutex);
2137 mutex_init(&hotkey_thread_data_mutex);
2138 #endif
2140 /* hotkey not supported on 570 */
2141 tp_features.hotkey = hkey_handle != NULL;
2143 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2144 str_supported(tp_features.hotkey));
2146 if (!tp_features.hotkey)
2147 return 1;
2149 tpacpi_disable_brightness_delay();
2151 hotkey_dev_attributes = create_attr_set(13, NULL);
2152 if (!hotkey_dev_attributes)
2153 return -ENOMEM;
2154 res = add_many_to_attr_set(hotkey_dev_attributes,
2155 hotkey_attributes,
2156 ARRAY_SIZE(hotkey_attributes));
2157 if (res)
2158 goto err_exit;
2160 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2161 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2162 for HKEY interface version 0x100 */
2163 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2164 if ((hkeyv >> 8) != 1) {
2165 printk(TPACPI_ERR "unknown version of the "
2166 "HKEY interface: 0x%x\n", hkeyv);
2167 printk(TPACPI_ERR "please report this to %s\n",
2168 TPACPI_MAIL);
2169 } else {
2171 * MHKV 0x100 in A31, R40, R40e,
2172 * T4x, X31, and later
2174 tp_features.hotkey_mask = 1;
2178 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2179 str_supported(tp_features.hotkey_mask));
2181 if (tp_features.hotkey_mask) {
2182 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2183 "MHKA", "qd")) {
2184 printk(TPACPI_ERR
2185 "missing MHKA handler, "
2186 "please report this to %s\n",
2187 TPACPI_MAIL);
2188 /* FN+F12, FN+F4, FN+F3 */
2189 hotkey_all_mask = 0x080cU;
2193 /* hotkey_source_mask *must* be zero for
2194 * the first hotkey_mask_get */
2195 res = hotkey_status_get(&hotkey_orig_status);
2196 if (res)
2197 goto err_exit;
2199 if (tp_features.hotkey_mask) {
2200 res = hotkey_mask_get();
2201 if (res)
2202 goto err_exit;
2204 hotkey_orig_mask = hotkey_mask;
2205 res = add_many_to_attr_set(
2206 hotkey_dev_attributes,
2207 hotkey_mask_attributes,
2208 ARRAY_SIZE(hotkey_mask_attributes));
2209 if (res)
2210 goto err_exit;
2213 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2214 if (tp_features.hotkey_mask) {
2215 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2216 & ~hotkey_all_mask;
2217 } else {
2218 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2221 vdbg_printk(TPACPI_DBG_INIT,
2222 "hotkey source mask 0x%08x, polling freq %d\n",
2223 hotkey_source_mask, hotkey_poll_freq);
2224 #endif
2226 /* Not all thinkpads have a hardware radio switch */
2227 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2228 tp_features.hotkey_wlsw = 1;
2229 printk(TPACPI_INFO
2230 "radio switch found; radios are %s\n",
2231 enabled(status, 0));
2233 if (tp_features.hotkey_wlsw)
2234 res = add_to_attr_set(hotkey_dev_attributes,
2235 &dev_attr_hotkey_radio_sw.attr);
2237 /* For X41t, X60t, X61t Tablets... */
2238 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2239 tp_features.hotkey_tablet = 1;
2240 printk(TPACPI_INFO
2241 "possible tablet mode switch found; "
2242 "ThinkPad in %s mode\n",
2243 (status & TP_HOTKEY_TABLET_MASK)?
2244 "tablet" : "laptop");
2245 res = add_to_attr_set(hotkey_dev_attributes,
2246 &dev_attr_hotkey_tablet_mode.attr);
2249 if (!res)
2250 res = register_attr_set_with_sysfs(
2251 hotkey_dev_attributes,
2252 &tpacpi_pdev->dev.kobj);
2253 if (res)
2254 goto err_exit;
2256 /* Set up key map */
2258 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2259 GFP_KERNEL);
2260 if (!hotkey_keycode_map) {
2261 printk(TPACPI_ERR
2262 "failed to allocate memory for key map\n");
2263 res = -ENOMEM;
2264 goto err_exit;
2267 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2268 dbg_printk(TPACPI_DBG_INIT,
2269 "using Lenovo default hot key map\n");
2270 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2271 TPACPI_HOTKEY_MAP_SIZE);
2272 } else {
2273 dbg_printk(TPACPI_DBG_INIT,
2274 "using IBM default hot key map\n");
2275 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2276 TPACPI_HOTKEY_MAP_SIZE);
2279 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2280 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2281 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2282 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2283 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2284 tpacpi_inputdev->keycode = hotkey_keycode_map;
2285 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2286 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2287 set_bit(hotkey_keycode_map[i],
2288 tpacpi_inputdev->keybit);
2289 } else {
2290 if (i < sizeof(hotkey_reserved_mask)*8)
2291 hotkey_reserved_mask |= 1 << i;
2295 if (tp_features.hotkey_wlsw) {
2296 set_bit(EV_SW, tpacpi_inputdev->evbit);
2297 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2299 if (tp_features.hotkey_tablet) {
2300 set_bit(EV_SW, tpacpi_inputdev->evbit);
2301 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2304 /* Do not issue duplicate brightness change events to
2305 * userspace */
2306 if (!tp_features.bright_acpimode)
2307 /* update bright_acpimode... */
2308 tpacpi_check_std_acpi_brightness_support();
2310 if (tp_features.bright_acpimode) {
2311 printk(TPACPI_INFO
2312 "This ThinkPad has standard ACPI backlight "
2313 "brightness control, supported by the ACPI "
2314 "video driver\n");
2315 printk(TPACPI_NOTICE
2316 "Disabling thinkpad-acpi brightness events "
2317 "by default...\n");
2319 /* The hotkey_reserved_mask change below is not
2320 * necessary while the keys are at KEY_RESERVED in the
2321 * default map, but better safe than sorry, leave it
2322 * here as a marker of what we have to do, especially
2323 * when we finally become able to set this at runtime
2324 * on response to X.org requests */
2325 hotkey_reserved_mask |=
2326 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2327 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2330 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2331 res = hotkey_status_set(1);
2332 if (res) {
2333 hotkey_exit();
2334 return res;
2336 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2337 & ~hotkey_reserved_mask)
2338 | hotkey_orig_mask);
2339 if (res < 0 && res != -ENXIO) {
2340 hotkey_exit();
2341 return res;
2344 dbg_printk(TPACPI_DBG_INIT,
2345 "legacy hot key reporting over procfs %s\n",
2346 (hotkey_report_mode < 2) ?
2347 "enabled" : "disabled");
2349 tpacpi_inputdev->open = &hotkey_inputdev_open;
2350 tpacpi_inputdev->close = &hotkey_inputdev_close;
2352 hotkey_poll_setup_safe(1);
2353 tpacpi_send_radiosw_update();
2354 tpacpi_input_send_tabletsw();
2356 return 0;
2358 err_exit:
2359 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2360 hotkey_dev_attributes = NULL;
2362 return (res < 0)? res : 1;
2365 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2367 u32 hkey;
2368 unsigned int scancode;
2369 int send_acpi_ev;
2370 int ignore_acpi_ev;
2371 int unk_ev;
2373 if (event != 0x80) {
2374 printk(TPACPI_ERR
2375 "unknown HKEY notification event %d\n", event);
2376 /* forward it to userspace, maybe it knows how to handle it */
2377 acpi_bus_generate_netlink_event(
2378 ibm->acpi->device->pnp.device_class,
2379 ibm->acpi->device->dev.bus_id,
2380 event, 0);
2381 return;
2384 while (1) {
2385 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2386 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2387 return;
2390 if (hkey == 0) {
2391 /* queue empty */
2392 return;
2395 send_acpi_ev = 1;
2396 ignore_acpi_ev = 0;
2397 unk_ev = 0;
2399 switch (hkey >> 12) {
2400 case 1:
2401 /* 0x1000-0x1FFF: key presses */
2402 scancode = hkey & 0xfff;
2403 if (scancode > 0 && scancode < 0x21) {
2404 scancode--;
2405 if (!(hotkey_source_mask & (1 << scancode))) {
2406 tpacpi_input_send_key(scancode);
2407 send_acpi_ev = 0;
2408 } else {
2409 ignore_acpi_ev = 1;
2411 } else {
2412 unk_ev = 1;
2414 break;
2415 case 2:
2416 /* Wakeup reason */
2417 switch (hkey) {
2418 case 0x2304: /* suspend, undock */
2419 case 0x2404: /* hibernation, undock */
2420 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2421 ignore_acpi_ev = 1;
2422 break;
2423 case 0x2305: /* suspend, bay eject */
2424 case 0x2405: /* hibernation, bay eject */
2425 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2426 ignore_acpi_ev = 1;
2427 break;
2428 default:
2429 unk_ev = 1;
2431 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2432 printk(TPACPI_INFO
2433 "woke up due to a hot-unplug "
2434 "request...\n");
2435 hotkey_wakeup_reason_notify_change();
2437 break;
2438 case 3:
2439 /* bay-related wakeups */
2440 if (hkey == 0x3003) {
2441 hotkey_autosleep_ack = 1;
2442 printk(TPACPI_INFO
2443 "bay ejected\n");
2444 hotkey_wakeup_hotunplug_complete_notify_change();
2445 } else {
2446 unk_ev = 1;
2448 break;
2449 case 4:
2450 /* dock-related wakeups */
2451 if (hkey == 0x4003) {
2452 hotkey_autosleep_ack = 1;
2453 printk(TPACPI_INFO
2454 "undocked\n");
2455 hotkey_wakeup_hotunplug_complete_notify_change();
2456 } else {
2457 unk_ev = 1;
2459 break;
2460 case 5:
2461 /* 0x5000-0x5FFF: human interface helpers */
2462 switch (hkey) {
2463 case 0x5010: /* Lenovo new BIOS: brightness changed */
2464 case 0x500b: /* X61t: tablet pen inserted into bay */
2465 case 0x500c: /* X61t: tablet pen removed from bay */
2466 break;
2467 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2468 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2469 tpacpi_input_send_tabletsw();
2470 hotkey_tablet_mode_notify_change();
2471 send_acpi_ev = 0;
2472 break;
2473 case 0x5001:
2474 case 0x5002:
2475 /* LID switch events. Do not propagate */
2476 ignore_acpi_ev = 1;
2477 break;
2478 default:
2479 unk_ev = 1;
2481 break;
2482 case 7:
2483 /* 0x7000-0x7FFF: misc */
2484 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2485 tpacpi_send_radiosw_update();
2486 send_acpi_ev = 0;
2487 break;
2489 /* fallthrough to default */
2490 default:
2491 unk_ev = 1;
2493 if (unk_ev) {
2494 printk(TPACPI_NOTICE
2495 "unhandled HKEY event 0x%04x\n", hkey);
2498 /* Legacy events */
2499 if (!ignore_acpi_ev &&
2500 (send_acpi_ev || hotkey_report_mode < 2)) {
2501 acpi_bus_generate_proc_event(ibm->acpi->device,
2502 event, hkey);
2505 /* netlink events */
2506 if (!ignore_acpi_ev && send_acpi_ev) {
2507 acpi_bus_generate_netlink_event(
2508 ibm->acpi->device->pnp.device_class,
2509 ibm->acpi->device->dev.bus_id,
2510 event, hkey);
2515 static void hotkey_suspend(pm_message_t state)
2517 /* Do these on suspend, we get the events on early resume! */
2518 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2519 hotkey_autosleep_ack = 0;
2522 static void hotkey_resume(void)
2524 tpacpi_disable_brightness_delay();
2526 if (hotkey_mask_get())
2527 printk(TPACPI_ERR
2528 "error while trying to read hot key mask "
2529 "from firmware\n");
2530 tpacpi_send_radiosw_update();
2531 hotkey_tablet_mode_notify_change();
2532 hotkey_wakeup_reason_notify_change();
2533 hotkey_wakeup_hotunplug_complete_notify_change();
2534 hotkey_poll_setup_safe(0);
2537 /* procfs -------------------------------------------------------------- */
2538 static int hotkey_read(char *p)
2540 int res, status;
2541 int len = 0;
2543 if (!tp_features.hotkey) {
2544 len += sprintf(p + len, "status:\t\tnot supported\n");
2545 return len;
2548 if (mutex_lock_interruptible(&hotkey_mutex))
2549 return -ERESTARTSYS;
2550 res = hotkey_status_get(&status);
2551 if (!res)
2552 res = hotkey_mask_get();
2553 mutex_unlock(&hotkey_mutex);
2554 if (res)
2555 return res;
2557 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2558 if (tp_features.hotkey_mask) {
2559 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2560 len += sprintf(p + len,
2561 "commands:\tenable, disable, reset, <mask>\n");
2562 } else {
2563 len += sprintf(p + len, "mask:\t\tnot supported\n");
2564 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2567 return len;
2570 static int hotkey_write(char *buf)
2572 int res, status;
2573 u32 mask;
2574 char *cmd;
2576 if (!tp_features.hotkey)
2577 return -ENODEV;
2579 if (mutex_lock_interruptible(&hotkey_mutex))
2580 return -ERESTARTSYS;
2582 status = -1;
2583 mask = hotkey_mask;
2585 res = 0;
2586 while ((cmd = next_cmd(&buf))) {
2587 if (strlencmp(cmd, "enable") == 0) {
2588 status = 1;
2589 } else if (strlencmp(cmd, "disable") == 0) {
2590 status = 0;
2591 } else if (strlencmp(cmd, "reset") == 0) {
2592 status = hotkey_orig_status;
2593 mask = hotkey_orig_mask;
2594 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2595 /* mask set */
2596 } else if (sscanf(cmd, "%x", &mask) == 1) {
2597 /* mask set */
2598 } else {
2599 res = -EINVAL;
2600 goto errexit;
2603 if (status != -1)
2604 res = hotkey_status_set(status);
2606 if (!res && mask != hotkey_mask)
2607 res = hotkey_mask_set(mask);
2609 errexit:
2610 mutex_unlock(&hotkey_mutex);
2611 return res;
2614 static const struct acpi_device_id ibm_htk_device_ids[] = {
2615 {TPACPI_ACPI_HKEY_HID, 0},
2616 {"", 0},
2619 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2620 .hid = ibm_htk_device_ids,
2621 .notify = hotkey_notify,
2622 .handle = &hkey_handle,
2623 .type = ACPI_DEVICE_NOTIFY,
2626 static struct ibm_struct hotkey_driver_data = {
2627 .name = "hotkey",
2628 .read = hotkey_read,
2629 .write = hotkey_write,
2630 .exit = hotkey_exit,
2631 .resume = hotkey_resume,
2632 .suspend = hotkey_suspend,
2633 .acpi = &ibm_hotkey_acpidriver,
2636 /*************************************************************************
2637 * Bluetooth subdriver
2640 enum {
2641 /* ACPI GBDC/SBDC bits */
2642 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2643 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2644 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2647 static struct rfkill *tpacpi_bluetooth_rfkill;
2649 static int bluetooth_get_radiosw(void)
2651 int status;
2653 if (!tp_features.bluetooth)
2654 return -ENODEV;
2656 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2657 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2658 return RFKILL_STATE_HARD_BLOCKED;
2660 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2661 return -EIO;
2663 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
2664 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2667 static void bluetooth_update_rfk(void)
2669 int status;
2671 if (!tpacpi_bluetooth_rfkill)
2672 return;
2674 status = bluetooth_get_radiosw();
2675 if (status < 0)
2676 return;
2677 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
2680 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
2682 int status;
2684 if (!tp_features.bluetooth)
2685 return -ENODEV;
2687 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2688 * reason to risk weird behaviour. */
2689 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2690 && radio_on)
2691 return -EPERM;
2693 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2694 return -EIO;
2695 if (radio_on)
2696 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2697 else
2698 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2699 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2700 return -EIO;
2702 if (update_rfk)
2703 bluetooth_update_rfk();
2705 return 0;
2708 /* sysfs bluetooth enable ---------------------------------------------- */
2709 static ssize_t bluetooth_enable_show(struct device *dev,
2710 struct device_attribute *attr,
2711 char *buf)
2713 int status;
2715 status = bluetooth_get_radiosw();
2716 if (status < 0)
2717 return status;
2719 return snprintf(buf, PAGE_SIZE, "%d\n",
2720 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2723 static ssize_t bluetooth_enable_store(struct device *dev,
2724 struct device_attribute *attr,
2725 const char *buf, size_t count)
2727 unsigned long t;
2728 int res;
2730 if (parse_strtoul(buf, 1, &t))
2731 return -EINVAL;
2733 res = bluetooth_set_radiosw(t, 1);
2735 return (res) ? res : count;
2738 static struct device_attribute dev_attr_bluetooth_enable =
2739 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
2740 bluetooth_enable_show, bluetooth_enable_store);
2742 /* --------------------------------------------------------------------- */
2744 static struct attribute *bluetooth_attributes[] = {
2745 &dev_attr_bluetooth_enable.attr,
2746 NULL
2749 static const struct attribute_group bluetooth_attr_group = {
2750 .attrs = bluetooth_attributes,
2753 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
2755 int bts = bluetooth_get_radiosw();
2757 if (bts < 0)
2758 return bts;
2760 *state = bts;
2761 return 0;
2764 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
2766 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
2769 static void bluetooth_exit(void)
2771 if (tpacpi_bluetooth_rfkill)
2772 rfkill_unregister(tpacpi_bluetooth_rfkill);
2774 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2775 &bluetooth_attr_group);
2778 static int __init bluetooth_init(struct ibm_init_struct *iibm)
2780 int res;
2781 int status = 0;
2783 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2785 TPACPI_ACPIHANDLE_INIT(hkey);
2787 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2788 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
2789 tp_features.bluetooth = hkey_handle &&
2790 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
2792 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2793 str_supported(tp_features.bluetooth),
2794 status);
2796 if (tp_features.bluetooth &&
2797 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2798 /* no bluetooth hardware present in system */
2799 tp_features.bluetooth = 0;
2800 dbg_printk(TPACPI_DBG_INIT,
2801 "bluetooth hardware not installed\n");
2804 if (!tp_features.bluetooth)
2805 return 1;
2807 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2808 &bluetooth_attr_group);
2809 if (res)
2810 return res;
2812 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
2813 &tpacpi_bluetooth_rfkill,
2814 RFKILL_TYPE_BLUETOOTH,
2815 "tpacpi_bluetooth_sw",
2816 tpacpi_bluetooth_rfk_set,
2817 tpacpi_bluetooth_rfk_get);
2818 if (res) {
2819 bluetooth_exit();
2820 return res;
2823 return 0;
2826 /* procfs -------------------------------------------------------------- */
2827 static int bluetooth_read(char *p)
2829 int len = 0;
2830 int status = bluetooth_get_radiosw();
2832 if (!tp_features.bluetooth)
2833 len += sprintf(p + len, "status:\t\tnot supported\n");
2834 else {
2835 len += sprintf(p + len, "status:\t\t%s\n",
2836 (status == RFKILL_STATE_UNBLOCKED) ?
2837 "enabled" : "disabled");
2838 len += sprintf(p + len, "commands:\tenable, disable\n");
2841 return len;
2844 static int bluetooth_write(char *buf)
2846 char *cmd;
2848 if (!tp_features.bluetooth)
2849 return -ENODEV;
2851 while ((cmd = next_cmd(&buf))) {
2852 if (strlencmp(cmd, "enable") == 0) {
2853 bluetooth_set_radiosw(1, 1);
2854 } else if (strlencmp(cmd, "disable") == 0) {
2855 bluetooth_set_radiosw(0, 1);
2856 } else
2857 return -EINVAL;
2860 return 0;
2863 static struct ibm_struct bluetooth_driver_data = {
2864 .name = "bluetooth",
2865 .read = bluetooth_read,
2866 .write = bluetooth_write,
2867 .exit = bluetooth_exit,
2870 /*************************************************************************
2871 * Wan subdriver
2874 enum {
2875 /* ACPI GWAN/SWAN bits */
2876 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2877 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2878 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2881 static struct rfkill *tpacpi_wan_rfkill;
2883 static int wan_get_radiosw(void)
2885 int status;
2887 if (!tp_features.wan)
2888 return -ENODEV;
2890 /* WLSW overrides WWAN in firmware/hardware, reflect that */
2891 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2892 return RFKILL_STATE_HARD_BLOCKED;
2894 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2895 return -EIO;
2897 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
2898 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
2901 static void wan_update_rfk(void)
2903 int status;
2905 if (!tpacpi_wan_rfkill)
2906 return;
2908 status = wan_get_radiosw();
2909 if (status < 0)
2910 return;
2911 rfkill_force_state(tpacpi_wan_rfkill, status);
2914 static int wan_set_radiosw(int radio_on, int update_rfk)
2916 int status;
2918 if (!tp_features.wan)
2919 return -ENODEV;
2921 /* WLSW overrides bluetooth in firmware/hardware, but there is no
2922 * reason to risk weird behaviour. */
2923 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
2924 && radio_on)
2925 return -EPERM;
2927 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2928 return -EIO;
2929 if (radio_on)
2930 status |= TP_ACPI_WANCARD_RADIOSSW;
2931 else
2932 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2933 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2934 return -EIO;
2936 if (update_rfk)
2937 wan_update_rfk();
2939 return 0;
2942 /* sysfs wan enable ---------------------------------------------------- */
2943 static ssize_t wan_enable_show(struct device *dev,
2944 struct device_attribute *attr,
2945 char *buf)
2947 int status;
2949 status = wan_get_radiosw();
2950 if (status < 0)
2951 return status;
2953 return snprintf(buf, PAGE_SIZE, "%d\n",
2954 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
2957 static ssize_t wan_enable_store(struct device *dev,
2958 struct device_attribute *attr,
2959 const char *buf, size_t count)
2961 unsigned long t;
2962 int res;
2964 if (parse_strtoul(buf, 1, &t))
2965 return -EINVAL;
2967 res = wan_set_radiosw(t, 1);
2969 return (res) ? res : count;
2972 static struct device_attribute dev_attr_wan_enable =
2973 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
2974 wan_enable_show, wan_enable_store);
2976 /* --------------------------------------------------------------------- */
2978 static struct attribute *wan_attributes[] = {
2979 &dev_attr_wan_enable.attr,
2980 NULL
2983 static const struct attribute_group wan_attr_group = {
2984 .attrs = wan_attributes,
2987 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
2989 int wans = wan_get_radiosw();
2991 if (wans < 0)
2992 return wans;
2994 *state = wans;
2995 return 0;
2998 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3000 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3003 static void wan_exit(void)
3005 if (tpacpi_wan_rfkill)
3006 rfkill_unregister(tpacpi_wan_rfkill);
3008 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3009 &wan_attr_group);
3012 static int __init wan_init(struct ibm_init_struct *iibm)
3014 int res;
3015 int status = 0;
3017 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3019 TPACPI_ACPIHANDLE_INIT(hkey);
3021 tp_features.wan = hkey_handle &&
3022 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3024 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3025 str_supported(tp_features.wan),
3026 status);
3028 if (tp_features.wan &&
3029 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3030 /* no wan hardware present in system */
3031 tp_features.wan = 0;
3032 dbg_printk(TPACPI_DBG_INIT,
3033 "wan hardware not installed\n");
3036 if (!tp_features.wan)
3037 return 1;
3039 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3040 &wan_attr_group);
3041 if (res)
3042 return res;
3044 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3045 &tpacpi_wan_rfkill,
3046 RFKILL_TYPE_WWAN,
3047 "tpacpi_wwan_sw",
3048 tpacpi_wan_rfk_set,
3049 tpacpi_wan_rfk_get);
3050 if (res) {
3051 wan_exit();
3052 return res;
3055 return 0;
3058 /* procfs -------------------------------------------------------------- */
3059 static int wan_read(char *p)
3061 int len = 0;
3062 int status = wan_get_radiosw();
3064 if (!tp_features.wan)
3065 len += sprintf(p + len, "status:\t\tnot supported\n");
3066 else {
3067 len += sprintf(p + len, "status:\t\t%s\n",
3068 (status == RFKILL_STATE_UNBLOCKED) ?
3069 "enabled" : "disabled");
3070 len += sprintf(p + len, "commands:\tenable, disable\n");
3073 return len;
3076 static int wan_write(char *buf)
3078 char *cmd;
3080 if (!tp_features.wan)
3081 return -ENODEV;
3083 while ((cmd = next_cmd(&buf))) {
3084 if (strlencmp(cmd, "enable") == 0) {
3085 wan_set_radiosw(1, 1);
3086 } else if (strlencmp(cmd, "disable") == 0) {
3087 wan_set_radiosw(0, 1);
3088 } else
3089 return -EINVAL;
3092 return 0;
3095 static struct ibm_struct wan_driver_data = {
3096 .name = "wan",
3097 .read = wan_read,
3098 .write = wan_write,
3099 .exit = wan_exit,
3102 /*************************************************************************
3103 * Video subdriver
3106 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3108 enum video_access_mode {
3109 TPACPI_VIDEO_NONE = 0,
3110 TPACPI_VIDEO_570, /* 570 */
3111 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3112 TPACPI_VIDEO_NEW, /* all others */
3115 enum { /* video status flags, based on VIDEO_570 */
3116 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3117 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3118 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3121 enum { /* TPACPI_VIDEO_570 constants */
3122 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3123 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3124 * video_status_flags */
3125 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3126 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3129 static enum video_access_mode video_supported;
3130 static int video_orig_autosw;
3132 static int video_autosw_get(void);
3133 static int video_autosw_set(int enable);
3135 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3137 static int __init video_init(struct ibm_init_struct *iibm)
3139 int ivga;
3141 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3143 TPACPI_ACPIHANDLE_INIT(vid);
3144 TPACPI_ACPIHANDLE_INIT(vid2);
3146 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3147 /* G41, assume IVGA doesn't change */
3148 vid_handle = vid2_handle;
3150 if (!vid_handle)
3151 /* video switching not supported on R30, R31 */
3152 video_supported = TPACPI_VIDEO_NONE;
3153 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3154 /* 570 */
3155 video_supported = TPACPI_VIDEO_570;
3156 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3157 /* 600e/x, 770e, 770x */
3158 video_supported = TPACPI_VIDEO_770;
3159 else
3160 /* all others */
3161 video_supported = TPACPI_VIDEO_NEW;
3163 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3164 str_supported(video_supported != TPACPI_VIDEO_NONE),
3165 video_supported);
3167 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3170 static void video_exit(void)
3172 dbg_printk(TPACPI_DBG_EXIT,
3173 "restoring original video autoswitch mode\n");
3174 if (video_autosw_set(video_orig_autosw))
3175 printk(TPACPI_ERR "error while trying to restore original "
3176 "video autoswitch mode\n");
3179 static int video_outputsw_get(void)
3181 int status = 0;
3182 int i;
3184 switch (video_supported) {
3185 case TPACPI_VIDEO_570:
3186 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3187 TP_ACPI_VIDEO_570_PHSCMD))
3188 return -EIO;
3189 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3190 break;
3191 case TPACPI_VIDEO_770:
3192 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3193 return -EIO;
3194 if (i)
3195 status |= TP_ACPI_VIDEO_S_LCD;
3196 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3197 return -EIO;
3198 if (i)
3199 status |= TP_ACPI_VIDEO_S_CRT;
3200 break;
3201 case TPACPI_VIDEO_NEW:
3202 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3203 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3204 return -EIO;
3205 if (i)
3206 status |= TP_ACPI_VIDEO_S_CRT;
3208 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3209 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3210 return -EIO;
3211 if (i)
3212 status |= TP_ACPI_VIDEO_S_LCD;
3213 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3214 return -EIO;
3215 if (i)
3216 status |= TP_ACPI_VIDEO_S_DVI;
3217 break;
3218 default:
3219 return -ENOSYS;
3222 return status;
3225 static int video_outputsw_set(int status)
3227 int autosw;
3228 int res = 0;
3230 switch (video_supported) {
3231 case TPACPI_VIDEO_570:
3232 res = acpi_evalf(NULL, NULL,
3233 "\\_SB.PHS2", "vdd",
3234 TP_ACPI_VIDEO_570_PHS2CMD,
3235 status | TP_ACPI_VIDEO_570_PHS2SET);
3236 break;
3237 case TPACPI_VIDEO_770:
3238 autosw = video_autosw_get();
3239 if (autosw < 0)
3240 return autosw;
3242 res = video_autosw_set(1);
3243 if (res)
3244 return res;
3245 res = acpi_evalf(vid_handle, NULL,
3246 "ASWT", "vdd", status * 0x100, 0);
3247 if (!autosw && video_autosw_set(autosw)) {
3248 printk(TPACPI_ERR
3249 "video auto-switch left enabled due to error\n");
3250 return -EIO;
3252 break;
3253 case TPACPI_VIDEO_NEW:
3254 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3255 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3256 break;
3257 default:
3258 return -ENOSYS;
3261 return (res)? 0 : -EIO;
3264 static int video_autosw_get(void)
3266 int autosw = 0;
3268 switch (video_supported) {
3269 case TPACPI_VIDEO_570:
3270 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3271 return -EIO;
3272 break;
3273 case TPACPI_VIDEO_770:
3274 case TPACPI_VIDEO_NEW:
3275 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3276 return -EIO;
3277 break;
3278 default:
3279 return -ENOSYS;
3282 return autosw & 1;
3285 static int video_autosw_set(int enable)
3287 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3288 return -EIO;
3289 return 0;
3292 static int video_outputsw_cycle(void)
3294 int autosw = video_autosw_get();
3295 int res;
3297 if (autosw < 0)
3298 return autosw;
3300 switch (video_supported) {
3301 case TPACPI_VIDEO_570:
3302 res = video_autosw_set(1);
3303 if (res)
3304 return res;
3305 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3306 break;
3307 case TPACPI_VIDEO_770:
3308 case TPACPI_VIDEO_NEW:
3309 res = video_autosw_set(1);
3310 if (res)
3311 return res;
3312 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3313 break;
3314 default:
3315 return -ENOSYS;
3317 if (!autosw && video_autosw_set(autosw)) {
3318 printk(TPACPI_ERR
3319 "video auto-switch left enabled due to error\n");
3320 return -EIO;
3323 return (res)? 0 : -EIO;
3326 static int video_expand_toggle(void)
3328 switch (video_supported) {
3329 case TPACPI_VIDEO_570:
3330 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3331 0 : -EIO;
3332 case TPACPI_VIDEO_770:
3333 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3334 0 : -EIO;
3335 case TPACPI_VIDEO_NEW:
3336 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3337 0 : -EIO;
3338 default:
3339 return -ENOSYS;
3341 /* not reached */
3344 static int video_read(char *p)
3346 int status, autosw;
3347 int len = 0;
3349 if (video_supported == TPACPI_VIDEO_NONE) {
3350 len += sprintf(p + len, "status:\t\tnot supported\n");
3351 return len;
3354 status = video_outputsw_get();
3355 if (status < 0)
3356 return status;
3358 autosw = video_autosw_get();
3359 if (autosw < 0)
3360 return autosw;
3362 len += sprintf(p + len, "status:\t\tsupported\n");
3363 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3364 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3365 if (video_supported == TPACPI_VIDEO_NEW)
3366 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3367 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3368 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3369 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3370 if (video_supported == TPACPI_VIDEO_NEW)
3371 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3372 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3373 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3375 return len;
3378 static int video_write(char *buf)
3380 char *cmd;
3381 int enable, disable, status;
3382 int res;
3384 if (video_supported == TPACPI_VIDEO_NONE)
3385 return -ENODEV;
3387 enable = 0;
3388 disable = 0;
3390 while ((cmd = next_cmd(&buf))) {
3391 if (strlencmp(cmd, "lcd_enable") == 0) {
3392 enable |= TP_ACPI_VIDEO_S_LCD;
3393 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3394 disable |= TP_ACPI_VIDEO_S_LCD;
3395 } else if (strlencmp(cmd, "crt_enable") == 0) {
3396 enable |= TP_ACPI_VIDEO_S_CRT;
3397 } else if (strlencmp(cmd, "crt_disable") == 0) {
3398 disable |= TP_ACPI_VIDEO_S_CRT;
3399 } else if (video_supported == TPACPI_VIDEO_NEW &&
3400 strlencmp(cmd, "dvi_enable") == 0) {
3401 enable |= TP_ACPI_VIDEO_S_DVI;
3402 } else if (video_supported == TPACPI_VIDEO_NEW &&
3403 strlencmp(cmd, "dvi_disable") == 0) {
3404 disable |= TP_ACPI_VIDEO_S_DVI;
3405 } else if (strlencmp(cmd, "auto_enable") == 0) {
3406 res = video_autosw_set(1);
3407 if (res)
3408 return res;
3409 } else if (strlencmp(cmd, "auto_disable") == 0) {
3410 res = video_autosw_set(0);
3411 if (res)
3412 return res;
3413 } else if (strlencmp(cmd, "video_switch") == 0) {
3414 res = video_outputsw_cycle();
3415 if (res)
3416 return res;
3417 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3418 res = video_expand_toggle();
3419 if (res)
3420 return res;
3421 } else
3422 return -EINVAL;
3425 if (enable || disable) {
3426 status = video_outputsw_get();
3427 if (status < 0)
3428 return status;
3429 res = video_outputsw_set((status & ~disable) | enable);
3430 if (res)
3431 return res;
3434 return 0;
3437 static struct ibm_struct video_driver_data = {
3438 .name = "video",
3439 .read = video_read,
3440 .write = video_write,
3441 .exit = video_exit,
3444 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3446 /*************************************************************************
3447 * Light (thinklight) subdriver
3450 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3451 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
3453 static int light_get_status(void)
3455 int status = 0;
3457 if (tp_features.light_status) {
3458 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3459 return -EIO;
3460 return (!!status);
3463 return -ENXIO;
3466 static int light_set_status(int status)
3468 int rc;
3470 if (tp_features.light) {
3471 if (cmos_handle) {
3472 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3473 (status)?
3474 TP_CMOS_THINKLIGHT_ON :
3475 TP_CMOS_THINKLIGHT_OFF);
3476 } else {
3477 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3478 (status)? 1 : 0);
3480 return (rc)? 0 : -EIO;
3483 return -ENXIO;
3486 static void light_set_status_worker(struct work_struct *work)
3488 struct tpacpi_led_classdev *data =
3489 container_of(work, struct tpacpi_led_classdev, work);
3491 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3492 light_set_status((data->new_brightness != LED_OFF));
3495 static void light_sysfs_set(struct led_classdev *led_cdev,
3496 enum led_brightness brightness)
3498 struct tpacpi_led_classdev *data =
3499 container_of(led_cdev,
3500 struct tpacpi_led_classdev,
3501 led_classdev);
3502 data->new_brightness = brightness;
3503 queue_work(tpacpi_wq, &data->work);
3506 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3508 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3511 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3512 .led_classdev = {
3513 .name = "tpacpi::thinklight",
3514 .brightness_set = &light_sysfs_set,
3515 .brightness_get = &light_sysfs_get,
3519 static int __init light_init(struct ibm_init_struct *iibm)
3521 int rc;
3523 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3525 TPACPI_ACPIHANDLE_INIT(ledb);
3526 TPACPI_ACPIHANDLE_INIT(lght);
3527 TPACPI_ACPIHANDLE_INIT(cmos);
3528 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
3530 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
3531 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
3533 if (tp_features.light)
3534 /* light status not supported on
3535 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
3536 tp_features.light_status =
3537 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
3539 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
3540 str_supported(tp_features.light),
3541 str_supported(tp_features.light_status));
3543 if (!tp_features.light)
3544 return 1;
3546 rc = led_classdev_register(&tpacpi_pdev->dev,
3547 &tpacpi_led_thinklight.led_classdev);
3549 if (rc < 0) {
3550 tp_features.light = 0;
3551 tp_features.light_status = 0;
3552 } else {
3553 rc = 0;
3556 return rc;
3559 static void light_exit(void)
3561 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3562 if (work_pending(&tpacpi_led_thinklight.work))
3563 flush_workqueue(tpacpi_wq);
3566 static int light_read(char *p)
3568 int len = 0;
3569 int status;
3571 if (!tp_features.light) {
3572 len += sprintf(p + len, "status:\t\tnot supported\n");
3573 } else if (!tp_features.light_status) {
3574 len += sprintf(p + len, "status:\t\tunknown\n");
3575 len += sprintf(p + len, "commands:\ton, off\n");
3576 } else {
3577 status = light_get_status();
3578 if (status < 0)
3579 return status;
3580 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
3581 len += sprintf(p + len, "commands:\ton, off\n");
3584 return len;
3587 static int light_write(char *buf)
3589 char *cmd;
3590 int newstatus = 0;
3592 if (!tp_features.light)
3593 return -ENODEV;
3595 while ((cmd = next_cmd(&buf))) {
3596 if (strlencmp(cmd, "on") == 0) {
3597 newstatus = 1;
3598 } else if (strlencmp(cmd, "off") == 0) {
3599 newstatus = 0;
3600 } else
3601 return -EINVAL;
3604 return light_set_status(newstatus);
3607 static struct ibm_struct light_driver_data = {
3608 .name = "light",
3609 .read = light_read,
3610 .write = light_write,
3611 .exit = light_exit,
3614 /*************************************************************************
3615 * Dock subdriver
3618 #ifdef CONFIG_THINKPAD_ACPI_DOCK
3620 static void dock_notify(struct ibm_struct *ibm, u32 event);
3621 static int dock_read(char *p);
3622 static int dock_write(char *buf);
3624 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
3625 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3626 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3627 "\\_SB.PCI.ISA.SLCE", /* 570 */
3628 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3630 /* don't list other alternatives as we install a notify handler on the 570 */
3631 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
3633 static const struct acpi_device_id ibm_pci_device_ids[] = {
3634 {PCI_ROOT_HID_STRING, 0},
3635 {"", 0},
3638 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3640 .notify = dock_notify,
3641 .handle = &dock_handle,
3642 .type = ACPI_SYSTEM_NOTIFY,
3645 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3646 * We just use it to get notifications of dock hotplug
3647 * in very old thinkpads */
3648 .hid = ibm_pci_device_ids,
3649 .notify = dock_notify,
3650 .handle = &pci_handle,
3651 .type = ACPI_SYSTEM_NOTIFY,
3655 static struct ibm_struct dock_driver_data[2] = {
3657 .name = "dock",
3658 .read = dock_read,
3659 .write = dock_write,
3660 .acpi = &ibm_dock_acpidriver[0],
3663 .name = "dock",
3664 .acpi = &ibm_dock_acpidriver[1],
3668 #define dock_docked() (_sta(dock_handle) & 1)
3670 static int __init dock_init(struct ibm_init_struct *iibm)
3672 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3674 TPACPI_ACPIHANDLE_INIT(dock);
3676 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3677 str_supported(dock_handle != NULL));
3679 return (dock_handle)? 0 : 1;
3682 static int __init dock_init2(struct ibm_init_struct *iibm)
3684 int dock2_needed;
3686 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3688 if (dock_driver_data[0].flags.acpi_driver_registered &&
3689 dock_driver_data[0].flags.acpi_notify_installed) {
3690 TPACPI_ACPIHANDLE_INIT(pci);
3691 dock2_needed = (pci_handle != NULL);
3692 vdbg_printk(TPACPI_DBG_INIT,
3693 "dock PCI handler for the TP 570 is %s\n",
3694 str_supported(dock2_needed));
3695 } else {
3696 vdbg_printk(TPACPI_DBG_INIT,
3697 "dock subdriver part 2 not required\n");
3698 dock2_needed = 0;
3701 return (dock2_needed)? 0 : 1;
3704 static void dock_notify(struct ibm_struct *ibm, u32 event)
3706 int docked = dock_docked();
3707 int pci = ibm->acpi->hid && ibm->acpi->device &&
3708 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
3709 int data;
3711 if (event == 1 && !pci) /* 570 */
3712 data = 1; /* button */
3713 else if (event == 1 && pci) /* 570 */
3714 data = 3; /* dock */
3715 else if (event == 3 && docked)
3716 data = 1; /* button */
3717 else if (event == 3 && !docked)
3718 data = 2; /* undock */
3719 else if (event == 0 && docked)
3720 data = 3; /* dock */
3721 else {
3722 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
3723 event, _sta(dock_handle));
3724 data = 0; /* unknown */
3726 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
3727 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3728 ibm->acpi->device->dev.bus_id,
3729 event, data);
3732 static int dock_read(char *p)
3734 int len = 0;
3735 int docked = dock_docked();
3737 if (!dock_handle)
3738 len += sprintf(p + len, "status:\t\tnot supported\n");
3739 else if (!docked)
3740 len += sprintf(p + len, "status:\t\tundocked\n");
3741 else {
3742 len += sprintf(p + len, "status:\t\tdocked\n");
3743 len += sprintf(p + len, "commands:\tdock, undock\n");
3746 return len;
3749 static int dock_write(char *buf)
3751 char *cmd;
3753 if (!dock_docked())
3754 return -ENODEV;
3756 while ((cmd = next_cmd(&buf))) {
3757 if (strlencmp(cmd, "undock") == 0) {
3758 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3759 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
3760 return -EIO;
3761 } else if (strlencmp(cmd, "dock") == 0) {
3762 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3763 return -EIO;
3764 } else
3765 return -EINVAL;
3768 return 0;
3771 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
3773 /*************************************************************************
3774 * Bay subdriver
3777 #ifdef CONFIG_THINKPAD_ACPI_BAY
3779 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
3780 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3781 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3782 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3783 ); /* A21e, R30, R31 */
3784 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
3785 "_EJ0", /* all others */
3786 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
3787 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
3788 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3789 ); /* all others */
3790 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
3791 "_EJ0", /* 770x */
3792 ); /* all others */
3794 static int __init bay_init(struct ibm_init_struct *iibm)
3796 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3798 TPACPI_ACPIHANDLE_INIT(bay);
3799 if (bay_handle)
3800 TPACPI_ACPIHANDLE_INIT(bay_ej);
3801 TPACPI_ACPIHANDLE_INIT(bay2);
3802 if (bay2_handle)
3803 TPACPI_ACPIHANDLE_INIT(bay2_ej);
3805 tp_features.bay_status = bay_handle &&
3806 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3807 tp_features.bay_status2 = bay2_handle &&
3808 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
3810 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3811 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3812 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3813 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
3815 vdbg_printk(TPACPI_DBG_INIT,
3816 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
3817 str_supported(tp_features.bay_status),
3818 str_supported(tp_features.bay_eject),
3819 str_supported(tp_features.bay_status2),
3820 str_supported(tp_features.bay_eject2));
3822 return (tp_features.bay_status || tp_features.bay_eject ||
3823 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
3826 static void bay_notify(struct ibm_struct *ibm, u32 event)
3828 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
3829 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3830 ibm->acpi->device->dev.bus_id,
3831 event, 0);
3834 #define bay_occupied(b) (_sta(b##_handle) & 1)
3836 static int bay_read(char *p)
3838 int len = 0;
3839 int occupied = bay_occupied(bay);
3840 int occupied2 = bay_occupied(bay2);
3841 int eject, eject2;
3843 len += sprintf(p + len, "status:\t\t%s\n",
3844 tp_features.bay_status ?
3845 (occupied ? "occupied" : "unoccupied") :
3846 "not supported");
3847 if (tp_features.bay_status2)
3848 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3849 "occupied" : "unoccupied");
3851 eject = tp_features.bay_eject && occupied;
3852 eject2 = tp_features.bay_eject2 && occupied2;
3854 if (eject && eject2)
3855 len += sprintf(p + len, "commands:\teject, eject2\n");
3856 else if (eject)
3857 len += sprintf(p + len, "commands:\teject\n");
3858 else if (eject2)
3859 len += sprintf(p + len, "commands:\teject2\n");
3861 return len;
3864 static int bay_write(char *buf)
3866 char *cmd;
3868 if (!tp_features.bay_eject && !tp_features.bay_eject2)
3869 return -ENODEV;
3871 while ((cmd = next_cmd(&buf))) {
3872 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
3873 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3874 return -EIO;
3875 } else if (tp_features.bay_eject2 &&
3876 strlencmp(cmd, "eject2") == 0) {
3877 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
3878 return -EIO;
3879 } else
3880 return -EINVAL;
3883 return 0;
3886 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3887 .notify = bay_notify,
3888 .handle = &bay_handle,
3889 .type = ACPI_SYSTEM_NOTIFY,
3892 static struct ibm_struct bay_driver_data = {
3893 .name = "bay",
3894 .read = bay_read,
3895 .write = bay_write,
3896 .acpi = &ibm_bay_acpidriver,
3899 #endif /* CONFIG_THINKPAD_ACPI_BAY */
3901 /*************************************************************************
3902 * CMOS subdriver
3905 /* sysfs cmos_command -------------------------------------------------- */
3906 static ssize_t cmos_command_store(struct device *dev,
3907 struct device_attribute *attr,
3908 const char *buf, size_t count)
3910 unsigned long cmos_cmd;
3911 int res;
3913 if (parse_strtoul(buf, 21, &cmos_cmd))
3914 return -EINVAL;
3916 res = issue_thinkpad_cmos_command(cmos_cmd);
3917 return (res)? res : count;
3920 static struct device_attribute dev_attr_cmos_command =
3921 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3923 /* --------------------------------------------------------------------- */
3925 static int __init cmos_init(struct ibm_init_struct *iibm)
3927 int res;
3929 vdbg_printk(TPACPI_DBG_INIT,
3930 "initializing cmos commands subdriver\n");
3932 TPACPI_ACPIHANDLE_INIT(cmos);
3934 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3935 str_supported(cmos_handle != NULL));
3937 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3938 if (res)
3939 return res;
3941 return (cmos_handle)? 0 : 1;
3944 static void cmos_exit(void)
3946 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3949 static int cmos_read(char *p)
3951 int len = 0;
3953 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3954 R30, R31, T20-22, X20-21 */
3955 if (!cmos_handle)
3956 len += sprintf(p + len, "status:\t\tnot supported\n");
3957 else {
3958 len += sprintf(p + len, "status:\t\tsupported\n");
3959 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
3962 return len;
3965 static int cmos_write(char *buf)
3967 char *cmd;
3968 int cmos_cmd, res;
3970 while ((cmd = next_cmd(&buf))) {
3971 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3972 cmos_cmd >= 0 && cmos_cmd <= 21) {
3973 /* cmos_cmd set */
3974 } else
3975 return -EINVAL;
3977 res = issue_thinkpad_cmos_command(cmos_cmd);
3978 if (res)
3979 return res;
3982 return 0;
3985 static struct ibm_struct cmos_driver_data = {
3986 .name = "cmos",
3987 .read = cmos_read,
3988 .write = cmos_write,
3989 .exit = cmos_exit,
3992 /*************************************************************************
3993 * LED subdriver
3996 enum led_access_mode {
3997 TPACPI_LED_NONE = 0,
3998 TPACPI_LED_570, /* 570 */
3999 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4000 TPACPI_LED_NEW, /* all others */
4003 enum { /* For TPACPI_LED_OLD */
4004 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4005 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4006 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4009 enum led_status_t {
4010 TPACPI_LED_OFF = 0,
4011 TPACPI_LED_ON,
4012 TPACPI_LED_BLINK,
4015 static enum led_access_mode led_supported;
4017 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4018 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4019 /* T20-22, X20-21 */
4020 "LED", /* all others */
4021 ); /* R30, R31 */
4023 #define TPACPI_LED_NUMLEDS 8
4024 static struct tpacpi_led_classdev *tpacpi_leds;
4025 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4026 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4027 /* there's a limit of 19 chars + NULL before 2.6.26 */
4028 "tpacpi::power",
4029 "tpacpi:orange:batt",
4030 "tpacpi:green:batt",
4031 "tpacpi::dock_active",
4032 "tpacpi::bay_active",
4033 "tpacpi::dock_batt",
4034 "tpacpi::unknown_led",
4035 "tpacpi::standby",
4038 static int led_get_status(const unsigned int led)
4040 int status;
4041 enum led_status_t led_s;
4043 switch (led_supported) {
4044 case TPACPI_LED_570:
4045 if (!acpi_evalf(ec_handle,
4046 &status, "GLED", "dd", 1 << led))
4047 return -EIO;
4048 led_s = (status == 0)?
4049 TPACPI_LED_OFF :
4050 ((status == 1)?
4051 TPACPI_LED_ON :
4052 TPACPI_LED_BLINK);
4053 tpacpi_led_state_cache[led] = led_s;
4054 return led_s;
4055 default:
4056 return -ENXIO;
4059 /* not reached */
4062 static int led_set_status(const unsigned int led,
4063 const enum led_status_t ledstatus)
4065 /* off, on, blink. Index is led_status_t */
4066 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4067 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4069 int rc = 0;
4071 switch (led_supported) {
4072 case TPACPI_LED_570:
4073 /* 570 */
4074 if (led > 7)
4075 return -EINVAL;
4076 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4077 (1 << led), led_sled_arg1[ledstatus]))
4078 rc = -EIO;
4079 break;
4080 case TPACPI_LED_OLD:
4081 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4082 if (led > 7)
4083 return -EINVAL;
4084 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4085 if (rc >= 0)
4086 rc = ec_write(TPACPI_LED_EC_HLBL,
4087 (ledstatus == TPACPI_LED_BLINK) << led);
4088 if (rc >= 0)
4089 rc = ec_write(TPACPI_LED_EC_HLCL,
4090 (ledstatus != TPACPI_LED_OFF) << led);
4091 break;
4092 case TPACPI_LED_NEW:
4093 /* all others */
4094 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4095 led, led_led_arg1[ledstatus]))
4096 rc = -EIO;
4097 break;
4098 default:
4099 rc = -ENXIO;
4102 if (!rc)
4103 tpacpi_led_state_cache[led] = ledstatus;
4105 return rc;
4108 static void led_sysfs_set_status(unsigned int led,
4109 enum led_brightness brightness)
4111 led_set_status(led,
4112 (brightness == LED_OFF) ?
4113 TPACPI_LED_OFF :
4114 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4115 TPACPI_LED_BLINK : TPACPI_LED_ON);
4118 static void led_set_status_worker(struct work_struct *work)
4120 struct tpacpi_led_classdev *data =
4121 container_of(work, struct tpacpi_led_classdev, work);
4123 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4124 led_sysfs_set_status(data->led, data->new_brightness);
4127 static void led_sysfs_set(struct led_classdev *led_cdev,
4128 enum led_brightness brightness)
4130 struct tpacpi_led_classdev *data = container_of(led_cdev,
4131 struct tpacpi_led_classdev, led_classdev);
4133 data->new_brightness = brightness;
4134 queue_work(tpacpi_wq, &data->work);
4137 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4138 unsigned long *delay_on, unsigned long *delay_off)
4140 struct tpacpi_led_classdev *data = container_of(led_cdev,
4141 struct tpacpi_led_classdev, led_classdev);
4143 /* Can we choose the flash rate? */
4144 if (*delay_on == 0 && *delay_off == 0) {
4145 /* yes. set them to the hardware blink rate (1 Hz) */
4146 *delay_on = 500; /* ms */
4147 *delay_off = 500; /* ms */
4148 } else if ((*delay_on != 500) || (*delay_off != 500))
4149 return -EINVAL;
4151 data->new_brightness = TPACPI_LED_BLINK;
4152 queue_work(tpacpi_wq, &data->work);
4154 return 0;
4157 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4159 int rc;
4161 struct tpacpi_led_classdev *data = container_of(led_cdev,
4162 struct tpacpi_led_classdev, led_classdev);
4164 rc = led_get_status(data->led);
4166 if (rc == TPACPI_LED_OFF || rc < 0)
4167 rc = LED_OFF; /* no error handling in led class :( */
4168 else
4169 rc = LED_FULL;
4171 return rc;
4174 static void led_exit(void)
4176 unsigned int i;
4178 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4179 if (tpacpi_leds[i].led_classdev.name)
4180 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4183 kfree(tpacpi_leds);
4186 static int __init led_init(struct ibm_init_struct *iibm)
4188 unsigned int i;
4189 int rc;
4191 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4193 TPACPI_ACPIHANDLE_INIT(led);
4195 if (!led_handle)
4196 /* led not supported on R30, R31 */
4197 led_supported = TPACPI_LED_NONE;
4198 else if (strlencmp(led_path, "SLED") == 0)
4199 /* 570 */
4200 led_supported = TPACPI_LED_570;
4201 else if (strlencmp(led_path, "SYSL") == 0)
4202 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4203 led_supported = TPACPI_LED_OLD;
4204 else
4205 /* all others */
4206 led_supported = TPACPI_LED_NEW;
4208 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4209 str_supported(led_supported), led_supported);
4211 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4212 GFP_KERNEL);
4213 if (!tpacpi_leds) {
4214 printk(TPACPI_ERR "Out of memory for LED data\n");
4215 return -ENOMEM;
4218 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4219 tpacpi_leds[i].led = i;
4221 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4222 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4223 if (led_supported == TPACPI_LED_570)
4224 tpacpi_leds[i].led_classdev.brightness_get =
4225 &led_sysfs_get;
4227 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4229 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4231 rc = led_classdev_register(&tpacpi_pdev->dev,
4232 &tpacpi_leds[i].led_classdev);
4233 if (rc < 0) {
4234 tpacpi_leds[i].led_classdev.name = NULL;
4235 led_exit();
4236 return rc;
4240 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4243 #define str_led_status(s) \
4244 ((s) == TPACPI_LED_OFF ? "off" : \
4245 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4247 static int led_read(char *p)
4249 int len = 0;
4251 if (!led_supported) {
4252 len += sprintf(p + len, "status:\t\tnot supported\n");
4253 return len;
4255 len += sprintf(p + len, "status:\t\tsupported\n");
4257 if (led_supported == TPACPI_LED_570) {
4258 /* 570 */
4259 int i, status;
4260 for (i = 0; i < 8; i++) {
4261 status = led_get_status(i);
4262 if (status < 0)
4263 return -EIO;
4264 len += sprintf(p + len, "%d:\t\t%s\n",
4265 i, str_led_status(status));
4269 len += sprintf(p + len, "commands:\t"
4270 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4272 return len;
4275 static int led_write(char *buf)
4277 char *cmd;
4278 int led, rc;
4279 enum led_status_t s;
4281 if (!led_supported)
4282 return -ENODEV;
4284 while ((cmd = next_cmd(&buf))) {
4285 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4286 return -EINVAL;
4288 if (strstr(cmd, "off")) {
4289 s = TPACPI_LED_OFF;
4290 } else if (strstr(cmd, "on")) {
4291 s = TPACPI_LED_ON;
4292 } else if (strstr(cmd, "blink")) {
4293 s = TPACPI_LED_BLINK;
4294 } else {
4295 return -EINVAL;
4298 rc = led_set_status(led, s);
4299 if (rc < 0)
4300 return rc;
4303 return 0;
4306 static struct ibm_struct led_driver_data = {
4307 .name = "led",
4308 .read = led_read,
4309 .write = led_write,
4310 .exit = led_exit,
4313 /*************************************************************************
4314 * Beep subdriver
4317 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4319 static int __init beep_init(struct ibm_init_struct *iibm)
4321 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4323 TPACPI_ACPIHANDLE_INIT(beep);
4325 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4326 str_supported(beep_handle != NULL));
4328 return (beep_handle)? 0 : 1;
4331 static int beep_read(char *p)
4333 int len = 0;
4335 if (!beep_handle)
4336 len += sprintf(p + len, "status:\t\tnot supported\n");
4337 else {
4338 len += sprintf(p + len, "status:\t\tsupported\n");
4339 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4342 return len;
4345 static int beep_write(char *buf)
4347 char *cmd;
4348 int beep_cmd;
4350 if (!beep_handle)
4351 return -ENODEV;
4353 while ((cmd = next_cmd(&buf))) {
4354 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4355 beep_cmd >= 0 && beep_cmd <= 17) {
4356 /* beep_cmd set */
4357 } else
4358 return -EINVAL;
4359 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4360 return -EIO;
4363 return 0;
4366 static struct ibm_struct beep_driver_data = {
4367 .name = "beep",
4368 .read = beep_read,
4369 .write = beep_write,
4372 /*************************************************************************
4373 * Thermal subdriver
4376 enum thermal_access_mode {
4377 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4378 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4379 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4380 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4381 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4384 enum { /* TPACPI_THERMAL_TPEC_* */
4385 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4386 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4387 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4390 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4391 struct ibm_thermal_sensors_struct {
4392 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4395 static enum thermal_access_mode thermal_read_mode;
4397 /* idx is zero-based */
4398 static int thermal_get_sensor(int idx, s32 *value)
4400 int t;
4401 s8 tmp;
4402 char tmpi[5];
4404 t = TP_EC_THERMAL_TMP0;
4406 switch (thermal_read_mode) {
4407 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4408 case TPACPI_THERMAL_TPEC_16:
4409 if (idx >= 8 && idx <= 15) {
4410 t = TP_EC_THERMAL_TMP8;
4411 idx -= 8;
4413 /* fallthrough */
4414 #endif
4415 case TPACPI_THERMAL_TPEC_8:
4416 if (idx <= 7) {
4417 if (!acpi_ec_read(t + idx, &tmp))
4418 return -EIO;
4419 *value = tmp * 1000;
4420 return 0;
4422 break;
4424 case TPACPI_THERMAL_ACPI_UPDT:
4425 if (idx <= 7) {
4426 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4427 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4428 return -EIO;
4429 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4430 return -EIO;
4431 *value = (t - 2732) * 100;
4432 return 0;
4434 break;
4436 case TPACPI_THERMAL_ACPI_TMP07:
4437 if (idx <= 7) {
4438 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4439 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4440 return -EIO;
4441 if (t > 127 || t < -127)
4442 t = TP_EC_THERMAL_TMP_NA;
4443 *value = t * 1000;
4444 return 0;
4446 break;
4448 case TPACPI_THERMAL_NONE:
4449 default:
4450 return -ENOSYS;
4453 return -EINVAL;
4456 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4458 int res, i;
4459 int n;
4461 n = 8;
4462 i = 0;
4464 if (!s)
4465 return -EINVAL;
4467 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4468 n = 16;
4470 for (i = 0 ; i < n; i++) {
4471 res = thermal_get_sensor(i, &s->temp[i]);
4472 if (res)
4473 return res;
4476 return n;
4479 /* sysfs temp##_input -------------------------------------------------- */
4481 static ssize_t thermal_temp_input_show(struct device *dev,
4482 struct device_attribute *attr,
4483 char *buf)
4485 struct sensor_device_attribute *sensor_attr =
4486 to_sensor_dev_attr(attr);
4487 int idx = sensor_attr->index;
4488 s32 value;
4489 int res;
4491 res = thermal_get_sensor(idx, &value);
4492 if (res)
4493 return res;
4494 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4495 return -ENXIO;
4497 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4500 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
4501 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4502 thermal_temp_input_show, NULL, _idxB)
4504 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4505 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4506 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4507 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4508 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4509 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4510 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4511 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4512 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4513 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4514 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4515 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4516 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4517 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4518 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4519 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4520 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4523 #define THERMAL_ATTRS(X) \
4524 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4526 static struct attribute *thermal_temp_input_attr[] = {
4527 THERMAL_ATTRS(8),
4528 THERMAL_ATTRS(9),
4529 THERMAL_ATTRS(10),
4530 THERMAL_ATTRS(11),
4531 THERMAL_ATTRS(12),
4532 THERMAL_ATTRS(13),
4533 THERMAL_ATTRS(14),
4534 THERMAL_ATTRS(15),
4535 THERMAL_ATTRS(0),
4536 THERMAL_ATTRS(1),
4537 THERMAL_ATTRS(2),
4538 THERMAL_ATTRS(3),
4539 THERMAL_ATTRS(4),
4540 THERMAL_ATTRS(5),
4541 THERMAL_ATTRS(6),
4542 THERMAL_ATTRS(7),
4543 NULL
4546 static const struct attribute_group thermal_temp_input16_group = {
4547 .attrs = thermal_temp_input_attr
4550 static const struct attribute_group thermal_temp_input8_group = {
4551 .attrs = &thermal_temp_input_attr[8]
4554 #undef THERMAL_SENSOR_ATTR_TEMP
4555 #undef THERMAL_ATTRS
4557 /* --------------------------------------------------------------------- */
4559 static int __init thermal_init(struct ibm_init_struct *iibm)
4561 u8 t, ta1, ta2;
4562 int i;
4563 int acpi_tmp7;
4564 int res;
4566 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4568 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
4570 if (thinkpad_id.ec_model) {
4572 * Direct EC access mode: sensors at registers
4573 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4574 * non-implemented, thermal sensors return 0x80 when
4575 * not available
4578 ta1 = ta2 = 0;
4579 for (i = 0; i < 8; i++) {
4580 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
4581 ta1 |= t;
4582 } else {
4583 ta1 = 0;
4584 break;
4586 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
4587 ta2 |= t;
4588 } else {
4589 ta1 = 0;
4590 break;
4593 if (ta1 == 0) {
4594 /* This is sheer paranoia, but we handle it anyway */
4595 if (acpi_tmp7) {
4596 printk(TPACPI_ERR
4597 "ThinkPad ACPI EC access misbehaving, "
4598 "falling back to ACPI TMPx access "
4599 "mode\n");
4600 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4601 } else {
4602 printk(TPACPI_ERR
4603 "ThinkPad ACPI EC access misbehaving, "
4604 "disabling thermal sensors access\n");
4605 thermal_read_mode = TPACPI_THERMAL_NONE;
4607 } else {
4608 thermal_read_mode =
4609 (ta2 != 0) ?
4610 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
4612 } else if (acpi_tmp7) {
4613 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4614 /* 600e/x, 770e, 770x */
4615 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
4616 } else {
4617 /* Standard ACPI TMPx access, max 8 sensors */
4618 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
4620 } else {
4621 /* temperatures not supported on 570, G4x, R30, R31, R32 */
4622 thermal_read_mode = TPACPI_THERMAL_NONE;
4625 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4626 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4627 thermal_read_mode);
4629 switch (thermal_read_mode) {
4630 case TPACPI_THERMAL_TPEC_16:
4631 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4632 &thermal_temp_input16_group);
4633 if (res)
4634 return res;
4635 break;
4636 case TPACPI_THERMAL_TPEC_8:
4637 case TPACPI_THERMAL_ACPI_TMP07:
4638 case TPACPI_THERMAL_ACPI_UPDT:
4639 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
4640 &thermal_temp_input8_group);
4641 if (res)
4642 return res;
4643 break;
4644 case TPACPI_THERMAL_NONE:
4645 default:
4646 return 1;
4649 return 0;
4652 static void thermal_exit(void)
4654 switch (thermal_read_mode) {
4655 case TPACPI_THERMAL_TPEC_16:
4656 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4657 &thermal_temp_input16_group);
4658 break;
4659 case TPACPI_THERMAL_TPEC_8:
4660 case TPACPI_THERMAL_ACPI_TMP07:
4661 case TPACPI_THERMAL_ACPI_UPDT:
4662 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
4663 &thermal_temp_input16_group);
4664 break;
4665 case TPACPI_THERMAL_NONE:
4666 default:
4667 break;
4671 static int thermal_read(char *p)
4673 int len = 0;
4674 int n, i;
4675 struct ibm_thermal_sensors_struct t;
4677 n = thermal_get_sensors(&t);
4678 if (unlikely(n < 0))
4679 return n;
4681 len += sprintf(p + len, "temperatures:\t");
4683 if (n > 0) {
4684 for (i = 0; i < (n - 1); i++)
4685 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4686 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4687 } else
4688 len += sprintf(p + len, "not supported\n");
4690 return len;
4693 static struct ibm_struct thermal_driver_data = {
4694 .name = "thermal",
4695 .read = thermal_read,
4696 .exit = thermal_exit,
4699 /*************************************************************************
4700 * EC Dump subdriver
4703 static u8 ecdump_regs[256];
4705 static int ecdump_read(char *p)
4707 int len = 0;
4708 int i, j;
4709 u8 v;
4711 len += sprintf(p + len, "EC "
4712 " +00 +01 +02 +03 +04 +05 +06 +07"
4713 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4714 for (i = 0; i < 256; i += 16) {
4715 len += sprintf(p + len, "EC 0x%02x:", i);
4716 for (j = 0; j < 16; j++) {
4717 if (!acpi_ec_read(i + j, &v))
4718 break;
4719 if (v != ecdump_regs[i + j])
4720 len += sprintf(p + len, " *%02x", v);
4721 else
4722 len += sprintf(p + len, " %02x", v);
4723 ecdump_regs[i + j] = v;
4725 len += sprintf(p + len, "\n");
4726 if (j != 16)
4727 break;
4730 /* These are way too dangerous to advertise openly... */
4731 #if 0
4732 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4733 " (<offset> is 00-ff, <value> is 00-ff)\n");
4734 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4735 " (<offset> is 00-ff, <value> is 0-255)\n");
4736 #endif
4737 return len;
4740 static int ecdump_write(char *buf)
4742 char *cmd;
4743 int i, v;
4745 while ((cmd = next_cmd(&buf))) {
4746 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4747 /* i and v set */
4748 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4749 /* i and v set */
4750 } else
4751 return -EINVAL;
4752 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4753 if (!acpi_ec_write(i, v))
4754 return -EIO;
4755 } else
4756 return -EINVAL;
4759 return 0;
4762 static struct ibm_struct ecdump_driver_data = {
4763 .name = "ecdump",
4764 .read = ecdump_read,
4765 .write = ecdump_write,
4766 .flags.experimental = 1,
4769 /*************************************************************************
4770 * Backlight/brightness subdriver
4773 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4775 enum {
4776 TP_EC_BACKLIGHT = 0x31,
4778 /* TP_EC_BACKLIGHT bitmasks */
4779 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4780 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4781 TP_EC_BACKLIGHT_MAPSW = 0x20,
4784 static struct backlight_device *ibm_backlight_device;
4785 static int brightness_mode;
4786 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4788 static struct mutex brightness_mutex;
4791 * ThinkPads can read brightness from two places: EC 0x31, or
4792 * CMOS NVRAM byte 0x5E, bits 0-3.
4794 * EC 0x31 has the following layout
4795 * Bit 7: unknown function
4796 * Bit 6: unknown function
4797 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4798 * Bit 4: must be set to zero to avoid problems
4799 * Bit 3-0: backlight brightness level
4801 * brightness_get_raw returns status data in the EC 0x31 layout
4803 static int brightness_get_raw(int *status)
4805 u8 lec = 0, lcmos = 0, level = 0;
4807 if (brightness_mode & 1) {
4808 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
4809 return -EIO;
4810 level = lec & TP_EC_BACKLIGHT_LVLMSK;
4812 if (brightness_mode & 2) {
4813 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4814 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4815 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4816 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4817 level = lcmos;
4820 if (brightness_mode == 3) {
4821 *status = lec; /* Prefer EC, CMOS is just a backing store */
4822 lec &= TP_EC_BACKLIGHT_LVLMSK;
4823 if (lec == lcmos)
4824 tp_warned.bright_cmos_ec_unsync = 0;
4825 else {
4826 if (!tp_warned.bright_cmos_ec_unsync) {
4827 printk(TPACPI_ERR
4828 "CMOS NVRAM (%u) and EC (%u) do not "
4829 "agree on display brightness level\n",
4830 (unsigned int) lcmos,
4831 (unsigned int) lec);
4832 tp_warned.bright_cmos_ec_unsync = 1;
4834 return -EIO;
4836 } else {
4837 *status = level;
4840 return 0;
4843 /* May return EINTR which can always be mapped to ERESTARTSYS */
4844 static int brightness_set(int value)
4846 int cmos_cmd, inc, i, res;
4847 int current_value;
4848 int command_bits;
4850 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4851 value < 0)
4852 return -EINVAL;
4854 res = mutex_lock_interruptible(&brightness_mutex);
4855 if (res < 0)
4856 return res;
4858 res = brightness_get_raw(&current_value);
4859 if (res < 0)
4860 goto errout;
4862 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4863 current_value &= TP_EC_BACKLIGHT_LVLMSK;
4865 cmos_cmd = value > current_value ?
4866 TP_CMOS_BRIGHTNESS_UP :
4867 TP_CMOS_BRIGHTNESS_DOWN;
4868 inc = (value > current_value)? 1 : -1;
4870 res = 0;
4871 for (i = current_value; i != value; i += inc) {
4872 if ((brightness_mode & 2) &&
4873 issue_thinkpad_cmos_command(cmos_cmd)) {
4874 res = -EIO;
4875 goto errout;
4877 if ((brightness_mode & 1) &&
4878 !acpi_ec_write(TP_EC_BACKLIGHT,
4879 (i + inc) | command_bits)) {
4880 res = -EIO;
4881 goto errout;;
4885 errout:
4886 mutex_unlock(&brightness_mutex);
4887 return res;
4890 /* sysfs backlight class ----------------------------------------------- */
4892 static int brightness_update_status(struct backlight_device *bd)
4894 /* it is the backlight class's job (caller) to handle
4895 * EINTR and other errors properly */
4896 return brightness_set(
4897 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4898 bd->props.power == FB_BLANK_UNBLANK) ?
4899 bd->props.brightness : 0);
4902 static int brightness_get(struct backlight_device *bd)
4904 int status, res;
4906 res = brightness_get_raw(&status);
4907 if (res < 0)
4908 return 0; /* FIXME: teach backlight about error handling */
4910 return status & TP_EC_BACKLIGHT_LVLMSK;
4913 static struct backlight_ops ibm_backlight_data = {
4914 .get_brightness = brightness_get,
4915 .update_status = brightness_update_status,
4918 /* --------------------------------------------------------------------- */
4920 static int __init brightness_init(struct ibm_init_struct *iibm)
4922 int b;
4924 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4926 mutex_init(&brightness_mutex);
4929 * We always attempt to detect acpi support, so as to switch
4930 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4931 * going to publish a backlight interface
4933 b = tpacpi_check_std_acpi_brightness_support();
4934 if (b > 0) {
4936 if (acpi_video_backlight_support()) {
4937 if (brightness_enable > 1) {
4938 printk(TPACPI_NOTICE
4939 "Standard ACPI backlight interface "
4940 "available, not loading native one.\n");
4941 return 1;
4942 } else if (brightness_enable == 1) {
4943 printk(TPACPI_NOTICE
4944 "Backlight control force enabled, even if standard "
4945 "ACPI backlight interface is available\n");
4947 } else {
4948 if (brightness_enable > 1) {
4949 printk(TPACPI_NOTICE
4950 "Standard ACPI backlight interface not "
4951 "available, thinkpad_acpi native "
4952 "brightness control enabled\n");
4957 if (!brightness_enable) {
4958 dbg_printk(TPACPI_DBG_INIT,
4959 "brightness support disabled by "
4960 "module parameter\n");
4961 return 1;
4964 if (b > 16) {
4965 printk(TPACPI_ERR
4966 "Unsupported brightness interface, "
4967 "please contact %s\n", TPACPI_MAIL);
4968 return 1;
4970 if (b == 16)
4971 tp_features.bright_16levels = 1;
4973 if (!brightness_mode) {
4974 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4975 brightness_mode = 2;
4976 else
4977 brightness_mode = 3;
4979 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4980 brightness_mode);
4983 if (brightness_mode > 3)
4984 return -EINVAL;
4986 if (brightness_get_raw(&b) < 0)
4987 return 1;
4989 if (tp_features.bright_16levels)
4990 printk(TPACPI_INFO
4991 "detected a 16-level brightness capable ThinkPad\n");
4993 ibm_backlight_device = backlight_device_register(
4994 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4995 &ibm_backlight_data);
4996 if (IS_ERR(ibm_backlight_device)) {
4997 printk(TPACPI_ERR "Could not register backlight device\n");
4998 return PTR_ERR(ibm_backlight_device);
5000 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5002 ibm_backlight_device->props.max_brightness =
5003 (tp_features.bright_16levels)? 15 : 7;
5004 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5005 backlight_update_status(ibm_backlight_device);
5007 return 0;
5010 static void brightness_exit(void)
5012 if (ibm_backlight_device) {
5013 vdbg_printk(TPACPI_DBG_EXIT,
5014 "calling backlight_device_unregister()\n");
5015 backlight_device_unregister(ibm_backlight_device);
5019 static int brightness_read(char *p)
5021 int len = 0;
5022 int level;
5024 level = brightness_get(NULL);
5025 if (level < 0) {
5026 len += sprintf(p + len, "level:\t\tunreadable\n");
5027 } else {
5028 len += sprintf(p + len, "level:\t\t%d\n", level);
5029 len += sprintf(p + len, "commands:\tup, down\n");
5030 len += sprintf(p + len, "commands:\tlevel <level>"
5031 " (<level> is 0-%d)\n",
5032 (tp_features.bright_16levels) ? 15 : 7);
5035 return len;
5038 static int brightness_write(char *buf)
5040 int level;
5041 int rc;
5042 char *cmd;
5043 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5045 level = brightness_get(NULL);
5046 if (level < 0)
5047 return level;
5049 while ((cmd = next_cmd(&buf))) {
5050 if (strlencmp(cmd, "up") == 0) {
5051 if (level < max_level)
5052 level++;
5053 } else if (strlencmp(cmd, "down") == 0) {
5054 if (level > 0)
5055 level--;
5056 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5057 level >= 0 && level <= max_level) {
5058 /* new level set */
5059 } else
5060 return -EINVAL;
5064 * Now we know what the final level should be, so we try to set it.
5065 * Doing it this way makes the syscall restartable in case of EINTR
5067 rc = brightness_set(level);
5068 return (rc == -EINTR)? ERESTARTSYS : rc;
5071 static struct ibm_struct brightness_driver_data = {
5072 .name = "brightness",
5073 .read = brightness_read,
5074 .write = brightness_write,
5075 .exit = brightness_exit,
5078 /*************************************************************************
5079 * Volume subdriver
5082 static int volume_offset = 0x30;
5084 static int volume_read(char *p)
5086 int len = 0;
5087 u8 level;
5089 if (!acpi_ec_read(volume_offset, &level)) {
5090 len += sprintf(p + len, "level:\t\tunreadable\n");
5091 } else {
5092 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5093 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5094 len += sprintf(p + len, "commands:\tup, down, mute\n");
5095 len += sprintf(p + len, "commands:\tlevel <level>"
5096 " (<level> is 0-15)\n");
5099 return len;
5102 static int volume_write(char *buf)
5104 int cmos_cmd, inc, i;
5105 u8 level, mute;
5106 int new_level, new_mute;
5107 char *cmd;
5109 while ((cmd = next_cmd(&buf))) {
5110 if (!acpi_ec_read(volume_offset, &level))
5111 return -EIO;
5112 new_mute = mute = level & 0x40;
5113 new_level = level = level & 0xf;
5115 if (strlencmp(cmd, "up") == 0) {
5116 if (mute)
5117 new_mute = 0;
5118 else
5119 new_level = level == 15 ? 15 : level + 1;
5120 } else if (strlencmp(cmd, "down") == 0) {
5121 if (mute)
5122 new_mute = 0;
5123 else
5124 new_level = level == 0 ? 0 : level - 1;
5125 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5126 new_level >= 0 && new_level <= 15) {
5127 /* new_level set */
5128 } else if (strlencmp(cmd, "mute") == 0) {
5129 new_mute = 0x40;
5130 } else
5131 return -EINVAL;
5133 if (new_level != level) {
5134 /* mute doesn't change */
5136 cmos_cmd = (new_level > level) ?
5137 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5138 inc = new_level > level ? 1 : -1;
5140 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5141 !acpi_ec_write(volume_offset, level)))
5142 return -EIO;
5144 for (i = level; i != new_level; i += inc)
5145 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5146 !acpi_ec_write(volume_offset, i + inc))
5147 return -EIO;
5149 if (mute &&
5150 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5151 !acpi_ec_write(volume_offset, new_level + mute))) {
5152 return -EIO;
5156 if (new_mute != mute) {
5157 /* level doesn't change */
5159 cmos_cmd = (new_mute) ?
5160 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5162 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5163 !acpi_ec_write(volume_offset, level + new_mute))
5164 return -EIO;
5168 return 0;
5171 static struct ibm_struct volume_driver_data = {
5172 .name = "volume",
5173 .read = volume_read,
5174 .write = volume_write,
5177 /*************************************************************************
5178 * Fan subdriver
5182 * FAN ACCESS MODES
5184 * TPACPI_FAN_RD_ACPI_GFAN:
5185 * ACPI GFAN method: returns fan level
5187 * see TPACPI_FAN_WR_ACPI_SFAN
5188 * EC 0x2f (HFSP) not available if GFAN exists
5190 * TPACPI_FAN_WR_ACPI_SFAN:
5191 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5193 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5194 * it for writing.
5196 * TPACPI_FAN_WR_TPEC:
5197 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5198 * Supported on almost all ThinkPads
5200 * Fan speed changes of any sort (including those caused by the
5201 * disengaged mode) are usually done slowly by the firmware as the
5202 * maximum ammount of fan duty cycle change per second seems to be
5203 * limited.
5205 * Reading is not available if GFAN exists.
5206 * Writing is not available if SFAN exists.
5208 * Bits
5209 * 7 automatic mode engaged;
5210 * (default operation mode of the ThinkPad)
5211 * fan level is ignored in this mode.
5212 * 6 full speed mode (takes precedence over bit 7);
5213 * not available on all thinkpads. May disable
5214 * the tachometer while the fan controller ramps up
5215 * the speed (which can take up to a few *minutes*).
5216 * Speeds up fan to 100% duty-cycle, which is far above
5217 * the standard RPM levels. It is not impossible that
5218 * it could cause hardware damage.
5219 * 5-3 unused in some models. Extra bits for fan level
5220 * in others, but still useless as all values above
5221 * 7 map to the same speed as level 7 in these models.
5222 * 2-0 fan level (0..7 usually)
5223 * 0x00 = stop
5224 * 0x07 = max (set when temperatures critical)
5225 * Some ThinkPads may have other levels, see
5226 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5228 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5229 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5230 * does so, its initial value is meaningless (0x07).
5232 * For firmware bugs, refer to:
5233 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5235 * ----
5237 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5238 * Main fan tachometer reading (in RPM)
5240 * This register is present on all ThinkPads with a new-style EC, and
5241 * it is known not to be present on the A21m/e, and T22, as there is
5242 * something else in offset 0x84 according to the ACPI DSDT. Other
5243 * ThinkPads from this same time period (and earlier) probably lack the
5244 * tachometer as well.
5246 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5247 * was never fixed by IBM to report the EC firmware version string
5248 * probably support the tachometer (like the early X models), so
5249 * detecting it is quite hard. We need more data to know for sure.
5251 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5252 * might result.
5254 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5255 * mode.
5257 * For firmware bugs, refer to:
5258 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5260 * TPACPI_FAN_WR_ACPI_FANS:
5261 * ThinkPad X31, X40, X41. Not available in the X60.
5263 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5264 * high speed. ACPI DSDT seems to map these three speeds to levels
5265 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5266 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5268 * The speeds are stored on handles
5269 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5271 * There are three default speed sets, acessible as handles:
5272 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5274 * ACPI DSDT switches which set is in use depending on various
5275 * factors.
5277 * TPACPI_FAN_WR_TPEC is also available and should be used to
5278 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5279 * but the ACPI tables just mention level 7.
5282 enum { /* Fan control constants */
5283 fan_status_offset = 0x2f, /* EC register 0x2f */
5284 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5285 * 0x84 must be read before 0x85 */
5287 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5288 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5290 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5293 enum fan_status_access_mode {
5294 TPACPI_FAN_NONE = 0, /* No fan status or control */
5295 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5296 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5299 enum fan_control_access_mode {
5300 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5301 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5302 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5303 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5306 enum fan_control_commands {
5307 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5308 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5309 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5310 * and also watchdog cmd */
5313 static int fan_control_allowed;
5315 static enum fan_status_access_mode fan_status_access_mode;
5316 static enum fan_control_access_mode fan_control_access_mode;
5317 static enum fan_control_commands fan_control_commands;
5319 static u8 fan_control_initial_status;
5320 static u8 fan_control_desired_level;
5321 static u8 fan_control_resume_level;
5322 static int fan_watchdog_maxinterval;
5324 static struct mutex fan_mutex;
5326 static void fan_watchdog_fire(struct work_struct *ignored);
5327 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5329 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5330 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5331 "\\FSPD", /* 600e/x, 770e, 770x */
5332 ); /* all others */
5333 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5334 "JFNS", /* 770x-JL */
5335 ); /* all others */
5338 * Call with fan_mutex held
5340 static void fan_update_desired_level(u8 status)
5342 if ((status &
5343 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5344 if (status > 7)
5345 fan_control_desired_level = 7;
5346 else
5347 fan_control_desired_level = status;
5351 static int fan_get_status(u8 *status)
5353 u8 s;
5355 /* TODO:
5356 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5358 switch (fan_status_access_mode) {
5359 case TPACPI_FAN_RD_ACPI_GFAN:
5360 /* 570, 600e/x, 770e, 770x */
5362 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5363 return -EIO;
5365 if (likely(status))
5366 *status = s & 0x07;
5368 break;
5370 case TPACPI_FAN_RD_TPEC:
5371 /* all except 570, 600e/x, 770e, 770x */
5372 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5373 return -EIO;
5375 if (likely(status))
5376 *status = s;
5378 break;
5380 default:
5381 return -ENXIO;
5384 return 0;
5387 static int fan_get_status_safe(u8 *status)
5389 int rc;
5390 u8 s;
5392 if (mutex_lock_interruptible(&fan_mutex))
5393 return -ERESTARTSYS;
5394 rc = fan_get_status(&s);
5395 if (!rc)
5396 fan_update_desired_level(s);
5397 mutex_unlock(&fan_mutex);
5399 if (status)
5400 *status = s;
5402 return rc;
5405 static int fan_get_speed(unsigned int *speed)
5407 u8 hi, lo;
5409 switch (fan_status_access_mode) {
5410 case TPACPI_FAN_RD_TPEC:
5411 /* all except 570, 600e/x, 770e, 770x */
5412 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5413 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5414 return -EIO;
5416 if (likely(speed))
5417 *speed = (hi << 8) | lo;
5419 break;
5421 default:
5422 return -ENXIO;
5425 return 0;
5428 static int fan_set_level(int level)
5430 if (!fan_control_allowed)
5431 return -EPERM;
5433 switch (fan_control_access_mode) {
5434 case TPACPI_FAN_WR_ACPI_SFAN:
5435 if (level >= 0 && level <= 7) {
5436 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5437 return -EIO;
5438 } else
5439 return -EINVAL;
5440 break;
5442 case TPACPI_FAN_WR_ACPI_FANS:
5443 case TPACPI_FAN_WR_TPEC:
5444 if (!(level & TP_EC_FAN_AUTO) &&
5445 !(level & TP_EC_FAN_FULLSPEED) &&
5446 ((level < 0) || (level > 7)))
5447 return -EINVAL;
5449 /* safety net should the EC not support AUTO
5450 * or FULLSPEED mode bits and just ignore them */
5451 if (level & TP_EC_FAN_FULLSPEED)
5452 level |= 7; /* safety min speed 7 */
5453 else if (level & TP_EC_FAN_AUTO)
5454 level |= 4; /* safety min speed 4 */
5456 if (!acpi_ec_write(fan_status_offset, level))
5457 return -EIO;
5458 else
5459 tp_features.fan_ctrl_status_undef = 0;
5460 break;
5462 default:
5463 return -ENXIO;
5465 return 0;
5468 static int fan_set_level_safe(int level)
5470 int rc;
5472 if (!fan_control_allowed)
5473 return -EPERM;
5475 if (mutex_lock_interruptible(&fan_mutex))
5476 return -ERESTARTSYS;
5478 if (level == TPACPI_FAN_LAST_LEVEL)
5479 level = fan_control_desired_level;
5481 rc = fan_set_level(level);
5482 if (!rc)
5483 fan_update_desired_level(level);
5485 mutex_unlock(&fan_mutex);
5486 return rc;
5489 static int fan_set_enable(void)
5491 u8 s;
5492 int rc;
5494 if (!fan_control_allowed)
5495 return -EPERM;
5497 if (mutex_lock_interruptible(&fan_mutex))
5498 return -ERESTARTSYS;
5500 switch (fan_control_access_mode) {
5501 case TPACPI_FAN_WR_ACPI_FANS:
5502 case TPACPI_FAN_WR_TPEC:
5503 rc = fan_get_status(&s);
5504 if (rc < 0)
5505 break;
5507 /* Don't go out of emergency fan mode */
5508 if (s != 7) {
5509 s &= 0x07;
5510 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5513 if (!acpi_ec_write(fan_status_offset, s))
5514 rc = -EIO;
5515 else {
5516 tp_features.fan_ctrl_status_undef = 0;
5517 rc = 0;
5519 break;
5521 case TPACPI_FAN_WR_ACPI_SFAN:
5522 rc = fan_get_status(&s);
5523 if (rc < 0)
5524 break;
5526 s &= 0x07;
5528 /* Set fan to at least level 4 */
5529 s |= 4;
5531 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
5532 rc = -EIO;
5533 else
5534 rc = 0;
5535 break;
5537 default:
5538 rc = -ENXIO;
5541 mutex_unlock(&fan_mutex);
5542 return rc;
5545 static int fan_set_disable(void)
5547 int rc;
5549 if (!fan_control_allowed)
5550 return -EPERM;
5552 if (mutex_lock_interruptible(&fan_mutex))
5553 return -ERESTARTSYS;
5555 rc = 0;
5556 switch (fan_control_access_mode) {
5557 case TPACPI_FAN_WR_ACPI_FANS:
5558 case TPACPI_FAN_WR_TPEC:
5559 if (!acpi_ec_write(fan_status_offset, 0x00))
5560 rc = -EIO;
5561 else {
5562 fan_control_desired_level = 0;
5563 tp_features.fan_ctrl_status_undef = 0;
5565 break;
5567 case TPACPI_FAN_WR_ACPI_SFAN:
5568 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5569 rc = -EIO;
5570 else
5571 fan_control_desired_level = 0;
5572 break;
5574 default:
5575 rc = -ENXIO;
5579 mutex_unlock(&fan_mutex);
5580 return rc;
5583 static int fan_set_speed(int speed)
5585 int rc;
5587 if (!fan_control_allowed)
5588 return -EPERM;
5590 if (mutex_lock_interruptible(&fan_mutex))
5591 return -ERESTARTSYS;
5593 rc = 0;
5594 switch (fan_control_access_mode) {
5595 case TPACPI_FAN_WR_ACPI_FANS:
5596 if (speed >= 0 && speed <= 65535) {
5597 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5598 speed, speed, speed))
5599 rc = -EIO;
5600 } else
5601 rc = -EINVAL;
5602 break;
5604 default:
5605 rc = -ENXIO;
5608 mutex_unlock(&fan_mutex);
5609 return rc;
5612 static void fan_watchdog_reset(void)
5614 static int fan_watchdog_active;
5616 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5617 return;
5619 if (fan_watchdog_active)
5620 cancel_delayed_work(&fan_watchdog_task);
5622 if (fan_watchdog_maxinterval > 0 &&
5623 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5624 fan_watchdog_active = 1;
5625 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
5626 msecs_to_jiffies(fan_watchdog_maxinterval
5627 * 1000))) {
5628 printk(TPACPI_ERR
5629 "failed to queue the fan watchdog, "
5630 "watchdog will not trigger\n");
5632 } else
5633 fan_watchdog_active = 0;
5636 static void fan_watchdog_fire(struct work_struct *ignored)
5638 int rc;
5640 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5641 return;
5643 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
5644 rc = fan_set_enable();
5645 if (rc < 0) {
5646 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
5647 "will try again later...\n", -rc);
5648 /* reschedule for later */
5649 fan_watchdog_reset();
5654 * SYSFS fan layout: hwmon compatible (device)
5656 * pwm*_enable:
5657 * 0: "disengaged" mode
5658 * 1: manual mode
5659 * 2: native EC "auto" mode (recommended, hardware default)
5661 * pwm*: set speed in manual mode, ignored otherwise.
5662 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5663 * interpolation.
5665 * fan*_input: tachometer reading, RPM
5668 * SYSFS fan layout: extensions
5670 * fan_watchdog (driver):
5671 * fan watchdog interval in seconds, 0 disables (default), max 120
5674 /* sysfs fan pwm1_enable ----------------------------------------------- */
5675 static ssize_t fan_pwm1_enable_show(struct device *dev,
5676 struct device_attribute *attr,
5677 char *buf)
5679 int res, mode;
5680 u8 status;
5682 res = fan_get_status_safe(&status);
5683 if (res)
5684 return res;
5686 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5687 if (status != fan_control_initial_status) {
5688 tp_features.fan_ctrl_status_undef = 0;
5689 } else {
5690 /* Return most likely status. In fact, it
5691 * might be the only possible status */
5692 status = TP_EC_FAN_AUTO;
5696 if (status & TP_EC_FAN_FULLSPEED) {
5697 mode = 0;
5698 } else if (status & TP_EC_FAN_AUTO) {
5699 mode = 2;
5700 } else
5701 mode = 1;
5703 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5706 static ssize_t fan_pwm1_enable_store(struct device *dev,
5707 struct device_attribute *attr,
5708 const char *buf, size_t count)
5710 unsigned long t;
5711 int res, level;
5713 if (parse_strtoul(buf, 2, &t))
5714 return -EINVAL;
5716 switch (t) {
5717 case 0:
5718 level = TP_EC_FAN_FULLSPEED;
5719 break;
5720 case 1:
5721 level = TPACPI_FAN_LAST_LEVEL;
5722 break;
5723 case 2:
5724 level = TP_EC_FAN_AUTO;
5725 break;
5726 case 3:
5727 /* reserved for software-controlled auto mode */
5728 return -ENOSYS;
5729 default:
5730 return -EINVAL;
5733 res = fan_set_level_safe(level);
5734 if (res == -ENXIO)
5735 return -EINVAL;
5736 else if (res < 0)
5737 return res;
5739 fan_watchdog_reset();
5741 return count;
5744 static struct device_attribute dev_attr_fan_pwm1_enable =
5745 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5746 fan_pwm1_enable_show, fan_pwm1_enable_store);
5748 /* sysfs fan pwm1 ------------------------------------------------------ */
5749 static ssize_t fan_pwm1_show(struct device *dev,
5750 struct device_attribute *attr,
5751 char *buf)
5753 int res;
5754 u8 status;
5756 res = fan_get_status_safe(&status);
5757 if (res)
5758 return res;
5760 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5761 if (status != fan_control_initial_status) {
5762 tp_features.fan_ctrl_status_undef = 0;
5763 } else {
5764 status = TP_EC_FAN_AUTO;
5768 if ((status &
5769 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5770 status = fan_control_desired_level;
5772 if (status > 7)
5773 status = 7;
5775 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5778 static ssize_t fan_pwm1_store(struct device *dev,
5779 struct device_attribute *attr,
5780 const char *buf, size_t count)
5782 unsigned long s;
5783 int rc;
5784 u8 status, newlevel;
5786 if (parse_strtoul(buf, 255, &s))
5787 return -EINVAL;
5789 /* scale down from 0-255 to 0-7 */
5790 newlevel = (s >> 5) & 0x07;
5792 if (mutex_lock_interruptible(&fan_mutex))
5793 return -ERESTARTSYS;
5795 rc = fan_get_status(&status);
5796 if (!rc && (status &
5797 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5798 rc = fan_set_level(newlevel);
5799 if (rc == -ENXIO)
5800 rc = -EINVAL;
5801 else if (!rc) {
5802 fan_update_desired_level(newlevel);
5803 fan_watchdog_reset();
5807 mutex_unlock(&fan_mutex);
5808 return (rc)? rc : count;
5811 static struct device_attribute dev_attr_fan_pwm1 =
5812 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5813 fan_pwm1_show, fan_pwm1_store);
5815 /* sysfs fan fan1_input ------------------------------------------------ */
5816 static ssize_t fan_fan1_input_show(struct device *dev,
5817 struct device_attribute *attr,
5818 char *buf)
5820 int res;
5821 unsigned int speed;
5823 res = fan_get_speed(&speed);
5824 if (res < 0)
5825 return res;
5827 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5830 static struct device_attribute dev_attr_fan_fan1_input =
5831 __ATTR(fan1_input, S_IRUGO,
5832 fan_fan1_input_show, NULL);
5834 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
5835 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5836 char *buf)
5838 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5841 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5842 const char *buf, size_t count)
5844 unsigned long t;
5846 if (parse_strtoul(buf, 120, &t))
5847 return -EINVAL;
5849 if (!fan_control_allowed)
5850 return -EPERM;
5852 fan_watchdog_maxinterval = t;
5853 fan_watchdog_reset();
5855 return count;
5858 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5859 fan_fan_watchdog_show, fan_fan_watchdog_store);
5861 /* --------------------------------------------------------------------- */
5862 static struct attribute *fan_attributes[] = {
5863 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5864 &dev_attr_fan_fan1_input.attr,
5865 NULL
5868 static const struct attribute_group fan_attr_group = {
5869 .attrs = fan_attributes,
5872 static int __init fan_init(struct ibm_init_struct *iibm)
5874 int rc;
5876 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5878 mutex_init(&fan_mutex);
5879 fan_status_access_mode = TPACPI_FAN_NONE;
5880 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5881 fan_control_commands = 0;
5882 fan_watchdog_maxinterval = 0;
5883 tp_features.fan_ctrl_status_undef = 0;
5884 fan_control_desired_level = 7;
5886 TPACPI_ACPIHANDLE_INIT(fans);
5887 TPACPI_ACPIHANDLE_INIT(gfan);
5888 TPACPI_ACPIHANDLE_INIT(sfan);
5890 if (gfan_handle) {
5891 /* 570, 600e/x, 770e, 770x */
5892 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
5893 } else {
5894 /* all other ThinkPads: note that even old-style
5895 * ThinkPad ECs supports the fan control register */
5896 if (likely(acpi_ec_read(fan_status_offset,
5897 &fan_control_initial_status))) {
5898 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
5900 /* In some ThinkPads, neither the EC nor the ACPI
5901 * DSDT initialize the fan status, and it ends up
5902 * being set to 0x07 when it *could* be either
5903 * 0x07 or 0x80.
5905 * Enable for TP-1Y (T43), TP-78 (R51e),
5906 * TP-76 (R52), TP-70 (T43, R52), which are known
5907 * to be buggy. */
5908 if (fan_control_initial_status == 0x07) {
5909 switch (thinkpad_id.ec_model) {
5910 case 0x5931: /* TP-1Y */
5911 case 0x3837: /* TP-78 */
5912 case 0x3637: /* TP-76 */
5913 case 0x3037: /* TP-70 */
5914 printk(TPACPI_NOTICE
5915 "fan_init: initial fan status "
5916 "is unknown, assuming it is "
5917 "in auto mode\n");
5918 tp_features.fan_ctrl_status_undef = 1;
5922 } else {
5923 printk(TPACPI_ERR
5924 "ThinkPad ACPI EC access misbehaving, "
5925 "fan status and control unavailable\n");
5926 return 1;
5930 if (sfan_handle) {
5931 /* 570, 770x-JL */
5932 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
5933 fan_control_commands |=
5934 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
5935 } else {
5936 if (!gfan_handle) {
5937 /* gfan without sfan means no fan control */
5938 /* all other models implement TP EC 0x2f control */
5940 if (fans_handle) {
5941 /* X31, X40, X41 */
5942 fan_control_access_mode =
5943 TPACPI_FAN_WR_ACPI_FANS;
5944 fan_control_commands |=
5945 TPACPI_FAN_CMD_SPEED |
5946 TPACPI_FAN_CMD_LEVEL |
5947 TPACPI_FAN_CMD_ENABLE;
5948 } else {
5949 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
5950 fan_control_commands |=
5951 TPACPI_FAN_CMD_LEVEL |
5952 TPACPI_FAN_CMD_ENABLE;
5957 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5958 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5959 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5960 fan_status_access_mode, fan_control_access_mode);
5962 /* fan control master switch */
5963 if (!fan_control_allowed) {
5964 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5965 fan_control_commands = 0;
5966 dbg_printk(TPACPI_DBG_INIT,
5967 "fan control features disabled by parameter\n");
5970 /* update fan_control_desired_level */
5971 if (fan_status_access_mode != TPACPI_FAN_NONE)
5972 fan_get_status_safe(NULL);
5974 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5975 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
5976 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5977 &fan_attr_group);
5978 if (rc < 0)
5979 return rc;
5981 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
5982 &driver_attr_fan_watchdog);
5983 if (rc < 0) {
5984 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5985 &fan_attr_group);
5986 return rc;
5988 return 0;
5989 } else
5990 return 1;
5993 static void fan_exit(void)
5995 vdbg_printk(TPACPI_DBG_EXIT,
5996 "cancelling any pending fan watchdog tasks\n");
5998 /* FIXME: can we really do this unconditionally? */
5999 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6000 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6001 &driver_attr_fan_watchdog);
6003 cancel_delayed_work(&fan_watchdog_task);
6004 flush_workqueue(tpacpi_wq);
6007 static void fan_suspend(pm_message_t state)
6009 int rc;
6011 if (!fan_control_allowed)
6012 return;
6014 /* Store fan status in cache */
6015 fan_control_resume_level = 0;
6016 rc = fan_get_status_safe(&fan_control_resume_level);
6017 if (rc < 0)
6018 printk(TPACPI_NOTICE
6019 "failed to read fan level for later "
6020 "restore during resume: %d\n", rc);
6022 /* if it is undefined, don't attempt to restore it.
6023 * KEEP THIS LAST */
6024 if (tp_features.fan_ctrl_status_undef)
6025 fan_control_resume_level = 0;
6028 static void fan_resume(void)
6030 u8 current_level = 7;
6031 bool do_set = false;
6032 int rc;
6034 /* DSDT *always* updates status on resume */
6035 tp_features.fan_ctrl_status_undef = 0;
6037 if (!fan_control_allowed ||
6038 !fan_control_resume_level ||
6039 (fan_get_status_safe(&current_level) < 0))
6040 return;
6042 switch (fan_control_access_mode) {
6043 case TPACPI_FAN_WR_ACPI_SFAN:
6044 /* never decrease fan level */
6045 do_set = (fan_control_resume_level > current_level);
6046 break;
6047 case TPACPI_FAN_WR_ACPI_FANS:
6048 case TPACPI_FAN_WR_TPEC:
6049 /* never decrease fan level, scale is:
6050 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6052 * We expect the firmware to set either 7 or AUTO, but we
6053 * handle FULLSPEED out of paranoia.
6055 * So, we can safely only restore FULLSPEED or 7, anything
6056 * else could slow the fan. Restoring AUTO is useless, at
6057 * best that's exactly what the DSDT already set (it is the
6058 * slower it uses).
6060 * Always keep in mind that the DSDT *will* have set the
6061 * fans to what the vendor supposes is the best level. We
6062 * muck with it only to speed the fan up.
6064 if (fan_control_resume_level != 7 &&
6065 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6066 return;
6067 else
6068 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6069 (current_level != fan_control_resume_level);
6070 break;
6071 default:
6072 return;
6074 if (do_set) {
6075 printk(TPACPI_NOTICE
6076 "restoring fan level to 0x%02x\n",
6077 fan_control_resume_level);
6078 rc = fan_set_level_safe(fan_control_resume_level);
6079 if (rc < 0)
6080 printk(TPACPI_NOTICE
6081 "failed to restore fan level: %d\n", rc);
6085 static int fan_read(char *p)
6087 int len = 0;
6088 int rc;
6089 u8 status;
6090 unsigned int speed = 0;
6092 switch (fan_status_access_mode) {
6093 case TPACPI_FAN_RD_ACPI_GFAN:
6094 /* 570, 600e/x, 770e, 770x */
6095 rc = fan_get_status_safe(&status);
6096 if (rc < 0)
6097 return rc;
6099 len += sprintf(p + len, "status:\t\t%s\n"
6100 "level:\t\t%d\n",
6101 (status != 0) ? "enabled" : "disabled", status);
6102 break;
6104 case TPACPI_FAN_RD_TPEC:
6105 /* all except 570, 600e/x, 770e, 770x */
6106 rc = fan_get_status_safe(&status);
6107 if (rc < 0)
6108 return rc;
6110 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6111 if (status != fan_control_initial_status)
6112 tp_features.fan_ctrl_status_undef = 0;
6113 else
6114 /* Return most likely status. In fact, it
6115 * might be the only possible status */
6116 status = TP_EC_FAN_AUTO;
6119 len += sprintf(p + len, "status:\t\t%s\n",
6120 (status != 0) ? "enabled" : "disabled");
6122 rc = fan_get_speed(&speed);
6123 if (rc < 0)
6124 return rc;
6126 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6128 if (status & TP_EC_FAN_FULLSPEED)
6129 /* Disengaged mode takes precedence */
6130 len += sprintf(p + len, "level:\t\tdisengaged\n");
6131 else if (status & TP_EC_FAN_AUTO)
6132 len += sprintf(p + len, "level:\t\tauto\n");
6133 else
6134 len += sprintf(p + len, "level:\t\t%d\n", status);
6135 break;
6137 case TPACPI_FAN_NONE:
6138 default:
6139 len += sprintf(p + len, "status:\t\tnot supported\n");
6142 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6143 len += sprintf(p + len, "commands:\tlevel <level>");
6145 switch (fan_control_access_mode) {
6146 case TPACPI_FAN_WR_ACPI_SFAN:
6147 len += sprintf(p + len, " (<level> is 0-7)\n");
6148 break;
6150 default:
6151 len += sprintf(p + len, " (<level> is 0-7, "
6152 "auto, disengaged, full-speed)\n");
6153 break;
6157 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6158 len += sprintf(p + len, "commands:\tenable, disable\n"
6159 "commands:\twatchdog <timeout> (<timeout> "
6160 "is 0 (off), 1-120 (seconds))\n");
6162 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6163 len += sprintf(p + len, "commands:\tspeed <speed>"
6164 " (<speed> is 0-65535)\n");
6166 return len;
6169 static int fan_write_cmd_level(const char *cmd, int *rc)
6171 int level;
6173 if (strlencmp(cmd, "level auto") == 0)
6174 level = TP_EC_FAN_AUTO;
6175 else if ((strlencmp(cmd, "level disengaged") == 0) |
6176 (strlencmp(cmd, "level full-speed") == 0))
6177 level = TP_EC_FAN_FULLSPEED;
6178 else if (sscanf(cmd, "level %d", &level) != 1)
6179 return 0;
6181 *rc = fan_set_level_safe(level);
6182 if (*rc == -ENXIO)
6183 printk(TPACPI_ERR "level command accepted for unsupported "
6184 "access mode %d", fan_control_access_mode);
6186 return 1;
6189 static int fan_write_cmd_enable(const char *cmd, int *rc)
6191 if (strlencmp(cmd, "enable") != 0)
6192 return 0;
6194 *rc = fan_set_enable();
6195 if (*rc == -ENXIO)
6196 printk(TPACPI_ERR "enable command accepted for unsupported "
6197 "access mode %d", fan_control_access_mode);
6199 return 1;
6202 static int fan_write_cmd_disable(const char *cmd, int *rc)
6204 if (strlencmp(cmd, "disable") != 0)
6205 return 0;
6207 *rc = fan_set_disable();
6208 if (*rc == -ENXIO)
6209 printk(TPACPI_ERR "disable command accepted for unsupported "
6210 "access mode %d", fan_control_access_mode);
6212 return 1;
6215 static int fan_write_cmd_speed(const char *cmd, int *rc)
6217 int speed;
6219 /* TODO:
6220 * Support speed <low> <medium> <high> ? */
6222 if (sscanf(cmd, "speed %d", &speed) != 1)
6223 return 0;
6225 *rc = fan_set_speed(speed);
6226 if (*rc == -ENXIO)
6227 printk(TPACPI_ERR "speed command accepted for unsupported "
6228 "access mode %d", fan_control_access_mode);
6230 return 1;
6233 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6235 int interval;
6237 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6238 return 0;
6240 if (interval < 0 || interval > 120)
6241 *rc = -EINVAL;
6242 else
6243 fan_watchdog_maxinterval = interval;
6245 return 1;
6248 static int fan_write(char *buf)
6250 char *cmd;
6251 int rc = 0;
6253 while (!rc && (cmd = next_cmd(&buf))) {
6254 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6255 fan_write_cmd_level(cmd, &rc)) &&
6256 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6257 (fan_write_cmd_enable(cmd, &rc) ||
6258 fan_write_cmd_disable(cmd, &rc) ||
6259 fan_write_cmd_watchdog(cmd, &rc))) &&
6260 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6261 fan_write_cmd_speed(cmd, &rc))
6263 rc = -EINVAL;
6264 else if (!rc)
6265 fan_watchdog_reset();
6268 return rc;
6271 static struct ibm_struct fan_driver_data = {
6272 .name = "fan",
6273 .read = fan_read,
6274 .write = fan_write,
6275 .exit = fan_exit,
6276 .suspend = fan_suspend,
6277 .resume = fan_resume,
6280 /****************************************************************************
6281 ****************************************************************************
6283 * Infrastructure
6285 ****************************************************************************
6286 ****************************************************************************/
6288 /* sysfs name ---------------------------------------------------------- */
6289 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6290 struct device_attribute *attr,
6291 char *buf)
6293 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6296 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6297 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6299 /* --------------------------------------------------------------------- */
6301 /* /proc support */
6302 static struct proc_dir_entry *proc_dir;
6305 * Module and infrastructure proble, init and exit handling
6308 static int force_load;
6310 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6311 static const char * __init str_supported(int is_supported)
6313 static char text_unsupported[] __initdata = "not supported";
6315 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6317 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6319 static void ibm_exit(struct ibm_struct *ibm)
6321 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6323 list_del_init(&ibm->all_drivers);
6325 if (ibm->flags.acpi_notify_installed) {
6326 dbg_printk(TPACPI_DBG_EXIT,
6327 "%s: acpi_remove_notify_handler\n", ibm->name);
6328 BUG_ON(!ibm->acpi);
6329 acpi_remove_notify_handler(*ibm->acpi->handle,
6330 ibm->acpi->type,
6331 dispatch_acpi_notify);
6332 ibm->flags.acpi_notify_installed = 0;
6333 ibm->flags.acpi_notify_installed = 0;
6336 if (ibm->flags.proc_created) {
6337 dbg_printk(TPACPI_DBG_EXIT,
6338 "%s: remove_proc_entry\n", ibm->name);
6339 remove_proc_entry(ibm->name, proc_dir);
6340 ibm->flags.proc_created = 0;
6343 if (ibm->flags.acpi_driver_registered) {
6344 dbg_printk(TPACPI_DBG_EXIT,
6345 "%s: acpi_bus_unregister_driver\n", ibm->name);
6346 BUG_ON(!ibm->acpi);
6347 acpi_bus_unregister_driver(ibm->acpi->driver);
6348 kfree(ibm->acpi->driver);
6349 ibm->acpi->driver = NULL;
6350 ibm->flags.acpi_driver_registered = 0;
6353 if (ibm->flags.init_called && ibm->exit) {
6354 ibm->exit();
6355 ibm->flags.init_called = 0;
6358 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6361 static int __init ibm_init(struct ibm_init_struct *iibm)
6363 int ret;
6364 struct ibm_struct *ibm = iibm->data;
6365 struct proc_dir_entry *entry;
6367 BUG_ON(ibm == NULL);
6369 INIT_LIST_HEAD(&ibm->all_drivers);
6371 if (ibm->flags.experimental && !experimental)
6372 return 0;
6374 dbg_printk(TPACPI_DBG_INIT,
6375 "probing for %s\n", ibm->name);
6377 if (iibm->init) {
6378 ret = iibm->init(iibm);
6379 if (ret > 0)
6380 return 0; /* probe failed */
6381 if (ret)
6382 return ret;
6384 ibm->flags.init_called = 1;
6387 if (ibm->acpi) {
6388 if (ibm->acpi->hid) {
6389 ret = register_tpacpi_subdriver(ibm);
6390 if (ret)
6391 goto err_out;
6394 if (ibm->acpi->notify) {
6395 ret = setup_acpi_notify(ibm);
6396 if (ret == -ENODEV) {
6397 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6398 ibm->name);
6399 ret = 0;
6400 goto err_out;
6402 if (ret < 0)
6403 goto err_out;
6407 dbg_printk(TPACPI_DBG_INIT,
6408 "%s installed\n", ibm->name);
6410 if (ibm->read) {
6411 entry = create_proc_entry(ibm->name,
6412 S_IFREG | S_IRUGO | S_IWUSR,
6413 proc_dir);
6414 if (!entry) {
6415 printk(TPACPI_ERR "unable to create proc entry %s\n",
6416 ibm->name);
6417 ret = -ENODEV;
6418 goto err_out;
6420 entry->owner = THIS_MODULE;
6421 entry->data = ibm;
6422 entry->read_proc = &dispatch_procfs_read;
6423 if (ibm->write)
6424 entry->write_proc = &dispatch_procfs_write;
6425 ibm->flags.proc_created = 1;
6428 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6430 return 0;
6432 err_out:
6433 dbg_printk(TPACPI_DBG_INIT,
6434 "%s: at error exit path with result %d\n",
6435 ibm->name, ret);
6437 ibm_exit(ibm);
6438 return (ret < 0)? ret : 0;
6441 /* Probing */
6443 /* returns 0 - probe ok, or < 0 - probe error.
6444 * Probe ok doesn't mean thinkpad found.
6445 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
6446 static int __must_check __init get_thinkpad_model_data(
6447 struct thinkpad_id_data *tp)
6449 const struct dmi_device *dev = NULL;
6450 char ec_fw_string[18];
6451 char const *s;
6453 if (!tp)
6454 return -EINVAL;
6456 memset(tp, 0, sizeof(*tp));
6458 if (dmi_name_in_vendors("IBM"))
6459 tp->vendor = PCI_VENDOR_ID_IBM;
6460 else if (dmi_name_in_vendors("LENOVO"))
6461 tp->vendor = PCI_VENDOR_ID_LENOVO;
6462 else
6463 return 0;
6465 s = dmi_get_system_info(DMI_BIOS_VERSION);
6466 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
6467 if (s && !tp->bios_version_str)
6468 return -ENOMEM;
6469 if (!tp->bios_version_str)
6470 return 0;
6471 tp->bios_model = tp->bios_version_str[0]
6472 | (tp->bios_version_str[1] << 8);
6475 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6476 * X32 or newer, all Z series; Some models must have an
6477 * up-to-date BIOS or they will not be detected.
6479 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6481 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
6482 if (sscanf(dev->name,
6483 "IBM ThinkPad Embedded Controller -[%17c",
6484 ec_fw_string) == 1) {
6485 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6486 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
6488 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6489 if (!tp->ec_version_str)
6490 return -ENOMEM;
6491 tp->ec_model = ec_fw_string[0]
6492 | (ec_fw_string[1] << 8);
6493 break;
6497 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
6498 if (s && !strnicmp(s, "ThinkPad", 8)) {
6499 tp->model_str = kstrdup(s, GFP_KERNEL);
6500 if (!tp->model_str)
6501 return -ENOMEM;
6504 s = dmi_get_system_info(DMI_PRODUCT_NAME);
6505 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
6506 if (s && !tp->nummodel_str)
6507 return -ENOMEM;
6509 return 0;
6512 static int __init probe_for_thinkpad(void)
6514 int is_thinkpad;
6516 if (acpi_disabled)
6517 return -ENODEV;
6520 * Non-ancient models have better DMI tagging, but very old models
6521 * don't.
6523 is_thinkpad = (thinkpad_id.model_str != NULL);
6525 /* ec is required because many other handles are relative to it */
6526 TPACPI_ACPIHANDLE_INIT(ec);
6527 if (!ec_handle) {
6528 if (is_thinkpad)
6529 printk(TPACPI_ERR
6530 "Not yet supported ThinkPad detected!\n");
6531 return -ENODEV;
6535 * Risks a regression on very old machines, but reduces potential
6536 * false positives a damn great deal
6538 if (!is_thinkpad)
6539 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
6541 if (!is_thinkpad && !force_load)
6542 return -ENODEV;
6544 return 0;
6548 /* Module init, exit, parameters */
6550 static struct ibm_init_struct ibms_init[] __initdata = {
6552 .init = thinkpad_acpi_driver_init,
6553 .data = &thinkpad_acpi_driver_data,
6556 .init = hotkey_init,
6557 .data = &hotkey_driver_data,
6560 .init = bluetooth_init,
6561 .data = &bluetooth_driver_data,
6564 .init = wan_init,
6565 .data = &wan_driver_data,
6567 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
6569 .init = video_init,
6570 .data = &video_driver_data,
6572 #endif
6574 .init = light_init,
6575 .data = &light_driver_data,
6577 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6579 .init = dock_init,
6580 .data = &dock_driver_data[0],
6583 .init = dock_init2,
6584 .data = &dock_driver_data[1],
6586 #endif
6587 #ifdef CONFIG_THINKPAD_ACPI_BAY
6589 .init = bay_init,
6590 .data = &bay_driver_data,
6592 #endif
6594 .init = cmos_init,
6595 .data = &cmos_driver_data,
6598 .init = led_init,
6599 .data = &led_driver_data,
6602 .init = beep_init,
6603 .data = &beep_driver_data,
6606 .init = thermal_init,
6607 .data = &thermal_driver_data,
6610 .data = &ecdump_driver_data,
6613 .init = brightness_init,
6614 .data = &brightness_driver_data,
6617 .data = &volume_driver_data,
6620 .init = fan_init,
6621 .data = &fan_driver_data,
6625 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6627 unsigned int i;
6628 struct ibm_struct *ibm;
6630 if (!kp || !kp->name || !val)
6631 return -EINVAL;
6633 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6634 ibm = ibms_init[i].data;
6635 WARN_ON(ibm == NULL);
6637 if (!ibm || !ibm->name)
6638 continue;
6640 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6641 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
6642 return -ENOSPC;
6643 strcpy(ibms_init[i].param, val);
6644 strcat(ibms_init[i].param, ",");
6645 return 0;
6649 return -EINVAL;
6652 module_param(experimental, int, 0);
6653 MODULE_PARM_DESC(experimental,
6654 "Enables experimental features when non-zero");
6656 module_param_named(debug, dbg_level, uint, 0);
6657 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
6659 module_param(force_load, bool, 0);
6660 MODULE_PARM_DESC(force_load,
6661 "Attempts to load the driver even on a "
6662 "mis-identified ThinkPad when true");
6664 module_param_named(fan_control, fan_control_allowed, bool, 0);
6665 MODULE_PARM_DESC(fan_control,
6666 "Enables setting fan parameters features when true");
6668 module_param_named(brightness_mode, brightness_mode, int, 0);
6669 MODULE_PARM_DESC(brightness_mode,
6670 "Selects brightness control strategy: "
6671 "0=auto, 1=EC, 2=CMOS, 3=both");
6673 module_param(brightness_enable, uint, 0);
6674 MODULE_PARM_DESC(brightness_enable,
6675 "Enables backlight control when 1, disables when 0");
6677 module_param(hotkey_report_mode, uint, 0);
6678 MODULE_PARM_DESC(hotkey_report_mode,
6679 "used for backwards compatibility with userspace, "
6680 "see documentation");
6682 #define TPACPI_PARAM(feature) \
6683 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
6684 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
6685 "at module load, see documentation")
6687 TPACPI_PARAM(hotkey);
6688 TPACPI_PARAM(bluetooth);
6689 TPACPI_PARAM(video);
6690 TPACPI_PARAM(light);
6691 #ifdef CONFIG_THINKPAD_ACPI_DOCK
6692 TPACPI_PARAM(dock);
6693 #endif
6694 #ifdef CONFIG_THINKPAD_ACPI_BAY
6695 TPACPI_PARAM(bay);
6696 #endif /* CONFIG_THINKPAD_ACPI_BAY */
6697 TPACPI_PARAM(cmos);
6698 TPACPI_PARAM(led);
6699 TPACPI_PARAM(beep);
6700 TPACPI_PARAM(ecdump);
6701 TPACPI_PARAM(brightness);
6702 TPACPI_PARAM(volume);
6703 TPACPI_PARAM(fan);
6705 static void thinkpad_acpi_module_exit(void)
6707 struct ibm_struct *ibm, *itmp;
6709 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6711 list_for_each_entry_safe_reverse(ibm, itmp,
6712 &tpacpi_all_drivers,
6713 all_drivers) {
6714 ibm_exit(ibm);
6717 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6719 if (tpacpi_inputdev) {
6720 if (tp_features.input_device_registered)
6721 input_unregister_device(tpacpi_inputdev);
6722 else
6723 input_free_device(tpacpi_inputdev);
6726 if (tpacpi_hwmon)
6727 hwmon_device_unregister(tpacpi_hwmon);
6729 if (tp_features.sensors_pdev_attrs_registered)
6730 device_remove_file(&tpacpi_sensors_pdev->dev,
6731 &dev_attr_thinkpad_acpi_pdev_name);
6732 if (tpacpi_sensors_pdev)
6733 platform_device_unregister(tpacpi_sensors_pdev);
6734 if (tpacpi_pdev)
6735 platform_device_unregister(tpacpi_pdev);
6737 if (tp_features.sensors_pdrv_attrs_registered)
6738 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6739 if (tp_features.platform_drv_attrs_registered)
6740 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6742 if (tp_features.sensors_pdrv_registered)
6743 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6745 if (tp_features.platform_drv_registered)
6746 platform_driver_unregister(&tpacpi_pdriver);
6748 if (proc_dir)
6749 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
6751 if (tpacpi_wq)
6752 destroy_workqueue(tpacpi_wq);
6754 kfree(thinkpad_id.bios_version_str);
6755 kfree(thinkpad_id.ec_version_str);
6756 kfree(thinkpad_id.model_str);
6760 static int __init thinkpad_acpi_module_init(void)
6762 int ret, i;
6764 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6766 /* Parameter checking */
6767 if (hotkey_report_mode > 2)
6768 return -EINVAL;
6770 /* Driver-level probe */
6772 ret = get_thinkpad_model_data(&thinkpad_id);
6773 if (ret) {
6774 printk(TPACPI_ERR
6775 "unable to get DMI data: %d\n", ret);
6776 thinkpad_acpi_module_exit();
6777 return ret;
6779 ret = probe_for_thinkpad();
6780 if (ret) {
6781 thinkpad_acpi_module_exit();
6782 return ret;
6785 /* Driver initialization */
6787 TPACPI_ACPIHANDLE_INIT(ecrd);
6788 TPACPI_ACPIHANDLE_INIT(ecwr);
6790 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6791 if (!tpacpi_wq) {
6792 thinkpad_acpi_module_exit();
6793 return -ENOMEM;
6796 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
6797 if (!proc_dir) {
6798 printk(TPACPI_ERR
6799 "unable to create proc dir " TPACPI_PROC_DIR);
6800 thinkpad_acpi_module_exit();
6801 return -ENODEV;
6803 proc_dir->owner = THIS_MODULE;
6805 ret = platform_driver_register(&tpacpi_pdriver);
6806 if (ret) {
6807 printk(TPACPI_ERR
6808 "unable to register main platform driver\n");
6809 thinkpad_acpi_module_exit();
6810 return ret;
6812 tp_features.platform_drv_registered = 1;
6814 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6815 if (ret) {
6816 printk(TPACPI_ERR
6817 "unable to register hwmon platform driver\n");
6818 thinkpad_acpi_module_exit();
6819 return ret;
6821 tp_features.sensors_pdrv_registered = 1;
6823 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
6824 if (!ret) {
6825 tp_features.platform_drv_attrs_registered = 1;
6826 ret = tpacpi_create_driver_attributes(
6827 &tpacpi_hwmon_pdriver.driver);
6829 if (ret) {
6830 printk(TPACPI_ERR
6831 "unable to create sysfs driver attributes\n");
6832 thinkpad_acpi_module_exit();
6833 return ret;
6835 tp_features.sensors_pdrv_attrs_registered = 1;
6838 /* Device initialization */
6839 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
6840 NULL, 0);
6841 if (IS_ERR(tpacpi_pdev)) {
6842 ret = PTR_ERR(tpacpi_pdev);
6843 tpacpi_pdev = NULL;
6844 printk(TPACPI_ERR "unable to register platform device\n");
6845 thinkpad_acpi_module_exit();
6846 return ret;
6848 tpacpi_sensors_pdev = platform_device_register_simple(
6849 TPACPI_HWMON_DRVR_NAME,
6850 -1, NULL, 0);
6851 if (IS_ERR(tpacpi_sensors_pdev)) {
6852 ret = PTR_ERR(tpacpi_sensors_pdev);
6853 tpacpi_sensors_pdev = NULL;
6854 printk(TPACPI_ERR
6855 "unable to register hwmon platform device\n");
6856 thinkpad_acpi_module_exit();
6857 return ret;
6859 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6860 &dev_attr_thinkpad_acpi_pdev_name);
6861 if (ret) {
6862 printk(TPACPI_ERR
6863 "unable to create sysfs hwmon device attributes\n");
6864 thinkpad_acpi_module_exit();
6865 return ret;
6867 tp_features.sensors_pdev_attrs_registered = 1;
6868 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
6869 if (IS_ERR(tpacpi_hwmon)) {
6870 ret = PTR_ERR(tpacpi_hwmon);
6871 tpacpi_hwmon = NULL;
6872 printk(TPACPI_ERR "unable to register hwmon device\n");
6873 thinkpad_acpi_module_exit();
6874 return ret;
6876 mutex_init(&tpacpi_inputdev_send_mutex);
6877 tpacpi_inputdev = input_allocate_device();
6878 if (!tpacpi_inputdev) {
6879 printk(TPACPI_ERR "unable to allocate input device\n");
6880 thinkpad_acpi_module_exit();
6881 return -ENOMEM;
6882 } else {
6883 /* Prepare input device, but don't register */
6884 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
6885 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
6886 tpacpi_inputdev->id.bustype = BUS_HOST;
6887 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6888 thinkpad_id.vendor :
6889 PCI_VENDOR_ID_IBM;
6890 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6891 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6893 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6894 ret = ibm_init(&ibms_init[i]);
6895 if (ret >= 0 && *ibms_init[i].param)
6896 ret = ibms_init[i].data->write(ibms_init[i].param);
6897 if (ret < 0) {
6898 thinkpad_acpi_module_exit();
6899 return ret;
6902 ret = input_register_device(tpacpi_inputdev);
6903 if (ret < 0) {
6904 printk(TPACPI_ERR "unable to register input device\n");
6905 thinkpad_acpi_module_exit();
6906 return ret;
6907 } else {
6908 tp_features.input_device_registered = 1;
6911 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
6912 return 0;
6915 /* Please remove this in year 2009 */
6916 MODULE_ALIAS("ibm_acpi");
6918 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6921 * DMI matching for module autoloading
6923 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6924 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6926 * Only models listed in thinkwiki will be supported, so add yours
6927 * if it is not there yet.
6929 #define IBM_BIOS_MODULE_ALIAS(__type) \
6930 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6932 /* Non-ancient thinkpads */
6933 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6934 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6936 /* Ancient thinkpad BIOSes have to be identified by
6937 * BIOS type or model number, and there are far less
6938 * BIOS types than model numbers... */
6939 IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6940 IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6941 IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6943 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
6944 MODULE_DESCRIPTION(TPACPI_DESC);
6945 MODULE_VERSION(TPACPI_VERSION);
6946 MODULE_LICENSE("GPL");
6948 module_init(thinkpad_acpi_module_init);
6949 module_exit(thinkpad_acpi_module_exit);