thinkpad-acpi: cleanup debug helpers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blob3367df9d4f992a60990aff2ed7638b66653a956b
1 /*
2 * thinkpad_acpi.c - ThinkPad ACPI Extras
5 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6 * Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
24 #define TPACPI_VERSION "0.22"
25 #define TPACPI_SYSFS_VERSION 0x020200
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/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>
80 #include <linux/pci_ids.h>
83 /* ThinkPad CMOS commands */
84 #define TP_CMOS_VOLUME_DOWN 0
85 #define TP_CMOS_VOLUME_UP 1
86 #define TP_CMOS_VOLUME_MUTE 2
87 #define TP_CMOS_BRIGHTNESS_UP 4
88 #define TP_CMOS_BRIGHTNESS_DOWN 5
89 #define TP_CMOS_THINKLIGHT_ON 12
90 #define TP_CMOS_THINKLIGHT_OFF 13
92 /* NVRAM Addresses */
93 enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
101 /* NVRAM bit masks */
102 enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
118 /* ACPI HIDs */
119 #define TPACPI_ACPI_HKEY_HID "IBM0068"
121 /* Input IDs */
122 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123 #define TPACPI_HKEY_INPUT_VERSION 0x4101
125 /* ACPI \WGSV commands */
126 enum {
127 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
128 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
129 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
130 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
133 /* TP_ACPI_WGSV_GET_STATE bits */
134 enum {
135 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
136 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
137 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
138 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
139 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
140 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
141 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
142 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
143 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
144 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
147 /****************************************************************************
148 * Main driver
151 #define TPACPI_NAME "thinkpad"
152 #define TPACPI_DESC "ThinkPad ACPI Extras"
153 #define TPACPI_FILE TPACPI_NAME "_acpi"
154 #define TPACPI_URL "http://ibm-acpi.sf.net/"
155 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
157 #define TPACPI_PROC_DIR "ibm"
158 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
159 #define TPACPI_DRVR_NAME TPACPI_FILE
160 #define TPACPI_DRVR_SHORTNAME "tpacpi"
161 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
163 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
164 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
166 #define TPACPI_MAX_ACPI_ARGS 3
168 /* rfkill switches */
169 enum {
170 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
171 TPACPI_RFK_WWAN_SW_ID,
172 TPACPI_RFK_UWB_SW_ID,
175 /* Debugging */
176 #define TPACPI_LOG TPACPI_FILE ": "
177 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
178 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
179 #define TPACPI_ERR KERN_ERR TPACPI_LOG
180 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
181 #define TPACPI_INFO KERN_INFO TPACPI_LOG
182 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
184 #define TPACPI_DBG_ALL 0xffff
185 #define TPACPI_DBG_INIT 0x0001
186 #define TPACPI_DBG_EXIT 0x0002
188 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
189 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
190 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
193 /****************************************************************************
194 * Driver-wide structs and misc. variables
197 struct ibm_struct;
199 struct tp_acpi_drv_struct {
200 const struct acpi_device_id *hid;
201 struct acpi_driver *driver;
203 void (*notify) (struct ibm_struct *, u32);
204 acpi_handle *handle;
205 u32 type;
206 struct acpi_device *device;
209 struct ibm_struct {
210 char *name;
212 int (*read) (char *);
213 int (*write) (char *);
214 void (*exit) (void);
215 void (*resume) (void);
216 void (*suspend) (pm_message_t state);
217 void (*shutdown) (void);
219 struct list_head all_drivers;
221 struct tp_acpi_drv_struct *acpi;
223 struct {
224 u8 acpi_driver_registered:1;
225 u8 acpi_notify_installed:1;
226 u8 proc_created:1;
227 u8 init_called:1;
228 u8 experimental:1;
229 } flags;
232 struct ibm_init_struct {
233 char param[32];
235 int (*init) (struct ibm_init_struct *);
236 struct ibm_struct *data;
239 static struct {
240 #ifdef CONFIG_THINKPAD_ACPI_BAY
241 u32 bay_status:1;
242 u32 bay_eject:1;
243 u32 bay_status2:1;
244 u32 bay_eject2:1;
245 #endif
246 u32 bluetooth:1;
247 u32 hotkey:1;
248 u32 hotkey_mask:1;
249 u32 hotkey_wlsw:1;
250 u32 hotkey_tablet:1;
251 u32 light:1;
252 u32 light_status:1;
253 u32 bright_16levels:1;
254 u32 bright_acpimode:1;
255 u32 wan:1;
256 u32 uwb:1;
257 u32 fan_ctrl_status_undef:1;
258 u32 input_device_registered:1;
259 u32 platform_drv_registered:1;
260 u32 platform_drv_attrs_registered:1;
261 u32 sensors_pdrv_registered:1;
262 u32 sensors_pdrv_attrs_registered:1;
263 u32 sensors_pdev_attrs_registered:1;
264 u32 hotkey_poll_active:1;
265 } tp_features;
267 static struct {
268 u16 hotkey_mask_ff:1;
269 u16 bright_cmos_ec_unsync:1;
270 } tp_warned;
272 struct thinkpad_id_data {
273 unsigned int vendor; /* ThinkPad vendor:
274 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
276 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
277 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
279 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
280 u16 ec_model;
282 char *model_str; /* ThinkPad T43 */
283 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
285 static struct thinkpad_id_data thinkpad_id;
287 static enum {
288 TPACPI_LIFE_INIT = 0,
289 TPACPI_LIFE_RUNNING,
290 TPACPI_LIFE_EXITING,
291 } tpacpi_lifecycle;
293 static int experimental;
294 static u32 dbg_level;
296 static struct workqueue_struct *tpacpi_wq;
298 /* Special LED class that can defer work */
299 struct tpacpi_led_classdev {
300 struct led_classdev led_classdev;
301 struct work_struct work;
302 enum led_brightness new_brightness;
303 unsigned int led;
306 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
307 static int dbg_wlswemul;
308 static int tpacpi_wlsw_emulstate;
309 static int dbg_bluetoothemul;
310 static int tpacpi_bluetooth_emulstate;
311 static int dbg_wwanemul;
312 static int tpacpi_wwan_emulstate;
313 static int dbg_uwbemul;
314 static int tpacpi_uwb_emulstate;
315 #endif
318 /*************************************************************************
319 * Debugging helpers
322 #define dbg_printk(a_dbg_level, format, arg...) \
323 do { if (dbg_level & (a_dbg_level)) \
324 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
325 } while (0)
327 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
328 #define vdbg_printk dbg_printk
329 static const char *str_supported(int is_supported);
330 #else
331 #define vdbg_printk(a_dbg_level, format, arg...) \
332 do { } while (0)
333 #endif
336 /****************************************************************************
337 ****************************************************************************
339 * ACPI Helpers and device model
341 ****************************************************************************
342 ****************************************************************************/
344 /*************************************************************************
345 * ACPI basic handles
348 static acpi_handle root_handle;
350 #define TPACPI_HANDLE(object, parent, paths...) \
351 static acpi_handle object##_handle; \
352 static acpi_handle *object##_parent = &parent##_handle; \
353 static char *object##_path; \
354 static char *object##_paths[] = { paths }
356 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
357 "\\_SB.PCI.ISA.EC", /* 570 */
358 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
359 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
360 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
361 "\\_SB.PCI0.ICH3.EC0", /* R31 */
362 "\\_SB.PCI0.LPC.EC", /* all others */
365 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
366 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
368 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
369 /* T4x, X31, X40 */
370 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
371 "\\CMS", /* R40, R40e */
372 ); /* all others */
374 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
375 "^HKEY", /* R30, R31 */
376 "HKEY", /* all others */
377 ); /* 570 */
379 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
380 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
381 "\\_SB.PCI0.VID0", /* 770e */
382 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
383 "\\_SB.PCI0.AGP.VID", /* all others */
384 ); /* R30, R31 */
387 /*************************************************************************
388 * ACPI helpers
391 static int acpi_evalf(acpi_handle handle,
392 void *res, char *method, char *fmt, ...)
394 char *fmt0 = fmt;
395 struct acpi_object_list params;
396 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
397 struct acpi_buffer result, *resultp;
398 union acpi_object out_obj;
399 acpi_status status;
400 va_list ap;
401 char res_type;
402 int success;
403 int quiet;
405 if (!*fmt) {
406 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
407 return 0;
410 if (*fmt == 'q') {
411 quiet = 1;
412 fmt++;
413 } else
414 quiet = 0;
416 res_type = *(fmt++);
418 params.count = 0;
419 params.pointer = &in_objs[0];
421 va_start(ap, fmt);
422 while (*fmt) {
423 char c = *(fmt++);
424 switch (c) {
425 case 'd': /* int */
426 in_objs[params.count].integer.value = va_arg(ap, int);
427 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
428 break;
429 /* add more types as needed */
430 default:
431 printk(TPACPI_ERR "acpi_evalf() called "
432 "with invalid format character '%c'\n", c);
433 return 0;
436 va_end(ap);
438 if (res_type != 'v') {
439 result.length = sizeof(out_obj);
440 result.pointer = &out_obj;
441 resultp = &result;
442 } else
443 resultp = NULL;
445 status = acpi_evaluate_object(handle, method, &params, resultp);
447 switch (res_type) {
448 case 'd': /* int */
449 if (res)
450 *(int *)res = out_obj.integer.value;
451 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
452 break;
453 case 'v': /* void */
454 success = status == AE_OK;
455 break;
456 /* add more types as needed */
457 default:
458 printk(TPACPI_ERR "acpi_evalf() called "
459 "with invalid format character '%c'\n", res_type);
460 return 0;
463 if (!success && !quiet)
464 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
465 method, fmt0, status);
467 return success;
470 static int acpi_ec_read(int i, u8 *p)
472 int v;
474 if (ecrd_handle) {
475 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
476 return 0;
477 *p = v;
478 } else {
479 if (ec_read(i, p) < 0)
480 return 0;
483 return 1;
486 static int acpi_ec_write(int i, u8 v)
488 if (ecwr_handle) {
489 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
490 return 0;
491 } else {
492 if (ec_write(i, v) < 0)
493 return 0;
496 return 1;
499 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
500 static int _sta(acpi_handle handle)
502 int status;
504 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
505 status = 0;
507 return status;
509 #endif
511 static int issue_thinkpad_cmos_command(int cmos_cmd)
513 if (!cmos_handle)
514 return -ENXIO;
516 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
517 return -EIO;
519 return 0;
522 /*************************************************************************
523 * ACPI device model
526 #define TPACPI_ACPIHANDLE_INIT(object) \
527 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
528 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
530 static void drv_acpi_handle_init(char *name,
531 acpi_handle *handle, acpi_handle parent,
532 char **paths, int num_paths, char **path)
534 int i;
535 acpi_status status;
537 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
538 name);
540 for (i = 0; i < num_paths; i++) {
541 status = acpi_get_handle(parent, paths[i], handle);
542 if (ACPI_SUCCESS(status)) {
543 *path = paths[i];
544 dbg_printk(TPACPI_DBG_INIT,
545 "Found ACPI handle %s for %s\n",
546 *path, name);
547 return;
551 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
552 name);
553 *handle = NULL;
556 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
558 struct ibm_struct *ibm = data;
560 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
561 return;
563 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
564 return;
566 ibm->acpi->notify(ibm, event);
569 static int __init setup_acpi_notify(struct ibm_struct *ibm)
571 acpi_status status;
572 int rc;
574 BUG_ON(!ibm->acpi);
576 if (!*ibm->acpi->handle)
577 return 0;
579 vdbg_printk(TPACPI_DBG_INIT,
580 "setting up ACPI notify for %s\n", ibm->name);
582 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
583 if (rc < 0) {
584 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
585 ibm->name, rc);
586 return -ENODEV;
589 ibm->acpi->device->driver_data = ibm;
590 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
591 TPACPI_ACPI_EVENT_PREFIX,
592 ibm->name);
594 status = acpi_install_notify_handler(*ibm->acpi->handle,
595 ibm->acpi->type, dispatch_acpi_notify, ibm);
596 if (ACPI_FAILURE(status)) {
597 if (status == AE_ALREADY_EXISTS) {
598 printk(TPACPI_NOTICE
599 "another device driver is already "
600 "handling %s events\n", ibm->name);
601 } else {
602 printk(TPACPI_ERR
603 "acpi_install_notify_handler(%s) failed: %d\n",
604 ibm->name, status);
606 return -ENODEV;
608 ibm->flags.acpi_notify_installed = 1;
609 return 0;
612 static int __init tpacpi_device_add(struct acpi_device *device)
614 return 0;
617 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
619 int rc;
621 dbg_printk(TPACPI_DBG_INIT,
622 "registering %s as an ACPI driver\n", ibm->name);
624 BUG_ON(!ibm->acpi);
626 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
627 if (!ibm->acpi->driver) {
628 printk(TPACPI_ERR
629 "failed to allocate memory for ibm->acpi->driver\n");
630 return -ENOMEM;
633 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
634 ibm->acpi->driver->ids = ibm->acpi->hid;
636 ibm->acpi->driver->ops.add = &tpacpi_device_add;
638 rc = acpi_bus_register_driver(ibm->acpi->driver);
639 if (rc < 0) {
640 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
641 ibm->name, rc);
642 kfree(ibm->acpi->driver);
643 ibm->acpi->driver = NULL;
644 } else if (!rc)
645 ibm->flags.acpi_driver_registered = 1;
647 return rc;
651 /****************************************************************************
652 ****************************************************************************
654 * Procfs Helpers
656 ****************************************************************************
657 ****************************************************************************/
659 static int dispatch_procfs_read(char *page, char **start, off_t off,
660 int count, int *eof, void *data)
662 struct ibm_struct *ibm = data;
663 int len;
665 if (!ibm || !ibm->read)
666 return -EINVAL;
668 len = ibm->read(page);
669 if (len < 0)
670 return len;
672 if (len <= off + count)
673 *eof = 1;
674 *start = page + off;
675 len -= off;
676 if (len > count)
677 len = count;
678 if (len < 0)
679 len = 0;
681 return len;
684 static int dispatch_procfs_write(struct file *file,
685 const char __user *userbuf,
686 unsigned long count, void *data)
688 struct ibm_struct *ibm = data;
689 char *kernbuf;
690 int ret;
692 if (!ibm || !ibm->write)
693 return -EINVAL;
695 kernbuf = kmalloc(count + 2, GFP_KERNEL);
696 if (!kernbuf)
697 return -ENOMEM;
699 if (copy_from_user(kernbuf, userbuf, count)) {
700 kfree(kernbuf);
701 return -EFAULT;
704 kernbuf[count] = 0;
705 strcat(kernbuf, ",");
706 ret = ibm->write(kernbuf);
707 if (ret == 0)
708 ret = count;
710 kfree(kernbuf);
712 return ret;
715 static char *next_cmd(char **cmds)
717 char *start = *cmds;
718 char *end;
720 while ((end = strchr(start, ',')) && end == start)
721 start = end + 1;
723 if (!end)
724 return NULL;
726 *end = 0;
727 *cmds = end + 1;
728 return start;
732 /****************************************************************************
733 ****************************************************************************
735 * Device model: input, hwmon and platform
737 ****************************************************************************
738 ****************************************************************************/
740 static struct platform_device *tpacpi_pdev;
741 static struct platform_device *tpacpi_sensors_pdev;
742 static struct device *tpacpi_hwmon;
743 static struct input_dev *tpacpi_inputdev;
744 static struct mutex tpacpi_inputdev_send_mutex;
745 static LIST_HEAD(tpacpi_all_drivers);
747 static int tpacpi_suspend_handler(struct platform_device *pdev,
748 pm_message_t state)
750 struct ibm_struct *ibm, *itmp;
752 list_for_each_entry_safe(ibm, itmp,
753 &tpacpi_all_drivers,
754 all_drivers) {
755 if (ibm->suspend)
756 (ibm->suspend)(state);
759 return 0;
762 static int tpacpi_resume_handler(struct platform_device *pdev)
764 struct ibm_struct *ibm, *itmp;
766 list_for_each_entry_safe(ibm, itmp,
767 &tpacpi_all_drivers,
768 all_drivers) {
769 if (ibm->resume)
770 (ibm->resume)();
773 return 0;
776 static void tpacpi_shutdown_handler(struct platform_device *pdev)
778 struct ibm_struct *ibm, *itmp;
780 list_for_each_entry_safe(ibm, itmp,
781 &tpacpi_all_drivers,
782 all_drivers) {
783 if (ibm->shutdown)
784 (ibm->shutdown)();
788 static struct platform_driver tpacpi_pdriver = {
789 .driver = {
790 .name = TPACPI_DRVR_NAME,
791 .owner = THIS_MODULE,
793 .suspend = tpacpi_suspend_handler,
794 .resume = tpacpi_resume_handler,
795 .shutdown = tpacpi_shutdown_handler,
798 static struct platform_driver tpacpi_hwmon_pdriver = {
799 .driver = {
800 .name = TPACPI_HWMON_DRVR_NAME,
801 .owner = THIS_MODULE,
805 /*************************************************************************
806 * sysfs support helpers
809 struct attribute_set {
810 unsigned int members, max_members;
811 struct attribute_group group;
814 struct attribute_set_obj {
815 struct attribute_set s;
816 struct attribute *a;
817 } __attribute__((packed));
819 static struct attribute_set *create_attr_set(unsigned int max_members,
820 const char *name)
822 struct attribute_set_obj *sobj;
824 if (max_members == 0)
825 return NULL;
827 /* Allocates space for implicit NULL at the end too */
828 sobj = kzalloc(sizeof(struct attribute_set_obj) +
829 max_members * sizeof(struct attribute *),
830 GFP_KERNEL);
831 if (!sobj)
832 return NULL;
833 sobj->s.max_members = max_members;
834 sobj->s.group.attrs = &sobj->a;
835 sobj->s.group.name = name;
837 return &sobj->s;
840 #define destroy_attr_set(_set) \
841 kfree(_set);
843 /* not multi-threaded safe, use it in a single thread per set */
844 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
846 if (!s || !attr)
847 return -EINVAL;
849 if (s->members >= s->max_members)
850 return -ENOMEM;
852 s->group.attrs[s->members] = attr;
853 s->members++;
855 return 0;
858 static int add_many_to_attr_set(struct attribute_set *s,
859 struct attribute **attr,
860 unsigned int count)
862 int i, res;
864 for (i = 0; i < count; i++) {
865 res = add_to_attr_set(s, attr[i]);
866 if (res)
867 return res;
870 return 0;
873 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
875 sysfs_remove_group(kobj, &s->group);
876 destroy_attr_set(s);
879 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
880 sysfs_create_group(_kobj, &_attr_set->group)
882 static int parse_strtoul(const char *buf,
883 unsigned long max, unsigned long *value)
885 char *endp;
887 while (*buf && isspace(*buf))
888 buf++;
889 *value = simple_strtoul(buf, &endp, 0);
890 while (*endp && isspace(*endp))
891 endp++;
892 if (*endp || *value > max)
893 return -EINVAL;
895 return 0;
898 static void tpacpi_disable_brightness_delay(void)
900 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
901 printk(TPACPI_NOTICE
902 "ACPI backlight control delay disabled\n");
905 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
907 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
908 union acpi_object *obj;
909 int rc;
911 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
912 obj = (union acpi_object *)buffer.pointer;
913 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
914 printk(TPACPI_ERR "Unknown _BCL data, "
915 "please report this to %s\n", TPACPI_MAIL);
916 rc = 0;
917 } else {
918 rc = obj->package.count;
920 } else {
921 return 0;
924 kfree(buffer.pointer);
925 return rc;
928 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
929 u32 lvl, void *context, void **rv)
931 char name[ACPI_PATH_SEGMENT_LENGTH];
932 struct acpi_buffer buffer = { sizeof(name), &name };
934 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
935 !strncmp("_BCL", name, sizeof(name) - 1)) {
936 BUG_ON(!rv || !*rv);
937 **(int **)rv = tpacpi_query_bcl_levels(handle);
938 return AE_CTRL_TERMINATE;
939 } else {
940 return AE_OK;
945 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
947 static int __init tpacpi_check_std_acpi_brightness_support(void)
949 int status;
950 int bcl_levels = 0;
951 void *bcl_ptr = &bcl_levels;
953 if (!vid_handle) {
954 TPACPI_ACPIHANDLE_INIT(vid);
956 if (!vid_handle)
957 return 0;
960 * Search for a _BCL method, and execute it. This is safe on all
961 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
962 * BIOS in ACPI backlight control mode. We do NOT have to care
963 * about calling the _BCL method in an enabled video device, any
964 * will do for our purposes.
967 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
968 tpacpi_acpi_walk_find_bcl, NULL,
969 &bcl_ptr);
971 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
972 tp_features.bright_acpimode = 1;
973 return (bcl_levels - 2);
976 return 0;
979 static int __init tpacpi_new_rfkill(const unsigned int id,
980 struct rfkill **rfk,
981 const enum rfkill_type rfktype,
982 const char *name,
983 const bool set_default,
984 int (*toggle_radio)(void *, enum rfkill_state),
985 int (*get_state)(void *, enum rfkill_state *))
987 int res;
988 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
990 res = get_state(NULL, &initial_state);
991 if (res < 0) {
992 printk(TPACPI_ERR
993 "failed to read initial state for %s, error %d; "
994 "will turn radio off\n", name, res);
995 } else if (set_default) {
996 /* try to set the initial state as the default for the rfkill
997 * type, since we ask the firmware to preserve it across S5 in
998 * NVRAM */
999 rfkill_set_default(rfktype,
1000 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1001 RFKILL_STATE_UNBLOCKED :
1002 RFKILL_STATE_SOFT_BLOCKED);
1005 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1006 if (!*rfk) {
1007 printk(TPACPI_ERR
1008 "failed to allocate memory for rfkill class\n");
1009 return -ENOMEM;
1012 (*rfk)->name = name;
1013 (*rfk)->get_state = get_state;
1014 (*rfk)->toggle_radio = toggle_radio;
1015 (*rfk)->state = initial_state;
1017 res = rfkill_register(*rfk);
1018 if (res < 0) {
1019 printk(TPACPI_ERR
1020 "failed to register %s rfkill switch: %d\n",
1021 name, res);
1022 rfkill_free(*rfk);
1023 *rfk = NULL;
1024 return res;
1027 return 0;
1030 /*************************************************************************
1031 * thinkpad-acpi driver attributes
1034 /* interface_version --------------------------------------------------- */
1035 static ssize_t tpacpi_driver_interface_version_show(
1036 struct device_driver *drv,
1037 char *buf)
1039 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1042 static DRIVER_ATTR(interface_version, S_IRUGO,
1043 tpacpi_driver_interface_version_show, NULL);
1045 /* debug_level --------------------------------------------------------- */
1046 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1047 char *buf)
1049 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1052 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1053 const char *buf, size_t count)
1055 unsigned long t;
1057 if (parse_strtoul(buf, 0xffff, &t))
1058 return -EINVAL;
1060 dbg_level = t;
1062 return count;
1065 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1066 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1068 /* version ------------------------------------------------------------- */
1069 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1070 char *buf)
1072 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1073 TPACPI_DESC, TPACPI_VERSION);
1076 static DRIVER_ATTR(version, S_IRUGO,
1077 tpacpi_driver_version_show, NULL);
1079 /* --------------------------------------------------------------------- */
1081 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1083 static void tpacpi_send_radiosw_update(void);
1085 /* wlsw_emulstate ------------------------------------------------------ */
1086 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1087 char *buf)
1089 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1092 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1093 const char *buf, size_t count)
1095 unsigned long t;
1097 if (parse_strtoul(buf, 1, &t))
1098 return -EINVAL;
1100 if (tpacpi_wlsw_emulstate != t) {
1101 tpacpi_wlsw_emulstate = !!t;
1102 tpacpi_send_radiosw_update();
1103 } else
1104 tpacpi_wlsw_emulstate = !!t;
1106 return count;
1109 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1110 tpacpi_driver_wlsw_emulstate_show,
1111 tpacpi_driver_wlsw_emulstate_store);
1113 /* bluetooth_emulstate ------------------------------------------------- */
1114 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1115 struct device_driver *drv,
1116 char *buf)
1118 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1121 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1122 struct device_driver *drv,
1123 const char *buf, size_t count)
1125 unsigned long t;
1127 if (parse_strtoul(buf, 1, &t))
1128 return -EINVAL;
1130 tpacpi_bluetooth_emulstate = !!t;
1132 return count;
1135 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1136 tpacpi_driver_bluetooth_emulstate_show,
1137 tpacpi_driver_bluetooth_emulstate_store);
1139 /* wwan_emulstate ------------------------------------------------- */
1140 static ssize_t tpacpi_driver_wwan_emulstate_show(
1141 struct device_driver *drv,
1142 char *buf)
1144 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1147 static ssize_t tpacpi_driver_wwan_emulstate_store(
1148 struct device_driver *drv,
1149 const char *buf, size_t count)
1151 unsigned long t;
1153 if (parse_strtoul(buf, 1, &t))
1154 return -EINVAL;
1156 tpacpi_wwan_emulstate = !!t;
1158 return count;
1161 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1162 tpacpi_driver_wwan_emulstate_show,
1163 tpacpi_driver_wwan_emulstate_store);
1165 /* uwb_emulstate ------------------------------------------------- */
1166 static ssize_t tpacpi_driver_uwb_emulstate_show(
1167 struct device_driver *drv,
1168 char *buf)
1170 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1173 static ssize_t tpacpi_driver_uwb_emulstate_store(
1174 struct device_driver *drv,
1175 const char *buf, size_t count)
1177 unsigned long t;
1179 if (parse_strtoul(buf, 1, &t))
1180 return -EINVAL;
1182 tpacpi_uwb_emulstate = !!t;
1184 return count;
1187 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1188 tpacpi_driver_uwb_emulstate_show,
1189 tpacpi_driver_uwb_emulstate_store);
1190 #endif
1192 /* --------------------------------------------------------------------- */
1194 static struct driver_attribute *tpacpi_driver_attributes[] = {
1195 &driver_attr_debug_level, &driver_attr_version,
1196 &driver_attr_interface_version,
1199 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1201 int i, res;
1203 i = 0;
1204 res = 0;
1205 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1206 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1207 i++;
1210 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1211 if (!res && dbg_wlswemul)
1212 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1213 if (!res && dbg_bluetoothemul)
1214 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1215 if (!res && dbg_wwanemul)
1216 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1217 if (!res && dbg_uwbemul)
1218 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1219 #endif
1221 return res;
1224 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1226 int i;
1228 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1229 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1231 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1232 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1233 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1234 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1235 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1236 #endif
1239 /****************************************************************************
1240 ****************************************************************************
1242 * Subdrivers
1244 ****************************************************************************
1245 ****************************************************************************/
1247 /*************************************************************************
1248 * thinkpad-acpi init subdriver
1251 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1253 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1254 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1256 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1257 (thinkpad_id.bios_version_str) ?
1258 thinkpad_id.bios_version_str : "unknown",
1259 (thinkpad_id.ec_version_str) ?
1260 thinkpad_id.ec_version_str : "unknown");
1262 if (thinkpad_id.vendor && thinkpad_id.model_str)
1263 printk(TPACPI_INFO "%s %s, model %s\n",
1264 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1265 "IBM" : ((thinkpad_id.vendor ==
1266 PCI_VENDOR_ID_LENOVO) ?
1267 "Lenovo" : "Unknown vendor"),
1268 thinkpad_id.model_str,
1269 (thinkpad_id.nummodel_str) ?
1270 thinkpad_id.nummodel_str : "unknown");
1272 return 0;
1275 static int thinkpad_acpi_driver_read(char *p)
1277 int len = 0;
1279 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1280 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1282 return len;
1285 static struct ibm_struct thinkpad_acpi_driver_data = {
1286 .name = "driver",
1287 .read = thinkpad_acpi_driver_read,
1290 /*************************************************************************
1291 * Hotkey subdriver
1294 enum { /* hot key scan codes (derived from ACPI DSDT) */
1295 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1296 TP_ACPI_HOTKEYSCAN_FNF2,
1297 TP_ACPI_HOTKEYSCAN_FNF3,
1298 TP_ACPI_HOTKEYSCAN_FNF4,
1299 TP_ACPI_HOTKEYSCAN_FNF5,
1300 TP_ACPI_HOTKEYSCAN_FNF6,
1301 TP_ACPI_HOTKEYSCAN_FNF7,
1302 TP_ACPI_HOTKEYSCAN_FNF8,
1303 TP_ACPI_HOTKEYSCAN_FNF9,
1304 TP_ACPI_HOTKEYSCAN_FNF10,
1305 TP_ACPI_HOTKEYSCAN_FNF11,
1306 TP_ACPI_HOTKEYSCAN_FNF12,
1307 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1308 TP_ACPI_HOTKEYSCAN_FNINSERT,
1309 TP_ACPI_HOTKEYSCAN_FNDELETE,
1310 TP_ACPI_HOTKEYSCAN_FNHOME,
1311 TP_ACPI_HOTKEYSCAN_FNEND,
1312 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1313 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1314 TP_ACPI_HOTKEYSCAN_FNSPACE,
1315 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1316 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1317 TP_ACPI_HOTKEYSCAN_MUTE,
1318 TP_ACPI_HOTKEYSCAN_THINKPAD,
1321 enum { /* Keys available through NVRAM polling */
1322 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1323 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1326 enum { /* Positions of some of the keys in hotkey masks */
1327 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1328 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1329 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1330 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1331 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1332 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1333 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1334 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1335 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1336 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1337 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1340 enum { /* NVRAM to ACPI HKEY group map */
1341 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1342 TP_ACPI_HKEY_ZOOM_MASK |
1343 TP_ACPI_HKEY_DISPSWTCH_MASK |
1344 TP_ACPI_HKEY_HIBERNATE_MASK,
1345 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1346 TP_ACPI_HKEY_BRGHTDWN_MASK,
1347 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1348 TP_ACPI_HKEY_VOLDWN_MASK |
1349 TP_ACPI_HKEY_MUTE_MASK,
1352 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1353 struct tp_nvram_state {
1354 u16 thinkpad_toggle:1;
1355 u16 zoom_toggle:1;
1356 u16 display_toggle:1;
1357 u16 thinklight_toggle:1;
1358 u16 hibernate_toggle:1;
1359 u16 displayexp_toggle:1;
1360 u16 display_state:1;
1361 u16 brightness_toggle:1;
1362 u16 volume_toggle:1;
1363 u16 mute:1;
1365 u8 brightness_level;
1366 u8 volume_level;
1369 static struct task_struct *tpacpi_hotkey_task;
1370 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1371 static int hotkey_poll_freq = 10; /* Hz */
1372 static struct mutex hotkey_thread_mutex;
1373 static struct mutex hotkey_thread_data_mutex;
1374 static unsigned int hotkey_config_change;
1376 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1378 #define hotkey_source_mask 0U
1380 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1382 static struct mutex hotkey_mutex;
1384 static enum { /* Reasons for waking up */
1385 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1386 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1387 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1388 } hotkey_wakeup_reason;
1390 static int hotkey_autosleep_ack;
1392 static int hotkey_orig_status;
1393 static u32 hotkey_orig_mask;
1394 static u32 hotkey_all_mask;
1395 static u32 hotkey_reserved_mask;
1396 static u32 hotkey_mask;
1398 static unsigned int hotkey_report_mode;
1400 static u16 *hotkey_keycode_map;
1402 static struct attribute_set *hotkey_dev_attributes;
1404 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1405 #define HOTKEY_CONFIG_CRITICAL_START \
1406 do { \
1407 mutex_lock(&hotkey_thread_data_mutex); \
1408 hotkey_config_change++; \
1409 } while (0);
1410 #define HOTKEY_CONFIG_CRITICAL_END \
1411 mutex_unlock(&hotkey_thread_data_mutex);
1412 #else
1413 #define HOTKEY_CONFIG_CRITICAL_START
1414 #define HOTKEY_CONFIG_CRITICAL_END
1415 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1417 /* HKEY.MHKG() return bits */
1418 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1420 static int hotkey_get_wlsw(int *status)
1422 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1423 if (dbg_wlswemul) {
1424 *status = !!tpacpi_wlsw_emulstate;
1425 return 0;
1427 #endif
1428 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1429 return -EIO;
1430 return 0;
1433 static int hotkey_get_tablet_mode(int *status)
1435 int s;
1437 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1438 return -EIO;
1440 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1441 return 0;
1445 * Call with hotkey_mutex held
1447 static int hotkey_mask_get(void)
1449 u32 m = 0;
1451 if (tp_features.hotkey_mask) {
1452 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1453 return -EIO;
1455 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1457 return 0;
1461 * Call with hotkey_mutex held
1463 static int hotkey_mask_set(u32 mask)
1465 int i;
1466 int rc = 0;
1468 if (tp_features.hotkey_mask) {
1469 if (!tp_warned.hotkey_mask_ff &&
1470 (mask == 0xffff || mask == 0xffffff ||
1471 mask == 0xffffffff)) {
1472 tp_warned.hotkey_mask_ff = 1;
1473 printk(TPACPI_NOTICE
1474 "setting the hotkey mask to 0x%08x is likely "
1475 "not the best way to go about it\n", mask);
1476 printk(TPACPI_NOTICE
1477 "please consider using the driver defaults, "
1478 "and refer to up-to-date thinkpad-acpi "
1479 "documentation\n");
1482 HOTKEY_CONFIG_CRITICAL_START
1483 for (i = 0; i < 32; i++) {
1484 u32 m = 1 << i;
1485 /* enable in firmware mask only keys not in NVRAM
1486 * mode, but enable the key in the cached hotkey_mask
1487 * regardless of mode, or the key will end up
1488 * disabled by hotkey_mask_get() */
1489 if (!acpi_evalf(hkey_handle,
1490 NULL, "MHKM", "vdd", i + 1,
1491 !!((mask & ~hotkey_source_mask) & m))) {
1492 rc = -EIO;
1493 break;
1494 } else {
1495 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1498 HOTKEY_CONFIG_CRITICAL_END
1500 /* hotkey_mask_get must be called unconditionally below */
1501 if (!hotkey_mask_get() && !rc &&
1502 (hotkey_mask & ~hotkey_source_mask) !=
1503 (mask & ~hotkey_source_mask)) {
1504 printk(TPACPI_NOTICE
1505 "requested hot key mask 0x%08x, but "
1506 "firmware forced it to 0x%08x\n",
1507 mask, hotkey_mask);
1509 } else {
1510 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1511 HOTKEY_CONFIG_CRITICAL_START
1512 hotkey_mask = mask & hotkey_source_mask;
1513 HOTKEY_CONFIG_CRITICAL_END
1514 hotkey_mask_get();
1515 if (hotkey_mask != mask) {
1516 printk(TPACPI_NOTICE
1517 "requested hot key mask 0x%08x, "
1518 "forced to 0x%08x (NVRAM poll mask is "
1519 "0x%08x): no firmware mask support\n",
1520 mask, hotkey_mask, hotkey_source_mask);
1522 #else
1523 hotkey_mask_get();
1524 rc = -ENXIO;
1525 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1528 return rc;
1531 static int hotkey_status_get(int *status)
1533 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1534 return -EIO;
1536 return 0;
1539 static int hotkey_status_set(int status)
1541 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1542 return -EIO;
1544 return 0;
1547 static void tpacpi_input_send_tabletsw(void)
1549 int state;
1551 if (tp_features.hotkey_tablet &&
1552 !hotkey_get_tablet_mode(&state)) {
1553 mutex_lock(&tpacpi_inputdev_send_mutex);
1555 input_report_switch(tpacpi_inputdev,
1556 SW_TABLET_MODE, !!state);
1557 input_sync(tpacpi_inputdev);
1559 mutex_unlock(&tpacpi_inputdev_send_mutex);
1563 static void tpacpi_input_send_key(unsigned int scancode)
1565 unsigned int keycode;
1567 keycode = hotkey_keycode_map[scancode];
1569 if (keycode != KEY_RESERVED) {
1570 mutex_lock(&tpacpi_inputdev_send_mutex);
1572 input_report_key(tpacpi_inputdev, keycode, 1);
1573 if (keycode == KEY_UNKNOWN)
1574 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1575 scancode);
1576 input_sync(tpacpi_inputdev);
1578 input_report_key(tpacpi_inputdev, keycode, 0);
1579 if (keycode == KEY_UNKNOWN)
1580 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1581 scancode);
1582 input_sync(tpacpi_inputdev);
1584 mutex_unlock(&tpacpi_inputdev_send_mutex);
1588 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1589 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1591 static void tpacpi_hotkey_send_key(unsigned int scancode)
1593 tpacpi_input_send_key(scancode);
1594 if (hotkey_report_mode < 2) {
1595 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1596 0x80, 0x1001 + scancode);
1600 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1602 u8 d;
1604 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1605 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1606 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1607 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1608 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1609 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1611 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1612 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1613 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1615 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1616 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1617 n->displayexp_toggle =
1618 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1620 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1621 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1622 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1623 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1624 n->brightness_toggle =
1625 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1627 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1628 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1629 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1630 >> TP_NVRAM_POS_LEVEL_VOLUME;
1631 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1632 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1636 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1637 do { \
1638 if ((mask & (1 << __scancode)) && \
1639 oldn->__member != newn->__member) \
1640 tpacpi_hotkey_send_key(__scancode); \
1641 } while (0)
1643 #define TPACPI_MAY_SEND_KEY(__scancode) \
1644 do { if (mask & (1 << __scancode)) \
1645 tpacpi_hotkey_send_key(__scancode); } while (0)
1647 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1648 struct tp_nvram_state *newn,
1649 u32 mask)
1651 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1652 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1653 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1654 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1656 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1658 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1660 /* handle volume */
1661 if (oldn->volume_toggle != newn->volume_toggle) {
1662 if (oldn->mute != newn->mute) {
1663 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1665 if (oldn->volume_level > newn->volume_level) {
1666 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1667 } else if (oldn->volume_level < newn->volume_level) {
1668 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1669 } else if (oldn->mute == newn->mute) {
1670 /* repeated key presses that didn't change state */
1671 if (newn->mute) {
1672 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1673 } else if (newn->volume_level != 0) {
1674 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1675 } else {
1676 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1681 /* handle brightness */
1682 if (oldn->brightness_toggle != newn->brightness_toggle) {
1683 if (oldn->brightness_level < newn->brightness_level) {
1684 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1685 } else if (oldn->brightness_level > newn->brightness_level) {
1686 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1687 } else {
1688 /* repeated key presses that didn't change state */
1689 if (newn->brightness_level != 0) {
1690 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1691 } else {
1692 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1698 #undef TPACPI_COMPARE_KEY
1699 #undef TPACPI_MAY_SEND_KEY
1701 static int hotkey_kthread(void *data)
1703 struct tp_nvram_state s[2];
1704 u32 mask;
1705 unsigned int si, so;
1706 unsigned long t;
1707 unsigned int change_detector, must_reset;
1709 mutex_lock(&hotkey_thread_mutex);
1711 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1712 goto exit;
1714 set_freezable();
1716 so = 0;
1717 si = 1;
1718 t = 0;
1720 /* Initial state for compares */
1721 mutex_lock(&hotkey_thread_data_mutex);
1722 change_detector = hotkey_config_change;
1723 mask = hotkey_source_mask & hotkey_mask;
1724 mutex_unlock(&hotkey_thread_data_mutex);
1725 hotkey_read_nvram(&s[so], mask);
1727 while (!kthread_should_stop() && hotkey_poll_freq) {
1728 if (t == 0)
1729 t = 1000/hotkey_poll_freq;
1730 t = msleep_interruptible(t);
1731 if (unlikely(kthread_should_stop()))
1732 break;
1733 must_reset = try_to_freeze();
1734 if (t > 0 && !must_reset)
1735 continue;
1737 mutex_lock(&hotkey_thread_data_mutex);
1738 if (must_reset || hotkey_config_change != change_detector) {
1739 /* forget old state on thaw or config change */
1740 si = so;
1741 t = 0;
1742 change_detector = hotkey_config_change;
1744 mask = hotkey_source_mask & hotkey_mask;
1745 mutex_unlock(&hotkey_thread_data_mutex);
1747 if (likely(mask)) {
1748 hotkey_read_nvram(&s[si], mask);
1749 if (likely(si != so)) {
1750 hotkey_compare_and_issue_event(&s[so], &s[si],
1751 mask);
1755 so = si;
1756 si ^= 1;
1759 exit:
1760 mutex_unlock(&hotkey_thread_mutex);
1761 return 0;
1764 static void hotkey_poll_stop_sync(void)
1766 if (tpacpi_hotkey_task) {
1767 if (frozen(tpacpi_hotkey_task) ||
1768 freezing(tpacpi_hotkey_task))
1769 thaw_process(tpacpi_hotkey_task);
1771 kthread_stop(tpacpi_hotkey_task);
1772 tpacpi_hotkey_task = NULL;
1773 mutex_lock(&hotkey_thread_mutex);
1774 /* at this point, the thread did exit */
1775 mutex_unlock(&hotkey_thread_mutex);
1779 /* call with hotkey_mutex held */
1780 static void hotkey_poll_setup(int may_warn)
1782 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1783 hotkey_poll_freq > 0 &&
1784 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1785 if (!tpacpi_hotkey_task) {
1786 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1787 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1788 if (IS_ERR(tpacpi_hotkey_task)) {
1789 tpacpi_hotkey_task = NULL;
1790 printk(TPACPI_ERR
1791 "could not create kernel thread "
1792 "for hotkey polling\n");
1795 } else {
1796 hotkey_poll_stop_sync();
1797 if (may_warn &&
1798 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1799 printk(TPACPI_NOTICE
1800 "hot keys 0x%08x require polling, "
1801 "which is currently disabled\n",
1802 hotkey_source_mask);
1807 static void hotkey_poll_setup_safe(int may_warn)
1809 mutex_lock(&hotkey_mutex);
1810 hotkey_poll_setup(may_warn);
1811 mutex_unlock(&hotkey_mutex);
1814 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1816 static void hotkey_poll_setup_safe(int __unused)
1820 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1822 static int hotkey_inputdev_open(struct input_dev *dev)
1824 switch (tpacpi_lifecycle) {
1825 case TPACPI_LIFE_INIT:
1827 * hotkey_init will call hotkey_poll_setup_safe
1828 * at the appropriate moment
1830 return 0;
1831 case TPACPI_LIFE_EXITING:
1832 return -EBUSY;
1833 case TPACPI_LIFE_RUNNING:
1834 hotkey_poll_setup_safe(0);
1835 return 0;
1838 /* Should only happen if tpacpi_lifecycle is corrupt */
1839 BUG();
1840 return -EBUSY;
1843 static void hotkey_inputdev_close(struct input_dev *dev)
1845 /* disable hotkey polling when possible */
1846 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1847 hotkey_poll_setup_safe(0);
1850 /* sysfs hotkey enable ------------------------------------------------- */
1851 static ssize_t hotkey_enable_show(struct device *dev,
1852 struct device_attribute *attr,
1853 char *buf)
1855 int res, status;
1857 res = hotkey_status_get(&status);
1858 if (res)
1859 return res;
1861 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1864 static ssize_t hotkey_enable_store(struct device *dev,
1865 struct device_attribute *attr,
1866 const char *buf, size_t count)
1868 unsigned long t;
1869 int res;
1871 if (parse_strtoul(buf, 1, &t))
1872 return -EINVAL;
1874 res = hotkey_status_set(t);
1876 return (res) ? res : count;
1879 static struct device_attribute dev_attr_hotkey_enable =
1880 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1881 hotkey_enable_show, hotkey_enable_store);
1883 /* sysfs hotkey mask --------------------------------------------------- */
1884 static ssize_t hotkey_mask_show(struct device *dev,
1885 struct device_attribute *attr,
1886 char *buf)
1888 int res;
1890 if (mutex_lock_killable(&hotkey_mutex))
1891 return -ERESTARTSYS;
1892 res = hotkey_mask_get();
1893 mutex_unlock(&hotkey_mutex);
1895 return (res)?
1896 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1899 static ssize_t hotkey_mask_store(struct device *dev,
1900 struct device_attribute *attr,
1901 const char *buf, size_t count)
1903 unsigned long t;
1904 int res;
1906 if (parse_strtoul(buf, 0xffffffffUL, &t))
1907 return -EINVAL;
1909 if (mutex_lock_killable(&hotkey_mutex))
1910 return -ERESTARTSYS;
1912 res = hotkey_mask_set(t);
1914 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1915 hotkey_poll_setup(1);
1916 #endif
1918 mutex_unlock(&hotkey_mutex);
1920 return (res) ? res : count;
1923 static struct device_attribute dev_attr_hotkey_mask =
1924 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1925 hotkey_mask_show, hotkey_mask_store);
1927 /* sysfs hotkey bios_enabled ------------------------------------------- */
1928 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1929 struct device_attribute *attr,
1930 char *buf)
1932 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1935 static struct device_attribute dev_attr_hotkey_bios_enabled =
1936 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1938 /* sysfs hotkey bios_mask ---------------------------------------------- */
1939 static ssize_t hotkey_bios_mask_show(struct device *dev,
1940 struct device_attribute *attr,
1941 char *buf)
1943 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1946 static struct device_attribute dev_attr_hotkey_bios_mask =
1947 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1949 /* sysfs hotkey all_mask ----------------------------------------------- */
1950 static ssize_t hotkey_all_mask_show(struct device *dev,
1951 struct device_attribute *attr,
1952 char *buf)
1954 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1955 hotkey_all_mask | hotkey_source_mask);
1958 static struct device_attribute dev_attr_hotkey_all_mask =
1959 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1961 /* sysfs hotkey recommended_mask --------------------------------------- */
1962 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1963 struct device_attribute *attr,
1964 char *buf)
1966 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1967 (hotkey_all_mask | hotkey_source_mask)
1968 & ~hotkey_reserved_mask);
1971 static struct device_attribute dev_attr_hotkey_recommended_mask =
1972 __ATTR(hotkey_recommended_mask, S_IRUGO,
1973 hotkey_recommended_mask_show, NULL);
1975 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1977 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1978 static ssize_t hotkey_source_mask_show(struct device *dev,
1979 struct device_attribute *attr,
1980 char *buf)
1982 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1985 static ssize_t hotkey_source_mask_store(struct device *dev,
1986 struct device_attribute *attr,
1987 const char *buf, size_t count)
1989 unsigned long t;
1991 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1992 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1993 return -EINVAL;
1995 if (mutex_lock_killable(&hotkey_mutex))
1996 return -ERESTARTSYS;
1998 HOTKEY_CONFIG_CRITICAL_START
1999 hotkey_source_mask = t;
2000 HOTKEY_CONFIG_CRITICAL_END
2002 hotkey_poll_setup(1);
2004 mutex_unlock(&hotkey_mutex);
2006 return count;
2009 static struct device_attribute dev_attr_hotkey_source_mask =
2010 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2011 hotkey_source_mask_show, hotkey_source_mask_store);
2013 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2014 static ssize_t hotkey_poll_freq_show(struct device *dev,
2015 struct device_attribute *attr,
2016 char *buf)
2018 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2021 static ssize_t hotkey_poll_freq_store(struct device *dev,
2022 struct device_attribute *attr,
2023 const char *buf, size_t count)
2025 unsigned long t;
2027 if (parse_strtoul(buf, 25, &t))
2028 return -EINVAL;
2030 if (mutex_lock_killable(&hotkey_mutex))
2031 return -ERESTARTSYS;
2033 hotkey_poll_freq = t;
2035 hotkey_poll_setup(1);
2036 mutex_unlock(&hotkey_mutex);
2038 return count;
2041 static struct device_attribute dev_attr_hotkey_poll_freq =
2042 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2043 hotkey_poll_freq_show, hotkey_poll_freq_store);
2045 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2047 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2048 static ssize_t hotkey_radio_sw_show(struct device *dev,
2049 struct device_attribute *attr,
2050 char *buf)
2052 int res, s;
2053 res = hotkey_get_wlsw(&s);
2054 if (res < 0)
2055 return res;
2057 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2060 static struct device_attribute dev_attr_hotkey_radio_sw =
2061 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2063 static void hotkey_radio_sw_notify_change(void)
2065 if (tp_features.hotkey_wlsw)
2066 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2067 "hotkey_radio_sw");
2070 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2071 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2072 struct device_attribute *attr,
2073 char *buf)
2075 int res, s;
2076 res = hotkey_get_tablet_mode(&s);
2077 if (res < 0)
2078 return res;
2080 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2083 static struct device_attribute dev_attr_hotkey_tablet_mode =
2084 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2086 static void hotkey_tablet_mode_notify_change(void)
2088 if (tp_features.hotkey_tablet)
2089 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2090 "hotkey_tablet_mode");
2093 /* sysfs hotkey report_mode -------------------------------------------- */
2094 static ssize_t hotkey_report_mode_show(struct device *dev,
2095 struct device_attribute *attr,
2096 char *buf)
2098 return snprintf(buf, PAGE_SIZE, "%d\n",
2099 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2102 static struct device_attribute dev_attr_hotkey_report_mode =
2103 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2105 /* sysfs wakeup reason (pollable) -------------------------------------- */
2106 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2107 struct device_attribute *attr,
2108 char *buf)
2110 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2113 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2114 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2116 static void hotkey_wakeup_reason_notify_change(void)
2118 if (tp_features.hotkey_mask)
2119 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2120 "wakeup_reason");
2123 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2124 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2125 struct device_attribute *attr,
2126 char *buf)
2128 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2131 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2132 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2133 hotkey_wakeup_hotunplug_complete_show, NULL);
2135 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2137 if (tp_features.hotkey_mask)
2138 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2139 "wakeup_hotunplug_complete");
2142 /* --------------------------------------------------------------------- */
2144 static struct attribute *hotkey_attributes[] __initdata = {
2145 &dev_attr_hotkey_enable.attr,
2146 &dev_attr_hotkey_bios_enabled.attr,
2147 &dev_attr_hotkey_report_mode.attr,
2148 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2149 &dev_attr_hotkey_mask.attr,
2150 &dev_attr_hotkey_all_mask.attr,
2151 &dev_attr_hotkey_recommended_mask.attr,
2152 &dev_attr_hotkey_source_mask.attr,
2153 &dev_attr_hotkey_poll_freq.attr,
2154 #endif
2157 static struct attribute *hotkey_mask_attributes[] __initdata = {
2158 &dev_attr_hotkey_bios_mask.attr,
2159 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2160 &dev_attr_hotkey_mask.attr,
2161 &dev_attr_hotkey_all_mask.attr,
2162 &dev_attr_hotkey_recommended_mask.attr,
2163 #endif
2164 &dev_attr_hotkey_wakeup_reason.attr,
2165 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2168 static void bluetooth_update_rfk(void);
2169 static void wan_update_rfk(void);
2170 static void uwb_update_rfk(void);
2171 static void tpacpi_send_radiosw_update(void)
2173 int wlsw;
2175 /* Sync these BEFORE sending any rfkill events */
2176 if (tp_features.bluetooth)
2177 bluetooth_update_rfk();
2178 if (tp_features.wan)
2179 wan_update_rfk();
2180 if (tp_features.uwb)
2181 uwb_update_rfk();
2183 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2184 mutex_lock(&tpacpi_inputdev_send_mutex);
2186 input_report_switch(tpacpi_inputdev,
2187 SW_RFKILL_ALL, !!wlsw);
2188 input_sync(tpacpi_inputdev);
2190 mutex_unlock(&tpacpi_inputdev_send_mutex);
2192 hotkey_radio_sw_notify_change();
2195 static void hotkey_exit(void)
2197 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2198 hotkey_poll_stop_sync();
2199 #endif
2201 if (hotkey_dev_attributes)
2202 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2204 kfree(hotkey_keycode_map);
2206 if (tp_features.hotkey) {
2207 dbg_printk(TPACPI_DBG_EXIT,
2208 "restoring original hot key mask\n");
2209 /* no short-circuit boolean operator below! */
2210 if ((hotkey_mask_set(hotkey_orig_mask) |
2211 hotkey_status_set(hotkey_orig_status)) != 0)
2212 printk(TPACPI_ERR
2213 "failed to restore hot key mask "
2214 "to BIOS defaults\n");
2218 static int __init hotkey_init(struct ibm_init_struct *iibm)
2220 /* Requirements for changing the default keymaps:
2222 * 1. Many of the keys are mapped to KEY_RESERVED for very
2223 * good reasons. Do not change them unless you have deep
2224 * knowledge on the IBM and Lenovo ThinkPad firmware for
2225 * the various ThinkPad models. The driver behaves
2226 * differently for KEY_RESERVED: such keys have their
2227 * hot key mask *unset* in mask_recommended, and also
2228 * in the initial hot key mask programmed into the
2229 * firmware at driver load time, which means the firm-
2230 * ware may react very differently if you change them to
2231 * something else;
2233 * 2. You must be subscribed to the linux-thinkpad and
2234 * ibm-acpi-devel mailing lists, and you should read the
2235 * list archives since 2007 if you want to change the
2236 * keymaps. This requirement exists so that you will
2237 * know the past history of problems with the thinkpad-
2238 * acpi driver keymaps, and also that you will be
2239 * listening to any bug reports;
2241 * 3. Do not send thinkpad-acpi specific patches directly to
2242 * for merging, *ever*. Send them to the linux-acpi
2243 * mailinglist for comments. Merging is to be done only
2244 * through acpi-test and the ACPI maintainer.
2246 * If the above is too much to ask, don't change the keymap.
2247 * Ask the thinkpad-acpi maintainer to do it, instead.
2249 static u16 ibm_keycode_map[] __initdata = {
2250 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2251 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2252 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2253 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2255 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2256 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2257 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2258 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2260 /* brightness: firmware always reacts to them, unless
2261 * X.org did some tricks in the radeon BIOS scratch
2262 * registers of *some* models */
2263 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2264 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2266 /* Thinklight: firmware always react to it */
2267 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2269 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2270 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2272 /* Volume: firmware always react to it and reprograms
2273 * the built-in *extra* mixer. Never map it to control
2274 * another mixer by default. */
2275 KEY_RESERVED, /* 0x14: VOLUME UP */
2276 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2277 KEY_RESERVED, /* 0x16: MUTE */
2279 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2281 /* (assignments unknown, please report if found) */
2282 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2283 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2285 static u16 lenovo_keycode_map[] __initdata = {
2286 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2287 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2288 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2289 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2291 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2292 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2293 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2294 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2296 /* These either have to go through ACPI video, or
2297 * act like in the IBM ThinkPads, so don't ever
2298 * enable them by default */
2299 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2300 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2302 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2304 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2305 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2307 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2308 * react to it and reprograms the built-in *extra* mixer.
2309 * Never map it to control another mixer by default.
2311 * T60?, T61, R60?, R61: firmware and EC tries to send
2312 * these over the regular keyboard, so these are no-ops,
2313 * but there are still weird bugs re. MUTE, so do not
2314 * change unless you get test reports from all Lenovo
2315 * models. May cause the BIOS to interfere with the
2316 * HDA mixer.
2318 KEY_RESERVED, /* 0x14: VOLUME UP */
2319 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2320 KEY_RESERVED, /* 0x16: MUTE */
2322 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2324 /* (assignments unknown, please report if found) */
2325 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2326 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2329 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2330 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2331 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2333 int res, i;
2334 int status;
2335 int hkeyv;
2337 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2339 BUG_ON(!tpacpi_inputdev);
2340 BUG_ON(tpacpi_inputdev->open != NULL ||
2341 tpacpi_inputdev->close != NULL);
2343 TPACPI_ACPIHANDLE_INIT(hkey);
2344 mutex_init(&hotkey_mutex);
2346 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2347 mutex_init(&hotkey_thread_mutex);
2348 mutex_init(&hotkey_thread_data_mutex);
2349 #endif
2351 /* hotkey not supported on 570 */
2352 tp_features.hotkey = hkey_handle != NULL;
2354 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2355 str_supported(tp_features.hotkey));
2357 if (!tp_features.hotkey)
2358 return 1;
2360 tpacpi_disable_brightness_delay();
2362 hotkey_dev_attributes = create_attr_set(13, NULL);
2363 if (!hotkey_dev_attributes)
2364 return -ENOMEM;
2365 res = add_many_to_attr_set(hotkey_dev_attributes,
2366 hotkey_attributes,
2367 ARRAY_SIZE(hotkey_attributes));
2368 if (res)
2369 goto err_exit;
2371 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2372 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2373 for HKEY interface version 0x100 */
2374 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2375 if ((hkeyv >> 8) != 1) {
2376 printk(TPACPI_ERR "unknown version of the "
2377 "HKEY interface: 0x%x\n", hkeyv);
2378 printk(TPACPI_ERR "please report this to %s\n",
2379 TPACPI_MAIL);
2380 } else {
2382 * MHKV 0x100 in A31, R40, R40e,
2383 * T4x, X31, and later
2385 tp_features.hotkey_mask = 1;
2389 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2390 str_supported(tp_features.hotkey_mask));
2392 if (tp_features.hotkey_mask) {
2393 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2394 "MHKA", "qd")) {
2395 printk(TPACPI_ERR
2396 "missing MHKA handler, "
2397 "please report this to %s\n",
2398 TPACPI_MAIL);
2399 /* FN+F12, FN+F4, FN+F3 */
2400 hotkey_all_mask = 0x080cU;
2404 /* hotkey_source_mask *must* be zero for
2405 * the first hotkey_mask_get */
2406 res = hotkey_status_get(&hotkey_orig_status);
2407 if (res)
2408 goto err_exit;
2410 if (tp_features.hotkey_mask) {
2411 res = hotkey_mask_get();
2412 if (res)
2413 goto err_exit;
2415 hotkey_orig_mask = hotkey_mask;
2416 res = add_many_to_attr_set(
2417 hotkey_dev_attributes,
2418 hotkey_mask_attributes,
2419 ARRAY_SIZE(hotkey_mask_attributes));
2420 if (res)
2421 goto err_exit;
2424 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2425 if (tp_features.hotkey_mask) {
2426 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2427 & ~hotkey_all_mask;
2428 } else {
2429 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2432 vdbg_printk(TPACPI_DBG_INIT,
2433 "hotkey source mask 0x%08x, polling freq %d\n",
2434 hotkey_source_mask, hotkey_poll_freq);
2435 #endif
2437 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2438 if (dbg_wlswemul) {
2439 tp_features.hotkey_wlsw = 1;
2440 printk(TPACPI_INFO
2441 "radio switch emulation enabled\n");
2442 } else
2443 #endif
2444 /* Not all thinkpads have a hardware radio switch */
2445 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2446 tp_features.hotkey_wlsw = 1;
2447 printk(TPACPI_INFO
2448 "radio switch found; radios are %s\n",
2449 enabled(status, 0));
2451 if (tp_features.hotkey_wlsw)
2452 res = add_to_attr_set(hotkey_dev_attributes,
2453 &dev_attr_hotkey_radio_sw.attr);
2455 /* For X41t, X60t, X61t Tablets... */
2456 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2457 tp_features.hotkey_tablet = 1;
2458 printk(TPACPI_INFO
2459 "possible tablet mode switch found; "
2460 "ThinkPad in %s mode\n",
2461 (status & TP_HOTKEY_TABLET_MASK)?
2462 "tablet" : "laptop");
2463 res = add_to_attr_set(hotkey_dev_attributes,
2464 &dev_attr_hotkey_tablet_mode.attr);
2467 if (!res)
2468 res = register_attr_set_with_sysfs(
2469 hotkey_dev_attributes,
2470 &tpacpi_pdev->dev.kobj);
2471 if (res)
2472 goto err_exit;
2474 /* Set up key map */
2476 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2477 GFP_KERNEL);
2478 if (!hotkey_keycode_map) {
2479 printk(TPACPI_ERR
2480 "failed to allocate memory for key map\n");
2481 res = -ENOMEM;
2482 goto err_exit;
2485 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2486 dbg_printk(TPACPI_DBG_INIT,
2487 "using Lenovo default hot key map\n");
2488 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2489 TPACPI_HOTKEY_MAP_SIZE);
2490 } else {
2491 dbg_printk(TPACPI_DBG_INIT,
2492 "using IBM default hot key map\n");
2493 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2494 TPACPI_HOTKEY_MAP_SIZE);
2497 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2498 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2499 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2500 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2501 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2502 tpacpi_inputdev->keycode = hotkey_keycode_map;
2503 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2504 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2505 set_bit(hotkey_keycode_map[i],
2506 tpacpi_inputdev->keybit);
2507 } else {
2508 if (i < sizeof(hotkey_reserved_mask)*8)
2509 hotkey_reserved_mask |= 1 << i;
2513 if (tp_features.hotkey_wlsw) {
2514 set_bit(EV_SW, tpacpi_inputdev->evbit);
2515 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2517 if (tp_features.hotkey_tablet) {
2518 set_bit(EV_SW, tpacpi_inputdev->evbit);
2519 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2522 /* Do not issue duplicate brightness change events to
2523 * userspace */
2524 if (!tp_features.bright_acpimode)
2525 /* update bright_acpimode... */
2526 tpacpi_check_std_acpi_brightness_support();
2528 if (tp_features.bright_acpimode) {
2529 printk(TPACPI_INFO
2530 "This ThinkPad has standard ACPI backlight "
2531 "brightness control, supported by the ACPI "
2532 "video driver\n");
2533 printk(TPACPI_NOTICE
2534 "Disabling thinkpad-acpi brightness events "
2535 "by default...\n");
2537 /* The hotkey_reserved_mask change below is not
2538 * necessary while the keys are at KEY_RESERVED in the
2539 * default map, but better safe than sorry, leave it
2540 * here as a marker of what we have to do, especially
2541 * when we finally become able to set this at runtime
2542 * on response to X.org requests */
2543 hotkey_reserved_mask |=
2544 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2545 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2548 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2549 res = hotkey_status_set(1);
2550 if (res) {
2551 hotkey_exit();
2552 return res;
2554 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2555 & ~hotkey_reserved_mask)
2556 | hotkey_orig_mask);
2557 if (res < 0 && res != -ENXIO) {
2558 hotkey_exit();
2559 return res;
2562 dbg_printk(TPACPI_DBG_INIT,
2563 "legacy hot key reporting over procfs %s\n",
2564 (hotkey_report_mode < 2) ?
2565 "enabled" : "disabled");
2567 tpacpi_inputdev->open = &hotkey_inputdev_open;
2568 tpacpi_inputdev->close = &hotkey_inputdev_close;
2570 hotkey_poll_setup_safe(1);
2571 tpacpi_send_radiosw_update();
2572 tpacpi_input_send_tabletsw();
2574 return 0;
2576 err_exit:
2577 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2578 hotkey_dev_attributes = NULL;
2580 return (res < 0)? res : 1;
2583 static bool hotkey_notify_hotkey(const u32 hkey,
2584 bool *send_acpi_ev,
2585 bool *ignore_acpi_ev)
2587 /* 0x1000-0x1FFF: key presses */
2588 unsigned int scancode = hkey & 0xfff;
2589 *send_acpi_ev = true;
2590 *ignore_acpi_ev = false;
2592 if (scancode > 0 && scancode < 0x21) {
2593 scancode--;
2594 if (!(hotkey_source_mask & (1 << scancode))) {
2595 tpacpi_input_send_key(scancode);
2596 *send_acpi_ev = false;
2597 } else {
2598 *ignore_acpi_ev = true;
2600 return true;
2602 return false;
2605 static bool hotkey_notify_wakeup(const u32 hkey,
2606 bool *send_acpi_ev,
2607 bool *ignore_acpi_ev)
2609 /* 0x2000-0x2FFF: Wakeup reason */
2610 *send_acpi_ev = true;
2611 *ignore_acpi_ev = false;
2613 switch (hkey) {
2614 case 0x2304: /* suspend, undock */
2615 case 0x2404: /* hibernation, undock */
2616 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2617 *ignore_acpi_ev = true;
2618 break;
2620 case 0x2305: /* suspend, bay eject */
2621 case 0x2405: /* hibernation, bay eject */
2622 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2623 *ignore_acpi_ev = true;
2624 break;
2626 case 0x2313: /* Battery on critical low level (S3) */
2627 case 0x2413: /* Battery on critical low level (S4) */
2628 printk(TPACPI_ALERT
2629 "EMERGENCY WAKEUP: battery almost empty\n");
2630 /* how to auto-heal: */
2631 /* 2313: woke up from S3, go to S4/S5 */
2632 /* 2413: woke up from S4, go to S5 */
2633 break;
2635 default:
2636 return false;
2639 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2640 printk(TPACPI_INFO
2641 "woke up due to a hot-unplug "
2642 "request...\n");
2643 hotkey_wakeup_reason_notify_change();
2645 return true;
2648 static bool hotkey_notify_usrevent(const u32 hkey,
2649 bool *send_acpi_ev,
2650 bool *ignore_acpi_ev)
2652 /* 0x5000-0x5FFF: human interface helpers */
2653 *send_acpi_ev = true;
2654 *ignore_acpi_ev = false;
2656 switch (hkey) {
2657 case 0x5010: /* Lenovo new BIOS: brightness changed */
2658 case 0x500b: /* X61t: tablet pen inserted into bay */
2659 case 0x500c: /* X61t: tablet pen removed from bay */
2660 return true;
2662 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2663 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2664 tpacpi_input_send_tabletsw();
2665 hotkey_tablet_mode_notify_change();
2666 *send_acpi_ev = false;
2667 return true;
2669 case 0x5001:
2670 case 0x5002:
2671 /* LID switch events. Do not propagate */
2672 *ignore_acpi_ev = true;
2673 return true;
2675 default:
2676 return false;
2680 static bool hotkey_notify_thermal(const u32 hkey,
2681 bool *send_acpi_ev,
2682 bool *ignore_acpi_ev)
2684 /* 0x6000-0x6FFF: thermal alarms */
2685 *send_acpi_ev = true;
2686 *ignore_acpi_ev = false;
2688 switch (hkey) {
2689 case 0x6011:
2690 printk(TPACPI_CRIT
2691 "THERMAL ALARM: battery is too hot!\n");
2692 /* recommended action: warn user through gui */
2693 return true;
2694 case 0x6012:
2695 printk(TPACPI_ALERT
2696 "THERMAL EMERGENCY: battery is extremely hot!\n");
2697 /* recommended action: immediate sleep/hibernate */
2698 return true;
2699 case 0x6021:
2700 printk(TPACPI_CRIT
2701 "THERMAL ALARM: "
2702 "a sensor reports something is too hot!\n");
2703 /* recommended action: warn user through gui, that */
2704 /* some internal component is too hot */
2705 return true;
2706 case 0x6022:
2707 printk(TPACPI_ALERT
2708 "THERMAL EMERGENCY: "
2709 "a sensor reports something is extremely hot!\n");
2710 /* recommended action: immediate sleep/hibernate */
2711 return true;
2712 case 0x6030:
2713 printk(TPACPI_INFO
2714 "EC reports that Thermal Table has changed\n");
2715 /* recommended action: do nothing, we don't have
2716 * Lenovo ATM information */
2717 return true;
2718 default:
2719 printk(TPACPI_ALERT
2720 "THERMAL ALERT: unknown thermal alarm received\n");
2721 return false;
2725 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2727 u32 hkey;
2728 bool send_acpi_ev;
2729 bool ignore_acpi_ev;
2730 bool known_ev;
2732 if (event != 0x80) {
2733 printk(TPACPI_ERR
2734 "unknown HKEY notification event %d\n", event);
2735 /* forward it to userspace, maybe it knows how to handle it */
2736 acpi_bus_generate_netlink_event(
2737 ibm->acpi->device->pnp.device_class,
2738 dev_name(&ibm->acpi->device->dev),
2739 event, 0);
2740 return;
2743 while (1) {
2744 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2745 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2746 return;
2749 if (hkey == 0) {
2750 /* queue empty */
2751 return;
2754 send_acpi_ev = true;
2755 ignore_acpi_ev = false;
2757 switch (hkey >> 12) {
2758 case 1:
2759 /* 0x1000-0x1FFF: key presses */
2760 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2761 &ignore_acpi_ev);
2762 break;
2763 case 2:
2764 /* 0x2000-0x2FFF: Wakeup reason */
2765 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2766 &ignore_acpi_ev);
2767 break;
2768 case 3:
2769 /* 0x3000-0x3FFF: bay-related wakeups */
2770 if (hkey == 0x3003) {
2771 hotkey_autosleep_ack = 1;
2772 printk(TPACPI_INFO
2773 "bay ejected\n");
2774 hotkey_wakeup_hotunplug_complete_notify_change();
2775 known_ev = true;
2776 } else {
2777 known_ev = false;
2779 break;
2780 case 4:
2781 /* 0x4000-0x4FFF: dock-related wakeups */
2782 if (hkey == 0x4003) {
2783 hotkey_autosleep_ack = 1;
2784 printk(TPACPI_INFO
2785 "undocked\n");
2786 hotkey_wakeup_hotunplug_complete_notify_change();
2787 known_ev = true;
2788 } else {
2789 known_ev = false;
2791 break;
2792 case 5:
2793 /* 0x5000-0x5FFF: human interface helpers */
2794 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2795 &ignore_acpi_ev);
2796 break;
2797 case 6:
2798 /* 0x6000-0x6FFF: thermal alarms */
2799 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2800 &ignore_acpi_ev);
2801 break;
2802 case 7:
2803 /* 0x7000-0x7FFF: misc */
2804 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2805 tpacpi_send_radiosw_update();
2806 send_acpi_ev = 0;
2807 known_ev = true;
2808 break;
2810 /* fallthrough to default */
2811 default:
2812 known_ev = false;
2814 if (!known_ev) {
2815 printk(TPACPI_NOTICE
2816 "unhandled HKEY event 0x%04x\n", hkey);
2817 printk(TPACPI_NOTICE
2818 "please report the conditions when this "
2819 "event happened to %s\n", TPACPI_MAIL);
2822 /* Legacy events */
2823 if (!ignore_acpi_ev &&
2824 (send_acpi_ev || hotkey_report_mode < 2)) {
2825 acpi_bus_generate_proc_event(ibm->acpi->device,
2826 event, hkey);
2829 /* netlink events */
2830 if (!ignore_acpi_ev && send_acpi_ev) {
2831 acpi_bus_generate_netlink_event(
2832 ibm->acpi->device->pnp.device_class,
2833 dev_name(&ibm->acpi->device->dev),
2834 event, hkey);
2839 static void hotkey_suspend(pm_message_t state)
2841 /* Do these on suspend, we get the events on early resume! */
2842 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2843 hotkey_autosleep_ack = 0;
2846 static void hotkey_resume(void)
2848 tpacpi_disable_brightness_delay();
2850 if (hotkey_mask_get())
2851 printk(TPACPI_ERR
2852 "error while trying to read hot key mask "
2853 "from firmware\n");
2854 tpacpi_send_radiosw_update();
2855 hotkey_tablet_mode_notify_change();
2856 hotkey_wakeup_reason_notify_change();
2857 hotkey_wakeup_hotunplug_complete_notify_change();
2858 hotkey_poll_setup_safe(0);
2861 /* procfs -------------------------------------------------------------- */
2862 static int hotkey_read(char *p)
2864 int res, status;
2865 int len = 0;
2867 if (!tp_features.hotkey) {
2868 len += sprintf(p + len, "status:\t\tnot supported\n");
2869 return len;
2872 if (mutex_lock_killable(&hotkey_mutex))
2873 return -ERESTARTSYS;
2874 res = hotkey_status_get(&status);
2875 if (!res)
2876 res = hotkey_mask_get();
2877 mutex_unlock(&hotkey_mutex);
2878 if (res)
2879 return res;
2881 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2882 if (tp_features.hotkey_mask) {
2883 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2884 len += sprintf(p + len,
2885 "commands:\tenable, disable, reset, <mask>\n");
2886 } else {
2887 len += sprintf(p + len, "mask:\t\tnot supported\n");
2888 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2891 return len;
2894 static int hotkey_write(char *buf)
2896 int res, status;
2897 u32 mask;
2898 char *cmd;
2900 if (!tp_features.hotkey)
2901 return -ENODEV;
2903 if (mutex_lock_killable(&hotkey_mutex))
2904 return -ERESTARTSYS;
2906 status = -1;
2907 mask = hotkey_mask;
2909 res = 0;
2910 while ((cmd = next_cmd(&buf))) {
2911 if (strlencmp(cmd, "enable") == 0) {
2912 status = 1;
2913 } else if (strlencmp(cmd, "disable") == 0) {
2914 status = 0;
2915 } else if (strlencmp(cmd, "reset") == 0) {
2916 status = hotkey_orig_status;
2917 mask = hotkey_orig_mask;
2918 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2919 /* mask set */
2920 } else if (sscanf(cmd, "%x", &mask) == 1) {
2921 /* mask set */
2922 } else {
2923 res = -EINVAL;
2924 goto errexit;
2927 if (status != -1)
2928 res = hotkey_status_set(status);
2930 if (!res && mask != hotkey_mask)
2931 res = hotkey_mask_set(mask);
2933 errexit:
2934 mutex_unlock(&hotkey_mutex);
2935 return res;
2938 static const struct acpi_device_id ibm_htk_device_ids[] = {
2939 {TPACPI_ACPI_HKEY_HID, 0},
2940 {"", 0},
2943 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2944 .hid = ibm_htk_device_ids,
2945 .notify = hotkey_notify,
2946 .handle = &hkey_handle,
2947 .type = ACPI_DEVICE_NOTIFY,
2950 static struct ibm_struct hotkey_driver_data = {
2951 .name = "hotkey",
2952 .read = hotkey_read,
2953 .write = hotkey_write,
2954 .exit = hotkey_exit,
2955 .resume = hotkey_resume,
2956 .suspend = hotkey_suspend,
2957 .acpi = &ibm_hotkey_acpidriver,
2960 /*************************************************************************
2961 * Bluetooth subdriver
2964 enum {
2965 /* ACPI GBDC/SBDC bits */
2966 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2967 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2968 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2969 off / last state */
2972 enum {
2973 /* ACPI \BLTH commands */
2974 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2975 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2976 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2977 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2978 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2981 static struct rfkill *tpacpi_bluetooth_rfkill;
2983 static void bluetooth_suspend(pm_message_t state)
2985 /* Try to make sure radio will resume powered off */
2986 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2987 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
2990 static int bluetooth_get_radiosw(void)
2992 int status;
2994 if (!tp_features.bluetooth)
2995 return -ENODEV;
2997 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
2998 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
2999 return RFKILL_STATE_HARD_BLOCKED;
3001 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3002 if (dbg_bluetoothemul)
3003 return (tpacpi_bluetooth_emulstate) ?
3004 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3005 #endif
3007 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3008 return -EIO;
3010 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3011 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3014 static void bluetooth_update_rfk(void)
3016 int status;
3018 if (!tpacpi_bluetooth_rfkill)
3019 return;
3021 status = bluetooth_get_radiosw();
3022 if (status < 0)
3023 return;
3024 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3027 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3029 int status;
3031 if (!tp_features.bluetooth)
3032 return -ENODEV;
3034 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3035 * reason to risk weird behaviour. */
3036 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3037 && radio_on)
3038 return -EPERM;
3040 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3041 if (dbg_bluetoothemul) {
3042 tpacpi_bluetooth_emulstate = !!radio_on;
3043 if (update_rfk)
3044 bluetooth_update_rfk();
3045 return 0;
3047 #endif
3049 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3050 if (radio_on)
3051 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3052 else
3053 status = 0;
3054 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3055 return -EIO;
3057 if (update_rfk)
3058 bluetooth_update_rfk();
3060 return 0;
3063 /* sysfs bluetooth enable ---------------------------------------------- */
3064 static ssize_t bluetooth_enable_show(struct device *dev,
3065 struct device_attribute *attr,
3066 char *buf)
3068 int status;
3070 status = bluetooth_get_radiosw();
3071 if (status < 0)
3072 return status;
3074 return snprintf(buf, PAGE_SIZE, "%d\n",
3075 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3078 static ssize_t bluetooth_enable_store(struct device *dev,
3079 struct device_attribute *attr,
3080 const char *buf, size_t count)
3082 unsigned long t;
3083 int res;
3085 if (parse_strtoul(buf, 1, &t))
3086 return -EINVAL;
3088 res = bluetooth_set_radiosw(t, 1);
3090 return (res) ? res : count;
3093 static struct device_attribute dev_attr_bluetooth_enable =
3094 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3095 bluetooth_enable_show, bluetooth_enable_store);
3097 /* --------------------------------------------------------------------- */
3099 static struct attribute *bluetooth_attributes[] = {
3100 &dev_attr_bluetooth_enable.attr,
3101 NULL
3104 static const struct attribute_group bluetooth_attr_group = {
3105 .attrs = bluetooth_attributes,
3108 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3110 int bts = bluetooth_get_radiosw();
3112 if (bts < 0)
3113 return bts;
3115 *state = bts;
3116 return 0;
3119 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3121 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3124 static void bluetooth_shutdown(void)
3126 /* Order firmware to save current state to NVRAM */
3127 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3128 TP_ACPI_BLTH_SAVE_STATE))
3129 printk(TPACPI_NOTICE
3130 "failed to save bluetooth state to NVRAM\n");
3133 static void bluetooth_exit(void)
3135 bluetooth_shutdown();
3137 if (tpacpi_bluetooth_rfkill)
3138 rfkill_unregister(tpacpi_bluetooth_rfkill);
3140 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3141 &bluetooth_attr_group);
3144 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3146 int res;
3147 int status = 0;
3149 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3151 TPACPI_ACPIHANDLE_INIT(hkey);
3153 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3154 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3155 tp_features.bluetooth = hkey_handle &&
3156 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3158 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3159 str_supported(tp_features.bluetooth),
3160 status);
3162 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3163 if (dbg_bluetoothemul) {
3164 tp_features.bluetooth = 1;
3165 printk(TPACPI_INFO
3166 "bluetooth switch emulation enabled\n");
3167 } else
3168 #endif
3169 if (tp_features.bluetooth &&
3170 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3171 /* no bluetooth hardware present in system */
3172 tp_features.bluetooth = 0;
3173 dbg_printk(TPACPI_DBG_INIT,
3174 "bluetooth hardware not installed\n");
3177 if (!tp_features.bluetooth)
3178 return 1;
3180 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3181 &bluetooth_attr_group);
3182 if (res)
3183 return res;
3185 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3186 &tpacpi_bluetooth_rfkill,
3187 RFKILL_TYPE_BLUETOOTH,
3188 "tpacpi_bluetooth_sw",
3189 true,
3190 tpacpi_bluetooth_rfk_set,
3191 tpacpi_bluetooth_rfk_get);
3192 if (res) {
3193 bluetooth_exit();
3194 return res;
3197 return 0;
3200 /* procfs -------------------------------------------------------------- */
3201 static int bluetooth_read(char *p)
3203 int len = 0;
3204 int status = bluetooth_get_radiosw();
3206 if (!tp_features.bluetooth)
3207 len += sprintf(p + len, "status:\t\tnot supported\n");
3208 else {
3209 len += sprintf(p + len, "status:\t\t%s\n",
3210 (status == RFKILL_STATE_UNBLOCKED) ?
3211 "enabled" : "disabled");
3212 len += sprintf(p + len, "commands:\tenable, disable\n");
3215 return len;
3218 static int bluetooth_write(char *buf)
3220 char *cmd;
3222 if (!tp_features.bluetooth)
3223 return -ENODEV;
3225 while ((cmd = next_cmd(&buf))) {
3226 if (strlencmp(cmd, "enable") == 0) {
3227 bluetooth_set_radiosw(1, 1);
3228 } else if (strlencmp(cmd, "disable") == 0) {
3229 bluetooth_set_radiosw(0, 1);
3230 } else
3231 return -EINVAL;
3234 return 0;
3237 static struct ibm_struct bluetooth_driver_data = {
3238 .name = "bluetooth",
3239 .read = bluetooth_read,
3240 .write = bluetooth_write,
3241 .exit = bluetooth_exit,
3242 .suspend = bluetooth_suspend,
3243 .shutdown = bluetooth_shutdown,
3246 /*************************************************************************
3247 * Wan subdriver
3250 enum {
3251 /* ACPI GWAN/SWAN bits */
3252 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3253 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3254 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3255 off / last state */
3258 static struct rfkill *tpacpi_wan_rfkill;
3260 static void wan_suspend(pm_message_t state)
3262 /* Try to make sure radio will resume powered off */
3263 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3264 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3267 static int wan_get_radiosw(void)
3269 int status;
3271 if (!tp_features.wan)
3272 return -ENODEV;
3274 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3275 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3276 return RFKILL_STATE_HARD_BLOCKED;
3278 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3279 if (dbg_wwanemul)
3280 return (tpacpi_wwan_emulstate) ?
3281 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3282 #endif
3284 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3285 return -EIO;
3287 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3288 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3291 static void wan_update_rfk(void)
3293 int status;
3295 if (!tpacpi_wan_rfkill)
3296 return;
3298 status = wan_get_radiosw();
3299 if (status < 0)
3300 return;
3301 rfkill_force_state(tpacpi_wan_rfkill, status);
3304 static int wan_set_radiosw(int radio_on, int update_rfk)
3306 int status;
3308 if (!tp_features.wan)
3309 return -ENODEV;
3311 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3312 * reason to risk weird behaviour. */
3313 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3314 && radio_on)
3315 return -EPERM;
3317 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3318 if (dbg_wwanemul) {
3319 tpacpi_wwan_emulstate = !!radio_on;
3320 if (update_rfk)
3321 wan_update_rfk();
3322 return 0;
3324 #endif
3326 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3327 if (radio_on)
3328 status = TP_ACPI_WANCARD_RADIOSSW;
3329 else
3330 status = 0;
3331 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3332 return -EIO;
3334 if (update_rfk)
3335 wan_update_rfk();
3337 return 0;
3340 /* sysfs wan enable ---------------------------------------------------- */
3341 static ssize_t wan_enable_show(struct device *dev,
3342 struct device_attribute *attr,
3343 char *buf)
3345 int status;
3347 status = wan_get_radiosw();
3348 if (status < 0)
3349 return status;
3351 return snprintf(buf, PAGE_SIZE, "%d\n",
3352 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3355 static ssize_t wan_enable_store(struct device *dev,
3356 struct device_attribute *attr,
3357 const char *buf, size_t count)
3359 unsigned long t;
3360 int res;
3362 if (parse_strtoul(buf, 1, &t))
3363 return -EINVAL;
3365 res = wan_set_radiosw(t, 1);
3367 return (res) ? res : count;
3370 static struct device_attribute dev_attr_wan_enable =
3371 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3372 wan_enable_show, wan_enable_store);
3374 /* --------------------------------------------------------------------- */
3376 static struct attribute *wan_attributes[] = {
3377 &dev_attr_wan_enable.attr,
3378 NULL
3381 static const struct attribute_group wan_attr_group = {
3382 .attrs = wan_attributes,
3385 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3387 int wans = wan_get_radiosw();
3389 if (wans < 0)
3390 return wans;
3392 *state = wans;
3393 return 0;
3396 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3398 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3401 static void wan_shutdown(void)
3403 /* Order firmware to save current state to NVRAM */
3404 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3405 TP_ACPI_WGSV_SAVE_STATE))
3406 printk(TPACPI_NOTICE
3407 "failed to save WWAN state to NVRAM\n");
3410 static void wan_exit(void)
3412 wan_shutdown();
3414 if (tpacpi_wan_rfkill)
3415 rfkill_unregister(tpacpi_wan_rfkill);
3417 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3418 &wan_attr_group);
3421 static int __init wan_init(struct ibm_init_struct *iibm)
3423 int res;
3424 int status = 0;
3426 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3428 TPACPI_ACPIHANDLE_INIT(hkey);
3430 tp_features.wan = hkey_handle &&
3431 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3433 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3434 str_supported(tp_features.wan),
3435 status);
3437 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3438 if (dbg_wwanemul) {
3439 tp_features.wan = 1;
3440 printk(TPACPI_INFO
3441 "wwan switch emulation enabled\n");
3442 } else
3443 #endif
3444 if (tp_features.wan &&
3445 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3446 /* no wan hardware present in system */
3447 tp_features.wan = 0;
3448 dbg_printk(TPACPI_DBG_INIT,
3449 "wan hardware not installed\n");
3452 if (!tp_features.wan)
3453 return 1;
3455 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3456 &wan_attr_group);
3457 if (res)
3458 return res;
3460 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3461 &tpacpi_wan_rfkill,
3462 RFKILL_TYPE_WWAN,
3463 "tpacpi_wwan_sw",
3464 true,
3465 tpacpi_wan_rfk_set,
3466 tpacpi_wan_rfk_get);
3467 if (res) {
3468 wan_exit();
3469 return res;
3472 return 0;
3475 /* procfs -------------------------------------------------------------- */
3476 static int wan_read(char *p)
3478 int len = 0;
3479 int status = wan_get_radiosw();
3481 if (!tp_features.wan)
3482 len += sprintf(p + len, "status:\t\tnot supported\n");
3483 else {
3484 len += sprintf(p + len, "status:\t\t%s\n",
3485 (status == RFKILL_STATE_UNBLOCKED) ?
3486 "enabled" : "disabled");
3487 len += sprintf(p + len, "commands:\tenable, disable\n");
3490 return len;
3493 static int wan_write(char *buf)
3495 char *cmd;
3497 if (!tp_features.wan)
3498 return -ENODEV;
3500 while ((cmd = next_cmd(&buf))) {
3501 if (strlencmp(cmd, "enable") == 0) {
3502 wan_set_radiosw(1, 1);
3503 } else if (strlencmp(cmd, "disable") == 0) {
3504 wan_set_radiosw(0, 1);
3505 } else
3506 return -EINVAL;
3509 return 0;
3512 static struct ibm_struct wan_driver_data = {
3513 .name = "wan",
3514 .read = wan_read,
3515 .write = wan_write,
3516 .exit = wan_exit,
3517 .suspend = wan_suspend,
3518 .shutdown = wan_shutdown,
3521 /*************************************************************************
3522 * UWB subdriver
3525 enum {
3526 /* ACPI GUWB/SUWB bits */
3527 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3528 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3531 static struct rfkill *tpacpi_uwb_rfkill;
3533 static int uwb_get_radiosw(void)
3535 int status;
3537 if (!tp_features.uwb)
3538 return -ENODEV;
3540 /* WLSW overrides UWB in firmware/hardware, reflect that */
3541 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3542 return RFKILL_STATE_HARD_BLOCKED;
3544 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3545 if (dbg_uwbemul)
3546 return (tpacpi_uwb_emulstate) ?
3547 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3548 #endif
3550 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3551 return -EIO;
3553 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3554 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3557 static void uwb_update_rfk(void)
3559 int status;
3561 if (!tpacpi_uwb_rfkill)
3562 return;
3564 status = uwb_get_radiosw();
3565 if (status < 0)
3566 return;
3567 rfkill_force_state(tpacpi_uwb_rfkill, status);
3570 static int uwb_set_radiosw(int radio_on, int update_rfk)
3572 int status;
3574 if (!tp_features.uwb)
3575 return -ENODEV;
3577 /* WLSW overrides UWB in firmware/hardware, but there is no
3578 * reason to risk weird behaviour. */
3579 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3580 && radio_on)
3581 return -EPERM;
3583 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3584 if (dbg_uwbemul) {
3585 tpacpi_uwb_emulstate = !!radio_on;
3586 if (update_rfk)
3587 uwb_update_rfk();
3588 return 0;
3590 #endif
3592 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3593 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3594 return -EIO;
3596 if (update_rfk)
3597 uwb_update_rfk();
3599 return 0;
3602 /* --------------------------------------------------------------------- */
3604 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3606 int uwbs = uwb_get_radiosw();
3608 if (uwbs < 0)
3609 return uwbs;
3611 *state = uwbs;
3612 return 0;
3615 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3617 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3620 static void uwb_exit(void)
3622 if (tpacpi_uwb_rfkill)
3623 rfkill_unregister(tpacpi_uwb_rfkill);
3626 static int __init uwb_init(struct ibm_init_struct *iibm)
3628 int res;
3629 int status = 0;
3631 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3633 TPACPI_ACPIHANDLE_INIT(hkey);
3635 tp_features.uwb = hkey_handle &&
3636 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3638 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3639 str_supported(tp_features.uwb),
3640 status);
3642 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3643 if (dbg_uwbemul) {
3644 tp_features.uwb = 1;
3645 printk(TPACPI_INFO
3646 "uwb switch emulation enabled\n");
3647 } else
3648 #endif
3649 if (tp_features.uwb &&
3650 !(status & TP_ACPI_UWB_HWPRESENT)) {
3651 /* no uwb hardware present in system */
3652 tp_features.uwb = 0;
3653 dbg_printk(TPACPI_DBG_INIT,
3654 "uwb hardware not installed\n");
3657 if (!tp_features.uwb)
3658 return 1;
3660 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3661 &tpacpi_uwb_rfkill,
3662 RFKILL_TYPE_UWB,
3663 "tpacpi_uwb_sw",
3664 false,
3665 tpacpi_uwb_rfk_set,
3666 tpacpi_uwb_rfk_get);
3668 return res;
3671 static struct ibm_struct uwb_driver_data = {
3672 .name = "uwb",
3673 .exit = uwb_exit,
3674 .flags.experimental = 1,
3677 /*************************************************************************
3678 * Video subdriver
3681 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3683 enum video_access_mode {
3684 TPACPI_VIDEO_NONE = 0,
3685 TPACPI_VIDEO_570, /* 570 */
3686 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3687 TPACPI_VIDEO_NEW, /* all others */
3690 enum { /* video status flags, based on VIDEO_570 */
3691 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3692 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3693 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3696 enum { /* TPACPI_VIDEO_570 constants */
3697 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3698 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3699 * video_status_flags */
3700 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3701 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3704 static enum video_access_mode video_supported;
3705 static int video_orig_autosw;
3707 static int video_autosw_get(void);
3708 static int video_autosw_set(int enable);
3710 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3712 static int __init video_init(struct ibm_init_struct *iibm)
3714 int ivga;
3716 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3718 TPACPI_ACPIHANDLE_INIT(vid);
3719 TPACPI_ACPIHANDLE_INIT(vid2);
3721 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3722 /* G41, assume IVGA doesn't change */
3723 vid_handle = vid2_handle;
3725 if (!vid_handle)
3726 /* video switching not supported on R30, R31 */
3727 video_supported = TPACPI_VIDEO_NONE;
3728 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3729 /* 570 */
3730 video_supported = TPACPI_VIDEO_570;
3731 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3732 /* 600e/x, 770e, 770x */
3733 video_supported = TPACPI_VIDEO_770;
3734 else
3735 /* all others */
3736 video_supported = TPACPI_VIDEO_NEW;
3738 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3739 str_supported(video_supported != TPACPI_VIDEO_NONE),
3740 video_supported);
3742 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3745 static void video_exit(void)
3747 dbg_printk(TPACPI_DBG_EXIT,
3748 "restoring original video autoswitch mode\n");
3749 if (video_autosw_set(video_orig_autosw))
3750 printk(TPACPI_ERR "error while trying to restore original "
3751 "video autoswitch mode\n");
3754 static int video_outputsw_get(void)
3756 int status = 0;
3757 int i;
3759 switch (video_supported) {
3760 case TPACPI_VIDEO_570:
3761 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3762 TP_ACPI_VIDEO_570_PHSCMD))
3763 return -EIO;
3764 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3765 break;
3766 case TPACPI_VIDEO_770:
3767 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3768 return -EIO;
3769 if (i)
3770 status |= TP_ACPI_VIDEO_S_LCD;
3771 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3772 return -EIO;
3773 if (i)
3774 status |= TP_ACPI_VIDEO_S_CRT;
3775 break;
3776 case TPACPI_VIDEO_NEW:
3777 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3778 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3779 return -EIO;
3780 if (i)
3781 status |= TP_ACPI_VIDEO_S_CRT;
3783 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3784 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3785 return -EIO;
3786 if (i)
3787 status |= TP_ACPI_VIDEO_S_LCD;
3788 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3789 return -EIO;
3790 if (i)
3791 status |= TP_ACPI_VIDEO_S_DVI;
3792 break;
3793 default:
3794 return -ENOSYS;
3797 return status;
3800 static int video_outputsw_set(int status)
3802 int autosw;
3803 int res = 0;
3805 switch (video_supported) {
3806 case TPACPI_VIDEO_570:
3807 res = acpi_evalf(NULL, NULL,
3808 "\\_SB.PHS2", "vdd",
3809 TP_ACPI_VIDEO_570_PHS2CMD,
3810 status | TP_ACPI_VIDEO_570_PHS2SET);
3811 break;
3812 case TPACPI_VIDEO_770:
3813 autosw = video_autosw_get();
3814 if (autosw < 0)
3815 return autosw;
3817 res = video_autosw_set(1);
3818 if (res)
3819 return res;
3820 res = acpi_evalf(vid_handle, NULL,
3821 "ASWT", "vdd", status * 0x100, 0);
3822 if (!autosw && video_autosw_set(autosw)) {
3823 printk(TPACPI_ERR
3824 "video auto-switch left enabled due to error\n");
3825 return -EIO;
3827 break;
3828 case TPACPI_VIDEO_NEW:
3829 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3830 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3831 break;
3832 default:
3833 return -ENOSYS;
3836 return (res)? 0 : -EIO;
3839 static int video_autosw_get(void)
3841 int autosw = 0;
3843 switch (video_supported) {
3844 case TPACPI_VIDEO_570:
3845 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3846 return -EIO;
3847 break;
3848 case TPACPI_VIDEO_770:
3849 case TPACPI_VIDEO_NEW:
3850 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3851 return -EIO;
3852 break;
3853 default:
3854 return -ENOSYS;
3857 return autosw & 1;
3860 static int video_autosw_set(int enable)
3862 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3863 return -EIO;
3864 return 0;
3867 static int video_outputsw_cycle(void)
3869 int autosw = video_autosw_get();
3870 int res;
3872 if (autosw < 0)
3873 return autosw;
3875 switch (video_supported) {
3876 case TPACPI_VIDEO_570:
3877 res = video_autosw_set(1);
3878 if (res)
3879 return res;
3880 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3881 break;
3882 case TPACPI_VIDEO_770:
3883 case TPACPI_VIDEO_NEW:
3884 res = video_autosw_set(1);
3885 if (res)
3886 return res;
3887 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3888 break;
3889 default:
3890 return -ENOSYS;
3892 if (!autosw && video_autosw_set(autosw)) {
3893 printk(TPACPI_ERR
3894 "video auto-switch left enabled due to error\n");
3895 return -EIO;
3898 return (res)? 0 : -EIO;
3901 static int video_expand_toggle(void)
3903 switch (video_supported) {
3904 case TPACPI_VIDEO_570:
3905 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3906 0 : -EIO;
3907 case TPACPI_VIDEO_770:
3908 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3909 0 : -EIO;
3910 case TPACPI_VIDEO_NEW:
3911 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3912 0 : -EIO;
3913 default:
3914 return -ENOSYS;
3916 /* not reached */
3919 static int video_read(char *p)
3921 int status, autosw;
3922 int len = 0;
3924 if (video_supported == TPACPI_VIDEO_NONE) {
3925 len += sprintf(p + len, "status:\t\tnot supported\n");
3926 return len;
3929 status = video_outputsw_get();
3930 if (status < 0)
3931 return status;
3933 autosw = video_autosw_get();
3934 if (autosw < 0)
3935 return autosw;
3937 len += sprintf(p + len, "status:\t\tsupported\n");
3938 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3939 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3940 if (video_supported == TPACPI_VIDEO_NEW)
3941 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3942 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3943 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3944 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3945 if (video_supported == TPACPI_VIDEO_NEW)
3946 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3947 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3948 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3950 return len;
3953 static int video_write(char *buf)
3955 char *cmd;
3956 int enable, disable, status;
3957 int res;
3959 if (video_supported == TPACPI_VIDEO_NONE)
3960 return -ENODEV;
3962 enable = 0;
3963 disable = 0;
3965 while ((cmd = next_cmd(&buf))) {
3966 if (strlencmp(cmd, "lcd_enable") == 0) {
3967 enable |= TP_ACPI_VIDEO_S_LCD;
3968 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3969 disable |= TP_ACPI_VIDEO_S_LCD;
3970 } else if (strlencmp(cmd, "crt_enable") == 0) {
3971 enable |= TP_ACPI_VIDEO_S_CRT;
3972 } else if (strlencmp(cmd, "crt_disable") == 0) {
3973 disable |= TP_ACPI_VIDEO_S_CRT;
3974 } else if (video_supported == TPACPI_VIDEO_NEW &&
3975 strlencmp(cmd, "dvi_enable") == 0) {
3976 enable |= TP_ACPI_VIDEO_S_DVI;
3977 } else if (video_supported == TPACPI_VIDEO_NEW &&
3978 strlencmp(cmd, "dvi_disable") == 0) {
3979 disable |= TP_ACPI_VIDEO_S_DVI;
3980 } else if (strlencmp(cmd, "auto_enable") == 0) {
3981 res = video_autosw_set(1);
3982 if (res)
3983 return res;
3984 } else if (strlencmp(cmd, "auto_disable") == 0) {
3985 res = video_autosw_set(0);
3986 if (res)
3987 return res;
3988 } else if (strlencmp(cmd, "video_switch") == 0) {
3989 res = video_outputsw_cycle();
3990 if (res)
3991 return res;
3992 } else if (strlencmp(cmd, "expand_toggle") == 0) {
3993 res = video_expand_toggle();
3994 if (res)
3995 return res;
3996 } else
3997 return -EINVAL;
4000 if (enable || disable) {
4001 status = video_outputsw_get();
4002 if (status < 0)
4003 return status;
4004 res = video_outputsw_set((status & ~disable) | enable);
4005 if (res)
4006 return res;
4009 return 0;
4012 static struct ibm_struct video_driver_data = {
4013 .name = "video",
4014 .read = video_read,
4015 .write = video_write,
4016 .exit = video_exit,
4019 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4021 /*************************************************************************
4022 * Light (thinklight) subdriver
4025 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4026 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4028 static int light_get_status(void)
4030 int status = 0;
4032 if (tp_features.light_status) {
4033 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4034 return -EIO;
4035 return (!!status);
4038 return -ENXIO;
4041 static int light_set_status(int status)
4043 int rc;
4045 if (tp_features.light) {
4046 if (cmos_handle) {
4047 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4048 (status)?
4049 TP_CMOS_THINKLIGHT_ON :
4050 TP_CMOS_THINKLIGHT_OFF);
4051 } else {
4052 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4053 (status)? 1 : 0);
4055 return (rc)? 0 : -EIO;
4058 return -ENXIO;
4061 static void light_set_status_worker(struct work_struct *work)
4063 struct tpacpi_led_classdev *data =
4064 container_of(work, struct tpacpi_led_classdev, work);
4066 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4067 light_set_status((data->new_brightness != LED_OFF));
4070 static void light_sysfs_set(struct led_classdev *led_cdev,
4071 enum led_brightness brightness)
4073 struct tpacpi_led_classdev *data =
4074 container_of(led_cdev,
4075 struct tpacpi_led_classdev,
4076 led_classdev);
4077 data->new_brightness = brightness;
4078 queue_work(tpacpi_wq, &data->work);
4081 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4083 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4086 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4087 .led_classdev = {
4088 .name = "tpacpi::thinklight",
4089 .brightness_set = &light_sysfs_set,
4090 .brightness_get = &light_sysfs_get,
4094 static int __init light_init(struct ibm_init_struct *iibm)
4096 int rc;
4098 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4100 TPACPI_ACPIHANDLE_INIT(ledb);
4101 TPACPI_ACPIHANDLE_INIT(lght);
4102 TPACPI_ACPIHANDLE_INIT(cmos);
4103 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4105 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4106 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4108 if (tp_features.light)
4109 /* light status not supported on
4110 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4111 tp_features.light_status =
4112 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4114 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4115 str_supported(tp_features.light),
4116 str_supported(tp_features.light_status));
4118 if (!tp_features.light)
4119 return 1;
4121 rc = led_classdev_register(&tpacpi_pdev->dev,
4122 &tpacpi_led_thinklight.led_classdev);
4124 if (rc < 0) {
4125 tp_features.light = 0;
4126 tp_features.light_status = 0;
4127 } else {
4128 rc = 0;
4131 return rc;
4134 static void light_exit(void)
4136 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4137 if (work_pending(&tpacpi_led_thinklight.work))
4138 flush_workqueue(tpacpi_wq);
4141 static int light_read(char *p)
4143 int len = 0;
4144 int status;
4146 if (!tp_features.light) {
4147 len += sprintf(p + len, "status:\t\tnot supported\n");
4148 } else if (!tp_features.light_status) {
4149 len += sprintf(p + len, "status:\t\tunknown\n");
4150 len += sprintf(p + len, "commands:\ton, off\n");
4151 } else {
4152 status = light_get_status();
4153 if (status < 0)
4154 return status;
4155 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4156 len += sprintf(p + len, "commands:\ton, off\n");
4159 return len;
4162 static int light_write(char *buf)
4164 char *cmd;
4165 int newstatus = 0;
4167 if (!tp_features.light)
4168 return -ENODEV;
4170 while ((cmd = next_cmd(&buf))) {
4171 if (strlencmp(cmd, "on") == 0) {
4172 newstatus = 1;
4173 } else if (strlencmp(cmd, "off") == 0) {
4174 newstatus = 0;
4175 } else
4176 return -EINVAL;
4179 return light_set_status(newstatus);
4182 static struct ibm_struct light_driver_data = {
4183 .name = "light",
4184 .read = light_read,
4185 .write = light_write,
4186 .exit = light_exit,
4189 /*************************************************************************
4190 * Dock subdriver
4193 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4195 static void dock_notify(struct ibm_struct *ibm, u32 event);
4196 static int dock_read(char *p);
4197 static int dock_write(char *buf);
4199 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4200 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4201 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4202 "\\_SB.PCI.ISA.SLCE", /* 570 */
4203 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4205 /* don't list other alternatives as we install a notify handler on the 570 */
4206 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4208 static const struct acpi_device_id ibm_pci_device_ids[] = {
4209 {PCI_ROOT_HID_STRING, 0},
4210 {"", 0},
4213 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4215 .notify = dock_notify,
4216 .handle = &dock_handle,
4217 .type = ACPI_SYSTEM_NOTIFY,
4220 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4221 * We just use it to get notifications of dock hotplug
4222 * in very old thinkpads */
4223 .hid = ibm_pci_device_ids,
4224 .notify = dock_notify,
4225 .handle = &pci_handle,
4226 .type = ACPI_SYSTEM_NOTIFY,
4230 static struct ibm_struct dock_driver_data[2] = {
4232 .name = "dock",
4233 .read = dock_read,
4234 .write = dock_write,
4235 .acpi = &ibm_dock_acpidriver[0],
4238 .name = "dock",
4239 .acpi = &ibm_dock_acpidriver[1],
4243 #define dock_docked() (_sta(dock_handle) & 1)
4245 static int __init dock_init(struct ibm_init_struct *iibm)
4247 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4249 TPACPI_ACPIHANDLE_INIT(dock);
4251 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4252 str_supported(dock_handle != NULL));
4254 return (dock_handle)? 0 : 1;
4257 static int __init dock_init2(struct ibm_init_struct *iibm)
4259 int dock2_needed;
4261 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4263 if (dock_driver_data[0].flags.acpi_driver_registered &&
4264 dock_driver_data[0].flags.acpi_notify_installed) {
4265 TPACPI_ACPIHANDLE_INIT(pci);
4266 dock2_needed = (pci_handle != NULL);
4267 vdbg_printk(TPACPI_DBG_INIT,
4268 "dock PCI handler for the TP 570 is %s\n",
4269 str_supported(dock2_needed));
4270 } else {
4271 vdbg_printk(TPACPI_DBG_INIT,
4272 "dock subdriver part 2 not required\n");
4273 dock2_needed = 0;
4276 return (dock2_needed)? 0 : 1;
4279 static void dock_notify(struct ibm_struct *ibm, u32 event)
4281 int docked = dock_docked();
4282 int pci = ibm->acpi->hid && ibm->acpi->device &&
4283 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4284 int data;
4286 if (event == 1 && !pci) /* 570 */
4287 data = 1; /* button */
4288 else if (event == 1 && pci) /* 570 */
4289 data = 3; /* dock */
4290 else if (event == 3 && docked)
4291 data = 1; /* button */
4292 else if (event == 3 && !docked)
4293 data = 2; /* undock */
4294 else if (event == 0 && docked)
4295 data = 3; /* dock */
4296 else {
4297 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4298 event, _sta(dock_handle));
4299 data = 0; /* unknown */
4301 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4302 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4303 dev_name(&ibm->acpi->device->dev),
4304 event, data);
4307 static int dock_read(char *p)
4309 int len = 0;
4310 int docked = dock_docked();
4312 if (!dock_handle)
4313 len += sprintf(p + len, "status:\t\tnot supported\n");
4314 else if (!docked)
4315 len += sprintf(p + len, "status:\t\tundocked\n");
4316 else {
4317 len += sprintf(p + len, "status:\t\tdocked\n");
4318 len += sprintf(p + len, "commands:\tdock, undock\n");
4321 return len;
4324 static int dock_write(char *buf)
4326 char *cmd;
4328 if (!dock_docked())
4329 return -ENODEV;
4331 while ((cmd = next_cmd(&buf))) {
4332 if (strlencmp(cmd, "undock") == 0) {
4333 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4334 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4335 return -EIO;
4336 } else if (strlencmp(cmd, "dock") == 0) {
4337 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4338 return -EIO;
4339 } else
4340 return -EINVAL;
4343 return 0;
4346 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4348 /*************************************************************************
4349 * Bay subdriver
4352 #ifdef CONFIG_THINKPAD_ACPI_BAY
4354 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4355 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4356 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4357 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4358 ); /* A21e, R30, R31 */
4359 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4360 "_EJ0", /* all others */
4361 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4362 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4363 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4364 ); /* all others */
4365 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4366 "_EJ0", /* 770x */
4367 ); /* all others */
4369 static int __init bay_init(struct ibm_init_struct *iibm)
4371 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4373 TPACPI_ACPIHANDLE_INIT(bay);
4374 if (bay_handle)
4375 TPACPI_ACPIHANDLE_INIT(bay_ej);
4376 TPACPI_ACPIHANDLE_INIT(bay2);
4377 if (bay2_handle)
4378 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4380 tp_features.bay_status = bay_handle &&
4381 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4382 tp_features.bay_status2 = bay2_handle &&
4383 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4385 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4386 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4387 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4388 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4390 vdbg_printk(TPACPI_DBG_INIT,
4391 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4392 str_supported(tp_features.bay_status),
4393 str_supported(tp_features.bay_eject),
4394 str_supported(tp_features.bay_status2),
4395 str_supported(tp_features.bay_eject2));
4397 return (tp_features.bay_status || tp_features.bay_eject ||
4398 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4401 static void bay_notify(struct ibm_struct *ibm, u32 event)
4403 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4404 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4405 dev_name(&ibm->acpi->device->dev),
4406 event, 0);
4409 #define bay_occupied(b) (_sta(b##_handle) & 1)
4411 static int bay_read(char *p)
4413 int len = 0;
4414 int occupied = bay_occupied(bay);
4415 int occupied2 = bay_occupied(bay2);
4416 int eject, eject2;
4418 len += sprintf(p + len, "status:\t\t%s\n",
4419 tp_features.bay_status ?
4420 (occupied ? "occupied" : "unoccupied") :
4421 "not supported");
4422 if (tp_features.bay_status2)
4423 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4424 "occupied" : "unoccupied");
4426 eject = tp_features.bay_eject && occupied;
4427 eject2 = tp_features.bay_eject2 && occupied2;
4429 if (eject && eject2)
4430 len += sprintf(p + len, "commands:\teject, eject2\n");
4431 else if (eject)
4432 len += sprintf(p + len, "commands:\teject\n");
4433 else if (eject2)
4434 len += sprintf(p + len, "commands:\teject2\n");
4436 return len;
4439 static int bay_write(char *buf)
4441 char *cmd;
4443 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4444 return -ENODEV;
4446 while ((cmd = next_cmd(&buf))) {
4447 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4448 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4449 return -EIO;
4450 } else if (tp_features.bay_eject2 &&
4451 strlencmp(cmd, "eject2") == 0) {
4452 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4453 return -EIO;
4454 } else
4455 return -EINVAL;
4458 return 0;
4461 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4462 .notify = bay_notify,
4463 .handle = &bay_handle,
4464 .type = ACPI_SYSTEM_NOTIFY,
4467 static struct ibm_struct bay_driver_data = {
4468 .name = "bay",
4469 .read = bay_read,
4470 .write = bay_write,
4471 .acpi = &ibm_bay_acpidriver,
4474 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4476 /*************************************************************************
4477 * CMOS subdriver
4480 /* sysfs cmos_command -------------------------------------------------- */
4481 static ssize_t cmos_command_store(struct device *dev,
4482 struct device_attribute *attr,
4483 const char *buf, size_t count)
4485 unsigned long cmos_cmd;
4486 int res;
4488 if (parse_strtoul(buf, 21, &cmos_cmd))
4489 return -EINVAL;
4491 res = issue_thinkpad_cmos_command(cmos_cmd);
4492 return (res)? res : count;
4495 static struct device_attribute dev_attr_cmos_command =
4496 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4498 /* --------------------------------------------------------------------- */
4500 static int __init cmos_init(struct ibm_init_struct *iibm)
4502 int res;
4504 vdbg_printk(TPACPI_DBG_INIT,
4505 "initializing cmos commands subdriver\n");
4507 TPACPI_ACPIHANDLE_INIT(cmos);
4509 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4510 str_supported(cmos_handle != NULL));
4512 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4513 if (res)
4514 return res;
4516 return (cmos_handle)? 0 : 1;
4519 static void cmos_exit(void)
4521 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4524 static int cmos_read(char *p)
4526 int len = 0;
4528 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4529 R30, R31, T20-22, X20-21 */
4530 if (!cmos_handle)
4531 len += sprintf(p + len, "status:\t\tnot supported\n");
4532 else {
4533 len += sprintf(p + len, "status:\t\tsupported\n");
4534 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4537 return len;
4540 static int cmos_write(char *buf)
4542 char *cmd;
4543 int cmos_cmd, res;
4545 while ((cmd = next_cmd(&buf))) {
4546 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4547 cmos_cmd >= 0 && cmos_cmd <= 21) {
4548 /* cmos_cmd set */
4549 } else
4550 return -EINVAL;
4552 res = issue_thinkpad_cmos_command(cmos_cmd);
4553 if (res)
4554 return res;
4557 return 0;
4560 static struct ibm_struct cmos_driver_data = {
4561 .name = "cmos",
4562 .read = cmos_read,
4563 .write = cmos_write,
4564 .exit = cmos_exit,
4567 /*************************************************************************
4568 * LED subdriver
4571 enum led_access_mode {
4572 TPACPI_LED_NONE = 0,
4573 TPACPI_LED_570, /* 570 */
4574 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4575 TPACPI_LED_NEW, /* all others */
4578 enum { /* For TPACPI_LED_OLD */
4579 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4580 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4581 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4584 enum led_status_t {
4585 TPACPI_LED_OFF = 0,
4586 TPACPI_LED_ON,
4587 TPACPI_LED_BLINK,
4590 static enum led_access_mode led_supported;
4592 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4593 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4594 /* T20-22, X20-21 */
4595 "LED", /* all others */
4596 ); /* R30, R31 */
4598 #define TPACPI_LED_NUMLEDS 8
4599 static struct tpacpi_led_classdev *tpacpi_leds;
4600 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4601 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4602 /* there's a limit of 19 chars + NULL before 2.6.26 */
4603 "tpacpi::power",
4604 "tpacpi:orange:batt",
4605 "tpacpi:green:batt",
4606 "tpacpi::dock_active",
4607 "tpacpi::bay_active",
4608 "tpacpi::dock_batt",
4609 "tpacpi::unknown_led",
4610 "tpacpi::standby",
4613 static int led_get_status(const unsigned int led)
4615 int status;
4616 enum led_status_t led_s;
4618 switch (led_supported) {
4619 case TPACPI_LED_570:
4620 if (!acpi_evalf(ec_handle,
4621 &status, "GLED", "dd", 1 << led))
4622 return -EIO;
4623 led_s = (status == 0)?
4624 TPACPI_LED_OFF :
4625 ((status == 1)?
4626 TPACPI_LED_ON :
4627 TPACPI_LED_BLINK);
4628 tpacpi_led_state_cache[led] = led_s;
4629 return led_s;
4630 default:
4631 return -ENXIO;
4634 /* not reached */
4637 static int led_set_status(const unsigned int led,
4638 const enum led_status_t ledstatus)
4640 /* off, on, blink. Index is led_status_t */
4641 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4642 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4644 int rc = 0;
4646 switch (led_supported) {
4647 case TPACPI_LED_570:
4648 /* 570 */
4649 if (led > 7)
4650 return -EINVAL;
4651 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4652 (1 << led), led_sled_arg1[ledstatus]))
4653 rc = -EIO;
4654 break;
4655 case TPACPI_LED_OLD:
4656 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4657 if (led > 7)
4658 return -EINVAL;
4659 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4660 if (rc >= 0)
4661 rc = ec_write(TPACPI_LED_EC_HLBL,
4662 (ledstatus == TPACPI_LED_BLINK) << led);
4663 if (rc >= 0)
4664 rc = ec_write(TPACPI_LED_EC_HLCL,
4665 (ledstatus != TPACPI_LED_OFF) << led);
4666 break;
4667 case TPACPI_LED_NEW:
4668 /* all others */
4669 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4670 led, led_led_arg1[ledstatus]))
4671 rc = -EIO;
4672 break;
4673 default:
4674 rc = -ENXIO;
4677 if (!rc)
4678 tpacpi_led_state_cache[led] = ledstatus;
4680 return rc;
4683 static void led_sysfs_set_status(unsigned int led,
4684 enum led_brightness brightness)
4686 led_set_status(led,
4687 (brightness == LED_OFF) ?
4688 TPACPI_LED_OFF :
4689 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4690 TPACPI_LED_BLINK : TPACPI_LED_ON);
4693 static void led_set_status_worker(struct work_struct *work)
4695 struct tpacpi_led_classdev *data =
4696 container_of(work, struct tpacpi_led_classdev, work);
4698 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4699 led_sysfs_set_status(data->led, data->new_brightness);
4702 static void led_sysfs_set(struct led_classdev *led_cdev,
4703 enum led_brightness brightness)
4705 struct tpacpi_led_classdev *data = container_of(led_cdev,
4706 struct tpacpi_led_classdev, led_classdev);
4708 data->new_brightness = brightness;
4709 queue_work(tpacpi_wq, &data->work);
4712 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4713 unsigned long *delay_on, unsigned long *delay_off)
4715 struct tpacpi_led_classdev *data = container_of(led_cdev,
4716 struct tpacpi_led_classdev, led_classdev);
4718 /* Can we choose the flash rate? */
4719 if (*delay_on == 0 && *delay_off == 0) {
4720 /* yes. set them to the hardware blink rate (1 Hz) */
4721 *delay_on = 500; /* ms */
4722 *delay_off = 500; /* ms */
4723 } else if ((*delay_on != 500) || (*delay_off != 500))
4724 return -EINVAL;
4726 data->new_brightness = TPACPI_LED_BLINK;
4727 queue_work(tpacpi_wq, &data->work);
4729 return 0;
4732 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4734 int rc;
4736 struct tpacpi_led_classdev *data = container_of(led_cdev,
4737 struct tpacpi_led_classdev, led_classdev);
4739 rc = led_get_status(data->led);
4741 if (rc == TPACPI_LED_OFF || rc < 0)
4742 rc = LED_OFF; /* no error handling in led class :( */
4743 else
4744 rc = LED_FULL;
4746 return rc;
4749 static void led_exit(void)
4751 unsigned int i;
4753 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4754 if (tpacpi_leds[i].led_classdev.name)
4755 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4758 kfree(tpacpi_leds);
4761 static int __init led_init(struct ibm_init_struct *iibm)
4763 unsigned int i;
4764 int rc;
4766 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4768 TPACPI_ACPIHANDLE_INIT(led);
4770 if (!led_handle)
4771 /* led not supported on R30, R31 */
4772 led_supported = TPACPI_LED_NONE;
4773 else if (strlencmp(led_path, "SLED") == 0)
4774 /* 570 */
4775 led_supported = TPACPI_LED_570;
4776 else if (strlencmp(led_path, "SYSL") == 0)
4777 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4778 led_supported = TPACPI_LED_OLD;
4779 else
4780 /* all others */
4781 led_supported = TPACPI_LED_NEW;
4783 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4784 str_supported(led_supported), led_supported);
4786 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4787 GFP_KERNEL);
4788 if (!tpacpi_leds) {
4789 printk(TPACPI_ERR "Out of memory for LED data\n");
4790 return -ENOMEM;
4793 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4794 tpacpi_leds[i].led = i;
4796 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4797 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4798 if (led_supported == TPACPI_LED_570)
4799 tpacpi_leds[i].led_classdev.brightness_get =
4800 &led_sysfs_get;
4802 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4804 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4806 rc = led_classdev_register(&tpacpi_pdev->dev,
4807 &tpacpi_leds[i].led_classdev);
4808 if (rc < 0) {
4809 tpacpi_leds[i].led_classdev.name = NULL;
4810 led_exit();
4811 return rc;
4815 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4818 #define str_led_status(s) \
4819 ((s) == TPACPI_LED_OFF ? "off" : \
4820 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4822 static int led_read(char *p)
4824 int len = 0;
4826 if (!led_supported) {
4827 len += sprintf(p + len, "status:\t\tnot supported\n");
4828 return len;
4830 len += sprintf(p + len, "status:\t\tsupported\n");
4832 if (led_supported == TPACPI_LED_570) {
4833 /* 570 */
4834 int i, status;
4835 for (i = 0; i < 8; i++) {
4836 status = led_get_status(i);
4837 if (status < 0)
4838 return -EIO;
4839 len += sprintf(p + len, "%d:\t\t%s\n",
4840 i, str_led_status(status));
4844 len += sprintf(p + len, "commands:\t"
4845 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4847 return len;
4850 static int led_write(char *buf)
4852 char *cmd;
4853 int led, rc;
4854 enum led_status_t s;
4856 if (!led_supported)
4857 return -ENODEV;
4859 while ((cmd = next_cmd(&buf))) {
4860 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4861 return -EINVAL;
4863 if (strstr(cmd, "off")) {
4864 s = TPACPI_LED_OFF;
4865 } else if (strstr(cmd, "on")) {
4866 s = TPACPI_LED_ON;
4867 } else if (strstr(cmd, "blink")) {
4868 s = TPACPI_LED_BLINK;
4869 } else {
4870 return -EINVAL;
4873 rc = led_set_status(led, s);
4874 if (rc < 0)
4875 return rc;
4878 return 0;
4881 static struct ibm_struct led_driver_data = {
4882 .name = "led",
4883 .read = led_read,
4884 .write = led_write,
4885 .exit = led_exit,
4888 /*************************************************************************
4889 * Beep subdriver
4892 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4894 static int __init beep_init(struct ibm_init_struct *iibm)
4896 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4898 TPACPI_ACPIHANDLE_INIT(beep);
4900 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4901 str_supported(beep_handle != NULL));
4903 return (beep_handle)? 0 : 1;
4906 static int beep_read(char *p)
4908 int len = 0;
4910 if (!beep_handle)
4911 len += sprintf(p + len, "status:\t\tnot supported\n");
4912 else {
4913 len += sprintf(p + len, "status:\t\tsupported\n");
4914 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4917 return len;
4920 static int beep_write(char *buf)
4922 char *cmd;
4923 int beep_cmd;
4925 if (!beep_handle)
4926 return -ENODEV;
4928 while ((cmd = next_cmd(&buf))) {
4929 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4930 beep_cmd >= 0 && beep_cmd <= 17) {
4931 /* beep_cmd set */
4932 } else
4933 return -EINVAL;
4934 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4935 return -EIO;
4938 return 0;
4941 static struct ibm_struct beep_driver_data = {
4942 .name = "beep",
4943 .read = beep_read,
4944 .write = beep_write,
4947 /*************************************************************************
4948 * Thermal subdriver
4951 enum thermal_access_mode {
4952 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4953 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4954 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4955 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4956 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4959 enum { /* TPACPI_THERMAL_TPEC_* */
4960 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4961 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4962 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4965 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4966 struct ibm_thermal_sensors_struct {
4967 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4970 static enum thermal_access_mode thermal_read_mode;
4972 /* idx is zero-based */
4973 static int thermal_get_sensor(int idx, s32 *value)
4975 int t;
4976 s8 tmp;
4977 char tmpi[5];
4979 t = TP_EC_THERMAL_TMP0;
4981 switch (thermal_read_mode) {
4982 #if TPACPI_MAX_THERMAL_SENSORS >= 16
4983 case TPACPI_THERMAL_TPEC_16:
4984 if (idx >= 8 && idx <= 15) {
4985 t = TP_EC_THERMAL_TMP8;
4986 idx -= 8;
4988 /* fallthrough */
4989 #endif
4990 case TPACPI_THERMAL_TPEC_8:
4991 if (idx <= 7) {
4992 if (!acpi_ec_read(t + idx, &tmp))
4993 return -EIO;
4994 *value = tmp * 1000;
4995 return 0;
4997 break;
4999 case TPACPI_THERMAL_ACPI_UPDT:
5000 if (idx <= 7) {
5001 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5002 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5003 return -EIO;
5004 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5005 return -EIO;
5006 *value = (t - 2732) * 100;
5007 return 0;
5009 break;
5011 case TPACPI_THERMAL_ACPI_TMP07:
5012 if (idx <= 7) {
5013 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5014 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5015 return -EIO;
5016 if (t > 127 || t < -127)
5017 t = TP_EC_THERMAL_TMP_NA;
5018 *value = t * 1000;
5019 return 0;
5021 break;
5023 case TPACPI_THERMAL_NONE:
5024 default:
5025 return -ENOSYS;
5028 return -EINVAL;
5031 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5033 int res, i;
5034 int n;
5036 n = 8;
5037 i = 0;
5039 if (!s)
5040 return -EINVAL;
5042 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5043 n = 16;
5045 for (i = 0 ; i < n; i++) {
5046 res = thermal_get_sensor(i, &s->temp[i]);
5047 if (res)
5048 return res;
5051 return n;
5054 /* sysfs temp##_input -------------------------------------------------- */
5056 static ssize_t thermal_temp_input_show(struct device *dev,
5057 struct device_attribute *attr,
5058 char *buf)
5060 struct sensor_device_attribute *sensor_attr =
5061 to_sensor_dev_attr(attr);
5062 int idx = sensor_attr->index;
5063 s32 value;
5064 int res;
5066 res = thermal_get_sensor(idx, &value);
5067 if (res)
5068 return res;
5069 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5070 return -ENXIO;
5072 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5075 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5076 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5077 thermal_temp_input_show, NULL, _idxB)
5079 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5080 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5081 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5082 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5083 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5084 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5085 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5086 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5087 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5088 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5089 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5090 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5091 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5092 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5093 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5094 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5095 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5098 #define THERMAL_ATTRS(X) \
5099 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5101 static struct attribute *thermal_temp_input_attr[] = {
5102 THERMAL_ATTRS(8),
5103 THERMAL_ATTRS(9),
5104 THERMAL_ATTRS(10),
5105 THERMAL_ATTRS(11),
5106 THERMAL_ATTRS(12),
5107 THERMAL_ATTRS(13),
5108 THERMAL_ATTRS(14),
5109 THERMAL_ATTRS(15),
5110 THERMAL_ATTRS(0),
5111 THERMAL_ATTRS(1),
5112 THERMAL_ATTRS(2),
5113 THERMAL_ATTRS(3),
5114 THERMAL_ATTRS(4),
5115 THERMAL_ATTRS(5),
5116 THERMAL_ATTRS(6),
5117 THERMAL_ATTRS(7),
5118 NULL
5121 static const struct attribute_group thermal_temp_input16_group = {
5122 .attrs = thermal_temp_input_attr
5125 static const struct attribute_group thermal_temp_input8_group = {
5126 .attrs = &thermal_temp_input_attr[8]
5129 #undef THERMAL_SENSOR_ATTR_TEMP
5130 #undef THERMAL_ATTRS
5132 /* --------------------------------------------------------------------- */
5134 static int __init thermal_init(struct ibm_init_struct *iibm)
5136 u8 t, ta1, ta2;
5137 int i;
5138 int acpi_tmp7;
5139 int res;
5141 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5143 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5145 if (thinkpad_id.ec_model) {
5147 * Direct EC access mode: sensors at registers
5148 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5149 * non-implemented, thermal sensors return 0x80 when
5150 * not available
5153 ta1 = ta2 = 0;
5154 for (i = 0; i < 8; i++) {
5155 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5156 ta1 |= t;
5157 } else {
5158 ta1 = 0;
5159 break;
5161 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5162 ta2 |= t;
5163 } else {
5164 ta1 = 0;
5165 break;
5168 if (ta1 == 0) {
5169 /* This is sheer paranoia, but we handle it anyway */
5170 if (acpi_tmp7) {
5171 printk(TPACPI_ERR
5172 "ThinkPad ACPI EC access misbehaving, "
5173 "falling back to ACPI TMPx access "
5174 "mode\n");
5175 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5176 } else {
5177 printk(TPACPI_ERR
5178 "ThinkPad ACPI EC access misbehaving, "
5179 "disabling thermal sensors access\n");
5180 thermal_read_mode = TPACPI_THERMAL_NONE;
5182 } else {
5183 thermal_read_mode =
5184 (ta2 != 0) ?
5185 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5187 } else if (acpi_tmp7) {
5188 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5189 /* 600e/x, 770e, 770x */
5190 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5191 } else {
5192 /* Standard ACPI TMPx access, max 8 sensors */
5193 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5195 } else {
5196 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5197 thermal_read_mode = TPACPI_THERMAL_NONE;
5200 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5201 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5202 thermal_read_mode);
5204 switch (thermal_read_mode) {
5205 case TPACPI_THERMAL_TPEC_16:
5206 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5207 &thermal_temp_input16_group);
5208 if (res)
5209 return res;
5210 break;
5211 case TPACPI_THERMAL_TPEC_8:
5212 case TPACPI_THERMAL_ACPI_TMP07:
5213 case TPACPI_THERMAL_ACPI_UPDT:
5214 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5215 &thermal_temp_input8_group);
5216 if (res)
5217 return res;
5218 break;
5219 case TPACPI_THERMAL_NONE:
5220 default:
5221 return 1;
5224 return 0;
5227 static void thermal_exit(void)
5229 switch (thermal_read_mode) {
5230 case TPACPI_THERMAL_TPEC_16:
5231 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5232 &thermal_temp_input16_group);
5233 break;
5234 case TPACPI_THERMAL_TPEC_8:
5235 case TPACPI_THERMAL_ACPI_TMP07:
5236 case TPACPI_THERMAL_ACPI_UPDT:
5237 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5238 &thermal_temp_input16_group);
5239 break;
5240 case TPACPI_THERMAL_NONE:
5241 default:
5242 break;
5246 static int thermal_read(char *p)
5248 int len = 0;
5249 int n, i;
5250 struct ibm_thermal_sensors_struct t;
5252 n = thermal_get_sensors(&t);
5253 if (unlikely(n < 0))
5254 return n;
5256 len += sprintf(p + len, "temperatures:\t");
5258 if (n > 0) {
5259 for (i = 0; i < (n - 1); i++)
5260 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5261 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5262 } else
5263 len += sprintf(p + len, "not supported\n");
5265 return len;
5268 static struct ibm_struct thermal_driver_data = {
5269 .name = "thermal",
5270 .read = thermal_read,
5271 .exit = thermal_exit,
5274 /*************************************************************************
5275 * EC Dump subdriver
5278 static u8 ecdump_regs[256];
5280 static int ecdump_read(char *p)
5282 int len = 0;
5283 int i, j;
5284 u8 v;
5286 len += sprintf(p + len, "EC "
5287 " +00 +01 +02 +03 +04 +05 +06 +07"
5288 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5289 for (i = 0; i < 256; i += 16) {
5290 len += sprintf(p + len, "EC 0x%02x:", i);
5291 for (j = 0; j < 16; j++) {
5292 if (!acpi_ec_read(i + j, &v))
5293 break;
5294 if (v != ecdump_regs[i + j])
5295 len += sprintf(p + len, " *%02x", v);
5296 else
5297 len += sprintf(p + len, " %02x", v);
5298 ecdump_regs[i + j] = v;
5300 len += sprintf(p + len, "\n");
5301 if (j != 16)
5302 break;
5305 /* These are way too dangerous to advertise openly... */
5306 #if 0
5307 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5308 " (<offset> is 00-ff, <value> is 00-ff)\n");
5309 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5310 " (<offset> is 00-ff, <value> is 0-255)\n");
5311 #endif
5312 return len;
5315 static int ecdump_write(char *buf)
5317 char *cmd;
5318 int i, v;
5320 while ((cmd = next_cmd(&buf))) {
5321 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5322 /* i and v set */
5323 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5324 /* i and v set */
5325 } else
5326 return -EINVAL;
5327 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5328 if (!acpi_ec_write(i, v))
5329 return -EIO;
5330 } else
5331 return -EINVAL;
5334 return 0;
5337 static struct ibm_struct ecdump_driver_data = {
5338 .name = "ecdump",
5339 .read = ecdump_read,
5340 .write = ecdump_write,
5341 .flags.experimental = 1,
5344 /*************************************************************************
5345 * Backlight/brightness subdriver
5348 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5350 enum {
5351 TP_EC_BACKLIGHT = 0x31,
5353 /* TP_EC_BACKLIGHT bitmasks */
5354 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5355 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5356 TP_EC_BACKLIGHT_MAPSW = 0x20,
5359 static struct backlight_device *ibm_backlight_device;
5360 static int brightness_mode;
5361 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5363 static struct mutex brightness_mutex;
5366 * ThinkPads can read brightness from two places: EC 0x31, or
5367 * CMOS NVRAM byte 0x5E, bits 0-3.
5369 * EC 0x31 has the following layout
5370 * Bit 7: unknown function
5371 * Bit 6: unknown function
5372 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5373 * Bit 4: must be set to zero to avoid problems
5374 * Bit 3-0: backlight brightness level
5376 * brightness_get_raw returns status data in the EC 0x31 layout
5378 static int brightness_get_raw(int *status)
5380 u8 lec = 0, lcmos = 0, level = 0;
5382 if (brightness_mode & 1) {
5383 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
5384 return -EIO;
5385 level = lec & TP_EC_BACKLIGHT_LVLMSK;
5387 if (brightness_mode & 2) {
5388 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5389 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5390 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5391 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
5392 level = lcmos;
5395 if (brightness_mode == 3) {
5396 *status = lec; /* Prefer EC, CMOS is just a backing store */
5397 lec &= TP_EC_BACKLIGHT_LVLMSK;
5398 if (lec == lcmos)
5399 tp_warned.bright_cmos_ec_unsync = 0;
5400 else {
5401 if (!tp_warned.bright_cmos_ec_unsync) {
5402 printk(TPACPI_ERR
5403 "CMOS NVRAM (%u) and EC (%u) do not "
5404 "agree on display brightness level\n",
5405 (unsigned int) lcmos,
5406 (unsigned int) lec);
5407 tp_warned.bright_cmos_ec_unsync = 1;
5409 return -EIO;
5411 } else {
5412 *status = level;
5415 return 0;
5418 /* May return EINTR which can always be mapped to ERESTARTSYS */
5419 static int brightness_set(int value)
5421 int cmos_cmd, inc, i, res;
5422 int current_value;
5423 int command_bits;
5425 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5426 value < 0)
5427 return -EINVAL;
5429 res = mutex_lock_killable(&brightness_mutex);
5430 if (res < 0)
5431 return res;
5433 res = brightness_get_raw(&current_value);
5434 if (res < 0)
5435 goto errout;
5437 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
5438 current_value &= TP_EC_BACKLIGHT_LVLMSK;
5440 cmos_cmd = value > current_value ?
5441 TP_CMOS_BRIGHTNESS_UP :
5442 TP_CMOS_BRIGHTNESS_DOWN;
5443 inc = (value > current_value)? 1 : -1;
5445 res = 0;
5446 for (i = current_value; i != value; i += inc) {
5447 if ((brightness_mode & 2) &&
5448 issue_thinkpad_cmos_command(cmos_cmd)) {
5449 res = -EIO;
5450 goto errout;
5452 if ((brightness_mode & 1) &&
5453 !acpi_ec_write(TP_EC_BACKLIGHT,
5454 (i + inc) | command_bits)) {
5455 res = -EIO;
5456 goto errout;;
5460 errout:
5461 mutex_unlock(&brightness_mutex);
5462 return res;
5465 /* sysfs backlight class ----------------------------------------------- */
5467 static int brightness_update_status(struct backlight_device *bd)
5469 /* it is the backlight class's job (caller) to handle
5470 * EINTR and other errors properly */
5471 return brightness_set(
5472 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5473 bd->props.power == FB_BLANK_UNBLANK) ?
5474 bd->props.brightness : 0);
5477 static int brightness_get(struct backlight_device *bd)
5479 int status, res;
5481 res = brightness_get_raw(&status);
5482 if (res < 0)
5483 return 0; /* FIXME: teach backlight about error handling */
5485 return status & TP_EC_BACKLIGHT_LVLMSK;
5488 static struct backlight_ops ibm_backlight_data = {
5489 .get_brightness = brightness_get,
5490 .update_status = brightness_update_status,
5493 /* --------------------------------------------------------------------- */
5495 static int __init brightness_init(struct ibm_init_struct *iibm)
5497 int b;
5499 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5501 mutex_init(&brightness_mutex);
5504 * We always attempt to detect acpi support, so as to switch
5505 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5506 * going to publish a backlight interface
5508 b = tpacpi_check_std_acpi_brightness_support();
5509 if (b > 0) {
5511 if (acpi_video_backlight_support()) {
5512 if (brightness_enable > 1) {
5513 printk(TPACPI_NOTICE
5514 "Standard ACPI backlight interface "
5515 "available, not loading native one.\n");
5516 return 1;
5517 } else if (brightness_enable == 1) {
5518 printk(TPACPI_NOTICE
5519 "Backlight control force enabled, even if standard "
5520 "ACPI backlight interface is available\n");
5522 } else {
5523 if (brightness_enable > 1) {
5524 printk(TPACPI_NOTICE
5525 "Standard ACPI backlight interface not "
5526 "available, thinkpad_acpi native "
5527 "brightness control enabled\n");
5532 if (!brightness_enable) {
5533 dbg_printk(TPACPI_DBG_INIT,
5534 "brightness support disabled by "
5535 "module parameter\n");
5536 return 1;
5539 if (b > 16) {
5540 printk(TPACPI_ERR
5541 "Unsupported brightness interface, "
5542 "please contact %s\n", TPACPI_MAIL);
5543 return 1;
5545 if (b == 16)
5546 tp_features.bright_16levels = 1;
5548 if (!brightness_mode) {
5549 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
5550 brightness_mode = 2;
5551 else
5552 brightness_mode = 3;
5554 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
5555 brightness_mode);
5558 if (brightness_mode > 3)
5559 return -EINVAL;
5561 if (brightness_get_raw(&b) < 0)
5562 return 1;
5564 if (tp_features.bright_16levels)
5565 printk(TPACPI_INFO
5566 "detected a 16-level brightness capable ThinkPad\n");
5568 ibm_backlight_device = backlight_device_register(
5569 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5570 &ibm_backlight_data);
5571 if (IS_ERR(ibm_backlight_device)) {
5572 printk(TPACPI_ERR "Could not register backlight device\n");
5573 return PTR_ERR(ibm_backlight_device);
5575 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
5577 ibm_backlight_device->props.max_brightness =
5578 (tp_features.bright_16levels)? 15 : 7;
5579 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5580 backlight_update_status(ibm_backlight_device);
5582 return 0;
5585 static void brightness_exit(void)
5587 if (ibm_backlight_device) {
5588 vdbg_printk(TPACPI_DBG_EXIT,
5589 "calling backlight_device_unregister()\n");
5590 backlight_device_unregister(ibm_backlight_device);
5594 static int brightness_read(char *p)
5596 int len = 0;
5597 int level;
5599 level = brightness_get(NULL);
5600 if (level < 0) {
5601 len += sprintf(p + len, "level:\t\tunreadable\n");
5602 } else {
5603 len += sprintf(p + len, "level:\t\t%d\n", level);
5604 len += sprintf(p + len, "commands:\tup, down\n");
5605 len += sprintf(p + len, "commands:\tlevel <level>"
5606 " (<level> is 0-%d)\n",
5607 (tp_features.bright_16levels) ? 15 : 7);
5610 return len;
5613 static int brightness_write(char *buf)
5615 int level;
5616 int rc;
5617 char *cmd;
5618 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5620 level = brightness_get(NULL);
5621 if (level < 0)
5622 return level;
5624 while ((cmd = next_cmd(&buf))) {
5625 if (strlencmp(cmd, "up") == 0) {
5626 if (level < max_level)
5627 level++;
5628 } else if (strlencmp(cmd, "down") == 0) {
5629 if (level > 0)
5630 level--;
5631 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5632 level >= 0 && level <= max_level) {
5633 /* new level set */
5634 } else
5635 return -EINVAL;
5639 * Now we know what the final level should be, so we try to set it.
5640 * Doing it this way makes the syscall restartable in case of EINTR
5642 rc = brightness_set(level);
5643 return (rc == -EINTR)? ERESTARTSYS : rc;
5646 static struct ibm_struct brightness_driver_data = {
5647 .name = "brightness",
5648 .read = brightness_read,
5649 .write = brightness_write,
5650 .exit = brightness_exit,
5653 /*************************************************************************
5654 * Volume subdriver
5657 static int volume_offset = 0x30;
5659 static int volume_read(char *p)
5661 int len = 0;
5662 u8 level;
5664 if (!acpi_ec_read(volume_offset, &level)) {
5665 len += sprintf(p + len, "level:\t\tunreadable\n");
5666 } else {
5667 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5668 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5669 len += sprintf(p + len, "commands:\tup, down, mute\n");
5670 len += sprintf(p + len, "commands:\tlevel <level>"
5671 " (<level> is 0-15)\n");
5674 return len;
5677 static int volume_write(char *buf)
5679 int cmos_cmd, inc, i;
5680 u8 level, mute;
5681 int new_level, new_mute;
5682 char *cmd;
5684 while ((cmd = next_cmd(&buf))) {
5685 if (!acpi_ec_read(volume_offset, &level))
5686 return -EIO;
5687 new_mute = mute = level & 0x40;
5688 new_level = level = level & 0xf;
5690 if (strlencmp(cmd, "up") == 0) {
5691 if (mute)
5692 new_mute = 0;
5693 else
5694 new_level = level == 15 ? 15 : level + 1;
5695 } else if (strlencmp(cmd, "down") == 0) {
5696 if (mute)
5697 new_mute = 0;
5698 else
5699 new_level = level == 0 ? 0 : level - 1;
5700 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5701 new_level >= 0 && new_level <= 15) {
5702 /* new_level set */
5703 } else if (strlencmp(cmd, "mute") == 0) {
5704 new_mute = 0x40;
5705 } else
5706 return -EINVAL;
5708 if (new_level != level) {
5709 /* mute doesn't change */
5711 cmos_cmd = (new_level > level) ?
5712 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5713 inc = new_level > level ? 1 : -1;
5715 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5716 !acpi_ec_write(volume_offset, level)))
5717 return -EIO;
5719 for (i = level; i != new_level; i += inc)
5720 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5721 !acpi_ec_write(volume_offset, i + inc))
5722 return -EIO;
5724 if (mute &&
5725 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5726 !acpi_ec_write(volume_offset, new_level + mute))) {
5727 return -EIO;
5731 if (new_mute != mute) {
5732 /* level doesn't change */
5734 cmos_cmd = (new_mute) ?
5735 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5737 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5738 !acpi_ec_write(volume_offset, level + new_mute))
5739 return -EIO;
5743 return 0;
5746 static struct ibm_struct volume_driver_data = {
5747 .name = "volume",
5748 .read = volume_read,
5749 .write = volume_write,
5752 /*************************************************************************
5753 * Fan subdriver
5757 * FAN ACCESS MODES
5759 * TPACPI_FAN_RD_ACPI_GFAN:
5760 * ACPI GFAN method: returns fan level
5762 * see TPACPI_FAN_WR_ACPI_SFAN
5763 * EC 0x2f (HFSP) not available if GFAN exists
5765 * TPACPI_FAN_WR_ACPI_SFAN:
5766 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5768 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5769 * it for writing.
5771 * TPACPI_FAN_WR_TPEC:
5772 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5773 * Supported on almost all ThinkPads
5775 * Fan speed changes of any sort (including those caused by the
5776 * disengaged mode) are usually done slowly by the firmware as the
5777 * maximum ammount of fan duty cycle change per second seems to be
5778 * limited.
5780 * Reading is not available if GFAN exists.
5781 * Writing is not available if SFAN exists.
5783 * Bits
5784 * 7 automatic mode engaged;
5785 * (default operation mode of the ThinkPad)
5786 * fan level is ignored in this mode.
5787 * 6 full speed mode (takes precedence over bit 7);
5788 * not available on all thinkpads. May disable
5789 * the tachometer while the fan controller ramps up
5790 * the speed (which can take up to a few *minutes*).
5791 * Speeds up fan to 100% duty-cycle, which is far above
5792 * the standard RPM levels. It is not impossible that
5793 * it could cause hardware damage.
5794 * 5-3 unused in some models. Extra bits for fan level
5795 * in others, but still useless as all values above
5796 * 7 map to the same speed as level 7 in these models.
5797 * 2-0 fan level (0..7 usually)
5798 * 0x00 = stop
5799 * 0x07 = max (set when temperatures critical)
5800 * Some ThinkPads may have other levels, see
5801 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5803 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5804 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5805 * does so, its initial value is meaningless (0x07).
5807 * For firmware bugs, refer to:
5808 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5810 * ----
5812 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5813 * Main fan tachometer reading (in RPM)
5815 * This register is present on all ThinkPads with a new-style EC, and
5816 * it is known not to be present on the A21m/e, and T22, as there is
5817 * something else in offset 0x84 according to the ACPI DSDT. Other
5818 * ThinkPads from this same time period (and earlier) probably lack the
5819 * tachometer as well.
5821 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5822 * was never fixed by IBM to report the EC firmware version string
5823 * probably support the tachometer (like the early X models), so
5824 * detecting it is quite hard. We need more data to know for sure.
5826 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5827 * might result.
5829 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5830 * mode.
5832 * For firmware bugs, refer to:
5833 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5835 * TPACPI_FAN_WR_ACPI_FANS:
5836 * ThinkPad X31, X40, X41. Not available in the X60.
5838 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5839 * high speed. ACPI DSDT seems to map these three speeds to levels
5840 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5841 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5843 * The speeds are stored on handles
5844 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5846 * There are three default speed sets, acessible as handles:
5847 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5849 * ACPI DSDT switches which set is in use depending on various
5850 * factors.
5852 * TPACPI_FAN_WR_TPEC is also available and should be used to
5853 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5854 * but the ACPI tables just mention level 7.
5857 enum { /* Fan control constants */
5858 fan_status_offset = 0x2f, /* EC register 0x2f */
5859 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5860 * 0x84 must be read before 0x85 */
5862 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5863 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5865 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5868 enum fan_status_access_mode {
5869 TPACPI_FAN_NONE = 0, /* No fan status or control */
5870 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5871 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5874 enum fan_control_access_mode {
5875 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5876 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5877 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5878 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5881 enum fan_control_commands {
5882 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5883 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5884 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5885 * and also watchdog cmd */
5888 static int fan_control_allowed;
5890 static enum fan_status_access_mode fan_status_access_mode;
5891 static enum fan_control_access_mode fan_control_access_mode;
5892 static enum fan_control_commands fan_control_commands;
5894 static u8 fan_control_initial_status;
5895 static u8 fan_control_desired_level;
5896 static u8 fan_control_resume_level;
5897 static int fan_watchdog_maxinterval;
5899 static struct mutex fan_mutex;
5901 static void fan_watchdog_fire(struct work_struct *ignored);
5902 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
5904 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5905 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
5906 "\\FSPD", /* 600e/x, 770e, 770x */
5907 ); /* all others */
5908 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
5909 "JFNS", /* 770x-JL */
5910 ); /* all others */
5913 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
5914 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
5915 * be in auto mode (0x80).
5917 * This is corrected by any write to HFSP either by the driver, or
5918 * by the firmware.
5920 * We assume 0x07 really means auto mode while this quirk is active,
5921 * as this is far more likely than the ThinkPad being in level 7,
5922 * which is only used by the firmware during thermal emergencies.
5925 static void fan_quirk1_detect(void)
5927 /* In some ThinkPads, neither the EC nor the ACPI
5928 * DSDT initialize the HFSP register, and it ends up
5929 * being initially set to 0x07 when it *could* be
5930 * either 0x07 or 0x80.
5932 * Enable for TP-1Y (T43), TP-78 (R51e),
5933 * TP-76 (R52), TP-70 (T43, R52), which are known
5934 * to be buggy. */
5935 if (fan_control_initial_status == 0x07) {
5936 switch (thinkpad_id.ec_model) {
5937 case 0x5931: /* TP-1Y */
5938 case 0x3837: /* TP-78 */
5939 case 0x3637: /* TP-76 */
5940 case 0x3037: /* TP-70 */
5941 printk(TPACPI_NOTICE
5942 "fan_init: initial fan status is unknown, "
5943 "assuming it is in auto mode\n");
5944 tp_features.fan_ctrl_status_undef = 1;
5950 static void fan_quirk1_handle(u8 *fan_status)
5952 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5953 if (*fan_status != fan_control_initial_status) {
5954 /* something changed the HFSP regisnter since
5955 * driver init time, so it is not undefined
5956 * anymore */
5957 tp_features.fan_ctrl_status_undef = 0;
5958 } else {
5959 /* Return most likely status. In fact, it
5960 * might be the only possible status */
5961 *fan_status = TP_EC_FAN_AUTO;
5967 * Call with fan_mutex held
5969 static void fan_update_desired_level(u8 status)
5971 if ((status &
5972 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5973 if (status > 7)
5974 fan_control_desired_level = 7;
5975 else
5976 fan_control_desired_level = status;
5980 static int fan_get_status(u8 *status)
5982 u8 s;
5984 /* TODO:
5985 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5987 switch (fan_status_access_mode) {
5988 case TPACPI_FAN_RD_ACPI_GFAN:
5989 /* 570, 600e/x, 770e, 770x */
5991 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5992 return -EIO;
5994 if (likely(status))
5995 *status = s & 0x07;
5997 break;
5999 case TPACPI_FAN_RD_TPEC:
6000 /* all except 570, 600e/x, 770e, 770x */
6001 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6002 return -EIO;
6004 if (likely(status)) {
6005 *status = s;
6006 fan_quirk1_handle(status);
6009 break;
6011 default:
6012 return -ENXIO;
6015 return 0;
6018 static int fan_get_status_safe(u8 *status)
6020 int rc;
6021 u8 s;
6023 if (mutex_lock_killable(&fan_mutex))
6024 return -ERESTARTSYS;
6025 rc = fan_get_status(&s);
6026 if (!rc)
6027 fan_update_desired_level(s);
6028 mutex_unlock(&fan_mutex);
6030 if (status)
6031 *status = s;
6033 return rc;
6036 static int fan_get_speed(unsigned int *speed)
6038 u8 hi, lo;
6040 switch (fan_status_access_mode) {
6041 case TPACPI_FAN_RD_TPEC:
6042 /* all except 570, 600e/x, 770e, 770x */
6043 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6044 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6045 return -EIO;
6047 if (likely(speed))
6048 *speed = (hi << 8) | lo;
6050 break;
6052 default:
6053 return -ENXIO;
6056 return 0;
6059 static int fan_set_level(int level)
6061 if (!fan_control_allowed)
6062 return -EPERM;
6064 switch (fan_control_access_mode) {
6065 case TPACPI_FAN_WR_ACPI_SFAN:
6066 if (level >= 0 && level <= 7) {
6067 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6068 return -EIO;
6069 } else
6070 return -EINVAL;
6071 break;
6073 case TPACPI_FAN_WR_ACPI_FANS:
6074 case TPACPI_FAN_WR_TPEC:
6075 if (!(level & TP_EC_FAN_AUTO) &&
6076 !(level & TP_EC_FAN_FULLSPEED) &&
6077 ((level < 0) || (level > 7)))
6078 return -EINVAL;
6080 /* safety net should the EC not support AUTO
6081 * or FULLSPEED mode bits and just ignore them */
6082 if (level & TP_EC_FAN_FULLSPEED)
6083 level |= 7; /* safety min speed 7 */
6084 else if (level & TP_EC_FAN_AUTO)
6085 level |= 4; /* safety min speed 4 */
6087 if (!acpi_ec_write(fan_status_offset, level))
6088 return -EIO;
6089 else
6090 tp_features.fan_ctrl_status_undef = 0;
6091 break;
6093 default:
6094 return -ENXIO;
6096 return 0;
6099 static int fan_set_level_safe(int level)
6101 int rc;
6103 if (!fan_control_allowed)
6104 return -EPERM;
6106 if (mutex_lock_killable(&fan_mutex))
6107 return -ERESTARTSYS;
6109 if (level == TPACPI_FAN_LAST_LEVEL)
6110 level = fan_control_desired_level;
6112 rc = fan_set_level(level);
6113 if (!rc)
6114 fan_update_desired_level(level);
6116 mutex_unlock(&fan_mutex);
6117 return rc;
6120 static int fan_set_enable(void)
6122 u8 s;
6123 int rc;
6125 if (!fan_control_allowed)
6126 return -EPERM;
6128 if (mutex_lock_killable(&fan_mutex))
6129 return -ERESTARTSYS;
6131 switch (fan_control_access_mode) {
6132 case TPACPI_FAN_WR_ACPI_FANS:
6133 case TPACPI_FAN_WR_TPEC:
6134 rc = fan_get_status(&s);
6135 if (rc < 0)
6136 break;
6138 /* Don't go out of emergency fan mode */
6139 if (s != 7) {
6140 s &= 0x07;
6141 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6144 if (!acpi_ec_write(fan_status_offset, s))
6145 rc = -EIO;
6146 else {
6147 tp_features.fan_ctrl_status_undef = 0;
6148 rc = 0;
6150 break;
6152 case TPACPI_FAN_WR_ACPI_SFAN:
6153 rc = fan_get_status(&s);
6154 if (rc < 0)
6155 break;
6157 s &= 0x07;
6159 /* Set fan to at least level 4 */
6160 s |= 4;
6162 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6163 rc = -EIO;
6164 else
6165 rc = 0;
6166 break;
6168 default:
6169 rc = -ENXIO;
6172 mutex_unlock(&fan_mutex);
6173 return rc;
6176 static int fan_set_disable(void)
6178 int rc;
6180 if (!fan_control_allowed)
6181 return -EPERM;
6183 if (mutex_lock_killable(&fan_mutex))
6184 return -ERESTARTSYS;
6186 rc = 0;
6187 switch (fan_control_access_mode) {
6188 case TPACPI_FAN_WR_ACPI_FANS:
6189 case TPACPI_FAN_WR_TPEC:
6190 if (!acpi_ec_write(fan_status_offset, 0x00))
6191 rc = -EIO;
6192 else {
6193 fan_control_desired_level = 0;
6194 tp_features.fan_ctrl_status_undef = 0;
6196 break;
6198 case TPACPI_FAN_WR_ACPI_SFAN:
6199 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6200 rc = -EIO;
6201 else
6202 fan_control_desired_level = 0;
6203 break;
6205 default:
6206 rc = -ENXIO;
6210 mutex_unlock(&fan_mutex);
6211 return rc;
6214 static int fan_set_speed(int speed)
6216 int rc;
6218 if (!fan_control_allowed)
6219 return -EPERM;
6221 if (mutex_lock_killable(&fan_mutex))
6222 return -ERESTARTSYS;
6224 rc = 0;
6225 switch (fan_control_access_mode) {
6226 case TPACPI_FAN_WR_ACPI_FANS:
6227 if (speed >= 0 && speed <= 65535) {
6228 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6229 speed, speed, speed))
6230 rc = -EIO;
6231 } else
6232 rc = -EINVAL;
6233 break;
6235 default:
6236 rc = -ENXIO;
6239 mutex_unlock(&fan_mutex);
6240 return rc;
6243 static void fan_watchdog_reset(void)
6245 static int fan_watchdog_active;
6247 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6248 return;
6250 if (fan_watchdog_active)
6251 cancel_delayed_work(&fan_watchdog_task);
6253 if (fan_watchdog_maxinterval > 0 &&
6254 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6255 fan_watchdog_active = 1;
6256 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6257 msecs_to_jiffies(fan_watchdog_maxinterval
6258 * 1000))) {
6259 printk(TPACPI_ERR
6260 "failed to queue the fan watchdog, "
6261 "watchdog will not trigger\n");
6263 } else
6264 fan_watchdog_active = 0;
6267 static void fan_watchdog_fire(struct work_struct *ignored)
6269 int rc;
6271 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6272 return;
6274 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6275 rc = fan_set_enable();
6276 if (rc < 0) {
6277 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6278 "will try again later...\n", -rc);
6279 /* reschedule for later */
6280 fan_watchdog_reset();
6285 * SYSFS fan layout: hwmon compatible (device)
6287 * pwm*_enable:
6288 * 0: "disengaged" mode
6289 * 1: manual mode
6290 * 2: native EC "auto" mode (recommended, hardware default)
6292 * pwm*: set speed in manual mode, ignored otherwise.
6293 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6294 * interpolation.
6296 * fan*_input: tachometer reading, RPM
6299 * SYSFS fan layout: extensions
6301 * fan_watchdog (driver):
6302 * fan watchdog interval in seconds, 0 disables (default), max 120
6305 /* sysfs fan pwm1_enable ----------------------------------------------- */
6306 static ssize_t fan_pwm1_enable_show(struct device *dev,
6307 struct device_attribute *attr,
6308 char *buf)
6310 int res, mode;
6311 u8 status;
6313 res = fan_get_status_safe(&status);
6314 if (res)
6315 return res;
6317 if (status & TP_EC_FAN_FULLSPEED) {
6318 mode = 0;
6319 } else if (status & TP_EC_FAN_AUTO) {
6320 mode = 2;
6321 } else
6322 mode = 1;
6324 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6327 static ssize_t fan_pwm1_enable_store(struct device *dev,
6328 struct device_attribute *attr,
6329 const char *buf, size_t count)
6331 unsigned long t;
6332 int res, level;
6334 if (parse_strtoul(buf, 2, &t))
6335 return -EINVAL;
6337 switch (t) {
6338 case 0:
6339 level = TP_EC_FAN_FULLSPEED;
6340 break;
6341 case 1:
6342 level = TPACPI_FAN_LAST_LEVEL;
6343 break;
6344 case 2:
6345 level = TP_EC_FAN_AUTO;
6346 break;
6347 case 3:
6348 /* reserved for software-controlled auto mode */
6349 return -ENOSYS;
6350 default:
6351 return -EINVAL;
6354 res = fan_set_level_safe(level);
6355 if (res == -ENXIO)
6356 return -EINVAL;
6357 else if (res < 0)
6358 return res;
6360 fan_watchdog_reset();
6362 return count;
6365 static struct device_attribute dev_attr_fan_pwm1_enable =
6366 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6367 fan_pwm1_enable_show, fan_pwm1_enable_store);
6369 /* sysfs fan pwm1 ------------------------------------------------------ */
6370 static ssize_t fan_pwm1_show(struct device *dev,
6371 struct device_attribute *attr,
6372 char *buf)
6374 int res;
6375 u8 status;
6377 res = fan_get_status_safe(&status);
6378 if (res)
6379 return res;
6381 if ((status &
6382 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6383 status = fan_control_desired_level;
6385 if (status > 7)
6386 status = 7;
6388 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6391 static ssize_t fan_pwm1_store(struct device *dev,
6392 struct device_attribute *attr,
6393 const char *buf, size_t count)
6395 unsigned long s;
6396 int rc;
6397 u8 status, newlevel;
6399 if (parse_strtoul(buf, 255, &s))
6400 return -EINVAL;
6402 /* scale down from 0-255 to 0-7 */
6403 newlevel = (s >> 5) & 0x07;
6405 if (mutex_lock_killable(&fan_mutex))
6406 return -ERESTARTSYS;
6408 rc = fan_get_status(&status);
6409 if (!rc && (status &
6410 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6411 rc = fan_set_level(newlevel);
6412 if (rc == -ENXIO)
6413 rc = -EINVAL;
6414 else if (!rc) {
6415 fan_update_desired_level(newlevel);
6416 fan_watchdog_reset();
6420 mutex_unlock(&fan_mutex);
6421 return (rc)? rc : count;
6424 static struct device_attribute dev_attr_fan_pwm1 =
6425 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6426 fan_pwm1_show, fan_pwm1_store);
6428 /* sysfs fan fan1_input ------------------------------------------------ */
6429 static ssize_t fan_fan1_input_show(struct device *dev,
6430 struct device_attribute *attr,
6431 char *buf)
6433 int res;
6434 unsigned int speed;
6436 res = fan_get_speed(&speed);
6437 if (res < 0)
6438 return res;
6440 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6443 static struct device_attribute dev_attr_fan_fan1_input =
6444 __ATTR(fan1_input, S_IRUGO,
6445 fan_fan1_input_show, NULL);
6447 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6448 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6449 char *buf)
6451 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6454 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6455 const char *buf, size_t count)
6457 unsigned long t;
6459 if (parse_strtoul(buf, 120, &t))
6460 return -EINVAL;
6462 if (!fan_control_allowed)
6463 return -EPERM;
6465 fan_watchdog_maxinterval = t;
6466 fan_watchdog_reset();
6468 return count;
6471 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6472 fan_fan_watchdog_show, fan_fan_watchdog_store);
6474 /* --------------------------------------------------------------------- */
6475 static struct attribute *fan_attributes[] = {
6476 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6477 &dev_attr_fan_fan1_input.attr,
6478 NULL
6481 static const struct attribute_group fan_attr_group = {
6482 .attrs = fan_attributes,
6485 static int __init fan_init(struct ibm_init_struct *iibm)
6487 int rc;
6489 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6491 mutex_init(&fan_mutex);
6492 fan_status_access_mode = TPACPI_FAN_NONE;
6493 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6494 fan_control_commands = 0;
6495 fan_watchdog_maxinterval = 0;
6496 tp_features.fan_ctrl_status_undef = 0;
6497 fan_control_desired_level = 7;
6499 TPACPI_ACPIHANDLE_INIT(fans);
6500 TPACPI_ACPIHANDLE_INIT(gfan);
6501 TPACPI_ACPIHANDLE_INIT(sfan);
6503 if (gfan_handle) {
6504 /* 570, 600e/x, 770e, 770x */
6505 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6506 } else {
6507 /* all other ThinkPads: note that even old-style
6508 * ThinkPad ECs supports the fan control register */
6509 if (likely(acpi_ec_read(fan_status_offset,
6510 &fan_control_initial_status))) {
6511 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6512 fan_quirk1_detect();
6513 } else {
6514 printk(TPACPI_ERR
6515 "ThinkPad ACPI EC access misbehaving, "
6516 "fan status and control unavailable\n");
6517 return 1;
6521 if (sfan_handle) {
6522 /* 570, 770x-JL */
6523 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6524 fan_control_commands |=
6525 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6526 } else {
6527 if (!gfan_handle) {
6528 /* gfan without sfan means no fan control */
6529 /* all other models implement TP EC 0x2f control */
6531 if (fans_handle) {
6532 /* X31, X40, X41 */
6533 fan_control_access_mode =
6534 TPACPI_FAN_WR_ACPI_FANS;
6535 fan_control_commands |=
6536 TPACPI_FAN_CMD_SPEED |
6537 TPACPI_FAN_CMD_LEVEL |
6538 TPACPI_FAN_CMD_ENABLE;
6539 } else {
6540 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6541 fan_control_commands |=
6542 TPACPI_FAN_CMD_LEVEL |
6543 TPACPI_FAN_CMD_ENABLE;
6548 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6549 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6550 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6551 fan_status_access_mode, fan_control_access_mode);
6553 /* fan control master switch */
6554 if (!fan_control_allowed) {
6555 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6556 fan_control_commands = 0;
6557 dbg_printk(TPACPI_DBG_INIT,
6558 "fan control features disabled by parameter\n");
6561 /* update fan_control_desired_level */
6562 if (fan_status_access_mode != TPACPI_FAN_NONE)
6563 fan_get_status_safe(NULL);
6565 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6566 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6567 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6568 &fan_attr_group);
6569 if (rc < 0)
6570 return rc;
6572 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6573 &driver_attr_fan_watchdog);
6574 if (rc < 0) {
6575 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6576 &fan_attr_group);
6577 return rc;
6579 return 0;
6580 } else
6581 return 1;
6584 static void fan_exit(void)
6586 vdbg_printk(TPACPI_DBG_EXIT,
6587 "cancelling any pending fan watchdog tasks\n");
6589 /* FIXME: can we really do this unconditionally? */
6590 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6591 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6592 &driver_attr_fan_watchdog);
6594 cancel_delayed_work(&fan_watchdog_task);
6595 flush_workqueue(tpacpi_wq);
6598 static void fan_suspend(pm_message_t state)
6600 int rc;
6602 if (!fan_control_allowed)
6603 return;
6605 /* Store fan status in cache */
6606 fan_control_resume_level = 0;
6607 rc = fan_get_status_safe(&fan_control_resume_level);
6608 if (rc < 0)
6609 printk(TPACPI_NOTICE
6610 "failed to read fan level for later "
6611 "restore during resume: %d\n", rc);
6613 /* if it is undefined, don't attempt to restore it.
6614 * KEEP THIS LAST */
6615 if (tp_features.fan_ctrl_status_undef)
6616 fan_control_resume_level = 0;
6619 static void fan_resume(void)
6621 u8 current_level = 7;
6622 bool do_set = false;
6623 int rc;
6625 /* DSDT *always* updates status on resume */
6626 tp_features.fan_ctrl_status_undef = 0;
6628 if (!fan_control_allowed ||
6629 !fan_control_resume_level ||
6630 (fan_get_status_safe(&current_level) < 0))
6631 return;
6633 switch (fan_control_access_mode) {
6634 case TPACPI_FAN_WR_ACPI_SFAN:
6635 /* never decrease fan level */
6636 do_set = (fan_control_resume_level > current_level);
6637 break;
6638 case TPACPI_FAN_WR_ACPI_FANS:
6639 case TPACPI_FAN_WR_TPEC:
6640 /* never decrease fan level, scale is:
6641 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6643 * We expect the firmware to set either 7 or AUTO, but we
6644 * handle FULLSPEED out of paranoia.
6646 * So, we can safely only restore FULLSPEED or 7, anything
6647 * else could slow the fan. Restoring AUTO is useless, at
6648 * best that's exactly what the DSDT already set (it is the
6649 * slower it uses).
6651 * Always keep in mind that the DSDT *will* have set the
6652 * fans to what the vendor supposes is the best level. We
6653 * muck with it only to speed the fan up.
6655 if (fan_control_resume_level != 7 &&
6656 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6657 return;
6658 else
6659 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6660 (current_level != fan_control_resume_level);
6661 break;
6662 default:
6663 return;
6665 if (do_set) {
6666 printk(TPACPI_NOTICE
6667 "restoring fan level to 0x%02x\n",
6668 fan_control_resume_level);
6669 rc = fan_set_level_safe(fan_control_resume_level);
6670 if (rc < 0)
6671 printk(TPACPI_NOTICE
6672 "failed to restore fan level: %d\n", rc);
6676 static int fan_read(char *p)
6678 int len = 0;
6679 int rc;
6680 u8 status;
6681 unsigned int speed = 0;
6683 switch (fan_status_access_mode) {
6684 case TPACPI_FAN_RD_ACPI_GFAN:
6685 /* 570, 600e/x, 770e, 770x */
6686 rc = fan_get_status_safe(&status);
6687 if (rc < 0)
6688 return rc;
6690 len += sprintf(p + len, "status:\t\t%s\n"
6691 "level:\t\t%d\n",
6692 (status != 0) ? "enabled" : "disabled", status);
6693 break;
6695 case TPACPI_FAN_RD_TPEC:
6696 /* all except 570, 600e/x, 770e, 770x */
6697 rc = fan_get_status_safe(&status);
6698 if (rc < 0)
6699 return rc;
6701 len += sprintf(p + len, "status:\t\t%s\n",
6702 (status != 0) ? "enabled" : "disabled");
6704 rc = fan_get_speed(&speed);
6705 if (rc < 0)
6706 return rc;
6708 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6710 if (status & TP_EC_FAN_FULLSPEED)
6711 /* Disengaged mode takes precedence */
6712 len += sprintf(p + len, "level:\t\tdisengaged\n");
6713 else if (status & TP_EC_FAN_AUTO)
6714 len += sprintf(p + len, "level:\t\tauto\n");
6715 else
6716 len += sprintf(p + len, "level:\t\t%d\n", status);
6717 break;
6719 case TPACPI_FAN_NONE:
6720 default:
6721 len += sprintf(p + len, "status:\t\tnot supported\n");
6724 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6725 len += sprintf(p + len, "commands:\tlevel <level>");
6727 switch (fan_control_access_mode) {
6728 case TPACPI_FAN_WR_ACPI_SFAN:
6729 len += sprintf(p + len, " (<level> is 0-7)\n");
6730 break;
6732 default:
6733 len += sprintf(p + len, " (<level> is 0-7, "
6734 "auto, disengaged, full-speed)\n");
6735 break;
6739 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6740 len += sprintf(p + len, "commands:\tenable, disable\n"
6741 "commands:\twatchdog <timeout> (<timeout> "
6742 "is 0 (off), 1-120 (seconds))\n");
6744 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6745 len += sprintf(p + len, "commands:\tspeed <speed>"
6746 " (<speed> is 0-65535)\n");
6748 return len;
6751 static int fan_write_cmd_level(const char *cmd, int *rc)
6753 int level;
6755 if (strlencmp(cmd, "level auto") == 0)
6756 level = TP_EC_FAN_AUTO;
6757 else if ((strlencmp(cmd, "level disengaged") == 0) |
6758 (strlencmp(cmd, "level full-speed") == 0))
6759 level = TP_EC_FAN_FULLSPEED;
6760 else if (sscanf(cmd, "level %d", &level) != 1)
6761 return 0;
6763 *rc = fan_set_level_safe(level);
6764 if (*rc == -ENXIO)
6765 printk(TPACPI_ERR "level command accepted for unsupported "
6766 "access mode %d", fan_control_access_mode);
6768 return 1;
6771 static int fan_write_cmd_enable(const char *cmd, int *rc)
6773 if (strlencmp(cmd, "enable") != 0)
6774 return 0;
6776 *rc = fan_set_enable();
6777 if (*rc == -ENXIO)
6778 printk(TPACPI_ERR "enable command accepted for unsupported "
6779 "access mode %d", fan_control_access_mode);
6781 return 1;
6784 static int fan_write_cmd_disable(const char *cmd, int *rc)
6786 if (strlencmp(cmd, "disable") != 0)
6787 return 0;
6789 *rc = fan_set_disable();
6790 if (*rc == -ENXIO)
6791 printk(TPACPI_ERR "disable command accepted for unsupported "
6792 "access mode %d", fan_control_access_mode);
6794 return 1;
6797 static int fan_write_cmd_speed(const char *cmd, int *rc)
6799 int speed;
6801 /* TODO:
6802 * Support speed <low> <medium> <high> ? */
6804 if (sscanf(cmd, "speed %d", &speed) != 1)
6805 return 0;
6807 *rc = fan_set_speed(speed);
6808 if (*rc == -ENXIO)
6809 printk(TPACPI_ERR "speed command accepted for unsupported "
6810 "access mode %d", fan_control_access_mode);
6812 return 1;
6815 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6817 int interval;
6819 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6820 return 0;
6822 if (interval < 0 || interval > 120)
6823 *rc = -EINVAL;
6824 else
6825 fan_watchdog_maxinterval = interval;
6827 return 1;
6830 static int fan_write(char *buf)
6832 char *cmd;
6833 int rc = 0;
6835 while (!rc && (cmd = next_cmd(&buf))) {
6836 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
6837 fan_write_cmd_level(cmd, &rc)) &&
6838 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
6839 (fan_write_cmd_enable(cmd, &rc) ||
6840 fan_write_cmd_disable(cmd, &rc) ||
6841 fan_write_cmd_watchdog(cmd, &rc))) &&
6842 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
6843 fan_write_cmd_speed(cmd, &rc))
6845 rc = -EINVAL;
6846 else if (!rc)
6847 fan_watchdog_reset();
6850 return rc;
6853 static struct ibm_struct fan_driver_data = {
6854 .name = "fan",
6855 .read = fan_read,
6856 .write = fan_write,
6857 .exit = fan_exit,
6858 .suspend = fan_suspend,
6859 .resume = fan_resume,
6862 /****************************************************************************
6863 ****************************************************************************
6865 * Infrastructure
6867 ****************************************************************************
6868 ****************************************************************************/
6870 /* sysfs name ---------------------------------------------------------- */
6871 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
6872 struct device_attribute *attr,
6873 char *buf)
6875 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
6878 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6879 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6881 /* --------------------------------------------------------------------- */
6883 /* /proc support */
6884 static struct proc_dir_entry *proc_dir;
6887 * Module and infrastructure proble, init and exit handling
6890 static int force_load;
6892 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
6893 static const char * __init str_supported(int is_supported)
6895 static char text_unsupported[] __initdata = "not supported";
6897 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
6899 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6901 static void ibm_exit(struct ibm_struct *ibm)
6903 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6905 list_del_init(&ibm->all_drivers);
6907 if (ibm->flags.acpi_notify_installed) {
6908 dbg_printk(TPACPI_DBG_EXIT,
6909 "%s: acpi_remove_notify_handler\n", ibm->name);
6910 BUG_ON(!ibm->acpi);
6911 acpi_remove_notify_handler(*ibm->acpi->handle,
6912 ibm->acpi->type,
6913 dispatch_acpi_notify);
6914 ibm->flags.acpi_notify_installed = 0;
6915 ibm->flags.acpi_notify_installed = 0;
6918 if (ibm->flags.proc_created) {
6919 dbg_printk(TPACPI_DBG_EXIT,
6920 "%s: remove_proc_entry\n", ibm->name);
6921 remove_proc_entry(ibm->name, proc_dir);
6922 ibm->flags.proc_created = 0;
6925 if (ibm->flags.acpi_driver_registered) {
6926 dbg_printk(TPACPI_DBG_EXIT,
6927 "%s: acpi_bus_unregister_driver\n", ibm->name);
6928 BUG_ON(!ibm->acpi);
6929 acpi_bus_unregister_driver(ibm->acpi->driver);
6930 kfree(ibm->acpi->driver);
6931 ibm->acpi->driver = NULL;
6932 ibm->flags.acpi_driver_registered = 0;
6935 if (ibm->flags.init_called && ibm->exit) {
6936 ibm->exit();
6937 ibm->flags.init_called = 0;
6940 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6943 static int __init ibm_init(struct ibm_init_struct *iibm)
6945 int ret;
6946 struct ibm_struct *ibm = iibm->data;
6947 struct proc_dir_entry *entry;
6949 BUG_ON(ibm == NULL);
6951 INIT_LIST_HEAD(&ibm->all_drivers);
6953 if (ibm->flags.experimental && !experimental)
6954 return 0;
6956 dbg_printk(TPACPI_DBG_INIT,
6957 "probing for %s\n", ibm->name);
6959 if (iibm->init) {
6960 ret = iibm->init(iibm);
6961 if (ret > 0)
6962 return 0; /* probe failed */
6963 if (ret)
6964 return ret;
6966 ibm->flags.init_called = 1;
6969 if (ibm->acpi) {
6970 if (ibm->acpi->hid) {
6971 ret = register_tpacpi_subdriver(ibm);
6972 if (ret)
6973 goto err_out;
6976 if (ibm->acpi->notify) {
6977 ret = setup_acpi_notify(ibm);
6978 if (ret == -ENODEV) {
6979 printk(TPACPI_NOTICE "disabling subdriver %s\n",
6980 ibm->name);
6981 ret = 0;
6982 goto err_out;
6984 if (ret < 0)
6985 goto err_out;
6989 dbg_printk(TPACPI_DBG_INIT,
6990 "%s installed\n", ibm->name);
6992 if (ibm->read) {
6993 entry = create_proc_entry(ibm->name,
6994 S_IFREG | S_IRUGO | S_IWUSR,
6995 proc_dir);
6996 if (!entry) {
6997 printk(TPACPI_ERR "unable to create proc entry %s\n",
6998 ibm->name);
6999 ret = -ENODEV;
7000 goto err_out;
7002 entry->owner = THIS_MODULE;
7003 entry->data = ibm;
7004 entry->read_proc = &dispatch_procfs_read;
7005 if (ibm->write)
7006 entry->write_proc = &dispatch_procfs_write;
7007 ibm->flags.proc_created = 1;
7010 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7012 return 0;
7014 err_out:
7015 dbg_printk(TPACPI_DBG_INIT,
7016 "%s: at error exit path with result %d\n",
7017 ibm->name, ret);
7019 ibm_exit(ibm);
7020 return (ret < 0)? ret : 0;
7023 /* Probing */
7025 /* returns 0 - probe ok, or < 0 - probe error.
7026 * Probe ok doesn't mean thinkpad found.
7027 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7028 static int __must_check __init get_thinkpad_model_data(
7029 struct thinkpad_id_data *tp)
7031 const struct dmi_device *dev = NULL;
7032 char ec_fw_string[18];
7033 char const *s;
7035 if (!tp)
7036 return -EINVAL;
7038 memset(tp, 0, sizeof(*tp));
7040 if (dmi_name_in_vendors("IBM"))
7041 tp->vendor = PCI_VENDOR_ID_IBM;
7042 else if (dmi_name_in_vendors("LENOVO"))
7043 tp->vendor = PCI_VENDOR_ID_LENOVO;
7044 else
7045 return 0;
7047 s = dmi_get_system_info(DMI_BIOS_VERSION);
7048 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7049 if (s && !tp->bios_version_str)
7050 return -ENOMEM;
7051 if (!tp->bios_version_str)
7052 return 0;
7053 tp->bios_model = tp->bios_version_str[0]
7054 | (tp->bios_version_str[1] << 8);
7057 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7058 * X32 or newer, all Z series; Some models must have an
7059 * up-to-date BIOS or they will not be detected.
7061 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7063 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7064 if (sscanf(dev->name,
7065 "IBM ThinkPad Embedded Controller -[%17c",
7066 ec_fw_string) == 1) {
7067 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7068 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7070 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7071 if (!tp->ec_version_str)
7072 return -ENOMEM;
7073 tp->ec_model = ec_fw_string[0]
7074 | (ec_fw_string[1] << 8);
7075 break;
7079 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7080 if (s && !strnicmp(s, "ThinkPad", 8)) {
7081 tp->model_str = kstrdup(s, GFP_KERNEL);
7082 if (!tp->model_str)
7083 return -ENOMEM;
7086 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7087 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7088 if (s && !tp->nummodel_str)
7089 return -ENOMEM;
7091 return 0;
7094 static int __init probe_for_thinkpad(void)
7096 int is_thinkpad;
7098 if (acpi_disabled)
7099 return -ENODEV;
7102 * Non-ancient models have better DMI tagging, but very old models
7103 * don't.
7105 is_thinkpad = (thinkpad_id.model_str != NULL);
7107 /* ec is required because many other handles are relative to it */
7108 TPACPI_ACPIHANDLE_INIT(ec);
7109 if (!ec_handle) {
7110 if (is_thinkpad)
7111 printk(TPACPI_ERR
7112 "Not yet supported ThinkPad detected!\n");
7113 return -ENODEV;
7117 * Risks a regression on very old machines, but reduces potential
7118 * false positives a damn great deal
7120 if (!is_thinkpad)
7121 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7123 if (!is_thinkpad && !force_load)
7124 return -ENODEV;
7126 return 0;
7130 /* Module init, exit, parameters */
7132 static struct ibm_init_struct ibms_init[] __initdata = {
7134 .init = thinkpad_acpi_driver_init,
7135 .data = &thinkpad_acpi_driver_data,
7138 .init = hotkey_init,
7139 .data = &hotkey_driver_data,
7142 .init = bluetooth_init,
7143 .data = &bluetooth_driver_data,
7146 .init = wan_init,
7147 .data = &wan_driver_data,
7150 .init = uwb_init,
7151 .data = &uwb_driver_data,
7153 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7155 .init = video_init,
7156 .data = &video_driver_data,
7158 #endif
7160 .init = light_init,
7161 .data = &light_driver_data,
7163 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7165 .init = dock_init,
7166 .data = &dock_driver_data[0],
7169 .init = dock_init2,
7170 .data = &dock_driver_data[1],
7172 #endif
7173 #ifdef CONFIG_THINKPAD_ACPI_BAY
7175 .init = bay_init,
7176 .data = &bay_driver_data,
7178 #endif
7180 .init = cmos_init,
7181 .data = &cmos_driver_data,
7184 .init = led_init,
7185 .data = &led_driver_data,
7188 .init = beep_init,
7189 .data = &beep_driver_data,
7192 .init = thermal_init,
7193 .data = &thermal_driver_data,
7196 .data = &ecdump_driver_data,
7199 .init = brightness_init,
7200 .data = &brightness_driver_data,
7203 .data = &volume_driver_data,
7206 .init = fan_init,
7207 .data = &fan_driver_data,
7211 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7213 unsigned int i;
7214 struct ibm_struct *ibm;
7216 if (!kp || !kp->name || !val)
7217 return -EINVAL;
7219 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7220 ibm = ibms_init[i].data;
7221 WARN_ON(ibm == NULL);
7223 if (!ibm || !ibm->name)
7224 continue;
7226 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7227 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7228 return -ENOSPC;
7229 strcpy(ibms_init[i].param, val);
7230 strcat(ibms_init[i].param, ",");
7231 return 0;
7235 return -EINVAL;
7238 module_param(experimental, int, 0);
7239 MODULE_PARM_DESC(experimental,
7240 "Enables experimental features when non-zero");
7242 module_param_named(debug, dbg_level, uint, 0);
7243 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7245 module_param(force_load, bool, 0);
7246 MODULE_PARM_DESC(force_load,
7247 "Attempts to load the driver even on a "
7248 "mis-identified ThinkPad when true");
7250 module_param_named(fan_control, fan_control_allowed, bool, 0);
7251 MODULE_PARM_DESC(fan_control,
7252 "Enables setting fan parameters features when true");
7254 module_param_named(brightness_mode, brightness_mode, int, 0);
7255 MODULE_PARM_DESC(brightness_mode,
7256 "Selects brightness control strategy: "
7257 "0=auto, 1=EC, 2=CMOS, 3=both");
7259 module_param(brightness_enable, uint, 0);
7260 MODULE_PARM_DESC(brightness_enable,
7261 "Enables backlight control when 1, disables when 0");
7263 module_param(hotkey_report_mode, uint, 0);
7264 MODULE_PARM_DESC(hotkey_report_mode,
7265 "used for backwards compatibility with userspace, "
7266 "see documentation");
7268 #define TPACPI_PARAM(feature) \
7269 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7270 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7271 "at module load, see documentation")
7273 TPACPI_PARAM(hotkey);
7274 TPACPI_PARAM(bluetooth);
7275 TPACPI_PARAM(video);
7276 TPACPI_PARAM(light);
7277 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7278 TPACPI_PARAM(dock);
7279 #endif
7280 #ifdef CONFIG_THINKPAD_ACPI_BAY
7281 TPACPI_PARAM(bay);
7282 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7283 TPACPI_PARAM(cmos);
7284 TPACPI_PARAM(led);
7285 TPACPI_PARAM(beep);
7286 TPACPI_PARAM(ecdump);
7287 TPACPI_PARAM(brightness);
7288 TPACPI_PARAM(volume);
7289 TPACPI_PARAM(fan);
7291 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7292 module_param(dbg_wlswemul, uint, 0);
7293 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7294 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7295 MODULE_PARM_DESC(wlsw_state,
7296 "Initial state of the emulated WLSW switch");
7298 module_param(dbg_bluetoothemul, uint, 0);
7299 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7300 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7301 MODULE_PARM_DESC(bluetooth_state,
7302 "Initial state of the emulated bluetooth switch");
7304 module_param(dbg_wwanemul, uint, 0);
7305 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7306 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7307 MODULE_PARM_DESC(wwan_state,
7308 "Initial state of the emulated WWAN switch");
7310 module_param(dbg_uwbemul, uint, 0);
7311 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7312 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7313 MODULE_PARM_DESC(uwb_state,
7314 "Initial state of the emulated UWB switch");
7315 #endif
7317 static void thinkpad_acpi_module_exit(void)
7319 struct ibm_struct *ibm, *itmp;
7321 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7323 list_for_each_entry_safe_reverse(ibm, itmp,
7324 &tpacpi_all_drivers,
7325 all_drivers) {
7326 ibm_exit(ibm);
7329 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7331 if (tpacpi_inputdev) {
7332 if (tp_features.input_device_registered)
7333 input_unregister_device(tpacpi_inputdev);
7334 else
7335 input_free_device(tpacpi_inputdev);
7338 if (tpacpi_hwmon)
7339 hwmon_device_unregister(tpacpi_hwmon);
7341 if (tp_features.sensors_pdev_attrs_registered)
7342 device_remove_file(&tpacpi_sensors_pdev->dev,
7343 &dev_attr_thinkpad_acpi_pdev_name);
7344 if (tpacpi_sensors_pdev)
7345 platform_device_unregister(tpacpi_sensors_pdev);
7346 if (tpacpi_pdev)
7347 platform_device_unregister(tpacpi_pdev);
7349 if (tp_features.sensors_pdrv_attrs_registered)
7350 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7351 if (tp_features.platform_drv_attrs_registered)
7352 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7354 if (tp_features.sensors_pdrv_registered)
7355 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7357 if (tp_features.platform_drv_registered)
7358 platform_driver_unregister(&tpacpi_pdriver);
7360 if (proc_dir)
7361 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7363 if (tpacpi_wq)
7364 destroy_workqueue(tpacpi_wq);
7366 kfree(thinkpad_id.bios_version_str);
7367 kfree(thinkpad_id.ec_version_str);
7368 kfree(thinkpad_id.model_str);
7372 static int __init thinkpad_acpi_module_init(void)
7374 int ret, i;
7376 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7378 /* Parameter checking */
7379 if (hotkey_report_mode > 2)
7380 return -EINVAL;
7382 /* Driver-level probe */
7384 ret = get_thinkpad_model_data(&thinkpad_id);
7385 if (ret) {
7386 printk(TPACPI_ERR
7387 "unable to get DMI data: %d\n", ret);
7388 thinkpad_acpi_module_exit();
7389 return ret;
7391 ret = probe_for_thinkpad();
7392 if (ret) {
7393 thinkpad_acpi_module_exit();
7394 return ret;
7397 /* Driver initialization */
7399 TPACPI_ACPIHANDLE_INIT(ecrd);
7400 TPACPI_ACPIHANDLE_INIT(ecwr);
7402 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7403 if (!tpacpi_wq) {
7404 thinkpad_acpi_module_exit();
7405 return -ENOMEM;
7408 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7409 if (!proc_dir) {
7410 printk(TPACPI_ERR
7411 "unable to create proc dir " TPACPI_PROC_DIR);
7412 thinkpad_acpi_module_exit();
7413 return -ENODEV;
7415 proc_dir->owner = THIS_MODULE;
7417 ret = platform_driver_register(&tpacpi_pdriver);
7418 if (ret) {
7419 printk(TPACPI_ERR
7420 "unable to register main platform driver\n");
7421 thinkpad_acpi_module_exit();
7422 return ret;
7424 tp_features.platform_drv_registered = 1;
7426 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7427 if (ret) {
7428 printk(TPACPI_ERR
7429 "unable to register hwmon platform driver\n");
7430 thinkpad_acpi_module_exit();
7431 return ret;
7433 tp_features.sensors_pdrv_registered = 1;
7435 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7436 if (!ret) {
7437 tp_features.platform_drv_attrs_registered = 1;
7438 ret = tpacpi_create_driver_attributes(
7439 &tpacpi_hwmon_pdriver.driver);
7441 if (ret) {
7442 printk(TPACPI_ERR
7443 "unable to create sysfs driver attributes\n");
7444 thinkpad_acpi_module_exit();
7445 return ret;
7447 tp_features.sensors_pdrv_attrs_registered = 1;
7450 /* Device initialization */
7451 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7452 NULL, 0);
7453 if (IS_ERR(tpacpi_pdev)) {
7454 ret = PTR_ERR(tpacpi_pdev);
7455 tpacpi_pdev = NULL;
7456 printk(TPACPI_ERR "unable to register platform device\n");
7457 thinkpad_acpi_module_exit();
7458 return ret;
7460 tpacpi_sensors_pdev = platform_device_register_simple(
7461 TPACPI_HWMON_DRVR_NAME,
7462 -1, NULL, 0);
7463 if (IS_ERR(tpacpi_sensors_pdev)) {
7464 ret = PTR_ERR(tpacpi_sensors_pdev);
7465 tpacpi_sensors_pdev = NULL;
7466 printk(TPACPI_ERR
7467 "unable to register hwmon platform device\n");
7468 thinkpad_acpi_module_exit();
7469 return ret;
7471 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7472 &dev_attr_thinkpad_acpi_pdev_name);
7473 if (ret) {
7474 printk(TPACPI_ERR
7475 "unable to create sysfs hwmon device attributes\n");
7476 thinkpad_acpi_module_exit();
7477 return ret;
7479 tp_features.sensors_pdev_attrs_registered = 1;
7480 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7481 if (IS_ERR(tpacpi_hwmon)) {
7482 ret = PTR_ERR(tpacpi_hwmon);
7483 tpacpi_hwmon = NULL;
7484 printk(TPACPI_ERR "unable to register hwmon device\n");
7485 thinkpad_acpi_module_exit();
7486 return ret;
7488 mutex_init(&tpacpi_inputdev_send_mutex);
7489 tpacpi_inputdev = input_allocate_device();
7490 if (!tpacpi_inputdev) {
7491 printk(TPACPI_ERR "unable to allocate input device\n");
7492 thinkpad_acpi_module_exit();
7493 return -ENOMEM;
7494 } else {
7495 /* Prepare input device, but don't register */
7496 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7497 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7498 tpacpi_inputdev->id.bustype = BUS_HOST;
7499 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7500 thinkpad_id.vendor :
7501 PCI_VENDOR_ID_IBM;
7502 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7503 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7505 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7506 ret = ibm_init(&ibms_init[i]);
7507 if (ret >= 0 && *ibms_init[i].param)
7508 ret = ibms_init[i].data->write(ibms_init[i].param);
7509 if (ret < 0) {
7510 thinkpad_acpi_module_exit();
7511 return ret;
7514 ret = input_register_device(tpacpi_inputdev);
7515 if (ret < 0) {
7516 printk(TPACPI_ERR "unable to register input device\n");
7517 thinkpad_acpi_module_exit();
7518 return ret;
7519 } else {
7520 tp_features.input_device_registered = 1;
7523 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7524 return 0;
7527 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7530 * DMI matching for module autoloading
7532 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7533 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7535 * Only models listed in thinkwiki will be supported, so add yours
7536 * if it is not there yet.
7538 #define IBM_BIOS_MODULE_ALIAS(__type) \
7539 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7541 /* Non-ancient thinkpads */
7542 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7543 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7545 /* Ancient thinkpad BIOSes have to be identified by
7546 * BIOS type or model number, and there are far less
7547 * BIOS types than model numbers... */
7548 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7549 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7550 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7552 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7553 MODULE_DESCRIPTION(TPACPI_DESC);
7554 MODULE_VERSION(TPACPI_VERSION);
7555 MODULE_LICENSE("GPL");
7557 module_init(thinkpad_acpi_module_init);
7558 module_exit(thinkpad_acpi_module_exit);