ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / platform / x86 / thinkpad_acpi.c
blobbb526ba9e6d842fc278744a43633eca54a7f7946
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-20090318-BETA"
25 #define TPACPI_SYSFS_VERSION 0x020300
28 * Changelog:
29 * 2007-10-20 changelog trimmed down
31 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
34 * 2006-11-22 0.13 new maintainer
35 * changelog now lives in git commit history, and will
36 * not be updated further in-file.
38 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
41 * 2005-01-16 0.9 use MODULE_VERSION
42 * thanks to Henrik Brix Andersen <brix@gentoo.org>
43 * fix parameter passing on module loading
44 * thanks to Rusty Russell <rusty@rustcorp.com.au>
45 * thanks to Jim Radford <radford@blackbean.org>
46 * 2004-11-08 0.8 fix init error case, don't return from a macro
47 * thanks to Chris Wright <chrisw@osdl.org>
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/types.h>
54 #include <linux/string.h>
55 #include <linux/list.h>
56 #include <linux/mutex.h>
57 #include <linux/sched.h>
58 #include <linux/kthread.h>
59 #include <linux/freezer.h>
60 #include <linux/delay.h>
62 #include <linux/nvram.h>
63 #include <linux/proc_fs.h>
64 #include <linux/sysfs.h>
65 #include <linux/backlight.h>
66 #include <linux/fb.h>
67 #include <linux/platform_device.h>
68 #include <linux/hwmon.h>
69 #include <linux/hwmon-sysfs.h>
70 #include <linux/input.h>
71 #include <linux/leds.h>
72 #include <linux/rfkill.h>
73 #include <asm/uaccess.h>
75 #include <linux/dmi.h>
76 #include <linux/jiffies.h>
77 #include <linux/workqueue.h>
79 #include <acpi/acpi_drivers.h>
81 #include <linux/pci_ids.h>
84 /* ThinkPad CMOS commands */
85 #define TP_CMOS_VOLUME_DOWN 0
86 #define TP_CMOS_VOLUME_UP 1
87 #define TP_CMOS_VOLUME_MUTE 2
88 #define TP_CMOS_BRIGHTNESS_UP 4
89 #define TP_CMOS_BRIGHTNESS_DOWN 5
90 #define TP_CMOS_THINKLIGHT_ON 12
91 #define TP_CMOS_THINKLIGHT_OFF 13
93 /* NVRAM Addresses */
94 enum tp_nvram_addr {
95 TP_NVRAM_ADDR_HK2 = 0x57,
96 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
97 TP_NVRAM_ADDR_VIDEO = 0x59,
98 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
99 TP_NVRAM_ADDR_MIXER = 0x60,
102 /* NVRAM bit masks */
103 enum {
104 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
105 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
106 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
107 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
108 TP_NVRAM_MASK_THINKLIGHT = 0x10,
109 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
110 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
111 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
112 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
113 TP_NVRAM_MASK_MUTE = 0x40,
114 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
115 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
116 TP_NVRAM_POS_LEVEL_VOLUME = 0,
119 /* ACPI HIDs */
120 #define TPACPI_ACPI_HKEY_HID "IBM0068"
122 /* Input IDs */
123 #define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
124 #define TPACPI_HKEY_INPUT_VERSION 0x4101
126 /* ACPI \WGSV commands */
127 enum {
128 TP_ACPI_WGSV_GET_STATE = 0x01, /* Get state information */
129 TP_ACPI_WGSV_PWR_ON_ON_RESUME = 0x02, /* Resume WWAN powered on */
130 TP_ACPI_WGSV_PWR_OFF_ON_RESUME = 0x03, /* Resume WWAN powered off */
131 TP_ACPI_WGSV_SAVE_STATE = 0x04, /* Save state for S4/S5 */
134 /* TP_ACPI_WGSV_GET_STATE bits */
135 enum {
136 TP_ACPI_WGSV_STATE_WWANEXIST = 0x0001, /* WWAN hw available */
137 TP_ACPI_WGSV_STATE_WWANPWR = 0x0002, /* WWAN radio enabled */
138 TP_ACPI_WGSV_STATE_WWANPWRRES = 0x0004, /* WWAN state at resume */
139 TP_ACPI_WGSV_STATE_WWANBIOSOFF = 0x0008, /* WWAN disabled in BIOS */
140 TP_ACPI_WGSV_STATE_BLTHEXIST = 0x0001, /* BLTH hw available */
141 TP_ACPI_WGSV_STATE_BLTHPWR = 0x0002, /* BLTH radio enabled */
142 TP_ACPI_WGSV_STATE_BLTHPWRRES = 0x0004, /* BLTH state at resume */
143 TP_ACPI_WGSV_STATE_BLTHBIOSOFF = 0x0008, /* BLTH disabled in BIOS */
144 TP_ACPI_WGSV_STATE_UWBEXIST = 0x0010, /* UWB hw available */
145 TP_ACPI_WGSV_STATE_UWBPWR = 0x0020, /* UWB radio enabled */
148 /****************************************************************************
149 * Main driver
152 #define TPACPI_NAME "thinkpad"
153 #define TPACPI_DESC "ThinkPad ACPI Extras"
154 #define TPACPI_FILE TPACPI_NAME "_acpi"
155 #define TPACPI_URL "http://ibm-acpi.sf.net/"
156 #define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
158 #define TPACPI_PROC_DIR "ibm"
159 #define TPACPI_ACPI_EVENT_PREFIX "ibm"
160 #define TPACPI_DRVR_NAME TPACPI_FILE
161 #define TPACPI_DRVR_SHORTNAME "tpacpi"
162 #define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
164 #define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
165 #define TPACPI_WORKQUEUE_NAME "ktpacpid"
167 #define TPACPI_MAX_ACPI_ARGS 3
169 /* rfkill switches */
170 enum {
171 TPACPI_RFK_BLUETOOTH_SW_ID = 0,
172 TPACPI_RFK_WWAN_SW_ID,
173 TPACPI_RFK_UWB_SW_ID,
176 /* printk headers */
177 #define TPACPI_LOG TPACPI_FILE ": "
178 #define TPACPI_EMERG KERN_EMERG TPACPI_LOG
179 #define TPACPI_ALERT KERN_ALERT TPACPI_LOG
180 #define TPACPI_CRIT KERN_CRIT TPACPI_LOG
181 #define TPACPI_ERR KERN_ERR TPACPI_LOG
182 #define TPACPI_WARN KERN_WARNING TPACPI_LOG
183 #define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
184 #define TPACPI_INFO KERN_INFO TPACPI_LOG
185 #define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
187 /* Debugging printk groups */
188 #define TPACPI_DBG_ALL 0xffff
189 #define TPACPI_DBG_DISCLOSEPID 0x8000
190 #define TPACPI_DBG_INIT 0x0001
191 #define TPACPI_DBG_EXIT 0x0002
192 #define TPACPI_DBG_BRGHT 0x0004
194 #define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
195 #define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
196 #define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
199 /****************************************************************************
200 * Driver-wide structs and misc. variables
203 struct ibm_struct;
205 struct tp_acpi_drv_struct {
206 const struct acpi_device_id *hid;
207 struct acpi_driver *driver;
209 void (*notify) (struct ibm_struct *, u32);
210 acpi_handle *handle;
211 u32 type;
212 struct acpi_device *device;
215 struct ibm_struct {
216 char *name;
218 int (*read) (char *);
219 int (*write) (char *);
220 void (*exit) (void);
221 void (*resume) (void);
222 void (*suspend) (pm_message_t state);
223 void (*shutdown) (void);
225 struct list_head all_drivers;
227 struct tp_acpi_drv_struct *acpi;
229 struct {
230 u8 acpi_driver_registered:1;
231 u8 acpi_notify_installed:1;
232 u8 proc_created:1;
233 u8 init_called:1;
234 u8 experimental:1;
235 } flags;
238 struct ibm_init_struct {
239 char param[32];
241 int (*init) (struct ibm_init_struct *);
242 struct ibm_struct *data;
245 static struct {
246 #ifdef CONFIG_THINKPAD_ACPI_BAY
247 u32 bay_status:1;
248 u32 bay_eject:1;
249 u32 bay_status2:1;
250 u32 bay_eject2:1;
251 #endif
252 u32 bluetooth:1;
253 u32 hotkey:1;
254 u32 hotkey_mask:1;
255 u32 hotkey_wlsw:1;
256 u32 hotkey_tablet:1;
257 u32 light:1;
258 u32 light_status:1;
259 u32 bright_16levels:1;
260 u32 bright_acpimode:1;
261 u32 wan:1;
262 u32 uwb:1;
263 u32 fan_ctrl_status_undef:1;
264 u32 input_device_registered:1;
265 u32 platform_drv_registered:1;
266 u32 platform_drv_attrs_registered:1;
267 u32 sensors_pdrv_registered:1;
268 u32 sensors_pdrv_attrs_registered:1;
269 u32 sensors_pdev_attrs_registered:1;
270 u32 hotkey_poll_active:1;
271 } tp_features;
273 static struct {
274 u16 hotkey_mask_ff:1;
275 } tp_warned;
277 struct thinkpad_id_data {
278 unsigned int vendor; /* ThinkPad vendor:
279 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
281 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
282 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
284 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
285 u16 ec_model;
287 char *model_str; /* ThinkPad T43 */
288 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
290 static struct thinkpad_id_data thinkpad_id;
292 static enum {
293 TPACPI_LIFE_INIT = 0,
294 TPACPI_LIFE_RUNNING,
295 TPACPI_LIFE_EXITING,
296 } tpacpi_lifecycle;
298 static int experimental;
299 static u32 dbg_level;
301 static struct workqueue_struct *tpacpi_wq;
303 /* Special LED class that can defer work */
304 struct tpacpi_led_classdev {
305 struct led_classdev led_classdev;
306 struct work_struct work;
307 enum led_brightness new_brightness;
308 unsigned int led;
311 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
312 static int dbg_wlswemul;
313 static int tpacpi_wlsw_emulstate;
314 static int dbg_bluetoothemul;
315 static int tpacpi_bluetooth_emulstate;
316 static int dbg_wwanemul;
317 static int tpacpi_wwan_emulstate;
318 static int dbg_uwbemul;
319 static int tpacpi_uwb_emulstate;
320 #endif
323 /*************************************************************************
324 * Debugging helpers
327 #define dbg_printk(a_dbg_level, format, arg...) \
328 do { if (dbg_level & (a_dbg_level)) \
329 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
330 } while (0)
332 #define dbg_printk_pid(a_dbg_level, format, arg...) \
333 do { if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING)) \
334 dbg_printk((TPACPI_DBG_DISCLOSEPID | a_dbg_level) , \
335 "(userspace PID %d) " format , \
336 task_tgid_vnr(current), ## arg); \
337 } while (0)
339 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
340 #define vdbg_printk dbg_printk
341 #define vdbg_printk_pid dbg_printk_pid
342 static const char *str_supported(int is_supported);
343 #else
344 #define vdbg_printk(a_dbg_level, format, arg...) \
345 do { } while (0)
346 #define vdbg_printk_pid(a_dbg_level, format, arg...) \
347 do { } while (0)
348 #endif
350 /****************************************************************************
351 ****************************************************************************
353 * ACPI Helpers and device model
355 ****************************************************************************
356 ****************************************************************************/
358 /*************************************************************************
359 * ACPI basic handles
362 static acpi_handle root_handle;
364 #define TPACPI_HANDLE(object, parent, paths...) \
365 static acpi_handle object##_handle; \
366 static acpi_handle *object##_parent = &parent##_handle; \
367 static char *object##_path; \
368 static char *object##_paths[] = { paths }
370 TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
371 "\\_SB.PCI.ISA.EC", /* 570 */
372 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
373 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
374 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
375 "\\_SB.PCI0.ICH3.EC0", /* R31 */
376 "\\_SB.PCI0.LPC.EC", /* all others */
379 TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
380 TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
382 TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
383 /* T4x, X31, X40 */
384 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
385 "\\CMS", /* R40, R40e */
386 ); /* all others */
388 TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
389 "^HKEY", /* R30, R31 */
390 "HKEY", /* all others */
391 ); /* 570 */
393 TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
394 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
395 "\\_SB.PCI0.VID0", /* 770e */
396 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
397 "\\_SB.PCI0.AGP.VID", /* all others */
398 ); /* R30, R31 */
401 /*************************************************************************
402 * ACPI helpers
405 static int acpi_evalf(acpi_handle handle,
406 void *res, char *method, char *fmt, ...)
408 char *fmt0 = fmt;
409 struct acpi_object_list params;
410 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
411 struct acpi_buffer result, *resultp;
412 union acpi_object out_obj;
413 acpi_status status;
414 va_list ap;
415 char res_type;
416 int success;
417 int quiet;
419 if (!*fmt) {
420 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
421 return 0;
424 if (*fmt == 'q') {
425 quiet = 1;
426 fmt++;
427 } else
428 quiet = 0;
430 res_type = *(fmt++);
432 params.count = 0;
433 params.pointer = &in_objs[0];
435 va_start(ap, fmt);
436 while (*fmt) {
437 char c = *(fmt++);
438 switch (c) {
439 case 'd': /* int */
440 in_objs[params.count].integer.value = va_arg(ap, int);
441 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
442 break;
443 /* add more types as needed */
444 default:
445 printk(TPACPI_ERR "acpi_evalf() called "
446 "with invalid format character '%c'\n", c);
447 return 0;
450 va_end(ap);
452 if (res_type != 'v') {
453 result.length = sizeof(out_obj);
454 result.pointer = &out_obj;
455 resultp = &result;
456 } else
457 resultp = NULL;
459 status = acpi_evaluate_object(handle, method, &params, resultp);
461 switch (res_type) {
462 case 'd': /* int */
463 if (res)
464 *(int *)res = out_obj.integer.value;
465 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
466 break;
467 case 'v': /* void */
468 success = status == AE_OK;
469 break;
470 /* add more types as needed */
471 default:
472 printk(TPACPI_ERR "acpi_evalf() called "
473 "with invalid format character '%c'\n", res_type);
474 return 0;
477 if (!success && !quiet)
478 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
479 method, fmt0, status);
481 return success;
484 static int acpi_ec_read(int i, u8 *p)
486 int v;
488 if (ecrd_handle) {
489 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
490 return 0;
491 *p = v;
492 } else {
493 if (ec_read(i, p) < 0)
494 return 0;
497 return 1;
500 static int acpi_ec_write(int i, u8 v)
502 if (ecwr_handle) {
503 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
504 return 0;
505 } else {
506 if (ec_write(i, v) < 0)
507 return 0;
510 return 1;
513 #if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
514 static int _sta(acpi_handle handle)
516 int status;
518 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
519 status = 0;
521 return status;
523 #endif
525 static int issue_thinkpad_cmos_command(int cmos_cmd)
527 if (!cmos_handle)
528 return -ENXIO;
530 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
531 return -EIO;
533 return 0;
536 /*************************************************************************
537 * ACPI device model
540 #define TPACPI_ACPIHANDLE_INIT(object) \
541 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
542 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
544 static void drv_acpi_handle_init(char *name,
545 acpi_handle *handle, acpi_handle parent,
546 char **paths, int num_paths, char **path)
548 int i;
549 acpi_status status;
551 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
552 name);
554 for (i = 0; i < num_paths; i++) {
555 status = acpi_get_handle(parent, paths[i], handle);
556 if (ACPI_SUCCESS(status)) {
557 *path = paths[i];
558 dbg_printk(TPACPI_DBG_INIT,
559 "Found ACPI handle %s for %s\n",
560 *path, name);
561 return;
565 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
566 name);
567 *handle = NULL;
570 static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
572 struct ibm_struct *ibm = data;
574 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
575 return;
577 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
578 return;
580 ibm->acpi->notify(ibm, event);
583 static int __init setup_acpi_notify(struct ibm_struct *ibm)
585 acpi_status status;
586 int rc;
588 BUG_ON(!ibm->acpi);
590 if (!*ibm->acpi->handle)
591 return 0;
593 vdbg_printk(TPACPI_DBG_INIT,
594 "setting up ACPI notify for %s\n", ibm->name);
596 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
597 if (rc < 0) {
598 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
599 ibm->name, rc);
600 return -ENODEV;
603 ibm->acpi->device->driver_data = ibm;
604 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
605 TPACPI_ACPI_EVENT_PREFIX,
606 ibm->name);
608 status = acpi_install_notify_handler(*ibm->acpi->handle,
609 ibm->acpi->type, dispatch_acpi_notify, ibm);
610 if (ACPI_FAILURE(status)) {
611 if (status == AE_ALREADY_EXISTS) {
612 printk(TPACPI_NOTICE
613 "another device driver is already "
614 "handling %s events\n", ibm->name);
615 } else {
616 printk(TPACPI_ERR
617 "acpi_install_notify_handler(%s) failed: %d\n",
618 ibm->name, status);
620 return -ENODEV;
622 ibm->flags.acpi_notify_installed = 1;
623 return 0;
626 static int __init tpacpi_device_add(struct acpi_device *device)
628 return 0;
631 static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
633 int rc;
635 dbg_printk(TPACPI_DBG_INIT,
636 "registering %s as an ACPI driver\n", ibm->name);
638 BUG_ON(!ibm->acpi);
640 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
641 if (!ibm->acpi->driver) {
642 printk(TPACPI_ERR
643 "failed to allocate memory for ibm->acpi->driver\n");
644 return -ENOMEM;
647 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
648 ibm->acpi->driver->ids = ibm->acpi->hid;
650 ibm->acpi->driver->ops.add = &tpacpi_device_add;
652 rc = acpi_bus_register_driver(ibm->acpi->driver);
653 if (rc < 0) {
654 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
655 ibm->name, rc);
656 kfree(ibm->acpi->driver);
657 ibm->acpi->driver = NULL;
658 } else if (!rc)
659 ibm->flags.acpi_driver_registered = 1;
661 return rc;
665 /****************************************************************************
666 ****************************************************************************
668 * Procfs Helpers
670 ****************************************************************************
671 ****************************************************************************/
673 static int dispatch_procfs_read(char *page, char **start, off_t off,
674 int count, int *eof, void *data)
676 struct ibm_struct *ibm = data;
677 int len;
679 if (!ibm || !ibm->read)
680 return -EINVAL;
682 len = ibm->read(page);
683 if (len < 0)
684 return len;
686 if (len <= off + count)
687 *eof = 1;
688 *start = page + off;
689 len -= off;
690 if (len > count)
691 len = count;
692 if (len < 0)
693 len = 0;
695 return len;
698 static int dispatch_procfs_write(struct file *file,
699 const char __user *userbuf,
700 unsigned long count, void *data)
702 struct ibm_struct *ibm = data;
703 char *kernbuf;
704 int ret;
706 if (!ibm || !ibm->write)
707 return -EINVAL;
709 kernbuf = kmalloc(count + 2, GFP_KERNEL);
710 if (!kernbuf)
711 return -ENOMEM;
713 if (copy_from_user(kernbuf, userbuf, count)) {
714 kfree(kernbuf);
715 return -EFAULT;
718 kernbuf[count] = 0;
719 strcat(kernbuf, ",");
720 ret = ibm->write(kernbuf);
721 if (ret == 0)
722 ret = count;
724 kfree(kernbuf);
726 return ret;
729 static char *next_cmd(char **cmds)
731 char *start = *cmds;
732 char *end;
734 while ((end = strchr(start, ',')) && end == start)
735 start = end + 1;
737 if (!end)
738 return NULL;
740 *end = 0;
741 *cmds = end + 1;
742 return start;
746 /****************************************************************************
747 ****************************************************************************
749 * Device model: input, hwmon and platform
751 ****************************************************************************
752 ****************************************************************************/
754 static struct platform_device *tpacpi_pdev;
755 static struct platform_device *tpacpi_sensors_pdev;
756 static struct device *tpacpi_hwmon;
757 static struct input_dev *tpacpi_inputdev;
758 static struct mutex tpacpi_inputdev_send_mutex;
759 static LIST_HEAD(tpacpi_all_drivers);
761 static int tpacpi_suspend_handler(struct platform_device *pdev,
762 pm_message_t state)
764 struct ibm_struct *ibm, *itmp;
766 list_for_each_entry_safe(ibm, itmp,
767 &tpacpi_all_drivers,
768 all_drivers) {
769 if (ibm->suspend)
770 (ibm->suspend)(state);
773 return 0;
776 static int tpacpi_resume_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->resume)
784 (ibm->resume)();
787 return 0;
790 static void tpacpi_shutdown_handler(struct platform_device *pdev)
792 struct ibm_struct *ibm, *itmp;
794 list_for_each_entry_safe(ibm, itmp,
795 &tpacpi_all_drivers,
796 all_drivers) {
797 if (ibm->shutdown)
798 (ibm->shutdown)();
802 static struct platform_driver tpacpi_pdriver = {
803 .driver = {
804 .name = TPACPI_DRVR_NAME,
805 .owner = THIS_MODULE,
807 .suspend = tpacpi_suspend_handler,
808 .resume = tpacpi_resume_handler,
809 .shutdown = tpacpi_shutdown_handler,
812 static struct platform_driver tpacpi_hwmon_pdriver = {
813 .driver = {
814 .name = TPACPI_HWMON_DRVR_NAME,
815 .owner = THIS_MODULE,
819 /*************************************************************************
820 * sysfs support helpers
823 struct attribute_set {
824 unsigned int members, max_members;
825 struct attribute_group group;
828 struct attribute_set_obj {
829 struct attribute_set s;
830 struct attribute *a;
831 } __attribute__((packed));
833 static struct attribute_set *create_attr_set(unsigned int max_members,
834 const char *name)
836 struct attribute_set_obj *sobj;
838 if (max_members == 0)
839 return NULL;
841 /* Allocates space for implicit NULL at the end too */
842 sobj = kzalloc(sizeof(struct attribute_set_obj) +
843 max_members * sizeof(struct attribute *),
844 GFP_KERNEL);
845 if (!sobj)
846 return NULL;
847 sobj->s.max_members = max_members;
848 sobj->s.group.attrs = &sobj->a;
849 sobj->s.group.name = name;
851 return &sobj->s;
854 #define destroy_attr_set(_set) \
855 kfree(_set);
857 /* not multi-threaded safe, use it in a single thread per set */
858 static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
860 if (!s || !attr)
861 return -EINVAL;
863 if (s->members >= s->max_members)
864 return -ENOMEM;
866 s->group.attrs[s->members] = attr;
867 s->members++;
869 return 0;
872 static int add_many_to_attr_set(struct attribute_set *s,
873 struct attribute **attr,
874 unsigned int count)
876 int i, res;
878 for (i = 0; i < count; i++) {
879 res = add_to_attr_set(s, attr[i]);
880 if (res)
881 return res;
884 return 0;
887 static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
889 sysfs_remove_group(kobj, &s->group);
890 destroy_attr_set(s);
893 #define register_attr_set_with_sysfs(_attr_set, _kobj) \
894 sysfs_create_group(_kobj, &_attr_set->group)
896 static int parse_strtoul(const char *buf,
897 unsigned long max, unsigned long *value)
899 char *endp;
901 while (*buf && isspace(*buf))
902 buf++;
903 *value = simple_strtoul(buf, &endp, 0);
904 while (*endp && isspace(*endp))
905 endp++;
906 if (*endp || *value > max)
907 return -EINVAL;
909 return 0;
912 static void tpacpi_disable_brightness_delay(void)
914 if (acpi_evalf(hkey_handle, NULL, "PWMS", "qvd", 0))
915 printk(TPACPI_NOTICE
916 "ACPI backlight control delay disabled\n");
919 static int __init tpacpi_query_bcl_levels(acpi_handle handle)
921 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
922 union acpi_object *obj;
923 int rc;
925 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
926 obj = (union acpi_object *)buffer.pointer;
927 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
928 printk(TPACPI_ERR "Unknown _BCL data, "
929 "please report this to %s\n", TPACPI_MAIL);
930 rc = 0;
931 } else {
932 rc = obj->package.count;
934 } else {
935 return 0;
938 kfree(buffer.pointer);
939 return rc;
942 static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
943 u32 lvl, void *context, void **rv)
945 char name[ACPI_PATH_SEGMENT_LENGTH];
946 struct acpi_buffer buffer = { sizeof(name), &name };
948 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
949 !strncmp("_BCL", name, sizeof(name) - 1)) {
950 BUG_ON(!rv || !*rv);
951 **(int **)rv = tpacpi_query_bcl_levels(handle);
952 return AE_CTRL_TERMINATE;
953 } else {
954 return AE_OK;
959 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
961 static int __init tpacpi_check_std_acpi_brightness_support(void)
963 int status;
964 int bcl_levels = 0;
965 void *bcl_ptr = &bcl_levels;
967 if (!vid_handle) {
968 TPACPI_ACPIHANDLE_INIT(vid);
970 if (!vid_handle)
971 return 0;
974 * Search for a _BCL method, and execute it. This is safe on all
975 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
976 * BIOS in ACPI backlight control mode. We do NOT have to care
977 * about calling the _BCL method in an enabled video device, any
978 * will do for our purposes.
981 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
982 tpacpi_acpi_walk_find_bcl, NULL,
983 &bcl_ptr);
985 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
986 tp_features.bright_acpimode = 1;
987 return (bcl_levels - 2);
990 return 0;
993 static int __init tpacpi_new_rfkill(const unsigned int id,
994 struct rfkill **rfk,
995 const enum rfkill_type rfktype,
996 const char *name,
997 const bool set_default,
998 int (*toggle_radio)(void *, enum rfkill_state),
999 int (*get_state)(void *, enum rfkill_state *))
1001 int res;
1002 enum rfkill_state initial_state = RFKILL_STATE_SOFT_BLOCKED;
1004 res = get_state(NULL, &initial_state);
1005 if (res < 0) {
1006 printk(TPACPI_ERR
1007 "failed to read initial state for %s, error %d; "
1008 "will turn radio off\n", name, res);
1009 } else if (set_default) {
1010 /* try to set the initial state as the default for the rfkill
1011 * type, since we ask the firmware to preserve it across S5 in
1012 * NVRAM */
1013 rfkill_set_default(rfktype,
1014 (initial_state == RFKILL_STATE_UNBLOCKED) ?
1015 RFKILL_STATE_UNBLOCKED :
1016 RFKILL_STATE_SOFT_BLOCKED);
1019 *rfk = rfkill_allocate(&tpacpi_pdev->dev, rfktype);
1020 if (!*rfk) {
1021 printk(TPACPI_ERR
1022 "failed to allocate memory for rfkill class\n");
1023 return -ENOMEM;
1026 (*rfk)->name = name;
1027 (*rfk)->get_state = get_state;
1028 (*rfk)->toggle_radio = toggle_radio;
1029 (*rfk)->state = initial_state;
1031 res = rfkill_register(*rfk);
1032 if (res < 0) {
1033 printk(TPACPI_ERR
1034 "failed to register %s rfkill switch: %d\n",
1035 name, res);
1036 rfkill_free(*rfk);
1037 *rfk = NULL;
1038 return res;
1041 return 0;
1044 /*************************************************************************
1045 * thinkpad-acpi driver attributes
1048 /* interface_version --------------------------------------------------- */
1049 static ssize_t tpacpi_driver_interface_version_show(
1050 struct device_driver *drv,
1051 char *buf)
1053 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
1056 static DRIVER_ATTR(interface_version, S_IRUGO,
1057 tpacpi_driver_interface_version_show, NULL);
1059 /* debug_level --------------------------------------------------------- */
1060 static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
1061 char *buf)
1063 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
1066 static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
1067 const char *buf, size_t count)
1069 unsigned long t;
1071 if (parse_strtoul(buf, 0xffff, &t))
1072 return -EINVAL;
1074 dbg_level = t;
1076 return count;
1079 static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
1080 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
1082 /* version ------------------------------------------------------------- */
1083 static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
1084 char *buf)
1086 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
1087 TPACPI_DESC, TPACPI_VERSION);
1090 static DRIVER_ATTR(version, S_IRUGO,
1091 tpacpi_driver_version_show, NULL);
1093 /* --------------------------------------------------------------------- */
1095 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1097 static void tpacpi_send_radiosw_update(void);
1099 /* wlsw_emulstate ------------------------------------------------------ */
1100 static ssize_t tpacpi_driver_wlsw_emulstate_show(struct device_driver *drv,
1101 char *buf)
1103 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wlsw_emulstate);
1106 static ssize_t tpacpi_driver_wlsw_emulstate_store(struct device_driver *drv,
1107 const char *buf, size_t count)
1109 unsigned long t;
1111 if (parse_strtoul(buf, 1, &t))
1112 return -EINVAL;
1114 if (tpacpi_wlsw_emulstate != t) {
1115 tpacpi_wlsw_emulstate = !!t;
1116 tpacpi_send_radiosw_update();
1117 } else
1118 tpacpi_wlsw_emulstate = !!t;
1120 return count;
1123 static DRIVER_ATTR(wlsw_emulstate, S_IWUSR | S_IRUGO,
1124 tpacpi_driver_wlsw_emulstate_show,
1125 tpacpi_driver_wlsw_emulstate_store);
1127 /* bluetooth_emulstate ------------------------------------------------- */
1128 static ssize_t tpacpi_driver_bluetooth_emulstate_show(
1129 struct device_driver *drv,
1130 char *buf)
1132 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_bluetooth_emulstate);
1135 static ssize_t tpacpi_driver_bluetooth_emulstate_store(
1136 struct device_driver *drv,
1137 const char *buf, size_t count)
1139 unsigned long t;
1141 if (parse_strtoul(buf, 1, &t))
1142 return -EINVAL;
1144 tpacpi_bluetooth_emulstate = !!t;
1146 return count;
1149 static DRIVER_ATTR(bluetooth_emulstate, S_IWUSR | S_IRUGO,
1150 tpacpi_driver_bluetooth_emulstate_show,
1151 tpacpi_driver_bluetooth_emulstate_store);
1153 /* wwan_emulstate ------------------------------------------------- */
1154 static ssize_t tpacpi_driver_wwan_emulstate_show(
1155 struct device_driver *drv,
1156 char *buf)
1158 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_wwan_emulstate);
1161 static ssize_t tpacpi_driver_wwan_emulstate_store(
1162 struct device_driver *drv,
1163 const char *buf, size_t count)
1165 unsigned long t;
1167 if (parse_strtoul(buf, 1, &t))
1168 return -EINVAL;
1170 tpacpi_wwan_emulstate = !!t;
1172 return count;
1175 static DRIVER_ATTR(wwan_emulstate, S_IWUSR | S_IRUGO,
1176 tpacpi_driver_wwan_emulstate_show,
1177 tpacpi_driver_wwan_emulstate_store);
1179 /* uwb_emulstate ------------------------------------------------- */
1180 static ssize_t tpacpi_driver_uwb_emulstate_show(
1181 struct device_driver *drv,
1182 char *buf)
1184 return snprintf(buf, PAGE_SIZE, "%d\n", !!tpacpi_uwb_emulstate);
1187 static ssize_t tpacpi_driver_uwb_emulstate_store(
1188 struct device_driver *drv,
1189 const char *buf, size_t count)
1191 unsigned long t;
1193 if (parse_strtoul(buf, 1, &t))
1194 return -EINVAL;
1196 tpacpi_uwb_emulstate = !!t;
1198 return count;
1201 static DRIVER_ATTR(uwb_emulstate, S_IWUSR | S_IRUGO,
1202 tpacpi_driver_uwb_emulstate_show,
1203 tpacpi_driver_uwb_emulstate_store);
1204 #endif
1206 /* --------------------------------------------------------------------- */
1208 static struct driver_attribute *tpacpi_driver_attributes[] = {
1209 &driver_attr_debug_level, &driver_attr_version,
1210 &driver_attr_interface_version,
1213 static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
1215 int i, res;
1217 i = 0;
1218 res = 0;
1219 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
1220 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
1221 i++;
1224 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1225 if (!res && dbg_wlswemul)
1226 res = driver_create_file(drv, &driver_attr_wlsw_emulstate);
1227 if (!res && dbg_bluetoothemul)
1228 res = driver_create_file(drv, &driver_attr_bluetooth_emulstate);
1229 if (!res && dbg_wwanemul)
1230 res = driver_create_file(drv, &driver_attr_wwan_emulstate);
1231 if (!res && dbg_uwbemul)
1232 res = driver_create_file(drv, &driver_attr_uwb_emulstate);
1233 #endif
1235 return res;
1238 static void tpacpi_remove_driver_attributes(struct device_driver *drv)
1240 int i;
1242 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
1243 driver_remove_file(drv, tpacpi_driver_attributes[i]);
1245 #ifdef THINKPAD_ACPI_DEBUGFACILITIES
1246 driver_remove_file(drv, &driver_attr_wlsw_emulstate);
1247 driver_remove_file(drv, &driver_attr_bluetooth_emulstate);
1248 driver_remove_file(drv, &driver_attr_wwan_emulstate);
1249 driver_remove_file(drv, &driver_attr_uwb_emulstate);
1250 #endif
1253 /****************************************************************************
1254 ****************************************************************************
1256 * Subdrivers
1258 ****************************************************************************
1259 ****************************************************************************/
1261 /*************************************************************************
1262 * thinkpad-acpi init subdriver
1265 static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
1267 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1268 printk(TPACPI_INFO "%s\n", TPACPI_URL);
1270 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
1271 (thinkpad_id.bios_version_str) ?
1272 thinkpad_id.bios_version_str : "unknown",
1273 (thinkpad_id.ec_version_str) ?
1274 thinkpad_id.ec_version_str : "unknown");
1276 if (thinkpad_id.vendor && thinkpad_id.model_str)
1277 printk(TPACPI_INFO "%s %s, model %s\n",
1278 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1279 "IBM" : ((thinkpad_id.vendor ==
1280 PCI_VENDOR_ID_LENOVO) ?
1281 "Lenovo" : "Unknown vendor"),
1282 thinkpad_id.model_str,
1283 (thinkpad_id.nummodel_str) ?
1284 thinkpad_id.nummodel_str : "unknown");
1286 return 0;
1289 static int thinkpad_acpi_driver_read(char *p)
1291 int len = 0;
1293 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1294 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
1296 return len;
1299 static struct ibm_struct thinkpad_acpi_driver_data = {
1300 .name = "driver",
1301 .read = thinkpad_acpi_driver_read,
1304 /*************************************************************************
1305 * Hotkey subdriver
1308 enum { /* hot key scan codes (derived from ACPI DSDT) */
1309 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1310 TP_ACPI_HOTKEYSCAN_FNF2,
1311 TP_ACPI_HOTKEYSCAN_FNF3,
1312 TP_ACPI_HOTKEYSCAN_FNF4,
1313 TP_ACPI_HOTKEYSCAN_FNF5,
1314 TP_ACPI_HOTKEYSCAN_FNF6,
1315 TP_ACPI_HOTKEYSCAN_FNF7,
1316 TP_ACPI_HOTKEYSCAN_FNF8,
1317 TP_ACPI_HOTKEYSCAN_FNF9,
1318 TP_ACPI_HOTKEYSCAN_FNF10,
1319 TP_ACPI_HOTKEYSCAN_FNF11,
1320 TP_ACPI_HOTKEYSCAN_FNF12,
1321 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1322 TP_ACPI_HOTKEYSCAN_FNINSERT,
1323 TP_ACPI_HOTKEYSCAN_FNDELETE,
1324 TP_ACPI_HOTKEYSCAN_FNHOME,
1325 TP_ACPI_HOTKEYSCAN_FNEND,
1326 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1327 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1328 TP_ACPI_HOTKEYSCAN_FNSPACE,
1329 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1330 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1331 TP_ACPI_HOTKEYSCAN_MUTE,
1332 TP_ACPI_HOTKEYSCAN_THINKPAD,
1335 enum { /* Keys available through NVRAM polling */
1336 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1337 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1340 enum { /* Positions of some of the keys in hotkey masks */
1341 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1342 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1343 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1344 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1345 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1346 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1347 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1348 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1349 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1350 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1351 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1354 enum { /* NVRAM to ACPI HKEY group map */
1355 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1356 TP_ACPI_HKEY_ZOOM_MASK |
1357 TP_ACPI_HKEY_DISPSWTCH_MASK |
1358 TP_ACPI_HKEY_HIBERNATE_MASK,
1359 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1360 TP_ACPI_HKEY_BRGHTDWN_MASK,
1361 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1362 TP_ACPI_HKEY_VOLDWN_MASK |
1363 TP_ACPI_HKEY_MUTE_MASK,
1366 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1367 struct tp_nvram_state {
1368 u16 thinkpad_toggle:1;
1369 u16 zoom_toggle:1;
1370 u16 display_toggle:1;
1371 u16 thinklight_toggle:1;
1372 u16 hibernate_toggle:1;
1373 u16 displayexp_toggle:1;
1374 u16 display_state:1;
1375 u16 brightness_toggle:1;
1376 u16 volume_toggle:1;
1377 u16 mute:1;
1379 u8 brightness_level;
1380 u8 volume_level;
1383 static struct task_struct *tpacpi_hotkey_task;
1384 static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1385 static int hotkey_poll_freq = 10; /* Hz */
1386 static struct mutex hotkey_thread_mutex;
1387 static struct mutex hotkey_thread_data_mutex;
1388 static unsigned int hotkey_config_change;
1390 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1392 #define hotkey_source_mask 0U
1394 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1396 static struct mutex hotkey_mutex;
1398 static enum { /* Reasons for waking up */
1399 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1400 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1401 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1402 } hotkey_wakeup_reason;
1404 static int hotkey_autosleep_ack;
1406 static u32 hotkey_orig_mask;
1407 static u32 hotkey_all_mask;
1408 static u32 hotkey_reserved_mask;
1409 static u32 hotkey_mask;
1411 static unsigned int hotkey_report_mode;
1413 static u16 *hotkey_keycode_map;
1415 static struct attribute_set *hotkey_dev_attributes;
1417 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1418 #define HOTKEY_CONFIG_CRITICAL_START \
1419 do { \
1420 mutex_lock(&hotkey_thread_data_mutex); \
1421 hotkey_config_change++; \
1422 } while (0);
1423 #define HOTKEY_CONFIG_CRITICAL_END \
1424 mutex_unlock(&hotkey_thread_data_mutex);
1425 #else
1426 #define HOTKEY_CONFIG_CRITICAL_START
1427 #define HOTKEY_CONFIG_CRITICAL_END
1428 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1430 /* HKEY.MHKG() return bits */
1431 #define TP_HOTKEY_TABLET_MASK (1 << 3)
1433 static int hotkey_get_wlsw(int *status)
1435 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
1436 if (dbg_wlswemul) {
1437 *status = !!tpacpi_wlsw_emulstate;
1438 return 0;
1440 #endif
1441 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1442 return -EIO;
1443 return 0;
1446 static int hotkey_get_tablet_mode(int *status)
1448 int s;
1450 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1451 return -EIO;
1453 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1454 return 0;
1458 * Call with hotkey_mutex held
1460 static int hotkey_mask_get(void)
1462 u32 m = 0;
1464 if (tp_features.hotkey_mask) {
1465 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
1466 return -EIO;
1468 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
1470 return 0;
1474 * Call with hotkey_mutex held
1476 static int hotkey_mask_set(u32 mask)
1478 int i;
1479 int rc = 0;
1481 if (tp_features.hotkey_mask) {
1482 if (!tp_warned.hotkey_mask_ff &&
1483 (mask == 0xffff || mask == 0xffffff ||
1484 mask == 0xffffffff)) {
1485 tp_warned.hotkey_mask_ff = 1;
1486 printk(TPACPI_NOTICE
1487 "setting the hotkey mask to 0x%08x is likely "
1488 "not the best way to go about it\n", mask);
1489 printk(TPACPI_NOTICE
1490 "please consider using the driver defaults, "
1491 "and refer to up-to-date thinkpad-acpi "
1492 "documentation\n");
1495 HOTKEY_CONFIG_CRITICAL_START
1496 for (i = 0; i < 32; i++) {
1497 u32 m = 1 << i;
1498 /* enable in firmware mask only keys not in NVRAM
1499 * mode, but enable the key in the cached hotkey_mask
1500 * regardless of mode, or the key will end up
1501 * disabled by hotkey_mask_get() */
1502 if (!acpi_evalf(hkey_handle,
1503 NULL, "MHKM", "vdd", i + 1,
1504 !!((mask & ~hotkey_source_mask) & m))) {
1505 rc = -EIO;
1506 break;
1507 } else {
1508 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1511 HOTKEY_CONFIG_CRITICAL_END
1513 /* hotkey_mask_get must be called unconditionally below */
1514 if (!hotkey_mask_get() && !rc &&
1515 (hotkey_mask & ~hotkey_source_mask) !=
1516 (mask & ~hotkey_source_mask)) {
1517 printk(TPACPI_NOTICE
1518 "requested hot key mask 0x%08x, but "
1519 "firmware forced it to 0x%08x\n",
1520 mask, hotkey_mask);
1522 } else {
1523 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1524 HOTKEY_CONFIG_CRITICAL_START
1525 hotkey_mask = mask & hotkey_source_mask;
1526 HOTKEY_CONFIG_CRITICAL_END
1527 hotkey_mask_get();
1528 if (hotkey_mask != mask) {
1529 printk(TPACPI_NOTICE
1530 "requested hot key mask 0x%08x, "
1531 "forced to 0x%08x (NVRAM poll mask is "
1532 "0x%08x): no firmware mask support\n",
1533 mask, hotkey_mask, hotkey_source_mask);
1535 #else
1536 hotkey_mask_get();
1537 rc = -ENXIO;
1538 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1541 return rc;
1544 static int hotkey_status_get(int *status)
1546 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1547 return -EIO;
1549 return 0;
1552 static int hotkey_status_set(bool enable)
1554 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", enable ? 1 : 0))
1555 return -EIO;
1557 return 0;
1560 static void tpacpi_input_send_tabletsw(void)
1562 int state;
1564 if (tp_features.hotkey_tablet &&
1565 !hotkey_get_tablet_mode(&state)) {
1566 mutex_lock(&tpacpi_inputdev_send_mutex);
1568 input_report_switch(tpacpi_inputdev,
1569 SW_TABLET_MODE, !!state);
1570 input_sync(tpacpi_inputdev);
1572 mutex_unlock(&tpacpi_inputdev_send_mutex);
1576 static void tpacpi_input_send_key(unsigned int scancode)
1578 unsigned int keycode;
1580 keycode = hotkey_keycode_map[scancode];
1582 if (keycode != KEY_RESERVED) {
1583 mutex_lock(&tpacpi_inputdev_send_mutex);
1585 input_report_key(tpacpi_inputdev, keycode, 1);
1586 if (keycode == KEY_UNKNOWN)
1587 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1588 scancode);
1589 input_sync(tpacpi_inputdev);
1591 input_report_key(tpacpi_inputdev, keycode, 0);
1592 if (keycode == KEY_UNKNOWN)
1593 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1594 scancode);
1595 input_sync(tpacpi_inputdev);
1597 mutex_unlock(&tpacpi_inputdev_send_mutex);
1601 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1602 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1604 static void tpacpi_hotkey_send_key(unsigned int scancode)
1606 tpacpi_input_send_key(scancode);
1607 if (hotkey_report_mode < 2) {
1608 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1609 0x80, 0x1001 + scancode);
1613 static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1615 u8 d;
1617 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1618 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1619 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1620 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1621 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1622 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1624 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1625 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1626 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1628 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1629 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1630 n->displayexp_toggle =
1631 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1633 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1634 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1635 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1636 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1637 n->brightness_toggle =
1638 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1640 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1641 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1642 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1643 >> TP_NVRAM_POS_LEVEL_VOLUME;
1644 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1645 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1649 #define TPACPI_COMPARE_KEY(__scancode, __member) \
1650 do { \
1651 if ((mask & (1 << __scancode)) && \
1652 oldn->__member != newn->__member) \
1653 tpacpi_hotkey_send_key(__scancode); \
1654 } while (0)
1656 #define TPACPI_MAY_SEND_KEY(__scancode) \
1657 do { if (mask & (1 << __scancode)) \
1658 tpacpi_hotkey_send_key(__scancode); } while (0)
1660 static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
1661 struct tp_nvram_state *newn,
1662 u32 mask)
1664 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1665 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1666 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1667 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1669 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1671 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1673 /* handle volume */
1674 if (oldn->volume_toggle != newn->volume_toggle) {
1675 if (oldn->mute != newn->mute) {
1676 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1678 if (oldn->volume_level > newn->volume_level) {
1679 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1680 } else if (oldn->volume_level < newn->volume_level) {
1681 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1682 } else if (oldn->mute == newn->mute) {
1683 /* repeated key presses that didn't change state */
1684 if (newn->mute) {
1685 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1686 } else if (newn->volume_level != 0) {
1687 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1688 } else {
1689 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1694 /* handle brightness */
1695 if (oldn->brightness_toggle != newn->brightness_toggle) {
1696 if (oldn->brightness_level < newn->brightness_level) {
1697 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1698 } else if (oldn->brightness_level > newn->brightness_level) {
1699 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1700 } else {
1701 /* repeated key presses that didn't change state */
1702 if (newn->brightness_level != 0) {
1703 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1704 } else {
1705 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1711 #undef TPACPI_COMPARE_KEY
1712 #undef TPACPI_MAY_SEND_KEY
1714 static int hotkey_kthread(void *data)
1716 struct tp_nvram_state s[2];
1717 u32 mask;
1718 unsigned int si, so;
1719 unsigned long t;
1720 unsigned int change_detector, must_reset;
1722 mutex_lock(&hotkey_thread_mutex);
1724 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1725 goto exit;
1727 set_freezable();
1729 so = 0;
1730 si = 1;
1731 t = 0;
1733 /* Initial state for compares */
1734 mutex_lock(&hotkey_thread_data_mutex);
1735 change_detector = hotkey_config_change;
1736 mask = hotkey_source_mask & hotkey_mask;
1737 mutex_unlock(&hotkey_thread_data_mutex);
1738 hotkey_read_nvram(&s[so], mask);
1740 while (!kthread_should_stop() && hotkey_poll_freq) {
1741 if (t == 0)
1742 t = 1000/hotkey_poll_freq;
1743 t = msleep_interruptible(t);
1744 if (unlikely(kthread_should_stop()))
1745 break;
1746 must_reset = try_to_freeze();
1747 if (t > 0 && !must_reset)
1748 continue;
1750 mutex_lock(&hotkey_thread_data_mutex);
1751 if (must_reset || hotkey_config_change != change_detector) {
1752 /* forget old state on thaw or config change */
1753 si = so;
1754 t = 0;
1755 change_detector = hotkey_config_change;
1757 mask = hotkey_source_mask & hotkey_mask;
1758 mutex_unlock(&hotkey_thread_data_mutex);
1760 if (likely(mask)) {
1761 hotkey_read_nvram(&s[si], mask);
1762 if (likely(si != so)) {
1763 hotkey_compare_and_issue_event(&s[so], &s[si],
1764 mask);
1768 so = si;
1769 si ^= 1;
1772 exit:
1773 mutex_unlock(&hotkey_thread_mutex);
1774 return 0;
1777 static void hotkey_poll_stop_sync(void)
1779 if (tpacpi_hotkey_task) {
1780 if (frozen(tpacpi_hotkey_task) ||
1781 freezing(tpacpi_hotkey_task))
1782 thaw_process(tpacpi_hotkey_task);
1784 kthread_stop(tpacpi_hotkey_task);
1785 tpacpi_hotkey_task = NULL;
1786 mutex_lock(&hotkey_thread_mutex);
1787 /* at this point, the thread did exit */
1788 mutex_unlock(&hotkey_thread_mutex);
1792 /* call with hotkey_mutex held */
1793 static void hotkey_poll_setup(int may_warn)
1795 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1796 hotkey_poll_freq > 0 &&
1797 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1798 if (!tpacpi_hotkey_task) {
1799 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
1800 NULL, TPACPI_NVRAM_KTHREAD_NAME);
1801 if (IS_ERR(tpacpi_hotkey_task)) {
1802 tpacpi_hotkey_task = NULL;
1803 printk(TPACPI_ERR
1804 "could not create kernel thread "
1805 "for hotkey polling\n");
1808 } else {
1809 hotkey_poll_stop_sync();
1810 if (may_warn &&
1811 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
1812 printk(TPACPI_NOTICE
1813 "hot keys 0x%08x require polling, "
1814 "which is currently disabled\n",
1815 hotkey_source_mask);
1820 static void hotkey_poll_setup_safe(int may_warn)
1822 mutex_lock(&hotkey_mutex);
1823 hotkey_poll_setup(may_warn);
1824 mutex_unlock(&hotkey_mutex);
1827 #else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1829 static void hotkey_poll_setup_safe(int __unused)
1833 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1835 static int hotkey_inputdev_open(struct input_dev *dev)
1837 switch (tpacpi_lifecycle) {
1838 case TPACPI_LIFE_INIT:
1840 * hotkey_init will call hotkey_poll_setup_safe
1841 * at the appropriate moment
1843 return 0;
1844 case TPACPI_LIFE_EXITING:
1845 return -EBUSY;
1846 case TPACPI_LIFE_RUNNING:
1847 hotkey_poll_setup_safe(0);
1848 return 0;
1851 /* Should only happen if tpacpi_lifecycle is corrupt */
1852 BUG();
1853 return -EBUSY;
1856 static void hotkey_inputdev_close(struct input_dev *dev)
1858 /* disable hotkey polling when possible */
1859 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1860 hotkey_poll_setup_safe(0);
1863 /* sysfs hotkey enable ------------------------------------------------- */
1864 static ssize_t hotkey_enable_show(struct device *dev,
1865 struct device_attribute *attr,
1866 char *buf)
1868 int res, status;
1870 res = hotkey_status_get(&status);
1871 if (res)
1872 return res;
1874 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1877 static ssize_t hotkey_enable_store(struct device *dev,
1878 struct device_attribute *attr,
1879 const char *buf, size_t count)
1881 unsigned long t;
1883 if (parse_strtoul(buf, 1, &t))
1884 return -EINVAL;
1886 if (t == 0)
1887 return -EPERM;
1889 return count;
1892 static struct device_attribute dev_attr_hotkey_enable =
1893 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
1894 hotkey_enable_show, hotkey_enable_store);
1896 /* sysfs hotkey mask --------------------------------------------------- */
1897 static ssize_t hotkey_mask_show(struct device *dev,
1898 struct device_attribute *attr,
1899 char *buf)
1901 int res;
1903 if (mutex_lock_killable(&hotkey_mutex))
1904 return -ERESTARTSYS;
1905 res = hotkey_mask_get();
1906 mutex_unlock(&hotkey_mutex);
1908 return (res)?
1909 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
1912 static ssize_t hotkey_mask_store(struct device *dev,
1913 struct device_attribute *attr,
1914 const char *buf, size_t count)
1916 unsigned long t;
1917 int res;
1919 if (parse_strtoul(buf, 0xffffffffUL, &t))
1920 return -EINVAL;
1922 if (mutex_lock_killable(&hotkey_mutex))
1923 return -ERESTARTSYS;
1925 res = hotkey_mask_set(t);
1927 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1928 hotkey_poll_setup(1);
1929 #endif
1931 mutex_unlock(&hotkey_mutex);
1933 return (res) ? res : count;
1936 static struct device_attribute dev_attr_hotkey_mask =
1937 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
1938 hotkey_mask_show, hotkey_mask_store);
1940 /* sysfs hotkey bios_enabled ------------------------------------------- */
1941 static ssize_t hotkey_bios_enabled_show(struct device *dev,
1942 struct device_attribute *attr,
1943 char *buf)
1945 return sprintf(buf, "0\n");
1948 static struct device_attribute dev_attr_hotkey_bios_enabled =
1949 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
1951 /* sysfs hotkey bios_mask ---------------------------------------------- */
1952 static ssize_t hotkey_bios_mask_show(struct device *dev,
1953 struct device_attribute *attr,
1954 char *buf)
1956 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
1959 static struct device_attribute dev_attr_hotkey_bios_mask =
1960 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
1962 /* sysfs hotkey all_mask ----------------------------------------------- */
1963 static ssize_t hotkey_all_mask_show(struct device *dev,
1964 struct device_attribute *attr,
1965 char *buf)
1967 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1968 hotkey_all_mask | hotkey_source_mask);
1971 static struct device_attribute dev_attr_hotkey_all_mask =
1972 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1974 /* sysfs hotkey recommended_mask --------------------------------------- */
1975 static ssize_t hotkey_recommended_mask_show(struct device *dev,
1976 struct device_attribute *attr,
1977 char *buf)
1979 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1980 (hotkey_all_mask | hotkey_source_mask)
1981 & ~hotkey_reserved_mask);
1984 static struct device_attribute dev_attr_hotkey_recommended_mask =
1985 __ATTR(hotkey_recommended_mask, S_IRUGO,
1986 hotkey_recommended_mask_show, NULL);
1988 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1990 /* sysfs hotkey hotkey_source_mask ------------------------------------- */
1991 static ssize_t hotkey_source_mask_show(struct device *dev,
1992 struct device_attribute *attr,
1993 char *buf)
1995 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1998 static ssize_t hotkey_source_mask_store(struct device *dev,
1999 struct device_attribute *attr,
2000 const char *buf, size_t count)
2002 unsigned long t;
2004 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
2005 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
2006 return -EINVAL;
2008 if (mutex_lock_killable(&hotkey_mutex))
2009 return -ERESTARTSYS;
2011 HOTKEY_CONFIG_CRITICAL_START
2012 hotkey_source_mask = t;
2013 HOTKEY_CONFIG_CRITICAL_END
2015 hotkey_poll_setup(1);
2017 mutex_unlock(&hotkey_mutex);
2019 return count;
2022 static struct device_attribute dev_attr_hotkey_source_mask =
2023 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
2024 hotkey_source_mask_show, hotkey_source_mask_store);
2026 /* sysfs hotkey hotkey_poll_freq --------------------------------------- */
2027 static ssize_t hotkey_poll_freq_show(struct device *dev,
2028 struct device_attribute *attr,
2029 char *buf)
2031 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
2034 static ssize_t hotkey_poll_freq_store(struct device *dev,
2035 struct device_attribute *attr,
2036 const char *buf, size_t count)
2038 unsigned long t;
2040 if (parse_strtoul(buf, 25, &t))
2041 return -EINVAL;
2043 if (mutex_lock_killable(&hotkey_mutex))
2044 return -ERESTARTSYS;
2046 hotkey_poll_freq = t;
2048 hotkey_poll_setup(1);
2049 mutex_unlock(&hotkey_mutex);
2051 return count;
2054 static struct device_attribute dev_attr_hotkey_poll_freq =
2055 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
2056 hotkey_poll_freq_show, hotkey_poll_freq_store);
2058 #endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
2060 /* sysfs hotkey radio_sw (pollable) ------------------------------------ */
2061 static ssize_t hotkey_radio_sw_show(struct device *dev,
2062 struct device_attribute *attr,
2063 char *buf)
2065 int res, s;
2066 res = hotkey_get_wlsw(&s);
2067 if (res < 0)
2068 return res;
2070 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2073 static struct device_attribute dev_attr_hotkey_radio_sw =
2074 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
2076 static void hotkey_radio_sw_notify_change(void)
2078 if (tp_features.hotkey_wlsw)
2079 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2080 "hotkey_radio_sw");
2083 /* sysfs hotkey tablet mode (pollable) --------------------------------- */
2084 static ssize_t hotkey_tablet_mode_show(struct device *dev,
2085 struct device_attribute *attr,
2086 char *buf)
2088 int res, s;
2089 res = hotkey_get_tablet_mode(&s);
2090 if (res < 0)
2091 return res;
2093 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
2096 static struct device_attribute dev_attr_hotkey_tablet_mode =
2097 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
2099 static void hotkey_tablet_mode_notify_change(void)
2101 if (tp_features.hotkey_tablet)
2102 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2103 "hotkey_tablet_mode");
2106 /* sysfs hotkey report_mode -------------------------------------------- */
2107 static ssize_t hotkey_report_mode_show(struct device *dev,
2108 struct device_attribute *attr,
2109 char *buf)
2111 return snprintf(buf, PAGE_SIZE, "%d\n",
2112 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
2115 static struct device_attribute dev_attr_hotkey_report_mode =
2116 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
2118 /* sysfs wakeup reason (pollable) -------------------------------------- */
2119 static ssize_t hotkey_wakeup_reason_show(struct device *dev,
2120 struct device_attribute *attr,
2121 char *buf)
2123 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
2126 static struct device_attribute dev_attr_hotkey_wakeup_reason =
2127 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
2129 static void hotkey_wakeup_reason_notify_change(void)
2131 if (tp_features.hotkey_mask)
2132 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2133 "wakeup_reason");
2136 /* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
2137 static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
2138 struct device_attribute *attr,
2139 char *buf)
2141 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
2144 static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
2145 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
2146 hotkey_wakeup_hotunplug_complete_show, NULL);
2148 static void hotkey_wakeup_hotunplug_complete_notify_change(void)
2150 if (tp_features.hotkey_mask)
2151 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
2152 "wakeup_hotunplug_complete");
2155 /* --------------------------------------------------------------------- */
2157 static struct attribute *hotkey_attributes[] __initdata = {
2158 &dev_attr_hotkey_enable.attr,
2159 &dev_attr_hotkey_bios_enabled.attr,
2160 &dev_attr_hotkey_report_mode.attr,
2161 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2162 &dev_attr_hotkey_mask.attr,
2163 &dev_attr_hotkey_all_mask.attr,
2164 &dev_attr_hotkey_recommended_mask.attr,
2165 &dev_attr_hotkey_source_mask.attr,
2166 &dev_attr_hotkey_poll_freq.attr,
2167 #endif
2170 static struct attribute *hotkey_mask_attributes[] __initdata = {
2171 &dev_attr_hotkey_bios_mask.attr,
2172 #ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2173 &dev_attr_hotkey_mask.attr,
2174 &dev_attr_hotkey_all_mask.attr,
2175 &dev_attr_hotkey_recommended_mask.attr,
2176 #endif
2177 &dev_attr_hotkey_wakeup_reason.attr,
2178 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
2181 static void bluetooth_update_rfk(void);
2182 static void wan_update_rfk(void);
2183 static void uwb_update_rfk(void);
2184 static void tpacpi_send_radiosw_update(void)
2186 int wlsw;
2188 /* Sync these BEFORE sending any rfkill events */
2189 if (tp_features.bluetooth)
2190 bluetooth_update_rfk();
2191 if (tp_features.wan)
2192 wan_update_rfk();
2193 if (tp_features.uwb)
2194 uwb_update_rfk();
2196 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
2197 mutex_lock(&tpacpi_inputdev_send_mutex);
2199 input_report_switch(tpacpi_inputdev,
2200 SW_RFKILL_ALL, !!wlsw);
2201 input_sync(tpacpi_inputdev);
2203 mutex_unlock(&tpacpi_inputdev_send_mutex);
2205 hotkey_radio_sw_notify_change();
2208 static void hotkey_exit(void)
2210 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2211 hotkey_poll_stop_sync();
2212 #endif
2214 if (hotkey_dev_attributes)
2215 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2217 kfree(hotkey_keycode_map);
2219 if (tp_features.hotkey) {
2220 dbg_printk(TPACPI_DBG_EXIT,
2221 "restoring original hot key mask\n");
2222 /* no short-circuit boolean operator below! */
2223 if ((hotkey_mask_set(hotkey_orig_mask) |
2224 hotkey_status_set(false)) != 0)
2225 printk(TPACPI_ERR
2226 "failed to restore hot key mask "
2227 "to BIOS defaults\n");
2231 static int __init hotkey_init(struct ibm_init_struct *iibm)
2233 /* Requirements for changing the default keymaps:
2235 * 1. Many of the keys are mapped to KEY_RESERVED for very
2236 * good reasons. Do not change them unless you have deep
2237 * knowledge on the IBM and Lenovo ThinkPad firmware for
2238 * the various ThinkPad models. The driver behaves
2239 * differently for KEY_RESERVED: such keys have their
2240 * hot key mask *unset* in mask_recommended, and also
2241 * in the initial hot key mask programmed into the
2242 * firmware at driver load time, which means the firm-
2243 * ware may react very differently if you change them to
2244 * something else;
2246 * 2. You must be subscribed to the linux-thinkpad and
2247 * ibm-acpi-devel mailing lists, and you should read the
2248 * list archives since 2007 if you want to change the
2249 * keymaps. This requirement exists so that you will
2250 * know the past history of problems with the thinkpad-
2251 * acpi driver keymaps, and also that you will be
2252 * listening to any bug reports;
2254 * 3. Do not send thinkpad-acpi specific patches directly to
2255 * for merging, *ever*. Send them to the linux-acpi
2256 * mailinglist for comments. Merging is to be done only
2257 * through acpi-test and the ACPI maintainer.
2259 * If the above is too much to ask, don't change the keymap.
2260 * Ask the thinkpad-acpi maintainer to do it, instead.
2262 static u16 ibm_keycode_map[] __initdata = {
2263 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2264 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
2265 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2266 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2268 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2269 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2270 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2271 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2273 /* brightness: firmware always reacts to them, unless
2274 * X.org did some tricks in the radeon BIOS scratch
2275 * registers of *some* models */
2276 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2277 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2279 /* Thinklight: firmware always react to it */
2280 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2282 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2283 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2285 /* Volume: firmware always react to it and reprograms
2286 * the built-in *extra* mixer. Never map it to control
2287 * another mixer by default. */
2288 KEY_RESERVED, /* 0x14: VOLUME UP */
2289 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2290 KEY_RESERVED, /* 0x16: MUTE */
2292 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2294 /* (assignments unknown, please report if found) */
2295 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2296 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2298 static u16 lenovo_keycode_map[] __initdata = {
2299 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
2300 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
2301 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
2302 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
2304 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
2305 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
2306 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2307 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
2309 /* These either have to go through ACPI video, or
2310 * act like in the IBM ThinkPads, so don't ever
2311 * enable them by default */
2312 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
2313 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
2315 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
2317 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2318 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
2320 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2321 * react to it and reprograms the built-in *extra* mixer.
2322 * Never map it to control another mixer by default.
2324 * T60?, T61, R60?, R61: firmware and EC tries to send
2325 * these over the regular keyboard, so these are no-ops,
2326 * but there are still weird bugs re. MUTE, so do not
2327 * change unless you get test reports from all Lenovo
2328 * models. May cause the BIOS to interfere with the
2329 * HDA mixer.
2331 KEY_RESERVED, /* 0x14: VOLUME UP */
2332 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2333 KEY_RESERVED, /* 0x16: MUTE */
2335 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
2337 /* (assignments unknown, please report if found) */
2338 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2339 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2342 #define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2343 #define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2344 #define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2346 int res, i;
2347 int status;
2348 int hkeyv;
2350 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2352 BUG_ON(!tpacpi_inputdev);
2353 BUG_ON(tpacpi_inputdev->open != NULL ||
2354 tpacpi_inputdev->close != NULL);
2356 TPACPI_ACPIHANDLE_INIT(hkey);
2357 mutex_init(&hotkey_mutex);
2359 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2360 mutex_init(&hotkey_thread_mutex);
2361 mutex_init(&hotkey_thread_data_mutex);
2362 #endif
2364 /* hotkey not supported on 570 */
2365 tp_features.hotkey = hkey_handle != NULL;
2367 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
2368 str_supported(tp_features.hotkey));
2370 if (!tp_features.hotkey)
2371 return 1;
2373 tpacpi_disable_brightness_delay();
2375 hotkey_dev_attributes = create_attr_set(13, NULL);
2376 if (!hotkey_dev_attributes)
2377 return -ENOMEM;
2378 res = add_many_to_attr_set(hotkey_dev_attributes,
2379 hotkey_attributes,
2380 ARRAY_SIZE(hotkey_attributes));
2381 if (res)
2382 goto err_exit;
2384 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2385 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2386 for HKEY interface version 0x100 */
2387 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2388 if ((hkeyv >> 8) != 1) {
2389 printk(TPACPI_ERR "unknown version of the "
2390 "HKEY interface: 0x%x\n", hkeyv);
2391 printk(TPACPI_ERR "please report this to %s\n",
2392 TPACPI_MAIL);
2393 } else {
2395 * MHKV 0x100 in A31, R40, R40e,
2396 * T4x, X31, and later
2398 tp_features.hotkey_mask = 1;
2402 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
2403 str_supported(tp_features.hotkey_mask));
2405 if (tp_features.hotkey_mask) {
2406 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
2407 "MHKA", "qd")) {
2408 printk(TPACPI_ERR
2409 "missing MHKA handler, "
2410 "please report this to %s\n",
2411 TPACPI_MAIL);
2412 /* FN+F12, FN+F4, FN+F3 */
2413 hotkey_all_mask = 0x080cU;
2417 /* hotkey_source_mask *must* be zero for
2418 * the first hotkey_mask_get */
2419 if (tp_features.hotkey_mask) {
2420 res = hotkey_mask_get();
2421 if (res)
2422 goto err_exit;
2424 hotkey_orig_mask = hotkey_mask;
2425 res = add_many_to_attr_set(
2426 hotkey_dev_attributes,
2427 hotkey_mask_attributes,
2428 ARRAY_SIZE(hotkey_mask_attributes));
2429 if (res)
2430 goto err_exit;
2433 #ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2434 if (tp_features.hotkey_mask) {
2435 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2436 & ~hotkey_all_mask;
2437 } else {
2438 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2441 vdbg_printk(TPACPI_DBG_INIT,
2442 "hotkey source mask 0x%08x, polling freq %d\n",
2443 hotkey_source_mask, hotkey_poll_freq);
2444 #endif
2446 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
2447 if (dbg_wlswemul) {
2448 tp_features.hotkey_wlsw = 1;
2449 printk(TPACPI_INFO
2450 "radio switch emulation enabled\n");
2451 } else
2452 #endif
2453 /* Not all thinkpads have a hardware radio switch */
2454 if (acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2455 tp_features.hotkey_wlsw = 1;
2456 printk(TPACPI_INFO
2457 "radio switch found; radios are %s\n",
2458 enabled(status, 0));
2460 if (tp_features.hotkey_wlsw)
2461 res = add_to_attr_set(hotkey_dev_attributes,
2462 &dev_attr_hotkey_radio_sw.attr);
2464 /* For X41t, X60t, X61t Tablets... */
2465 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2466 tp_features.hotkey_tablet = 1;
2467 printk(TPACPI_INFO
2468 "possible tablet mode switch found; "
2469 "ThinkPad in %s mode\n",
2470 (status & TP_HOTKEY_TABLET_MASK)?
2471 "tablet" : "laptop");
2472 res = add_to_attr_set(hotkey_dev_attributes,
2473 &dev_attr_hotkey_tablet_mode.attr);
2476 if (!res)
2477 res = register_attr_set_with_sysfs(
2478 hotkey_dev_attributes,
2479 &tpacpi_pdev->dev.kobj);
2480 if (res)
2481 goto err_exit;
2483 /* Set up key map */
2485 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2486 GFP_KERNEL);
2487 if (!hotkey_keycode_map) {
2488 printk(TPACPI_ERR
2489 "failed to allocate memory for key map\n");
2490 res = -ENOMEM;
2491 goto err_exit;
2494 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2495 dbg_printk(TPACPI_DBG_INIT,
2496 "using Lenovo default hot key map\n");
2497 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2498 TPACPI_HOTKEY_MAP_SIZE);
2499 } else {
2500 dbg_printk(TPACPI_DBG_INIT,
2501 "using IBM default hot key map\n");
2502 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2503 TPACPI_HOTKEY_MAP_SIZE);
2506 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2507 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2508 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
2509 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2510 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2511 tpacpi_inputdev->keycode = hotkey_keycode_map;
2512 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
2513 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2514 set_bit(hotkey_keycode_map[i],
2515 tpacpi_inputdev->keybit);
2516 } else {
2517 if (i < sizeof(hotkey_reserved_mask)*8)
2518 hotkey_reserved_mask |= 1 << i;
2522 if (tp_features.hotkey_wlsw) {
2523 set_bit(EV_SW, tpacpi_inputdev->evbit);
2524 set_bit(SW_RFKILL_ALL, tpacpi_inputdev->swbit);
2526 if (tp_features.hotkey_tablet) {
2527 set_bit(EV_SW, tpacpi_inputdev->evbit);
2528 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2531 /* Do not issue duplicate brightness change events to
2532 * userspace */
2533 if (!tp_features.bright_acpimode)
2534 /* update bright_acpimode... */
2535 tpacpi_check_std_acpi_brightness_support();
2537 if (tp_features.bright_acpimode) {
2538 printk(TPACPI_INFO
2539 "This ThinkPad has standard ACPI backlight "
2540 "brightness control, supported by the ACPI "
2541 "video driver\n");
2542 printk(TPACPI_NOTICE
2543 "Disabling thinkpad-acpi brightness events "
2544 "by default...\n");
2546 /* The hotkey_reserved_mask change below is not
2547 * necessary while the keys are at KEY_RESERVED in the
2548 * default map, but better safe than sorry, leave it
2549 * here as a marker of what we have to do, especially
2550 * when we finally become able to set this at runtime
2551 * on response to X.org requests */
2552 hotkey_reserved_mask |=
2553 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2554 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2557 dbg_printk(TPACPI_DBG_INIT, "enabling hot key handling\n");
2558 res = hotkey_status_set(true);
2559 if (res) {
2560 hotkey_exit();
2561 return res;
2563 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2564 & ~hotkey_reserved_mask)
2565 | hotkey_orig_mask);
2566 if (res < 0 && res != -ENXIO) {
2567 hotkey_exit();
2568 return res;
2571 dbg_printk(TPACPI_DBG_INIT,
2572 "legacy hot key reporting over procfs %s\n",
2573 (hotkey_report_mode < 2) ?
2574 "enabled" : "disabled");
2576 tpacpi_inputdev->open = &hotkey_inputdev_open;
2577 tpacpi_inputdev->close = &hotkey_inputdev_close;
2579 hotkey_poll_setup_safe(1);
2580 tpacpi_send_radiosw_update();
2581 tpacpi_input_send_tabletsw();
2583 return 0;
2585 err_exit:
2586 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2587 hotkey_dev_attributes = NULL;
2589 return (res < 0)? res : 1;
2592 static bool hotkey_notify_hotkey(const u32 hkey,
2593 bool *send_acpi_ev,
2594 bool *ignore_acpi_ev)
2596 /* 0x1000-0x1FFF: key presses */
2597 unsigned int scancode = hkey & 0xfff;
2598 *send_acpi_ev = true;
2599 *ignore_acpi_ev = false;
2601 if (scancode > 0 && scancode < 0x21) {
2602 scancode--;
2603 if (!(hotkey_source_mask & (1 << scancode))) {
2604 tpacpi_input_send_key(scancode);
2605 *send_acpi_ev = false;
2606 } else {
2607 *ignore_acpi_ev = true;
2609 return true;
2611 return false;
2614 static bool hotkey_notify_wakeup(const u32 hkey,
2615 bool *send_acpi_ev,
2616 bool *ignore_acpi_ev)
2618 /* 0x2000-0x2FFF: Wakeup reason */
2619 *send_acpi_ev = true;
2620 *ignore_acpi_ev = false;
2622 switch (hkey) {
2623 case 0x2304: /* suspend, undock */
2624 case 0x2404: /* hibernation, undock */
2625 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2626 *ignore_acpi_ev = true;
2627 break;
2629 case 0x2305: /* suspend, bay eject */
2630 case 0x2405: /* hibernation, bay eject */
2631 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2632 *ignore_acpi_ev = true;
2633 break;
2635 case 0x2313: /* Battery on critical low level (S3) */
2636 case 0x2413: /* Battery on critical low level (S4) */
2637 printk(TPACPI_ALERT
2638 "EMERGENCY WAKEUP: battery almost empty\n");
2639 /* how to auto-heal: */
2640 /* 2313: woke up from S3, go to S4/S5 */
2641 /* 2413: woke up from S4, go to S5 */
2642 break;
2644 default:
2645 return false;
2648 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2649 printk(TPACPI_INFO
2650 "woke up due to a hot-unplug "
2651 "request...\n");
2652 hotkey_wakeup_reason_notify_change();
2654 return true;
2657 static bool hotkey_notify_usrevent(const u32 hkey,
2658 bool *send_acpi_ev,
2659 bool *ignore_acpi_ev)
2661 /* 0x5000-0x5FFF: human interface helpers */
2662 *send_acpi_ev = true;
2663 *ignore_acpi_ev = false;
2665 switch (hkey) {
2666 case 0x5010: /* Lenovo new BIOS: brightness changed */
2667 case 0x500b: /* X61t: tablet pen inserted into bay */
2668 case 0x500c: /* X61t: tablet pen removed from bay */
2669 return true;
2671 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2672 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2673 tpacpi_input_send_tabletsw();
2674 hotkey_tablet_mode_notify_change();
2675 *send_acpi_ev = false;
2676 return true;
2678 case 0x5001:
2679 case 0x5002:
2680 /* LID switch events. Do not propagate */
2681 *ignore_acpi_ev = true;
2682 return true;
2684 default:
2685 return false;
2689 static bool hotkey_notify_thermal(const u32 hkey,
2690 bool *send_acpi_ev,
2691 bool *ignore_acpi_ev)
2693 /* 0x6000-0x6FFF: thermal alarms */
2694 *send_acpi_ev = true;
2695 *ignore_acpi_ev = false;
2697 switch (hkey) {
2698 case 0x6011:
2699 printk(TPACPI_CRIT
2700 "THERMAL ALARM: battery is too hot!\n");
2701 /* recommended action: warn user through gui */
2702 return true;
2703 case 0x6012:
2704 printk(TPACPI_ALERT
2705 "THERMAL EMERGENCY: battery is extremely hot!\n");
2706 /* recommended action: immediate sleep/hibernate */
2707 return true;
2708 case 0x6021:
2709 printk(TPACPI_CRIT
2710 "THERMAL ALARM: "
2711 "a sensor reports something is too hot!\n");
2712 /* recommended action: warn user through gui, that */
2713 /* some internal component is too hot */
2714 return true;
2715 case 0x6022:
2716 printk(TPACPI_ALERT
2717 "THERMAL EMERGENCY: "
2718 "a sensor reports something is extremely hot!\n");
2719 /* recommended action: immediate sleep/hibernate */
2720 return true;
2721 case 0x6030:
2722 printk(TPACPI_INFO
2723 "EC reports that Thermal Table has changed\n");
2724 /* recommended action: do nothing, we don't have
2725 * Lenovo ATM information */
2726 return true;
2727 default:
2728 printk(TPACPI_ALERT
2729 "THERMAL ALERT: unknown thermal alarm received\n");
2730 return false;
2734 static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2736 u32 hkey;
2737 bool send_acpi_ev;
2738 bool ignore_acpi_ev;
2739 bool known_ev;
2741 if (event != 0x80) {
2742 printk(TPACPI_ERR
2743 "unknown HKEY notification event %d\n", event);
2744 /* forward it to userspace, maybe it knows how to handle it */
2745 acpi_bus_generate_netlink_event(
2746 ibm->acpi->device->pnp.device_class,
2747 dev_name(&ibm->acpi->device->dev),
2748 event, 0);
2749 return;
2752 while (1) {
2753 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
2754 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
2755 return;
2758 if (hkey == 0) {
2759 /* queue empty */
2760 return;
2763 send_acpi_ev = true;
2764 ignore_acpi_ev = false;
2766 switch (hkey >> 12) {
2767 case 1:
2768 /* 0x1000-0x1FFF: key presses */
2769 known_ev = hotkey_notify_hotkey(hkey, &send_acpi_ev,
2770 &ignore_acpi_ev);
2771 break;
2772 case 2:
2773 /* 0x2000-0x2FFF: Wakeup reason */
2774 known_ev = hotkey_notify_wakeup(hkey, &send_acpi_ev,
2775 &ignore_acpi_ev);
2776 break;
2777 case 3:
2778 /* 0x3000-0x3FFF: bay-related wakeups */
2779 if (hkey == 0x3003) {
2780 hotkey_autosleep_ack = 1;
2781 printk(TPACPI_INFO
2782 "bay ejected\n");
2783 hotkey_wakeup_hotunplug_complete_notify_change();
2784 known_ev = true;
2785 } else {
2786 known_ev = false;
2788 break;
2789 case 4:
2790 /* 0x4000-0x4FFF: dock-related wakeups */
2791 if (hkey == 0x4003) {
2792 hotkey_autosleep_ack = 1;
2793 printk(TPACPI_INFO
2794 "undocked\n");
2795 hotkey_wakeup_hotunplug_complete_notify_change();
2796 known_ev = true;
2797 } else {
2798 known_ev = false;
2800 break;
2801 case 5:
2802 /* 0x5000-0x5FFF: human interface helpers */
2803 known_ev = hotkey_notify_usrevent(hkey, &send_acpi_ev,
2804 &ignore_acpi_ev);
2805 break;
2806 case 6:
2807 /* 0x6000-0x6FFF: thermal alarms */
2808 known_ev = hotkey_notify_thermal(hkey, &send_acpi_ev,
2809 &ignore_acpi_ev);
2810 break;
2811 case 7:
2812 /* 0x7000-0x7FFF: misc */
2813 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2814 tpacpi_send_radiosw_update();
2815 send_acpi_ev = 0;
2816 known_ev = true;
2817 break;
2819 /* fallthrough to default */
2820 default:
2821 known_ev = false;
2823 if (!known_ev) {
2824 printk(TPACPI_NOTICE
2825 "unhandled HKEY event 0x%04x\n", hkey);
2826 printk(TPACPI_NOTICE
2827 "please report the conditions when this "
2828 "event happened to %s\n", TPACPI_MAIL);
2831 /* Legacy events */
2832 if (!ignore_acpi_ev &&
2833 (send_acpi_ev || hotkey_report_mode < 2)) {
2834 acpi_bus_generate_proc_event(ibm->acpi->device,
2835 event, hkey);
2838 /* netlink events */
2839 if (!ignore_acpi_ev && send_acpi_ev) {
2840 acpi_bus_generate_netlink_event(
2841 ibm->acpi->device->pnp.device_class,
2842 dev_name(&ibm->acpi->device->dev),
2843 event, hkey);
2848 static void hotkey_suspend(pm_message_t state)
2850 /* Do these on suspend, we get the events on early resume! */
2851 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2852 hotkey_autosleep_ack = 0;
2855 static void hotkey_resume(void)
2857 tpacpi_disable_brightness_delay();
2859 if (hotkey_mask_get())
2860 printk(TPACPI_ERR
2861 "error while trying to read hot key mask "
2862 "from firmware\n");
2863 tpacpi_send_radiosw_update();
2864 hotkey_tablet_mode_notify_change();
2865 hotkey_wakeup_reason_notify_change();
2866 hotkey_wakeup_hotunplug_complete_notify_change();
2867 hotkey_poll_setup_safe(0);
2870 /* procfs -------------------------------------------------------------- */
2871 static int hotkey_read(char *p)
2873 int res, status;
2874 int len = 0;
2876 if (!tp_features.hotkey) {
2877 len += sprintf(p + len, "status:\t\tnot supported\n");
2878 return len;
2881 if (mutex_lock_killable(&hotkey_mutex))
2882 return -ERESTARTSYS;
2883 res = hotkey_status_get(&status);
2884 if (!res)
2885 res = hotkey_mask_get();
2886 mutex_unlock(&hotkey_mutex);
2887 if (res)
2888 return res;
2890 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
2891 if (tp_features.hotkey_mask) {
2892 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
2893 len += sprintf(p + len,
2894 "commands:\tenable, disable, reset, <mask>\n");
2895 } else {
2896 len += sprintf(p + len, "mask:\t\tnot supported\n");
2897 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2900 return len;
2903 static void hotkey_enabledisable_warn(void)
2905 WARN(1, TPACPI_WARN
2906 "hotkey enable/disable functionality has been "
2907 "removed from the driver\n");
2910 static int hotkey_write(char *buf)
2912 int res;
2913 u32 mask;
2914 char *cmd;
2916 if (!tp_features.hotkey)
2917 return -ENODEV;
2919 if (mutex_lock_killable(&hotkey_mutex))
2920 return -ERESTARTSYS;
2922 mask = hotkey_mask;
2924 res = 0;
2925 while ((cmd = next_cmd(&buf))) {
2926 if (strlencmp(cmd, "enable") == 0) {
2927 hotkey_enabledisable_warn();
2928 } else if (strlencmp(cmd, "disable") == 0) {
2929 hotkey_enabledisable_warn();
2930 res = -EPERM;
2931 } else if (strlencmp(cmd, "reset") == 0) {
2932 mask = hotkey_orig_mask;
2933 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2934 /* mask set */
2935 } else if (sscanf(cmd, "%x", &mask) == 1) {
2936 /* mask set */
2937 } else {
2938 res = -EINVAL;
2939 goto errexit;
2942 if (!res && mask != hotkey_mask)
2943 res = hotkey_mask_set(mask);
2945 errexit:
2946 mutex_unlock(&hotkey_mutex);
2947 return res;
2950 static const struct acpi_device_id ibm_htk_device_ids[] = {
2951 {TPACPI_ACPI_HKEY_HID, 0},
2952 {"", 0},
2955 static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
2956 .hid = ibm_htk_device_ids,
2957 .notify = hotkey_notify,
2958 .handle = &hkey_handle,
2959 .type = ACPI_DEVICE_NOTIFY,
2962 static struct ibm_struct hotkey_driver_data = {
2963 .name = "hotkey",
2964 .read = hotkey_read,
2965 .write = hotkey_write,
2966 .exit = hotkey_exit,
2967 .resume = hotkey_resume,
2968 .suspend = hotkey_suspend,
2969 .acpi = &ibm_hotkey_acpidriver,
2972 /*************************************************************************
2973 * Bluetooth subdriver
2976 enum {
2977 /* ACPI GBDC/SBDC bits */
2978 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2979 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2980 TP_ACPI_BLUETOOTH_RESUMECTRL = 0x04, /* Bluetooth state at resume:
2981 off / last state */
2984 enum {
2985 /* ACPI \BLTH commands */
2986 TP_ACPI_BLTH_GET_ULTRAPORT_ID = 0x00, /* Get Ultraport BT ID */
2987 TP_ACPI_BLTH_GET_PWR_ON_RESUME = 0x01, /* Get power-on-resume state */
2988 TP_ACPI_BLTH_PWR_ON_ON_RESUME = 0x02, /* Resume powered on */
2989 TP_ACPI_BLTH_PWR_OFF_ON_RESUME = 0x03, /* Resume powered off */
2990 TP_ACPI_BLTH_SAVE_STATE = 0x05, /* Save state for S4/S5 */
2993 static struct rfkill *tpacpi_bluetooth_rfkill;
2995 static void bluetooth_suspend(pm_message_t state)
2997 /* Try to make sure radio will resume powered off */
2998 acpi_evalf(NULL, NULL, "\\BLTH", "vd",
2999 TP_ACPI_BLTH_PWR_OFF_ON_RESUME);
3002 static int bluetooth_get_radiosw(void)
3004 int status;
3006 if (!tp_features.bluetooth)
3007 return -ENODEV;
3009 /* WLSW overrides bluetooth in firmware/hardware, reflect that */
3010 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3011 return RFKILL_STATE_HARD_BLOCKED;
3013 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3014 if (dbg_bluetoothemul)
3015 return (tpacpi_bluetooth_emulstate) ?
3016 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3017 #endif
3019 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
3020 return -EIO;
3022 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0) ?
3023 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3026 static void bluetooth_update_rfk(void)
3028 int status;
3030 if (!tpacpi_bluetooth_rfkill)
3031 return;
3033 status = bluetooth_get_radiosw();
3034 if (status < 0)
3035 return;
3036 rfkill_force_state(tpacpi_bluetooth_rfkill, status);
3039 static int bluetooth_set_radiosw(int radio_on, int update_rfk)
3041 int status;
3043 if (!tp_features.bluetooth)
3044 return -ENODEV;
3046 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3047 * reason to risk weird behaviour. */
3048 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3049 && radio_on)
3050 return -EPERM;
3052 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3053 if (dbg_bluetoothemul) {
3054 tpacpi_bluetooth_emulstate = !!radio_on;
3055 if (update_rfk)
3056 bluetooth_update_rfk();
3057 return 0;
3059 #endif
3061 /* We make sure to keep TP_ACPI_BLUETOOTH_RESUMECTRL off */
3062 if (radio_on)
3063 status = TP_ACPI_BLUETOOTH_RADIOSSW;
3064 else
3065 status = 0;
3066 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
3067 return -EIO;
3069 if (update_rfk)
3070 bluetooth_update_rfk();
3072 return 0;
3075 /* sysfs bluetooth enable ---------------------------------------------- */
3076 static ssize_t bluetooth_enable_show(struct device *dev,
3077 struct device_attribute *attr,
3078 char *buf)
3080 int status;
3082 status = bluetooth_get_radiosw();
3083 if (status < 0)
3084 return status;
3086 return snprintf(buf, PAGE_SIZE, "%d\n",
3087 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3090 static ssize_t bluetooth_enable_store(struct device *dev,
3091 struct device_attribute *attr,
3092 const char *buf, size_t count)
3094 unsigned long t;
3095 int res;
3097 if (parse_strtoul(buf, 1, &t))
3098 return -EINVAL;
3100 res = bluetooth_set_radiosw(t, 1);
3102 return (res) ? res : count;
3105 static struct device_attribute dev_attr_bluetooth_enable =
3106 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
3107 bluetooth_enable_show, bluetooth_enable_store);
3109 /* --------------------------------------------------------------------- */
3111 static struct attribute *bluetooth_attributes[] = {
3112 &dev_attr_bluetooth_enable.attr,
3113 NULL
3116 static const struct attribute_group bluetooth_attr_group = {
3117 .attrs = bluetooth_attributes,
3120 static int tpacpi_bluetooth_rfk_get(void *data, enum rfkill_state *state)
3122 int bts = bluetooth_get_radiosw();
3124 if (bts < 0)
3125 return bts;
3127 *state = bts;
3128 return 0;
3131 static int tpacpi_bluetooth_rfk_set(void *data, enum rfkill_state state)
3133 return bluetooth_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3136 static void bluetooth_shutdown(void)
3138 /* Order firmware to save current state to NVRAM */
3139 if (!acpi_evalf(NULL, NULL, "\\BLTH", "vd",
3140 TP_ACPI_BLTH_SAVE_STATE))
3141 printk(TPACPI_NOTICE
3142 "failed to save bluetooth state to NVRAM\n");
3145 static void bluetooth_exit(void)
3147 bluetooth_shutdown();
3149 if (tpacpi_bluetooth_rfkill)
3150 rfkill_unregister(tpacpi_bluetooth_rfkill);
3152 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3153 &bluetooth_attr_group);
3156 static int __init bluetooth_init(struct ibm_init_struct *iibm)
3158 int res;
3159 int status = 0;
3161 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
3163 TPACPI_ACPIHANDLE_INIT(hkey);
3165 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3166 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
3167 tp_features.bluetooth = hkey_handle &&
3168 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
3170 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
3171 str_supported(tp_features.bluetooth),
3172 status);
3174 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3175 if (dbg_bluetoothemul) {
3176 tp_features.bluetooth = 1;
3177 printk(TPACPI_INFO
3178 "bluetooth switch emulation enabled\n");
3179 } else
3180 #endif
3181 if (tp_features.bluetooth &&
3182 !(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
3183 /* no bluetooth hardware present in system */
3184 tp_features.bluetooth = 0;
3185 dbg_printk(TPACPI_DBG_INIT,
3186 "bluetooth hardware not installed\n");
3189 if (!tp_features.bluetooth)
3190 return 1;
3192 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3193 &bluetooth_attr_group);
3194 if (res)
3195 return res;
3197 res = tpacpi_new_rfkill(TPACPI_RFK_BLUETOOTH_SW_ID,
3198 &tpacpi_bluetooth_rfkill,
3199 RFKILL_TYPE_BLUETOOTH,
3200 "tpacpi_bluetooth_sw",
3201 true,
3202 tpacpi_bluetooth_rfk_set,
3203 tpacpi_bluetooth_rfk_get);
3204 if (res) {
3205 bluetooth_exit();
3206 return res;
3209 return 0;
3212 /* procfs -------------------------------------------------------------- */
3213 static int bluetooth_read(char *p)
3215 int len = 0;
3216 int status = bluetooth_get_radiosw();
3218 if (!tp_features.bluetooth)
3219 len += sprintf(p + len, "status:\t\tnot supported\n");
3220 else {
3221 len += sprintf(p + len, "status:\t\t%s\n",
3222 (status == RFKILL_STATE_UNBLOCKED) ?
3223 "enabled" : "disabled");
3224 len += sprintf(p + len, "commands:\tenable, disable\n");
3227 return len;
3230 static int bluetooth_write(char *buf)
3232 char *cmd;
3234 if (!tp_features.bluetooth)
3235 return -ENODEV;
3237 while ((cmd = next_cmd(&buf))) {
3238 if (strlencmp(cmd, "enable") == 0) {
3239 bluetooth_set_radiosw(1, 1);
3240 } else if (strlencmp(cmd, "disable") == 0) {
3241 bluetooth_set_radiosw(0, 1);
3242 } else
3243 return -EINVAL;
3246 return 0;
3249 static struct ibm_struct bluetooth_driver_data = {
3250 .name = "bluetooth",
3251 .read = bluetooth_read,
3252 .write = bluetooth_write,
3253 .exit = bluetooth_exit,
3254 .suspend = bluetooth_suspend,
3255 .shutdown = bluetooth_shutdown,
3258 /*************************************************************************
3259 * Wan subdriver
3262 enum {
3263 /* ACPI GWAN/SWAN bits */
3264 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
3265 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
3266 TP_ACPI_WANCARD_RESUMECTRL = 0x04, /* Wan state at resume:
3267 off / last state */
3270 static struct rfkill *tpacpi_wan_rfkill;
3272 static void wan_suspend(pm_message_t state)
3274 /* Try to make sure radio will resume powered off */
3275 acpi_evalf(NULL, NULL, "\\WGSV", "qvd",
3276 TP_ACPI_WGSV_PWR_OFF_ON_RESUME);
3279 static int wan_get_radiosw(void)
3281 int status;
3283 if (!tp_features.wan)
3284 return -ENODEV;
3286 /* WLSW overrides WWAN in firmware/hardware, reflect that */
3287 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3288 return RFKILL_STATE_HARD_BLOCKED;
3290 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3291 if (dbg_wwanemul)
3292 return (tpacpi_wwan_emulstate) ?
3293 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3294 #endif
3296 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
3297 return -EIO;
3299 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0) ?
3300 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3303 static void wan_update_rfk(void)
3305 int status;
3307 if (!tpacpi_wan_rfkill)
3308 return;
3310 status = wan_get_radiosw();
3311 if (status < 0)
3312 return;
3313 rfkill_force_state(tpacpi_wan_rfkill, status);
3316 static int wan_set_radiosw(int radio_on, int update_rfk)
3318 int status;
3320 if (!tp_features.wan)
3321 return -ENODEV;
3323 /* WLSW overrides bluetooth in firmware/hardware, but there is no
3324 * reason to risk weird behaviour. */
3325 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3326 && radio_on)
3327 return -EPERM;
3329 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3330 if (dbg_wwanemul) {
3331 tpacpi_wwan_emulstate = !!radio_on;
3332 if (update_rfk)
3333 wan_update_rfk();
3334 return 0;
3336 #endif
3338 /* We make sure to keep TP_ACPI_WANCARD_RESUMECTRL off */
3339 if (radio_on)
3340 status = TP_ACPI_WANCARD_RADIOSSW;
3341 else
3342 status = 0;
3343 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
3344 return -EIO;
3346 if (update_rfk)
3347 wan_update_rfk();
3349 return 0;
3352 /* sysfs wan enable ---------------------------------------------------- */
3353 static ssize_t wan_enable_show(struct device *dev,
3354 struct device_attribute *attr,
3355 char *buf)
3357 int status;
3359 status = wan_get_radiosw();
3360 if (status < 0)
3361 return status;
3363 return snprintf(buf, PAGE_SIZE, "%d\n",
3364 (status == RFKILL_STATE_UNBLOCKED) ? 1 : 0);
3367 static ssize_t wan_enable_store(struct device *dev,
3368 struct device_attribute *attr,
3369 const char *buf, size_t count)
3371 unsigned long t;
3372 int res;
3374 if (parse_strtoul(buf, 1, &t))
3375 return -EINVAL;
3377 res = wan_set_radiosw(t, 1);
3379 return (res) ? res : count;
3382 static struct device_attribute dev_attr_wan_enable =
3383 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
3384 wan_enable_show, wan_enable_store);
3386 /* --------------------------------------------------------------------- */
3388 static struct attribute *wan_attributes[] = {
3389 &dev_attr_wan_enable.attr,
3390 NULL
3393 static const struct attribute_group wan_attr_group = {
3394 .attrs = wan_attributes,
3397 static int tpacpi_wan_rfk_get(void *data, enum rfkill_state *state)
3399 int wans = wan_get_radiosw();
3401 if (wans < 0)
3402 return wans;
3404 *state = wans;
3405 return 0;
3408 static int tpacpi_wan_rfk_set(void *data, enum rfkill_state state)
3410 return wan_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3413 static void wan_shutdown(void)
3415 /* Order firmware to save current state to NVRAM */
3416 if (!acpi_evalf(NULL, NULL, "\\WGSV", "vd",
3417 TP_ACPI_WGSV_SAVE_STATE))
3418 printk(TPACPI_NOTICE
3419 "failed to save WWAN state to NVRAM\n");
3422 static void wan_exit(void)
3424 wan_shutdown();
3426 if (tpacpi_wan_rfkill)
3427 rfkill_unregister(tpacpi_wan_rfkill);
3429 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
3430 &wan_attr_group);
3433 static int __init wan_init(struct ibm_init_struct *iibm)
3435 int res;
3436 int status = 0;
3438 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
3440 TPACPI_ACPIHANDLE_INIT(hkey);
3442 tp_features.wan = hkey_handle &&
3443 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
3445 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
3446 str_supported(tp_features.wan),
3447 status);
3449 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3450 if (dbg_wwanemul) {
3451 tp_features.wan = 1;
3452 printk(TPACPI_INFO
3453 "wwan switch emulation enabled\n");
3454 } else
3455 #endif
3456 if (tp_features.wan &&
3457 !(status & TP_ACPI_WANCARD_HWPRESENT)) {
3458 /* no wan hardware present in system */
3459 tp_features.wan = 0;
3460 dbg_printk(TPACPI_DBG_INIT,
3461 "wan hardware not installed\n");
3464 if (!tp_features.wan)
3465 return 1;
3467 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
3468 &wan_attr_group);
3469 if (res)
3470 return res;
3472 res = tpacpi_new_rfkill(TPACPI_RFK_WWAN_SW_ID,
3473 &tpacpi_wan_rfkill,
3474 RFKILL_TYPE_WWAN,
3475 "tpacpi_wwan_sw",
3476 true,
3477 tpacpi_wan_rfk_set,
3478 tpacpi_wan_rfk_get);
3479 if (res) {
3480 wan_exit();
3481 return res;
3484 return 0;
3487 /* procfs -------------------------------------------------------------- */
3488 static int wan_read(char *p)
3490 int len = 0;
3491 int status = wan_get_radiosw();
3493 if (!tp_features.wan)
3494 len += sprintf(p + len, "status:\t\tnot supported\n");
3495 else {
3496 len += sprintf(p + len, "status:\t\t%s\n",
3497 (status == RFKILL_STATE_UNBLOCKED) ?
3498 "enabled" : "disabled");
3499 len += sprintf(p + len, "commands:\tenable, disable\n");
3502 return len;
3505 static int wan_write(char *buf)
3507 char *cmd;
3509 if (!tp_features.wan)
3510 return -ENODEV;
3512 while ((cmd = next_cmd(&buf))) {
3513 if (strlencmp(cmd, "enable") == 0) {
3514 wan_set_radiosw(1, 1);
3515 } else if (strlencmp(cmd, "disable") == 0) {
3516 wan_set_radiosw(0, 1);
3517 } else
3518 return -EINVAL;
3521 return 0;
3524 static struct ibm_struct wan_driver_data = {
3525 .name = "wan",
3526 .read = wan_read,
3527 .write = wan_write,
3528 .exit = wan_exit,
3529 .suspend = wan_suspend,
3530 .shutdown = wan_shutdown,
3533 /*************************************************************************
3534 * UWB subdriver
3537 enum {
3538 /* ACPI GUWB/SUWB bits */
3539 TP_ACPI_UWB_HWPRESENT = 0x01, /* UWB hw available */
3540 TP_ACPI_UWB_RADIOSSW = 0x02, /* UWB radio enabled */
3543 static struct rfkill *tpacpi_uwb_rfkill;
3545 static int uwb_get_radiosw(void)
3547 int status;
3549 if (!tp_features.uwb)
3550 return -ENODEV;
3552 /* WLSW overrides UWB in firmware/hardware, reflect that */
3553 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status)
3554 return RFKILL_STATE_HARD_BLOCKED;
3556 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3557 if (dbg_uwbemul)
3558 return (tpacpi_uwb_emulstate) ?
3559 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3560 #endif
3562 if (!acpi_evalf(hkey_handle, &status, "GUWB", "d"))
3563 return -EIO;
3565 return ((status & TP_ACPI_UWB_RADIOSSW) != 0) ?
3566 RFKILL_STATE_UNBLOCKED : RFKILL_STATE_SOFT_BLOCKED;
3569 static void uwb_update_rfk(void)
3571 int status;
3573 if (!tpacpi_uwb_rfkill)
3574 return;
3576 status = uwb_get_radiosw();
3577 if (status < 0)
3578 return;
3579 rfkill_force_state(tpacpi_uwb_rfkill, status);
3582 static int uwb_set_radiosw(int radio_on, int update_rfk)
3584 int status;
3586 if (!tp_features.uwb)
3587 return -ENODEV;
3589 /* WLSW overrides UWB in firmware/hardware, but there is no
3590 * reason to risk weird behaviour. */
3591 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&status) && !status
3592 && radio_on)
3593 return -EPERM;
3595 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3596 if (dbg_uwbemul) {
3597 tpacpi_uwb_emulstate = !!radio_on;
3598 if (update_rfk)
3599 uwb_update_rfk();
3600 return 0;
3602 #endif
3604 status = (radio_on) ? TP_ACPI_UWB_RADIOSSW : 0;
3605 if (!acpi_evalf(hkey_handle, NULL, "SUWB", "vd", status))
3606 return -EIO;
3608 if (update_rfk)
3609 uwb_update_rfk();
3611 return 0;
3614 /* --------------------------------------------------------------------- */
3616 static int tpacpi_uwb_rfk_get(void *data, enum rfkill_state *state)
3618 int uwbs = uwb_get_radiosw();
3620 if (uwbs < 0)
3621 return uwbs;
3623 *state = uwbs;
3624 return 0;
3627 static int tpacpi_uwb_rfk_set(void *data, enum rfkill_state state)
3629 return uwb_set_radiosw((state == RFKILL_STATE_UNBLOCKED), 0);
3632 static void uwb_exit(void)
3634 if (tpacpi_uwb_rfkill)
3635 rfkill_unregister(tpacpi_uwb_rfkill);
3638 static int __init uwb_init(struct ibm_init_struct *iibm)
3640 int res;
3641 int status = 0;
3643 vdbg_printk(TPACPI_DBG_INIT, "initializing uwb subdriver\n");
3645 TPACPI_ACPIHANDLE_INIT(hkey);
3647 tp_features.uwb = hkey_handle &&
3648 acpi_evalf(hkey_handle, &status, "GUWB", "qd");
3650 vdbg_printk(TPACPI_DBG_INIT, "uwb is %s, status 0x%02x\n",
3651 str_supported(tp_features.uwb),
3652 status);
3654 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
3655 if (dbg_uwbemul) {
3656 tp_features.uwb = 1;
3657 printk(TPACPI_INFO
3658 "uwb switch emulation enabled\n");
3659 } else
3660 #endif
3661 if (tp_features.uwb &&
3662 !(status & TP_ACPI_UWB_HWPRESENT)) {
3663 /* no uwb hardware present in system */
3664 tp_features.uwb = 0;
3665 dbg_printk(TPACPI_DBG_INIT,
3666 "uwb hardware not installed\n");
3669 if (!tp_features.uwb)
3670 return 1;
3672 res = tpacpi_new_rfkill(TPACPI_RFK_UWB_SW_ID,
3673 &tpacpi_uwb_rfkill,
3674 RFKILL_TYPE_UWB,
3675 "tpacpi_uwb_sw",
3676 false,
3677 tpacpi_uwb_rfk_set,
3678 tpacpi_uwb_rfk_get);
3680 return res;
3683 static struct ibm_struct uwb_driver_data = {
3684 .name = "uwb",
3685 .exit = uwb_exit,
3686 .flags.experimental = 1,
3689 /*************************************************************************
3690 * Video subdriver
3693 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
3695 enum video_access_mode {
3696 TPACPI_VIDEO_NONE = 0,
3697 TPACPI_VIDEO_570, /* 570 */
3698 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
3699 TPACPI_VIDEO_NEW, /* all others */
3702 enum { /* video status flags, based on VIDEO_570 */
3703 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
3704 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
3705 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
3708 enum { /* TPACPI_VIDEO_570 constants */
3709 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
3710 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
3711 * video_status_flags */
3712 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
3713 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
3716 static enum video_access_mode video_supported;
3717 static int video_orig_autosw;
3719 static int video_autosw_get(void);
3720 static int video_autosw_set(int enable);
3722 TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
3724 static int __init video_init(struct ibm_init_struct *iibm)
3726 int ivga;
3728 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
3730 TPACPI_ACPIHANDLE_INIT(vid);
3731 TPACPI_ACPIHANDLE_INIT(vid2);
3733 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
3734 /* G41, assume IVGA doesn't change */
3735 vid_handle = vid2_handle;
3737 if (!vid_handle)
3738 /* video switching not supported on R30, R31 */
3739 video_supported = TPACPI_VIDEO_NONE;
3740 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
3741 /* 570 */
3742 video_supported = TPACPI_VIDEO_570;
3743 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
3744 /* 600e/x, 770e, 770x */
3745 video_supported = TPACPI_VIDEO_770;
3746 else
3747 /* all others */
3748 video_supported = TPACPI_VIDEO_NEW;
3750 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
3751 str_supported(video_supported != TPACPI_VIDEO_NONE),
3752 video_supported);
3754 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
3757 static void video_exit(void)
3759 dbg_printk(TPACPI_DBG_EXIT,
3760 "restoring original video autoswitch mode\n");
3761 if (video_autosw_set(video_orig_autosw))
3762 printk(TPACPI_ERR "error while trying to restore original "
3763 "video autoswitch mode\n");
3766 static int video_outputsw_get(void)
3768 int status = 0;
3769 int i;
3771 switch (video_supported) {
3772 case TPACPI_VIDEO_570:
3773 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
3774 TP_ACPI_VIDEO_570_PHSCMD))
3775 return -EIO;
3776 status = i & TP_ACPI_VIDEO_570_PHSMASK;
3777 break;
3778 case TPACPI_VIDEO_770:
3779 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
3780 return -EIO;
3781 if (i)
3782 status |= TP_ACPI_VIDEO_S_LCD;
3783 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
3784 return -EIO;
3785 if (i)
3786 status |= TP_ACPI_VIDEO_S_CRT;
3787 break;
3788 case TPACPI_VIDEO_NEW:
3789 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3790 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3791 return -EIO;
3792 if (i)
3793 status |= TP_ACPI_VIDEO_S_CRT;
3795 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3796 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3797 return -EIO;
3798 if (i)
3799 status |= TP_ACPI_VIDEO_S_LCD;
3800 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3801 return -EIO;
3802 if (i)
3803 status |= TP_ACPI_VIDEO_S_DVI;
3804 break;
3805 default:
3806 return -ENOSYS;
3809 return status;
3812 static int video_outputsw_set(int status)
3814 int autosw;
3815 int res = 0;
3817 switch (video_supported) {
3818 case TPACPI_VIDEO_570:
3819 res = acpi_evalf(NULL, NULL,
3820 "\\_SB.PHS2", "vdd",
3821 TP_ACPI_VIDEO_570_PHS2CMD,
3822 status | TP_ACPI_VIDEO_570_PHS2SET);
3823 break;
3824 case TPACPI_VIDEO_770:
3825 autosw = video_autosw_get();
3826 if (autosw < 0)
3827 return autosw;
3829 res = video_autosw_set(1);
3830 if (res)
3831 return res;
3832 res = acpi_evalf(vid_handle, NULL,
3833 "ASWT", "vdd", status * 0x100, 0);
3834 if (!autosw && video_autosw_set(autosw)) {
3835 printk(TPACPI_ERR
3836 "video auto-switch left enabled due to error\n");
3837 return -EIO;
3839 break;
3840 case TPACPI_VIDEO_NEW:
3841 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
3842 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
3843 break;
3844 default:
3845 return -ENOSYS;
3848 return (res)? 0 : -EIO;
3851 static int video_autosw_get(void)
3853 int autosw = 0;
3855 switch (video_supported) {
3856 case TPACPI_VIDEO_570:
3857 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3858 return -EIO;
3859 break;
3860 case TPACPI_VIDEO_770:
3861 case TPACPI_VIDEO_NEW:
3862 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3863 return -EIO;
3864 break;
3865 default:
3866 return -ENOSYS;
3869 return autosw & 1;
3872 static int video_autosw_set(int enable)
3874 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
3875 return -EIO;
3876 return 0;
3879 static int video_outputsw_cycle(void)
3881 int autosw = video_autosw_get();
3882 int res;
3884 if (autosw < 0)
3885 return autosw;
3887 switch (video_supported) {
3888 case TPACPI_VIDEO_570:
3889 res = video_autosw_set(1);
3890 if (res)
3891 return res;
3892 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3893 break;
3894 case TPACPI_VIDEO_770:
3895 case TPACPI_VIDEO_NEW:
3896 res = video_autosw_set(1);
3897 if (res)
3898 return res;
3899 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3900 break;
3901 default:
3902 return -ENOSYS;
3904 if (!autosw && video_autosw_set(autosw)) {
3905 printk(TPACPI_ERR
3906 "video auto-switch left enabled due to error\n");
3907 return -EIO;
3910 return (res)? 0 : -EIO;
3913 static int video_expand_toggle(void)
3915 switch (video_supported) {
3916 case TPACPI_VIDEO_570:
3917 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3918 0 : -EIO;
3919 case TPACPI_VIDEO_770:
3920 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3921 0 : -EIO;
3922 case TPACPI_VIDEO_NEW:
3923 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3924 0 : -EIO;
3925 default:
3926 return -ENOSYS;
3928 /* not reached */
3931 static int video_read(char *p)
3933 int status, autosw;
3934 int len = 0;
3936 if (video_supported == TPACPI_VIDEO_NONE) {
3937 len += sprintf(p + len, "status:\t\tnot supported\n");
3938 return len;
3941 status = video_outputsw_get();
3942 if (status < 0)
3943 return status;
3945 autosw = video_autosw_get();
3946 if (autosw < 0)
3947 return autosw;
3949 len += sprintf(p + len, "status:\t\tsupported\n");
3950 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3951 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
3952 if (video_supported == TPACPI_VIDEO_NEW)
3953 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3954 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3955 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3956 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
3957 if (video_supported == TPACPI_VIDEO_NEW)
3958 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3959 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3960 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3962 return len;
3965 static int video_write(char *buf)
3967 char *cmd;
3968 int enable, disable, status;
3969 int res;
3971 if (video_supported == TPACPI_VIDEO_NONE)
3972 return -ENODEV;
3974 enable = 0;
3975 disable = 0;
3977 while ((cmd = next_cmd(&buf))) {
3978 if (strlencmp(cmd, "lcd_enable") == 0) {
3979 enable |= TP_ACPI_VIDEO_S_LCD;
3980 } else if (strlencmp(cmd, "lcd_disable") == 0) {
3981 disable |= TP_ACPI_VIDEO_S_LCD;
3982 } else if (strlencmp(cmd, "crt_enable") == 0) {
3983 enable |= TP_ACPI_VIDEO_S_CRT;
3984 } else if (strlencmp(cmd, "crt_disable") == 0) {
3985 disable |= TP_ACPI_VIDEO_S_CRT;
3986 } else if (video_supported == TPACPI_VIDEO_NEW &&
3987 strlencmp(cmd, "dvi_enable") == 0) {
3988 enable |= TP_ACPI_VIDEO_S_DVI;
3989 } else if (video_supported == TPACPI_VIDEO_NEW &&
3990 strlencmp(cmd, "dvi_disable") == 0) {
3991 disable |= TP_ACPI_VIDEO_S_DVI;
3992 } else if (strlencmp(cmd, "auto_enable") == 0) {
3993 res = video_autosw_set(1);
3994 if (res)
3995 return res;
3996 } else if (strlencmp(cmd, "auto_disable") == 0) {
3997 res = video_autosw_set(0);
3998 if (res)
3999 return res;
4000 } else if (strlencmp(cmd, "video_switch") == 0) {
4001 res = video_outputsw_cycle();
4002 if (res)
4003 return res;
4004 } else if (strlencmp(cmd, "expand_toggle") == 0) {
4005 res = video_expand_toggle();
4006 if (res)
4007 return res;
4008 } else
4009 return -EINVAL;
4012 if (enable || disable) {
4013 status = video_outputsw_get();
4014 if (status < 0)
4015 return status;
4016 res = video_outputsw_set((status & ~disable) | enable);
4017 if (res)
4018 return res;
4021 return 0;
4024 static struct ibm_struct video_driver_data = {
4025 .name = "video",
4026 .read = video_read,
4027 .write = video_write,
4028 .exit = video_exit,
4031 #endif /* CONFIG_THINKPAD_ACPI_VIDEO */
4033 /*************************************************************************
4034 * Light (thinklight) subdriver
4037 TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
4038 TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
4040 static int light_get_status(void)
4042 int status = 0;
4044 if (tp_features.light_status) {
4045 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
4046 return -EIO;
4047 return (!!status);
4050 return -ENXIO;
4053 static int light_set_status(int status)
4055 int rc;
4057 if (tp_features.light) {
4058 if (cmos_handle) {
4059 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
4060 (status)?
4061 TP_CMOS_THINKLIGHT_ON :
4062 TP_CMOS_THINKLIGHT_OFF);
4063 } else {
4064 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
4065 (status)? 1 : 0);
4067 return (rc)? 0 : -EIO;
4070 return -ENXIO;
4073 static void light_set_status_worker(struct work_struct *work)
4075 struct tpacpi_led_classdev *data =
4076 container_of(work, struct tpacpi_led_classdev, work);
4078 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4079 light_set_status((data->new_brightness != LED_OFF));
4082 static void light_sysfs_set(struct led_classdev *led_cdev,
4083 enum led_brightness brightness)
4085 struct tpacpi_led_classdev *data =
4086 container_of(led_cdev,
4087 struct tpacpi_led_classdev,
4088 led_classdev);
4089 data->new_brightness = brightness;
4090 queue_work(tpacpi_wq, &data->work);
4093 static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
4095 return (light_get_status() == 1)? LED_FULL : LED_OFF;
4098 static struct tpacpi_led_classdev tpacpi_led_thinklight = {
4099 .led_classdev = {
4100 .name = "tpacpi::thinklight",
4101 .brightness_set = &light_sysfs_set,
4102 .brightness_get = &light_sysfs_get,
4106 static int __init light_init(struct ibm_init_struct *iibm)
4108 int rc;
4110 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
4112 TPACPI_ACPIHANDLE_INIT(ledb);
4113 TPACPI_ACPIHANDLE_INIT(lght);
4114 TPACPI_ACPIHANDLE_INIT(cmos);
4115 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
4117 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
4118 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
4120 if (tp_features.light)
4121 /* light status not supported on
4122 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
4123 tp_features.light_status =
4124 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
4126 vdbg_printk(TPACPI_DBG_INIT, "light is %s, light status is %s\n",
4127 str_supported(tp_features.light),
4128 str_supported(tp_features.light_status));
4130 if (!tp_features.light)
4131 return 1;
4133 rc = led_classdev_register(&tpacpi_pdev->dev,
4134 &tpacpi_led_thinklight.led_classdev);
4136 if (rc < 0) {
4137 tp_features.light = 0;
4138 tp_features.light_status = 0;
4139 } else {
4140 rc = 0;
4143 return rc;
4146 static void light_exit(void)
4148 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
4149 if (work_pending(&tpacpi_led_thinklight.work))
4150 flush_workqueue(tpacpi_wq);
4153 static int light_read(char *p)
4155 int len = 0;
4156 int status;
4158 if (!tp_features.light) {
4159 len += sprintf(p + len, "status:\t\tnot supported\n");
4160 } else if (!tp_features.light_status) {
4161 len += sprintf(p + len, "status:\t\tunknown\n");
4162 len += sprintf(p + len, "commands:\ton, off\n");
4163 } else {
4164 status = light_get_status();
4165 if (status < 0)
4166 return status;
4167 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
4168 len += sprintf(p + len, "commands:\ton, off\n");
4171 return len;
4174 static int light_write(char *buf)
4176 char *cmd;
4177 int newstatus = 0;
4179 if (!tp_features.light)
4180 return -ENODEV;
4182 while ((cmd = next_cmd(&buf))) {
4183 if (strlencmp(cmd, "on") == 0) {
4184 newstatus = 1;
4185 } else if (strlencmp(cmd, "off") == 0) {
4186 newstatus = 0;
4187 } else
4188 return -EINVAL;
4191 return light_set_status(newstatus);
4194 static struct ibm_struct light_driver_data = {
4195 .name = "light",
4196 .read = light_read,
4197 .write = light_write,
4198 .exit = light_exit,
4201 /*************************************************************************
4202 * Dock subdriver
4205 #ifdef CONFIG_THINKPAD_ACPI_DOCK
4207 static void dock_notify(struct ibm_struct *ibm, u32 event);
4208 static int dock_read(char *p);
4209 static int dock_write(char *buf);
4211 TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
4212 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
4213 "\\_SB.PCI0.PCI1.DOCK", /* all others */
4214 "\\_SB.PCI.ISA.SLCE", /* 570 */
4215 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
4217 /* don't list other alternatives as we install a notify handler on the 570 */
4218 TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
4220 static const struct acpi_device_id ibm_pci_device_ids[] = {
4221 {PCI_ROOT_HID_STRING, 0},
4222 {"", 0},
4225 static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
4227 .notify = dock_notify,
4228 .handle = &dock_handle,
4229 .type = ACPI_SYSTEM_NOTIFY,
4232 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
4233 * We just use it to get notifications of dock hotplug
4234 * in very old thinkpads */
4235 .hid = ibm_pci_device_ids,
4236 .notify = dock_notify,
4237 .handle = &pci_handle,
4238 .type = ACPI_SYSTEM_NOTIFY,
4242 static struct ibm_struct dock_driver_data[2] = {
4244 .name = "dock",
4245 .read = dock_read,
4246 .write = dock_write,
4247 .acpi = &ibm_dock_acpidriver[0],
4250 .name = "dock",
4251 .acpi = &ibm_dock_acpidriver[1],
4255 #define dock_docked() (_sta(dock_handle) & 1)
4257 static int __init dock_init(struct ibm_init_struct *iibm)
4259 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
4261 TPACPI_ACPIHANDLE_INIT(dock);
4263 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
4264 str_supported(dock_handle != NULL));
4266 return (dock_handle)? 0 : 1;
4269 static int __init dock_init2(struct ibm_init_struct *iibm)
4271 int dock2_needed;
4273 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
4275 if (dock_driver_data[0].flags.acpi_driver_registered &&
4276 dock_driver_data[0].flags.acpi_notify_installed) {
4277 TPACPI_ACPIHANDLE_INIT(pci);
4278 dock2_needed = (pci_handle != NULL);
4279 vdbg_printk(TPACPI_DBG_INIT,
4280 "dock PCI handler for the TP 570 is %s\n",
4281 str_supported(dock2_needed));
4282 } else {
4283 vdbg_printk(TPACPI_DBG_INIT,
4284 "dock subdriver part 2 not required\n");
4285 dock2_needed = 0;
4288 return (dock2_needed)? 0 : 1;
4291 static void dock_notify(struct ibm_struct *ibm, u32 event)
4293 int docked = dock_docked();
4294 int pci = ibm->acpi->hid && ibm->acpi->device &&
4295 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
4296 int data;
4298 if (event == 1 && !pci) /* 570 */
4299 data = 1; /* button */
4300 else if (event == 1 && pci) /* 570 */
4301 data = 3; /* dock */
4302 else if (event == 3 && docked)
4303 data = 1; /* button */
4304 else if (event == 3 && !docked)
4305 data = 2; /* undock */
4306 else if (event == 0 && docked)
4307 data = 3; /* dock */
4308 else {
4309 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
4310 event, _sta(dock_handle));
4311 data = 0; /* unknown */
4313 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
4314 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4315 dev_name(&ibm->acpi->device->dev),
4316 event, data);
4319 static int dock_read(char *p)
4321 int len = 0;
4322 int docked = dock_docked();
4324 if (!dock_handle)
4325 len += sprintf(p + len, "status:\t\tnot supported\n");
4326 else if (!docked)
4327 len += sprintf(p + len, "status:\t\tundocked\n");
4328 else {
4329 len += sprintf(p + len, "status:\t\tdocked\n");
4330 len += sprintf(p + len, "commands:\tdock, undock\n");
4333 return len;
4336 static int dock_write(char *buf)
4338 char *cmd;
4340 if (!dock_docked())
4341 return -ENODEV;
4343 while ((cmd = next_cmd(&buf))) {
4344 if (strlencmp(cmd, "undock") == 0) {
4345 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
4346 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
4347 return -EIO;
4348 } else if (strlencmp(cmd, "dock") == 0) {
4349 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
4350 return -EIO;
4351 } else
4352 return -EINVAL;
4355 return 0;
4358 #endif /* CONFIG_THINKPAD_ACPI_DOCK */
4360 /*************************************************************************
4361 * Bay subdriver
4364 #ifdef CONFIG_THINKPAD_ACPI_BAY
4366 TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
4367 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
4368 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
4369 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
4370 ); /* A21e, R30, R31 */
4371 TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
4372 "_EJ0", /* all others */
4373 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
4374 TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
4375 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
4376 ); /* all others */
4377 TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
4378 "_EJ0", /* 770x */
4379 ); /* all others */
4381 static int __init bay_init(struct ibm_init_struct *iibm)
4383 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
4385 TPACPI_ACPIHANDLE_INIT(bay);
4386 if (bay_handle)
4387 TPACPI_ACPIHANDLE_INIT(bay_ej);
4388 TPACPI_ACPIHANDLE_INIT(bay2);
4389 if (bay2_handle)
4390 TPACPI_ACPIHANDLE_INIT(bay2_ej);
4392 tp_features.bay_status = bay_handle &&
4393 acpi_evalf(bay_handle, NULL, "_STA", "qv");
4394 tp_features.bay_status2 = bay2_handle &&
4395 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
4397 tp_features.bay_eject = bay_handle && bay_ej_handle &&
4398 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
4399 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
4400 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
4402 vdbg_printk(TPACPI_DBG_INIT,
4403 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
4404 str_supported(tp_features.bay_status),
4405 str_supported(tp_features.bay_eject),
4406 str_supported(tp_features.bay_status2),
4407 str_supported(tp_features.bay_eject2));
4409 return (tp_features.bay_status || tp_features.bay_eject ||
4410 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
4413 static void bay_notify(struct ibm_struct *ibm, u32 event)
4415 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
4416 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
4417 dev_name(&ibm->acpi->device->dev),
4418 event, 0);
4421 #define bay_occupied(b) (_sta(b##_handle) & 1)
4423 static int bay_read(char *p)
4425 int len = 0;
4426 int occupied = bay_occupied(bay);
4427 int occupied2 = bay_occupied(bay2);
4428 int eject, eject2;
4430 len += sprintf(p + len, "status:\t\t%s\n",
4431 tp_features.bay_status ?
4432 (occupied ? "occupied" : "unoccupied") :
4433 "not supported");
4434 if (tp_features.bay_status2)
4435 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
4436 "occupied" : "unoccupied");
4438 eject = tp_features.bay_eject && occupied;
4439 eject2 = tp_features.bay_eject2 && occupied2;
4441 if (eject && eject2)
4442 len += sprintf(p + len, "commands:\teject, eject2\n");
4443 else if (eject)
4444 len += sprintf(p + len, "commands:\teject\n");
4445 else if (eject2)
4446 len += sprintf(p + len, "commands:\teject2\n");
4448 return len;
4451 static int bay_write(char *buf)
4453 char *cmd;
4455 if (!tp_features.bay_eject && !tp_features.bay_eject2)
4456 return -ENODEV;
4458 while ((cmd = next_cmd(&buf))) {
4459 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
4460 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
4461 return -EIO;
4462 } else if (tp_features.bay_eject2 &&
4463 strlencmp(cmd, "eject2") == 0) {
4464 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
4465 return -EIO;
4466 } else
4467 return -EINVAL;
4470 return 0;
4473 static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
4474 .notify = bay_notify,
4475 .handle = &bay_handle,
4476 .type = ACPI_SYSTEM_NOTIFY,
4479 static struct ibm_struct bay_driver_data = {
4480 .name = "bay",
4481 .read = bay_read,
4482 .write = bay_write,
4483 .acpi = &ibm_bay_acpidriver,
4486 #endif /* CONFIG_THINKPAD_ACPI_BAY */
4488 /*************************************************************************
4489 * CMOS subdriver
4492 /* sysfs cmos_command -------------------------------------------------- */
4493 static ssize_t cmos_command_store(struct device *dev,
4494 struct device_attribute *attr,
4495 const char *buf, size_t count)
4497 unsigned long cmos_cmd;
4498 int res;
4500 if (parse_strtoul(buf, 21, &cmos_cmd))
4501 return -EINVAL;
4503 res = issue_thinkpad_cmos_command(cmos_cmd);
4504 return (res)? res : count;
4507 static struct device_attribute dev_attr_cmos_command =
4508 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
4510 /* --------------------------------------------------------------------- */
4512 static int __init cmos_init(struct ibm_init_struct *iibm)
4514 int res;
4516 vdbg_printk(TPACPI_DBG_INIT,
4517 "initializing cmos commands subdriver\n");
4519 TPACPI_ACPIHANDLE_INIT(cmos);
4521 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
4522 str_supported(cmos_handle != NULL));
4524 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4525 if (res)
4526 return res;
4528 return (cmos_handle)? 0 : 1;
4531 static void cmos_exit(void)
4533 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
4536 static int cmos_read(char *p)
4538 int len = 0;
4540 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
4541 R30, R31, T20-22, X20-21 */
4542 if (!cmos_handle)
4543 len += sprintf(p + len, "status:\t\tnot supported\n");
4544 else {
4545 len += sprintf(p + len, "status:\t\tsupported\n");
4546 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
4549 return len;
4552 static int cmos_write(char *buf)
4554 char *cmd;
4555 int cmos_cmd, res;
4557 while ((cmd = next_cmd(&buf))) {
4558 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
4559 cmos_cmd >= 0 && cmos_cmd <= 21) {
4560 /* cmos_cmd set */
4561 } else
4562 return -EINVAL;
4564 res = issue_thinkpad_cmos_command(cmos_cmd);
4565 if (res)
4566 return res;
4569 return 0;
4572 static struct ibm_struct cmos_driver_data = {
4573 .name = "cmos",
4574 .read = cmos_read,
4575 .write = cmos_write,
4576 .exit = cmos_exit,
4579 /*************************************************************************
4580 * LED subdriver
4583 enum led_access_mode {
4584 TPACPI_LED_NONE = 0,
4585 TPACPI_LED_570, /* 570 */
4586 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4587 TPACPI_LED_NEW, /* all others */
4590 enum { /* For TPACPI_LED_OLD */
4591 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
4592 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
4593 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
4596 enum led_status_t {
4597 TPACPI_LED_OFF = 0,
4598 TPACPI_LED_ON,
4599 TPACPI_LED_BLINK,
4602 static enum led_access_mode led_supported;
4604 TPACPI_HANDLE(led, ec, "SLED", /* 570 */
4605 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
4606 /* T20-22, X20-21 */
4607 "LED", /* all others */
4608 ); /* R30, R31 */
4610 #define TPACPI_LED_NUMLEDS 8
4611 static struct tpacpi_led_classdev *tpacpi_leds;
4612 static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
4613 static const char * const tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
4614 /* there's a limit of 19 chars + NULL before 2.6.26 */
4615 "tpacpi::power",
4616 "tpacpi:orange:batt",
4617 "tpacpi:green:batt",
4618 "tpacpi::dock_active",
4619 "tpacpi::bay_active",
4620 "tpacpi::dock_batt",
4621 "tpacpi::unknown_led",
4622 "tpacpi::standby",
4624 #define TPACPI_SAFE_LEDS 0x0081U
4626 static inline bool tpacpi_is_led_restricted(const unsigned int led)
4628 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4629 return false;
4630 #else
4631 return (TPACPI_SAFE_LEDS & (1 << led)) == 0;
4632 #endif
4635 static int led_get_status(const unsigned int led)
4637 int status;
4638 enum led_status_t led_s;
4640 switch (led_supported) {
4641 case TPACPI_LED_570:
4642 if (!acpi_evalf(ec_handle,
4643 &status, "GLED", "dd", 1 << led))
4644 return -EIO;
4645 led_s = (status == 0)?
4646 TPACPI_LED_OFF :
4647 ((status == 1)?
4648 TPACPI_LED_ON :
4649 TPACPI_LED_BLINK);
4650 tpacpi_led_state_cache[led] = led_s;
4651 return led_s;
4652 default:
4653 return -ENXIO;
4656 /* not reached */
4659 static int led_set_status(const unsigned int led,
4660 const enum led_status_t ledstatus)
4662 /* off, on, blink. Index is led_status_t */
4663 static const unsigned int led_sled_arg1[] = { 0, 1, 3 };
4664 static const unsigned int led_led_arg1[] = { 0, 0x80, 0xc0 };
4666 int rc = 0;
4668 switch (led_supported) {
4669 case TPACPI_LED_570:
4670 /* 570 */
4671 if (unlikely(led > 7))
4672 return -EINVAL;
4673 if (unlikely(tpacpi_is_led_restricted(led)))
4674 return -EPERM;
4675 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4676 (1 << led), led_sled_arg1[ledstatus]))
4677 rc = -EIO;
4678 break;
4679 case TPACPI_LED_OLD:
4680 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
4681 if (unlikely(led > 7))
4682 return -EINVAL;
4683 if (unlikely(tpacpi_is_led_restricted(led)))
4684 return -EPERM;
4685 rc = ec_write(TPACPI_LED_EC_HLMS, (1 << led));
4686 if (rc >= 0)
4687 rc = ec_write(TPACPI_LED_EC_HLBL,
4688 (ledstatus == TPACPI_LED_BLINK) << led);
4689 if (rc >= 0)
4690 rc = ec_write(TPACPI_LED_EC_HLCL,
4691 (ledstatus != TPACPI_LED_OFF) << led);
4692 break;
4693 case TPACPI_LED_NEW:
4694 /* all others */
4695 if (unlikely(led >= TPACPI_LED_NUMLEDS))
4696 return -EINVAL;
4697 if (unlikely(tpacpi_is_led_restricted(led)))
4698 return -EPERM;
4699 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
4700 led, led_led_arg1[ledstatus]))
4701 rc = -EIO;
4702 break;
4703 default:
4704 rc = -ENXIO;
4707 if (!rc)
4708 tpacpi_led_state_cache[led] = ledstatus;
4710 return rc;
4713 static void led_sysfs_set_status(unsigned int led,
4714 enum led_brightness brightness)
4716 led_set_status(led,
4717 (brightness == LED_OFF) ?
4718 TPACPI_LED_OFF :
4719 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
4720 TPACPI_LED_BLINK : TPACPI_LED_ON);
4723 static void led_set_status_worker(struct work_struct *work)
4725 struct tpacpi_led_classdev *data =
4726 container_of(work, struct tpacpi_led_classdev, work);
4728 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
4729 led_sysfs_set_status(data->led, data->new_brightness);
4732 static void led_sysfs_set(struct led_classdev *led_cdev,
4733 enum led_brightness brightness)
4735 struct tpacpi_led_classdev *data = container_of(led_cdev,
4736 struct tpacpi_led_classdev, led_classdev);
4738 data->new_brightness = brightness;
4739 queue_work(tpacpi_wq, &data->work);
4742 static int led_sysfs_blink_set(struct led_classdev *led_cdev,
4743 unsigned long *delay_on, unsigned long *delay_off)
4745 struct tpacpi_led_classdev *data = container_of(led_cdev,
4746 struct tpacpi_led_classdev, led_classdev);
4748 /* Can we choose the flash rate? */
4749 if (*delay_on == 0 && *delay_off == 0) {
4750 /* yes. set them to the hardware blink rate (1 Hz) */
4751 *delay_on = 500; /* ms */
4752 *delay_off = 500; /* ms */
4753 } else if ((*delay_on != 500) || (*delay_off != 500))
4754 return -EINVAL;
4756 data->new_brightness = TPACPI_LED_BLINK;
4757 queue_work(tpacpi_wq, &data->work);
4759 return 0;
4762 static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
4764 int rc;
4766 struct tpacpi_led_classdev *data = container_of(led_cdev,
4767 struct tpacpi_led_classdev, led_classdev);
4769 rc = led_get_status(data->led);
4771 if (rc == TPACPI_LED_OFF || rc < 0)
4772 rc = LED_OFF; /* no error handling in led class :( */
4773 else
4774 rc = LED_FULL;
4776 return rc;
4779 static void led_exit(void)
4781 unsigned int i;
4783 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4784 if (tpacpi_leds[i].led_classdev.name)
4785 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
4788 kfree(tpacpi_leds);
4791 static int __init tpacpi_init_led(unsigned int led)
4793 int rc;
4795 tpacpi_leds[led].led = led;
4797 tpacpi_leds[led].led_classdev.brightness_set = &led_sysfs_set;
4798 tpacpi_leds[led].led_classdev.blink_set = &led_sysfs_blink_set;
4799 if (led_supported == TPACPI_LED_570)
4800 tpacpi_leds[led].led_classdev.brightness_get =
4801 &led_sysfs_get;
4803 tpacpi_leds[led].led_classdev.name = tpacpi_led_names[led];
4805 INIT_WORK(&tpacpi_leds[led].work, led_set_status_worker);
4807 rc = led_classdev_register(&tpacpi_pdev->dev,
4808 &tpacpi_leds[led].led_classdev);
4809 if (rc < 0)
4810 tpacpi_leds[led].led_classdev.name = NULL;
4812 return rc;
4815 static int __init led_init(struct ibm_init_struct *iibm)
4817 unsigned int i;
4818 int rc;
4820 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
4822 TPACPI_ACPIHANDLE_INIT(led);
4824 if (!led_handle)
4825 /* led not supported on R30, R31 */
4826 led_supported = TPACPI_LED_NONE;
4827 else if (strlencmp(led_path, "SLED") == 0)
4828 /* 570 */
4829 led_supported = TPACPI_LED_570;
4830 else if (strlencmp(led_path, "SYSL") == 0)
4831 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
4832 led_supported = TPACPI_LED_OLD;
4833 else
4834 /* all others */
4835 led_supported = TPACPI_LED_NEW;
4837 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4838 str_supported(led_supported), led_supported);
4840 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4841 GFP_KERNEL);
4842 if (!tpacpi_leds) {
4843 printk(TPACPI_ERR "Out of memory for LED data\n");
4844 return -ENOMEM;
4847 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4848 if (!tpacpi_is_led_restricted(i)) {
4849 rc = tpacpi_init_led(i);
4850 if (rc < 0) {
4851 led_exit();
4852 return rc;
4857 #ifdef CONFIG_THINKPAD_ACPI_UNSAFE_LEDS
4858 if (led_supported != TPACPI_LED_NONE)
4859 printk(TPACPI_NOTICE
4860 "warning: userspace override of important "
4861 "firmware LEDs is enabled\n");
4862 #endif
4863 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
4866 #define str_led_status(s) \
4867 ((s) == TPACPI_LED_OFF ? "off" : \
4868 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
4870 static int led_read(char *p)
4872 int len = 0;
4874 if (!led_supported) {
4875 len += sprintf(p + len, "status:\t\tnot supported\n");
4876 return len;
4878 len += sprintf(p + len, "status:\t\tsupported\n");
4880 if (led_supported == TPACPI_LED_570) {
4881 /* 570 */
4882 int i, status;
4883 for (i = 0; i < 8; i++) {
4884 status = led_get_status(i);
4885 if (status < 0)
4886 return -EIO;
4887 len += sprintf(p + len, "%d:\t\t%s\n",
4888 i, str_led_status(status));
4892 len += sprintf(p + len, "commands:\t"
4893 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
4895 return len;
4898 static int led_write(char *buf)
4900 char *cmd;
4901 int led, rc;
4902 enum led_status_t s;
4904 if (!led_supported)
4905 return -ENODEV;
4907 while ((cmd = next_cmd(&buf))) {
4908 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
4909 return -EINVAL;
4911 if (strstr(cmd, "off")) {
4912 s = TPACPI_LED_OFF;
4913 } else if (strstr(cmd, "on")) {
4914 s = TPACPI_LED_ON;
4915 } else if (strstr(cmd, "blink")) {
4916 s = TPACPI_LED_BLINK;
4917 } else {
4918 return -EINVAL;
4921 rc = led_set_status(led, s);
4922 if (rc < 0)
4923 return rc;
4926 return 0;
4929 static struct ibm_struct led_driver_data = {
4930 .name = "led",
4931 .read = led_read,
4932 .write = led_write,
4933 .exit = led_exit,
4936 /*************************************************************************
4937 * Beep subdriver
4940 TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
4942 static int __init beep_init(struct ibm_init_struct *iibm)
4944 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4946 TPACPI_ACPIHANDLE_INIT(beep);
4948 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4949 str_supported(beep_handle != NULL));
4951 return (beep_handle)? 0 : 1;
4954 static int beep_read(char *p)
4956 int len = 0;
4958 if (!beep_handle)
4959 len += sprintf(p + len, "status:\t\tnot supported\n");
4960 else {
4961 len += sprintf(p + len, "status:\t\tsupported\n");
4962 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4965 return len;
4968 static int beep_write(char *buf)
4970 char *cmd;
4971 int beep_cmd;
4973 if (!beep_handle)
4974 return -ENODEV;
4976 while ((cmd = next_cmd(&buf))) {
4977 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4978 beep_cmd >= 0 && beep_cmd <= 17) {
4979 /* beep_cmd set */
4980 } else
4981 return -EINVAL;
4982 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
4983 return -EIO;
4986 return 0;
4989 static struct ibm_struct beep_driver_data = {
4990 .name = "beep",
4991 .read = beep_read,
4992 .write = beep_write,
4995 /*************************************************************************
4996 * Thermal subdriver
4999 enum thermal_access_mode {
5000 TPACPI_THERMAL_NONE = 0, /* No thermal support */
5001 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
5002 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
5003 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
5004 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
5007 enum { /* TPACPI_THERMAL_TPEC_* */
5008 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
5009 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
5010 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
5013 #define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
5014 struct ibm_thermal_sensors_struct {
5015 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
5018 static enum thermal_access_mode thermal_read_mode;
5020 /* idx is zero-based */
5021 static int thermal_get_sensor(int idx, s32 *value)
5023 int t;
5024 s8 tmp;
5025 char tmpi[5];
5027 t = TP_EC_THERMAL_TMP0;
5029 switch (thermal_read_mode) {
5030 #if TPACPI_MAX_THERMAL_SENSORS >= 16
5031 case TPACPI_THERMAL_TPEC_16:
5032 if (idx >= 8 && idx <= 15) {
5033 t = TP_EC_THERMAL_TMP8;
5034 idx -= 8;
5036 /* fallthrough */
5037 #endif
5038 case TPACPI_THERMAL_TPEC_8:
5039 if (idx <= 7) {
5040 if (!acpi_ec_read(t + idx, &tmp))
5041 return -EIO;
5042 *value = tmp * 1000;
5043 return 0;
5045 break;
5047 case TPACPI_THERMAL_ACPI_UPDT:
5048 if (idx <= 7) {
5049 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5050 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
5051 return -EIO;
5052 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5053 return -EIO;
5054 *value = (t - 2732) * 100;
5055 return 0;
5057 break;
5059 case TPACPI_THERMAL_ACPI_TMP07:
5060 if (idx <= 7) {
5061 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
5062 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
5063 return -EIO;
5064 if (t > 127 || t < -127)
5065 t = TP_EC_THERMAL_TMP_NA;
5066 *value = t * 1000;
5067 return 0;
5069 break;
5071 case TPACPI_THERMAL_NONE:
5072 default:
5073 return -ENOSYS;
5076 return -EINVAL;
5079 static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
5081 int res, i;
5082 int n;
5084 n = 8;
5085 i = 0;
5087 if (!s)
5088 return -EINVAL;
5090 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
5091 n = 16;
5093 for (i = 0 ; i < n; i++) {
5094 res = thermal_get_sensor(i, &s->temp[i]);
5095 if (res)
5096 return res;
5099 return n;
5102 /* sysfs temp##_input -------------------------------------------------- */
5104 static ssize_t thermal_temp_input_show(struct device *dev,
5105 struct device_attribute *attr,
5106 char *buf)
5108 struct sensor_device_attribute *sensor_attr =
5109 to_sensor_dev_attr(attr);
5110 int idx = sensor_attr->index;
5111 s32 value;
5112 int res;
5114 res = thermal_get_sensor(idx, &value);
5115 if (res)
5116 return res;
5117 if (value == TP_EC_THERMAL_TMP_NA * 1000)
5118 return -ENXIO;
5120 return snprintf(buf, PAGE_SIZE, "%d\n", value);
5123 #define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
5124 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
5125 thermal_temp_input_show, NULL, _idxB)
5127 static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
5128 THERMAL_SENSOR_ATTR_TEMP(1, 0),
5129 THERMAL_SENSOR_ATTR_TEMP(2, 1),
5130 THERMAL_SENSOR_ATTR_TEMP(3, 2),
5131 THERMAL_SENSOR_ATTR_TEMP(4, 3),
5132 THERMAL_SENSOR_ATTR_TEMP(5, 4),
5133 THERMAL_SENSOR_ATTR_TEMP(6, 5),
5134 THERMAL_SENSOR_ATTR_TEMP(7, 6),
5135 THERMAL_SENSOR_ATTR_TEMP(8, 7),
5136 THERMAL_SENSOR_ATTR_TEMP(9, 8),
5137 THERMAL_SENSOR_ATTR_TEMP(10, 9),
5138 THERMAL_SENSOR_ATTR_TEMP(11, 10),
5139 THERMAL_SENSOR_ATTR_TEMP(12, 11),
5140 THERMAL_SENSOR_ATTR_TEMP(13, 12),
5141 THERMAL_SENSOR_ATTR_TEMP(14, 13),
5142 THERMAL_SENSOR_ATTR_TEMP(15, 14),
5143 THERMAL_SENSOR_ATTR_TEMP(16, 15),
5146 #define THERMAL_ATTRS(X) \
5147 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
5149 static struct attribute *thermal_temp_input_attr[] = {
5150 THERMAL_ATTRS(8),
5151 THERMAL_ATTRS(9),
5152 THERMAL_ATTRS(10),
5153 THERMAL_ATTRS(11),
5154 THERMAL_ATTRS(12),
5155 THERMAL_ATTRS(13),
5156 THERMAL_ATTRS(14),
5157 THERMAL_ATTRS(15),
5158 THERMAL_ATTRS(0),
5159 THERMAL_ATTRS(1),
5160 THERMAL_ATTRS(2),
5161 THERMAL_ATTRS(3),
5162 THERMAL_ATTRS(4),
5163 THERMAL_ATTRS(5),
5164 THERMAL_ATTRS(6),
5165 THERMAL_ATTRS(7),
5166 NULL
5169 static const struct attribute_group thermal_temp_input16_group = {
5170 .attrs = thermal_temp_input_attr
5173 static const struct attribute_group thermal_temp_input8_group = {
5174 .attrs = &thermal_temp_input_attr[8]
5177 #undef THERMAL_SENSOR_ATTR_TEMP
5178 #undef THERMAL_ATTRS
5180 /* --------------------------------------------------------------------- */
5182 static int __init thermal_init(struct ibm_init_struct *iibm)
5184 u8 t, ta1, ta2;
5185 int i;
5186 int acpi_tmp7;
5187 int res;
5189 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
5191 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
5193 if (thinkpad_id.ec_model) {
5195 * Direct EC access mode: sensors at registers
5196 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
5197 * non-implemented, thermal sensors return 0x80 when
5198 * not available
5201 ta1 = ta2 = 0;
5202 for (i = 0; i < 8; i++) {
5203 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
5204 ta1 |= t;
5205 } else {
5206 ta1 = 0;
5207 break;
5209 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
5210 ta2 |= t;
5211 } else {
5212 ta1 = 0;
5213 break;
5216 if (ta1 == 0) {
5217 /* This is sheer paranoia, but we handle it anyway */
5218 if (acpi_tmp7) {
5219 printk(TPACPI_ERR
5220 "ThinkPad ACPI EC access misbehaving, "
5221 "falling back to ACPI TMPx access "
5222 "mode\n");
5223 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5224 } else {
5225 printk(TPACPI_ERR
5226 "ThinkPad ACPI EC access misbehaving, "
5227 "disabling thermal sensors access\n");
5228 thermal_read_mode = TPACPI_THERMAL_NONE;
5230 } else {
5231 thermal_read_mode =
5232 (ta2 != 0) ?
5233 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
5235 } else if (acpi_tmp7) {
5236 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
5237 /* 600e/x, 770e, 770x */
5238 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
5239 } else {
5240 /* Standard ACPI TMPx access, max 8 sensors */
5241 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
5243 } else {
5244 /* temperatures not supported on 570, G4x, R30, R31, R32 */
5245 thermal_read_mode = TPACPI_THERMAL_NONE;
5248 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
5249 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
5250 thermal_read_mode);
5252 switch (thermal_read_mode) {
5253 case TPACPI_THERMAL_TPEC_16:
5254 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5255 &thermal_temp_input16_group);
5256 if (res)
5257 return res;
5258 break;
5259 case TPACPI_THERMAL_TPEC_8:
5260 case TPACPI_THERMAL_ACPI_TMP07:
5261 case TPACPI_THERMAL_ACPI_UPDT:
5262 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
5263 &thermal_temp_input8_group);
5264 if (res)
5265 return res;
5266 break;
5267 case TPACPI_THERMAL_NONE:
5268 default:
5269 return 1;
5272 return 0;
5275 static void thermal_exit(void)
5277 switch (thermal_read_mode) {
5278 case TPACPI_THERMAL_TPEC_16:
5279 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5280 &thermal_temp_input16_group);
5281 break;
5282 case TPACPI_THERMAL_TPEC_8:
5283 case TPACPI_THERMAL_ACPI_TMP07:
5284 case TPACPI_THERMAL_ACPI_UPDT:
5285 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
5286 &thermal_temp_input16_group);
5287 break;
5288 case TPACPI_THERMAL_NONE:
5289 default:
5290 break;
5294 static int thermal_read(char *p)
5296 int len = 0;
5297 int n, i;
5298 struct ibm_thermal_sensors_struct t;
5300 n = thermal_get_sensors(&t);
5301 if (unlikely(n < 0))
5302 return n;
5304 len += sprintf(p + len, "temperatures:\t");
5306 if (n > 0) {
5307 for (i = 0; i < (n - 1); i++)
5308 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
5309 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
5310 } else
5311 len += sprintf(p + len, "not supported\n");
5313 return len;
5316 static struct ibm_struct thermal_driver_data = {
5317 .name = "thermal",
5318 .read = thermal_read,
5319 .exit = thermal_exit,
5322 /*************************************************************************
5323 * EC Dump subdriver
5326 static u8 ecdump_regs[256];
5328 static int ecdump_read(char *p)
5330 int len = 0;
5331 int i, j;
5332 u8 v;
5334 len += sprintf(p + len, "EC "
5335 " +00 +01 +02 +03 +04 +05 +06 +07"
5336 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
5337 for (i = 0; i < 256; i += 16) {
5338 len += sprintf(p + len, "EC 0x%02x:", i);
5339 for (j = 0; j < 16; j++) {
5340 if (!acpi_ec_read(i + j, &v))
5341 break;
5342 if (v != ecdump_regs[i + j])
5343 len += sprintf(p + len, " *%02x", v);
5344 else
5345 len += sprintf(p + len, " %02x", v);
5346 ecdump_regs[i + j] = v;
5348 len += sprintf(p + len, "\n");
5349 if (j != 16)
5350 break;
5353 /* These are way too dangerous to advertise openly... */
5354 #if 0
5355 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
5356 " (<offset> is 00-ff, <value> is 00-ff)\n");
5357 len += sprintf(p + len, "commands:\t0x<offset> <value> "
5358 " (<offset> is 00-ff, <value> is 0-255)\n");
5359 #endif
5360 return len;
5363 static int ecdump_write(char *buf)
5365 char *cmd;
5366 int i, v;
5368 while ((cmd = next_cmd(&buf))) {
5369 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
5370 /* i and v set */
5371 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
5372 /* i and v set */
5373 } else
5374 return -EINVAL;
5375 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
5376 if (!acpi_ec_write(i, v))
5377 return -EIO;
5378 } else
5379 return -EINVAL;
5382 return 0;
5385 static struct ibm_struct ecdump_driver_data = {
5386 .name = "ecdump",
5387 .read = ecdump_read,
5388 .write = ecdump_write,
5389 .flags.experimental = 1,
5392 /*************************************************************************
5393 * Backlight/brightness subdriver
5396 #define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
5399 * ThinkPads can read brightness from two places: EC HBRV (0x31), or
5400 * CMOS NVRAM byte 0x5E, bits 0-3.
5402 * EC HBRV (0x31) has the following layout
5403 * Bit 7: unknown function
5404 * Bit 6: unknown function
5405 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
5406 * Bit 4: must be set to zero to avoid problems
5407 * Bit 3-0: backlight brightness level
5409 * brightness_get_raw returns status data in the HBRV layout
5412 enum {
5413 TP_EC_BACKLIGHT = 0x31,
5415 /* TP_EC_BACKLIGHT bitmasks */
5416 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
5417 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
5418 TP_EC_BACKLIGHT_MAPSW = 0x20,
5421 enum tpacpi_brightness_access_mode {
5422 TPACPI_BRGHT_MODE_AUTO = 0, /* Not implemented yet */
5423 TPACPI_BRGHT_MODE_EC, /* EC control */
5424 TPACPI_BRGHT_MODE_UCMS_STEP, /* UCMS step-based control */
5425 TPACPI_BRGHT_MODE_ECNVRAM, /* EC control w/ NVRAM store */
5426 TPACPI_BRGHT_MODE_MAX
5429 static struct backlight_device *ibm_backlight_device;
5431 static enum tpacpi_brightness_access_mode brightness_mode =
5432 TPACPI_BRGHT_MODE_MAX;
5434 static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
5436 static struct mutex brightness_mutex;
5438 /* NVRAM brightness access,
5439 * call with brightness_mutex held! */
5440 static unsigned int tpacpi_brightness_nvram_get(void)
5442 u8 lnvram;
5444 lnvram = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
5445 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5446 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
5447 lnvram &= (tp_features.bright_16levels) ? 0x0f : 0x07;
5449 return lnvram;
5452 static void tpacpi_brightness_checkpoint_nvram(void)
5454 u8 lec = 0;
5455 u8 b_nvram;
5457 if (brightness_mode != TPACPI_BRGHT_MODE_ECNVRAM)
5458 return;
5460 vdbg_printk(TPACPI_DBG_BRGHT,
5461 "trying to checkpoint backlight level to NVRAM...\n");
5463 if (mutex_lock_killable(&brightness_mutex) < 0)
5464 return;
5466 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5467 goto unlock;
5468 lec &= TP_EC_BACKLIGHT_LVLMSK;
5469 b_nvram = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
5471 if (lec != ((b_nvram & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
5472 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS)) {
5473 /* NVRAM needs update */
5474 b_nvram &= ~(TP_NVRAM_MASK_LEVEL_BRIGHTNESS <<
5475 TP_NVRAM_POS_LEVEL_BRIGHTNESS);
5476 b_nvram |= lec;
5477 nvram_write_byte(b_nvram, TP_NVRAM_ADDR_BRIGHTNESS);
5478 dbg_printk(TPACPI_DBG_BRGHT,
5479 "updated NVRAM backlight level to %u (0x%02x)\n",
5480 (unsigned int) lec, (unsigned int) b_nvram);
5481 } else
5482 vdbg_printk(TPACPI_DBG_BRGHT,
5483 "NVRAM backlight level already is %u (0x%02x)\n",
5484 (unsigned int) lec, (unsigned int) b_nvram);
5486 unlock:
5487 mutex_unlock(&brightness_mutex);
5491 /* call with brightness_mutex held! */
5492 static int tpacpi_brightness_get_raw(int *status)
5494 u8 lec = 0;
5496 switch (brightness_mode) {
5497 case TPACPI_BRGHT_MODE_UCMS_STEP:
5498 *status = tpacpi_brightness_nvram_get();
5499 return 0;
5500 case TPACPI_BRGHT_MODE_EC:
5501 case TPACPI_BRGHT_MODE_ECNVRAM:
5502 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5503 return -EIO;
5504 *status = lec;
5505 return 0;
5506 default:
5507 return -ENXIO;
5511 /* call with brightness_mutex held! */
5512 /* do NOT call with illegal backlight level value */
5513 static int tpacpi_brightness_set_ec(unsigned int value)
5515 u8 lec = 0;
5517 if (unlikely(!acpi_ec_read(TP_EC_BACKLIGHT, &lec)))
5518 return -EIO;
5520 if (unlikely(!acpi_ec_write(TP_EC_BACKLIGHT,
5521 (lec & TP_EC_BACKLIGHT_CMDMSK) |
5522 (value & TP_EC_BACKLIGHT_LVLMSK))))
5523 return -EIO;
5525 return 0;
5528 /* call with brightness_mutex held! */
5529 static int tpacpi_brightness_set_ucmsstep(unsigned int value)
5531 int cmos_cmd, inc;
5532 unsigned int current_value, i;
5534 current_value = tpacpi_brightness_nvram_get();
5536 if (value == current_value)
5537 return 0;
5539 cmos_cmd = (value > current_value) ?
5540 TP_CMOS_BRIGHTNESS_UP :
5541 TP_CMOS_BRIGHTNESS_DOWN;
5542 inc = (value > current_value) ? 1 : -1;
5544 for (i = current_value; i != value; i += inc)
5545 if (issue_thinkpad_cmos_command(cmos_cmd))
5546 return -EIO;
5548 return 0;
5551 /* May return EINTR which can always be mapped to ERESTARTSYS */
5552 static int brightness_set(unsigned int value)
5554 int res;
5556 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
5557 value < 0)
5558 return -EINVAL;
5560 res = mutex_lock_killable(&brightness_mutex);
5561 if (res < 0)
5562 return res;
5564 switch (brightness_mode) {
5565 case TPACPI_BRGHT_MODE_EC:
5566 case TPACPI_BRGHT_MODE_ECNVRAM:
5567 res = tpacpi_brightness_set_ec(value);
5568 break;
5569 case TPACPI_BRGHT_MODE_UCMS_STEP:
5570 res = tpacpi_brightness_set_ucmsstep(value);
5571 break;
5572 default:
5573 res = -ENXIO;
5576 mutex_unlock(&brightness_mutex);
5577 return res;
5580 /* sysfs backlight class ----------------------------------------------- */
5582 static int brightness_update_status(struct backlight_device *bd)
5584 unsigned int level =
5585 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
5586 bd->props.power == FB_BLANK_UNBLANK) ?
5587 bd->props.brightness : 0;
5589 dbg_printk_pid(TPACPI_DBG_BRGHT,
5590 "sysfs backlight set level to %d\n",
5591 level);
5593 /* it is the backlight class's job (caller) to handle
5594 * EINTR and other errors properly */
5595 return brightness_set(level);
5598 static int brightness_get(struct backlight_device *bd)
5600 int status, res;
5602 res = mutex_lock_killable(&brightness_mutex);
5603 if (res < 0)
5604 return 0;
5606 res = tpacpi_brightness_get_raw(&status);
5608 mutex_unlock(&brightness_mutex);
5610 if (res < 0)
5611 return 0;
5613 return status & TP_EC_BACKLIGHT_LVLMSK;
5616 static struct backlight_ops ibm_backlight_data = {
5617 .get_brightness = brightness_get,
5618 .update_status = brightness_update_status,
5621 /* --------------------------------------------------------------------- */
5623 static int __init brightness_init(struct ibm_init_struct *iibm)
5625 int b;
5627 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
5629 mutex_init(&brightness_mutex);
5632 * We always attempt to detect acpi support, so as to switch
5633 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
5634 * going to publish a backlight interface
5636 b = tpacpi_check_std_acpi_brightness_support();
5637 if (b > 0) {
5639 if (acpi_video_backlight_support()) {
5640 if (brightness_enable > 1) {
5641 printk(TPACPI_NOTICE
5642 "Standard ACPI backlight interface "
5643 "available, not loading native one.\n");
5644 return 1;
5645 } else if (brightness_enable == 1) {
5646 printk(TPACPI_NOTICE
5647 "Backlight control force enabled, even if standard "
5648 "ACPI backlight interface is available\n");
5650 } else {
5651 if (brightness_enable > 1) {
5652 printk(TPACPI_NOTICE
5653 "Standard ACPI backlight interface not "
5654 "available, thinkpad_acpi native "
5655 "brightness control enabled\n");
5660 if (!brightness_enable) {
5661 dbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5662 "brightness support disabled by "
5663 "module parameter\n");
5664 return 1;
5667 if (b > 16) {
5668 printk(TPACPI_ERR
5669 "Unsupported brightness interface, "
5670 "please contact %s\n", TPACPI_MAIL);
5671 return 1;
5673 if (b == 16)
5674 tp_features.bright_16levels = 1;
5677 * Check for module parameter bogosity, note that we
5678 * init brightness_mode to TPACPI_BRGHT_MODE_MAX in order to be
5679 * able to detect "unspecified"
5681 if (brightness_mode > TPACPI_BRGHT_MODE_MAX)
5682 return -EINVAL;
5684 /* TPACPI_BRGHT_MODE_AUTO not implemented yet, just use default */
5685 if (brightness_mode == TPACPI_BRGHT_MODE_AUTO ||
5686 brightness_mode == TPACPI_BRGHT_MODE_MAX) {
5687 if (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) {
5689 * IBM models that define HBRV probably have
5690 * EC-based backlight level control
5692 if (acpi_evalf(ec_handle, NULL, "HBRV", "qd"))
5693 /* T40-T43, R50-R52, R50e, R51e, X31-X41 */
5694 brightness_mode = TPACPI_BRGHT_MODE_ECNVRAM;
5695 else
5696 /* all other IBM ThinkPads */
5697 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5698 } else
5699 /* All Lenovo ThinkPads */
5700 brightness_mode = TPACPI_BRGHT_MODE_UCMS_STEP;
5702 dbg_printk(TPACPI_DBG_BRGHT,
5703 "selected brightness_mode=%d\n",
5704 brightness_mode);
5707 if (tpacpi_brightness_get_raw(&b) < 0)
5708 return 1;
5710 if (tp_features.bright_16levels)
5711 printk(TPACPI_INFO
5712 "detected a 16-level brightness capable ThinkPad\n");
5714 ibm_backlight_device = backlight_device_register(
5715 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
5716 &ibm_backlight_data);
5717 if (IS_ERR(ibm_backlight_device)) {
5718 printk(TPACPI_ERR "Could not register backlight device\n");
5719 return PTR_ERR(ibm_backlight_device);
5721 vdbg_printk(TPACPI_DBG_INIT | TPACPI_DBG_BRGHT,
5722 "brightness is supported\n");
5724 ibm_backlight_device->props.max_brightness =
5725 (tp_features.bright_16levels)? 15 : 7;
5726 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
5727 backlight_update_status(ibm_backlight_device);
5729 return 0;
5732 static void brightness_suspend(pm_message_t state)
5734 tpacpi_brightness_checkpoint_nvram();
5737 static void brightness_shutdown(void)
5739 tpacpi_brightness_checkpoint_nvram();
5742 static void brightness_exit(void)
5744 if (ibm_backlight_device) {
5745 vdbg_printk(TPACPI_DBG_EXIT | TPACPI_DBG_BRGHT,
5746 "calling backlight_device_unregister()\n");
5747 backlight_device_unregister(ibm_backlight_device);
5750 tpacpi_brightness_checkpoint_nvram();
5753 static int brightness_read(char *p)
5755 int len = 0;
5756 int level;
5758 level = brightness_get(NULL);
5759 if (level < 0) {
5760 len += sprintf(p + len, "level:\t\tunreadable\n");
5761 } else {
5762 len += sprintf(p + len, "level:\t\t%d\n", level);
5763 len += sprintf(p + len, "commands:\tup, down\n");
5764 len += sprintf(p + len, "commands:\tlevel <level>"
5765 " (<level> is 0-%d)\n",
5766 (tp_features.bright_16levels) ? 15 : 7);
5769 return len;
5772 static int brightness_write(char *buf)
5774 int level;
5775 int rc;
5776 char *cmd;
5777 int max_level = (tp_features.bright_16levels) ? 15 : 7;
5779 level = brightness_get(NULL);
5780 if (level < 0)
5781 return level;
5783 while ((cmd = next_cmd(&buf))) {
5784 if (strlencmp(cmd, "up") == 0) {
5785 if (level < max_level)
5786 level++;
5787 } else if (strlencmp(cmd, "down") == 0) {
5788 if (level > 0)
5789 level--;
5790 } else if (sscanf(cmd, "level %d", &level) == 1 &&
5791 level >= 0 && level <= max_level) {
5792 /* new level set */
5793 } else
5794 return -EINVAL;
5797 dbg_printk_pid(TPACPI_DBG_BRGHT,
5798 "procfs set brightness to %d\n",
5799 level);
5802 * Now we know what the final level should be, so we try to set it.
5803 * Doing it this way makes the syscall restartable in case of EINTR
5805 rc = brightness_set(level);
5806 return (rc == -EINTR)? ERESTARTSYS : rc;
5809 static struct ibm_struct brightness_driver_data = {
5810 .name = "brightness",
5811 .read = brightness_read,
5812 .write = brightness_write,
5813 .exit = brightness_exit,
5814 .suspend = brightness_suspend,
5815 .shutdown = brightness_shutdown,
5818 /*************************************************************************
5819 * Volume subdriver
5822 static int volume_offset = 0x30;
5824 static int volume_read(char *p)
5826 int len = 0;
5827 u8 level;
5829 if (!acpi_ec_read(volume_offset, &level)) {
5830 len += sprintf(p + len, "level:\t\tunreadable\n");
5831 } else {
5832 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
5833 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
5834 len += sprintf(p + len, "commands:\tup, down, mute\n");
5835 len += sprintf(p + len, "commands:\tlevel <level>"
5836 " (<level> is 0-15)\n");
5839 return len;
5842 static int volume_write(char *buf)
5844 int cmos_cmd, inc, i;
5845 u8 level, mute;
5846 int new_level, new_mute;
5847 char *cmd;
5849 while ((cmd = next_cmd(&buf))) {
5850 if (!acpi_ec_read(volume_offset, &level))
5851 return -EIO;
5852 new_mute = mute = level & 0x40;
5853 new_level = level = level & 0xf;
5855 if (strlencmp(cmd, "up") == 0) {
5856 if (mute)
5857 new_mute = 0;
5858 else
5859 new_level = level == 15 ? 15 : level + 1;
5860 } else if (strlencmp(cmd, "down") == 0) {
5861 if (mute)
5862 new_mute = 0;
5863 else
5864 new_level = level == 0 ? 0 : level - 1;
5865 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
5866 new_level >= 0 && new_level <= 15) {
5867 /* new_level set */
5868 } else if (strlencmp(cmd, "mute") == 0) {
5869 new_mute = 0x40;
5870 } else
5871 return -EINVAL;
5873 if (new_level != level) {
5874 /* mute doesn't change */
5876 cmos_cmd = (new_level > level) ?
5877 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
5878 inc = new_level > level ? 1 : -1;
5880 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
5881 !acpi_ec_write(volume_offset, level)))
5882 return -EIO;
5884 for (i = level; i != new_level; i += inc)
5885 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5886 !acpi_ec_write(volume_offset, i + inc))
5887 return -EIO;
5889 if (mute &&
5890 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
5891 !acpi_ec_write(volume_offset, new_level + mute))) {
5892 return -EIO;
5896 if (new_mute != mute) {
5897 /* level doesn't change */
5899 cmos_cmd = (new_mute) ?
5900 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
5902 if (issue_thinkpad_cmos_command(cmos_cmd) ||
5903 !acpi_ec_write(volume_offset, level + new_mute))
5904 return -EIO;
5908 return 0;
5911 static struct ibm_struct volume_driver_data = {
5912 .name = "volume",
5913 .read = volume_read,
5914 .write = volume_write,
5917 /*************************************************************************
5918 * Fan subdriver
5922 * FAN ACCESS MODES
5924 * TPACPI_FAN_RD_ACPI_GFAN:
5925 * ACPI GFAN method: returns fan level
5927 * see TPACPI_FAN_WR_ACPI_SFAN
5928 * EC 0x2f (HFSP) not available if GFAN exists
5930 * TPACPI_FAN_WR_ACPI_SFAN:
5931 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
5933 * EC 0x2f (HFSP) might be available *for reading*, but do not use
5934 * it for writing.
5936 * TPACPI_FAN_WR_TPEC:
5937 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
5938 * Supported on almost all ThinkPads
5940 * Fan speed changes of any sort (including those caused by the
5941 * disengaged mode) are usually done slowly by the firmware as the
5942 * maximum ammount of fan duty cycle change per second seems to be
5943 * limited.
5945 * Reading is not available if GFAN exists.
5946 * Writing is not available if SFAN exists.
5948 * Bits
5949 * 7 automatic mode engaged;
5950 * (default operation mode of the ThinkPad)
5951 * fan level is ignored in this mode.
5952 * 6 full speed mode (takes precedence over bit 7);
5953 * not available on all thinkpads. May disable
5954 * the tachometer while the fan controller ramps up
5955 * the speed (which can take up to a few *minutes*).
5956 * Speeds up fan to 100% duty-cycle, which is far above
5957 * the standard RPM levels. It is not impossible that
5958 * it could cause hardware damage.
5959 * 5-3 unused in some models. Extra bits for fan level
5960 * in others, but still useless as all values above
5961 * 7 map to the same speed as level 7 in these models.
5962 * 2-0 fan level (0..7 usually)
5963 * 0x00 = stop
5964 * 0x07 = max (set when temperatures critical)
5965 * Some ThinkPads may have other levels, see
5966 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
5968 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5969 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5970 * does so, its initial value is meaningless (0x07).
5972 * For firmware bugs, refer to:
5973 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5975 * ----
5977 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5978 * Main fan tachometer reading (in RPM)
5980 * This register is present on all ThinkPads with a new-style EC, and
5981 * it is known not to be present on the A21m/e, and T22, as there is
5982 * something else in offset 0x84 according to the ACPI DSDT. Other
5983 * ThinkPads from this same time period (and earlier) probably lack the
5984 * tachometer as well.
5986 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5987 * was never fixed by IBM to report the EC firmware version string
5988 * probably support the tachometer (like the early X models), so
5989 * detecting it is quite hard. We need more data to know for sure.
5991 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5992 * might result.
5994 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5995 * mode.
5997 * For firmware bugs, refer to:
5998 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
6000 * TPACPI_FAN_WR_ACPI_FANS:
6001 * ThinkPad X31, X40, X41. Not available in the X60.
6003 * FANS ACPI handle: takes three arguments: low speed, medium speed,
6004 * high speed. ACPI DSDT seems to map these three speeds to levels
6005 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
6006 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
6008 * The speeds are stored on handles
6009 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
6011 * There are three default speed sets, acessible as handles:
6012 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
6014 * ACPI DSDT switches which set is in use depending on various
6015 * factors.
6017 * TPACPI_FAN_WR_TPEC is also available and should be used to
6018 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
6019 * but the ACPI tables just mention level 7.
6022 enum { /* Fan control constants */
6023 fan_status_offset = 0x2f, /* EC register 0x2f */
6024 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
6025 * 0x84 must be read before 0x85 */
6027 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
6028 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
6030 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
6033 enum fan_status_access_mode {
6034 TPACPI_FAN_NONE = 0, /* No fan status or control */
6035 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
6036 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
6039 enum fan_control_access_mode {
6040 TPACPI_FAN_WR_NONE = 0, /* No fan control */
6041 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
6042 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
6043 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
6046 enum fan_control_commands {
6047 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
6048 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
6049 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
6050 * and also watchdog cmd */
6053 static int fan_control_allowed;
6055 static enum fan_status_access_mode fan_status_access_mode;
6056 static enum fan_control_access_mode fan_control_access_mode;
6057 static enum fan_control_commands fan_control_commands;
6059 static u8 fan_control_initial_status;
6060 static u8 fan_control_desired_level;
6061 static u8 fan_control_resume_level;
6062 static int fan_watchdog_maxinterval;
6064 static struct mutex fan_mutex;
6066 static void fan_watchdog_fire(struct work_struct *ignored);
6067 static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
6069 TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
6070 TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
6071 "\\FSPD", /* 600e/x, 770e, 770x */
6072 ); /* all others */
6073 TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
6074 "JFNS", /* 770x-JL */
6075 ); /* all others */
6078 * Unitialized HFSP quirk: ACPI DSDT and EC fail to initialize the
6079 * HFSP register at boot, so it contains 0x07 but the Thinkpad could
6080 * be in auto mode (0x80).
6082 * This is corrected by any write to HFSP either by the driver, or
6083 * by the firmware.
6085 * We assume 0x07 really means auto mode while this quirk is active,
6086 * as this is far more likely than the ThinkPad being in level 7,
6087 * which is only used by the firmware during thermal emergencies.
6090 static void fan_quirk1_detect(void)
6092 /* In some ThinkPads, neither the EC nor the ACPI
6093 * DSDT initialize the HFSP register, and it ends up
6094 * being initially set to 0x07 when it *could* be
6095 * either 0x07 or 0x80.
6097 * Enable for TP-1Y (T43), TP-78 (R51e),
6098 * TP-76 (R52), TP-70 (T43, R52), which are known
6099 * to be buggy. */
6100 if (fan_control_initial_status == 0x07) {
6101 switch (thinkpad_id.ec_model) {
6102 case 0x5931: /* TP-1Y */
6103 case 0x3837: /* TP-78 */
6104 case 0x3637: /* TP-76 */
6105 case 0x3037: /* TP-70 */
6106 printk(TPACPI_NOTICE
6107 "fan_init: initial fan status is unknown, "
6108 "assuming it is in auto mode\n");
6109 tp_features.fan_ctrl_status_undef = 1;
6115 static void fan_quirk1_handle(u8 *fan_status)
6117 if (unlikely(tp_features.fan_ctrl_status_undef)) {
6118 if (*fan_status != fan_control_initial_status) {
6119 /* something changed the HFSP regisnter since
6120 * driver init time, so it is not undefined
6121 * anymore */
6122 tp_features.fan_ctrl_status_undef = 0;
6123 } else {
6124 /* Return most likely status. In fact, it
6125 * might be the only possible status */
6126 *fan_status = TP_EC_FAN_AUTO;
6132 * Call with fan_mutex held
6134 static void fan_update_desired_level(u8 status)
6136 if ((status &
6137 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6138 if (status > 7)
6139 fan_control_desired_level = 7;
6140 else
6141 fan_control_desired_level = status;
6145 static int fan_get_status(u8 *status)
6147 u8 s;
6149 /* TODO:
6150 * Add TPACPI_FAN_RD_ACPI_FANS ? */
6152 switch (fan_status_access_mode) {
6153 case TPACPI_FAN_RD_ACPI_GFAN:
6154 /* 570, 600e/x, 770e, 770x */
6156 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
6157 return -EIO;
6159 if (likely(status))
6160 *status = s & 0x07;
6162 break;
6164 case TPACPI_FAN_RD_TPEC:
6165 /* all except 570, 600e/x, 770e, 770x */
6166 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
6167 return -EIO;
6169 if (likely(status)) {
6170 *status = s;
6171 fan_quirk1_handle(status);
6174 break;
6176 default:
6177 return -ENXIO;
6180 return 0;
6183 static int fan_get_status_safe(u8 *status)
6185 int rc;
6186 u8 s;
6188 if (mutex_lock_killable(&fan_mutex))
6189 return -ERESTARTSYS;
6190 rc = fan_get_status(&s);
6191 if (!rc)
6192 fan_update_desired_level(s);
6193 mutex_unlock(&fan_mutex);
6195 if (status)
6196 *status = s;
6198 return rc;
6201 static int fan_get_speed(unsigned int *speed)
6203 u8 hi, lo;
6205 switch (fan_status_access_mode) {
6206 case TPACPI_FAN_RD_TPEC:
6207 /* all except 570, 600e/x, 770e, 770x */
6208 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
6209 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
6210 return -EIO;
6212 if (likely(speed))
6213 *speed = (hi << 8) | lo;
6215 break;
6217 default:
6218 return -ENXIO;
6221 return 0;
6224 static int fan_set_level(int level)
6226 if (!fan_control_allowed)
6227 return -EPERM;
6229 switch (fan_control_access_mode) {
6230 case TPACPI_FAN_WR_ACPI_SFAN:
6231 if (level >= 0 && level <= 7) {
6232 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
6233 return -EIO;
6234 } else
6235 return -EINVAL;
6236 break;
6238 case TPACPI_FAN_WR_ACPI_FANS:
6239 case TPACPI_FAN_WR_TPEC:
6240 if (!(level & TP_EC_FAN_AUTO) &&
6241 !(level & TP_EC_FAN_FULLSPEED) &&
6242 ((level < 0) || (level > 7)))
6243 return -EINVAL;
6245 /* safety net should the EC not support AUTO
6246 * or FULLSPEED mode bits and just ignore them */
6247 if (level & TP_EC_FAN_FULLSPEED)
6248 level |= 7; /* safety min speed 7 */
6249 else if (level & TP_EC_FAN_AUTO)
6250 level |= 4; /* safety min speed 4 */
6252 if (!acpi_ec_write(fan_status_offset, level))
6253 return -EIO;
6254 else
6255 tp_features.fan_ctrl_status_undef = 0;
6256 break;
6258 default:
6259 return -ENXIO;
6261 return 0;
6264 static int fan_set_level_safe(int level)
6266 int rc;
6268 if (!fan_control_allowed)
6269 return -EPERM;
6271 if (mutex_lock_killable(&fan_mutex))
6272 return -ERESTARTSYS;
6274 if (level == TPACPI_FAN_LAST_LEVEL)
6275 level = fan_control_desired_level;
6277 rc = fan_set_level(level);
6278 if (!rc)
6279 fan_update_desired_level(level);
6281 mutex_unlock(&fan_mutex);
6282 return rc;
6285 static int fan_set_enable(void)
6287 u8 s;
6288 int rc;
6290 if (!fan_control_allowed)
6291 return -EPERM;
6293 if (mutex_lock_killable(&fan_mutex))
6294 return -ERESTARTSYS;
6296 switch (fan_control_access_mode) {
6297 case TPACPI_FAN_WR_ACPI_FANS:
6298 case TPACPI_FAN_WR_TPEC:
6299 rc = fan_get_status(&s);
6300 if (rc < 0)
6301 break;
6303 /* Don't go out of emergency fan mode */
6304 if (s != 7) {
6305 s &= 0x07;
6306 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
6309 if (!acpi_ec_write(fan_status_offset, s))
6310 rc = -EIO;
6311 else {
6312 tp_features.fan_ctrl_status_undef = 0;
6313 rc = 0;
6315 break;
6317 case TPACPI_FAN_WR_ACPI_SFAN:
6318 rc = fan_get_status(&s);
6319 if (rc < 0)
6320 break;
6322 s &= 0x07;
6324 /* Set fan to at least level 4 */
6325 s |= 4;
6327 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
6328 rc = -EIO;
6329 else
6330 rc = 0;
6331 break;
6333 default:
6334 rc = -ENXIO;
6337 mutex_unlock(&fan_mutex);
6338 return rc;
6341 static int fan_set_disable(void)
6343 int rc;
6345 if (!fan_control_allowed)
6346 return -EPERM;
6348 if (mutex_lock_killable(&fan_mutex))
6349 return -ERESTARTSYS;
6351 rc = 0;
6352 switch (fan_control_access_mode) {
6353 case TPACPI_FAN_WR_ACPI_FANS:
6354 case TPACPI_FAN_WR_TPEC:
6355 if (!acpi_ec_write(fan_status_offset, 0x00))
6356 rc = -EIO;
6357 else {
6358 fan_control_desired_level = 0;
6359 tp_features.fan_ctrl_status_undef = 0;
6361 break;
6363 case TPACPI_FAN_WR_ACPI_SFAN:
6364 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
6365 rc = -EIO;
6366 else
6367 fan_control_desired_level = 0;
6368 break;
6370 default:
6371 rc = -ENXIO;
6375 mutex_unlock(&fan_mutex);
6376 return rc;
6379 static int fan_set_speed(int speed)
6381 int rc;
6383 if (!fan_control_allowed)
6384 return -EPERM;
6386 if (mutex_lock_killable(&fan_mutex))
6387 return -ERESTARTSYS;
6389 rc = 0;
6390 switch (fan_control_access_mode) {
6391 case TPACPI_FAN_WR_ACPI_FANS:
6392 if (speed >= 0 && speed <= 65535) {
6393 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
6394 speed, speed, speed))
6395 rc = -EIO;
6396 } else
6397 rc = -EINVAL;
6398 break;
6400 default:
6401 rc = -ENXIO;
6404 mutex_unlock(&fan_mutex);
6405 return rc;
6408 static void fan_watchdog_reset(void)
6410 static int fan_watchdog_active;
6412 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
6413 return;
6415 if (fan_watchdog_active)
6416 cancel_delayed_work(&fan_watchdog_task);
6418 if (fan_watchdog_maxinterval > 0 &&
6419 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
6420 fan_watchdog_active = 1;
6421 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
6422 msecs_to_jiffies(fan_watchdog_maxinterval
6423 * 1000))) {
6424 printk(TPACPI_ERR
6425 "failed to queue the fan watchdog, "
6426 "watchdog will not trigger\n");
6428 } else
6429 fan_watchdog_active = 0;
6432 static void fan_watchdog_fire(struct work_struct *ignored)
6434 int rc;
6436 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
6437 return;
6439 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
6440 rc = fan_set_enable();
6441 if (rc < 0) {
6442 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
6443 "will try again later...\n", -rc);
6444 /* reschedule for later */
6445 fan_watchdog_reset();
6450 * SYSFS fan layout: hwmon compatible (device)
6452 * pwm*_enable:
6453 * 0: "disengaged" mode
6454 * 1: manual mode
6455 * 2: native EC "auto" mode (recommended, hardware default)
6457 * pwm*: set speed in manual mode, ignored otherwise.
6458 * 0 is level 0; 255 is level 7. Intermediate points done with linear
6459 * interpolation.
6461 * fan*_input: tachometer reading, RPM
6464 * SYSFS fan layout: extensions
6466 * fan_watchdog (driver):
6467 * fan watchdog interval in seconds, 0 disables (default), max 120
6470 /* sysfs fan pwm1_enable ----------------------------------------------- */
6471 static ssize_t fan_pwm1_enable_show(struct device *dev,
6472 struct device_attribute *attr,
6473 char *buf)
6475 int res, mode;
6476 u8 status;
6478 res = fan_get_status_safe(&status);
6479 if (res)
6480 return res;
6482 if (status & TP_EC_FAN_FULLSPEED) {
6483 mode = 0;
6484 } else if (status & TP_EC_FAN_AUTO) {
6485 mode = 2;
6486 } else
6487 mode = 1;
6489 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
6492 static ssize_t fan_pwm1_enable_store(struct device *dev,
6493 struct device_attribute *attr,
6494 const char *buf, size_t count)
6496 unsigned long t;
6497 int res, level;
6499 if (parse_strtoul(buf, 2, &t))
6500 return -EINVAL;
6502 switch (t) {
6503 case 0:
6504 level = TP_EC_FAN_FULLSPEED;
6505 break;
6506 case 1:
6507 level = TPACPI_FAN_LAST_LEVEL;
6508 break;
6509 case 2:
6510 level = TP_EC_FAN_AUTO;
6511 break;
6512 case 3:
6513 /* reserved for software-controlled auto mode */
6514 return -ENOSYS;
6515 default:
6516 return -EINVAL;
6519 res = fan_set_level_safe(level);
6520 if (res == -ENXIO)
6521 return -EINVAL;
6522 else if (res < 0)
6523 return res;
6525 fan_watchdog_reset();
6527 return count;
6530 static struct device_attribute dev_attr_fan_pwm1_enable =
6531 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
6532 fan_pwm1_enable_show, fan_pwm1_enable_store);
6534 /* sysfs fan pwm1 ------------------------------------------------------ */
6535 static ssize_t fan_pwm1_show(struct device *dev,
6536 struct device_attribute *attr,
6537 char *buf)
6539 int res;
6540 u8 status;
6542 res = fan_get_status_safe(&status);
6543 if (res)
6544 return res;
6546 if ((status &
6547 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
6548 status = fan_control_desired_level;
6550 if (status > 7)
6551 status = 7;
6553 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
6556 static ssize_t fan_pwm1_store(struct device *dev,
6557 struct device_attribute *attr,
6558 const char *buf, size_t count)
6560 unsigned long s;
6561 int rc;
6562 u8 status, newlevel;
6564 if (parse_strtoul(buf, 255, &s))
6565 return -EINVAL;
6567 /* scale down from 0-255 to 0-7 */
6568 newlevel = (s >> 5) & 0x07;
6570 if (mutex_lock_killable(&fan_mutex))
6571 return -ERESTARTSYS;
6573 rc = fan_get_status(&status);
6574 if (!rc && (status &
6575 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
6576 rc = fan_set_level(newlevel);
6577 if (rc == -ENXIO)
6578 rc = -EINVAL;
6579 else if (!rc) {
6580 fan_update_desired_level(newlevel);
6581 fan_watchdog_reset();
6585 mutex_unlock(&fan_mutex);
6586 return (rc)? rc : count;
6589 static struct device_attribute dev_attr_fan_pwm1 =
6590 __ATTR(pwm1, S_IWUSR | S_IRUGO,
6591 fan_pwm1_show, fan_pwm1_store);
6593 /* sysfs fan fan1_input ------------------------------------------------ */
6594 static ssize_t fan_fan1_input_show(struct device *dev,
6595 struct device_attribute *attr,
6596 char *buf)
6598 int res;
6599 unsigned int speed;
6601 res = fan_get_speed(&speed);
6602 if (res < 0)
6603 return res;
6605 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
6608 static struct device_attribute dev_attr_fan_fan1_input =
6609 __ATTR(fan1_input, S_IRUGO,
6610 fan_fan1_input_show, NULL);
6612 /* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
6613 static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
6614 char *buf)
6616 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
6619 static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
6620 const char *buf, size_t count)
6622 unsigned long t;
6624 if (parse_strtoul(buf, 120, &t))
6625 return -EINVAL;
6627 if (!fan_control_allowed)
6628 return -EPERM;
6630 fan_watchdog_maxinterval = t;
6631 fan_watchdog_reset();
6633 return count;
6636 static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
6637 fan_fan_watchdog_show, fan_fan_watchdog_store);
6639 /* --------------------------------------------------------------------- */
6640 static struct attribute *fan_attributes[] = {
6641 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
6642 &dev_attr_fan_fan1_input.attr,
6643 NULL
6646 static const struct attribute_group fan_attr_group = {
6647 .attrs = fan_attributes,
6650 static int __init fan_init(struct ibm_init_struct *iibm)
6652 int rc;
6654 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
6656 mutex_init(&fan_mutex);
6657 fan_status_access_mode = TPACPI_FAN_NONE;
6658 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6659 fan_control_commands = 0;
6660 fan_watchdog_maxinterval = 0;
6661 tp_features.fan_ctrl_status_undef = 0;
6662 fan_control_desired_level = 7;
6664 TPACPI_ACPIHANDLE_INIT(fans);
6665 TPACPI_ACPIHANDLE_INIT(gfan);
6666 TPACPI_ACPIHANDLE_INIT(sfan);
6668 if (gfan_handle) {
6669 /* 570, 600e/x, 770e, 770x */
6670 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
6671 } else {
6672 /* all other ThinkPads: note that even old-style
6673 * ThinkPad ECs supports the fan control register */
6674 if (likely(acpi_ec_read(fan_status_offset,
6675 &fan_control_initial_status))) {
6676 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
6677 fan_quirk1_detect();
6678 } else {
6679 printk(TPACPI_ERR
6680 "ThinkPad ACPI EC access misbehaving, "
6681 "fan status and control unavailable\n");
6682 return 1;
6686 if (sfan_handle) {
6687 /* 570, 770x-JL */
6688 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
6689 fan_control_commands |=
6690 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
6691 } else {
6692 if (!gfan_handle) {
6693 /* gfan without sfan means no fan control */
6694 /* all other models implement TP EC 0x2f control */
6696 if (fans_handle) {
6697 /* X31, X40, X41 */
6698 fan_control_access_mode =
6699 TPACPI_FAN_WR_ACPI_FANS;
6700 fan_control_commands |=
6701 TPACPI_FAN_CMD_SPEED |
6702 TPACPI_FAN_CMD_LEVEL |
6703 TPACPI_FAN_CMD_ENABLE;
6704 } else {
6705 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
6706 fan_control_commands |=
6707 TPACPI_FAN_CMD_LEVEL |
6708 TPACPI_FAN_CMD_ENABLE;
6713 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
6714 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
6715 fan_control_access_mode != TPACPI_FAN_WR_NONE),
6716 fan_status_access_mode, fan_control_access_mode);
6718 /* fan control master switch */
6719 if (!fan_control_allowed) {
6720 fan_control_access_mode = TPACPI_FAN_WR_NONE;
6721 fan_control_commands = 0;
6722 dbg_printk(TPACPI_DBG_INIT,
6723 "fan control features disabled by parameter\n");
6726 /* update fan_control_desired_level */
6727 if (fan_status_access_mode != TPACPI_FAN_NONE)
6728 fan_get_status_safe(NULL);
6730 if (fan_status_access_mode != TPACPI_FAN_NONE ||
6731 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
6732 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
6733 &fan_attr_group);
6734 if (rc < 0)
6735 return rc;
6737 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
6738 &driver_attr_fan_watchdog);
6739 if (rc < 0) {
6740 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
6741 &fan_attr_group);
6742 return rc;
6744 return 0;
6745 } else
6746 return 1;
6749 static void fan_exit(void)
6751 vdbg_printk(TPACPI_DBG_EXIT,
6752 "cancelling any pending fan watchdog tasks\n");
6754 /* FIXME: can we really do this unconditionally? */
6755 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
6756 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
6757 &driver_attr_fan_watchdog);
6759 cancel_delayed_work(&fan_watchdog_task);
6760 flush_workqueue(tpacpi_wq);
6763 static void fan_suspend(pm_message_t state)
6765 int rc;
6767 if (!fan_control_allowed)
6768 return;
6770 /* Store fan status in cache */
6771 fan_control_resume_level = 0;
6772 rc = fan_get_status_safe(&fan_control_resume_level);
6773 if (rc < 0)
6774 printk(TPACPI_NOTICE
6775 "failed to read fan level for later "
6776 "restore during resume: %d\n", rc);
6778 /* if it is undefined, don't attempt to restore it.
6779 * KEEP THIS LAST */
6780 if (tp_features.fan_ctrl_status_undef)
6781 fan_control_resume_level = 0;
6784 static void fan_resume(void)
6786 u8 current_level = 7;
6787 bool do_set = false;
6788 int rc;
6790 /* DSDT *always* updates status on resume */
6791 tp_features.fan_ctrl_status_undef = 0;
6793 if (!fan_control_allowed ||
6794 !fan_control_resume_level ||
6795 (fan_get_status_safe(&current_level) < 0))
6796 return;
6798 switch (fan_control_access_mode) {
6799 case TPACPI_FAN_WR_ACPI_SFAN:
6800 /* never decrease fan level */
6801 do_set = (fan_control_resume_level > current_level);
6802 break;
6803 case TPACPI_FAN_WR_ACPI_FANS:
6804 case TPACPI_FAN_WR_TPEC:
6805 /* never decrease fan level, scale is:
6806 * TP_EC_FAN_FULLSPEED > 7 >= TP_EC_FAN_AUTO
6808 * We expect the firmware to set either 7 or AUTO, but we
6809 * handle FULLSPEED out of paranoia.
6811 * So, we can safely only restore FULLSPEED or 7, anything
6812 * else could slow the fan. Restoring AUTO is useless, at
6813 * best that's exactly what the DSDT already set (it is the
6814 * slower it uses).
6816 * Always keep in mind that the DSDT *will* have set the
6817 * fans to what the vendor supposes is the best level. We
6818 * muck with it only to speed the fan up.
6820 if (fan_control_resume_level != 7 &&
6821 !(fan_control_resume_level & TP_EC_FAN_FULLSPEED))
6822 return;
6823 else
6824 do_set = !(current_level & TP_EC_FAN_FULLSPEED) &&
6825 (current_level != fan_control_resume_level);
6826 break;
6827 default:
6828 return;
6830 if (do_set) {
6831 printk(TPACPI_NOTICE
6832 "restoring fan level to 0x%02x\n",
6833 fan_control_resume_level);
6834 rc = fan_set_level_safe(fan_control_resume_level);
6835 if (rc < 0)
6836 printk(TPACPI_NOTICE
6837 "failed to restore fan level: %d\n", rc);
6841 static int fan_read(char *p)
6843 int len = 0;
6844 int rc;
6845 u8 status;
6846 unsigned int speed = 0;
6848 switch (fan_status_access_mode) {
6849 case TPACPI_FAN_RD_ACPI_GFAN:
6850 /* 570, 600e/x, 770e, 770x */
6851 rc = fan_get_status_safe(&status);
6852 if (rc < 0)
6853 return rc;
6855 len += sprintf(p + len, "status:\t\t%s\n"
6856 "level:\t\t%d\n",
6857 (status != 0) ? "enabled" : "disabled", status);
6858 break;
6860 case TPACPI_FAN_RD_TPEC:
6861 /* all except 570, 600e/x, 770e, 770x */
6862 rc = fan_get_status_safe(&status);
6863 if (rc < 0)
6864 return rc;
6866 len += sprintf(p + len, "status:\t\t%s\n",
6867 (status != 0) ? "enabled" : "disabled");
6869 rc = fan_get_speed(&speed);
6870 if (rc < 0)
6871 return rc;
6873 len += sprintf(p + len, "speed:\t\t%d\n", speed);
6875 if (status & TP_EC_FAN_FULLSPEED)
6876 /* Disengaged mode takes precedence */
6877 len += sprintf(p + len, "level:\t\tdisengaged\n");
6878 else if (status & TP_EC_FAN_AUTO)
6879 len += sprintf(p + len, "level:\t\tauto\n");
6880 else
6881 len += sprintf(p + len, "level:\t\t%d\n", status);
6882 break;
6884 case TPACPI_FAN_NONE:
6885 default:
6886 len += sprintf(p + len, "status:\t\tnot supported\n");
6889 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
6890 len += sprintf(p + len, "commands:\tlevel <level>");
6892 switch (fan_control_access_mode) {
6893 case TPACPI_FAN_WR_ACPI_SFAN:
6894 len += sprintf(p + len, " (<level> is 0-7)\n");
6895 break;
6897 default:
6898 len += sprintf(p + len, " (<level> is 0-7, "
6899 "auto, disengaged, full-speed)\n");
6900 break;
6904 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
6905 len += sprintf(p + len, "commands:\tenable, disable\n"
6906 "commands:\twatchdog <timeout> (<timeout> "
6907 "is 0 (off), 1-120 (seconds))\n");
6909 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
6910 len += sprintf(p + len, "commands:\tspeed <speed>"
6911 " (<speed> is 0-65535)\n");
6913 return len;
6916 static int fan_write_cmd_level(const char *cmd, int *rc)
6918 int level;
6920 if (strlencmp(cmd, "level auto") == 0)
6921 level = TP_EC_FAN_AUTO;
6922 else if ((strlencmp(cmd, "level disengaged") == 0) |
6923 (strlencmp(cmd, "level full-speed") == 0))
6924 level = TP_EC_FAN_FULLSPEED;
6925 else if (sscanf(cmd, "level %d", &level) != 1)
6926 return 0;
6928 *rc = fan_set_level_safe(level);
6929 if (*rc == -ENXIO)
6930 printk(TPACPI_ERR "level command accepted for unsupported "
6931 "access mode %d", fan_control_access_mode);
6933 return 1;
6936 static int fan_write_cmd_enable(const char *cmd, int *rc)
6938 if (strlencmp(cmd, "enable") != 0)
6939 return 0;
6941 *rc = fan_set_enable();
6942 if (*rc == -ENXIO)
6943 printk(TPACPI_ERR "enable command accepted for unsupported "
6944 "access mode %d", fan_control_access_mode);
6946 return 1;
6949 static int fan_write_cmd_disable(const char *cmd, int *rc)
6951 if (strlencmp(cmd, "disable") != 0)
6952 return 0;
6954 *rc = fan_set_disable();
6955 if (*rc == -ENXIO)
6956 printk(TPACPI_ERR "disable command accepted for unsupported "
6957 "access mode %d", fan_control_access_mode);
6959 return 1;
6962 static int fan_write_cmd_speed(const char *cmd, int *rc)
6964 int speed;
6966 /* TODO:
6967 * Support speed <low> <medium> <high> ? */
6969 if (sscanf(cmd, "speed %d", &speed) != 1)
6970 return 0;
6972 *rc = fan_set_speed(speed);
6973 if (*rc == -ENXIO)
6974 printk(TPACPI_ERR "speed command accepted for unsupported "
6975 "access mode %d", fan_control_access_mode);
6977 return 1;
6980 static int fan_write_cmd_watchdog(const char *cmd, int *rc)
6982 int interval;
6984 if (sscanf(cmd, "watchdog %d", &interval) != 1)
6985 return 0;
6987 if (interval < 0 || interval > 120)
6988 *rc = -EINVAL;
6989 else
6990 fan_watchdog_maxinterval = interval;
6992 return 1;
6995 static int fan_write(char *buf)
6997 char *cmd;
6998 int rc = 0;
7000 while (!rc && (cmd = next_cmd(&buf))) {
7001 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
7002 fan_write_cmd_level(cmd, &rc)) &&
7003 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
7004 (fan_write_cmd_enable(cmd, &rc) ||
7005 fan_write_cmd_disable(cmd, &rc) ||
7006 fan_write_cmd_watchdog(cmd, &rc))) &&
7007 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
7008 fan_write_cmd_speed(cmd, &rc))
7010 rc = -EINVAL;
7011 else if (!rc)
7012 fan_watchdog_reset();
7015 return rc;
7018 static struct ibm_struct fan_driver_data = {
7019 .name = "fan",
7020 .read = fan_read,
7021 .write = fan_write,
7022 .exit = fan_exit,
7023 .suspend = fan_suspend,
7024 .resume = fan_resume,
7027 /****************************************************************************
7028 ****************************************************************************
7030 * Infrastructure
7032 ****************************************************************************
7033 ****************************************************************************/
7035 /* sysfs name ---------------------------------------------------------- */
7036 static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
7037 struct device_attribute *attr,
7038 char *buf)
7040 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
7043 static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
7044 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
7046 /* --------------------------------------------------------------------- */
7048 /* /proc support */
7049 static struct proc_dir_entry *proc_dir;
7052 * Module and infrastructure proble, init and exit handling
7055 static int force_load;
7057 #ifdef CONFIG_THINKPAD_ACPI_DEBUG
7058 static const char * __init str_supported(int is_supported)
7060 static char text_unsupported[] __initdata = "not supported";
7062 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
7064 #endif /* CONFIG_THINKPAD_ACPI_DEBUG */
7066 static void ibm_exit(struct ibm_struct *ibm)
7068 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
7070 list_del_init(&ibm->all_drivers);
7072 if (ibm->flags.acpi_notify_installed) {
7073 dbg_printk(TPACPI_DBG_EXIT,
7074 "%s: acpi_remove_notify_handler\n", ibm->name);
7075 BUG_ON(!ibm->acpi);
7076 acpi_remove_notify_handler(*ibm->acpi->handle,
7077 ibm->acpi->type,
7078 dispatch_acpi_notify);
7079 ibm->flags.acpi_notify_installed = 0;
7080 ibm->flags.acpi_notify_installed = 0;
7083 if (ibm->flags.proc_created) {
7084 dbg_printk(TPACPI_DBG_EXIT,
7085 "%s: remove_proc_entry\n", ibm->name);
7086 remove_proc_entry(ibm->name, proc_dir);
7087 ibm->flags.proc_created = 0;
7090 if (ibm->flags.acpi_driver_registered) {
7091 dbg_printk(TPACPI_DBG_EXIT,
7092 "%s: acpi_bus_unregister_driver\n", ibm->name);
7093 BUG_ON(!ibm->acpi);
7094 acpi_bus_unregister_driver(ibm->acpi->driver);
7095 kfree(ibm->acpi->driver);
7096 ibm->acpi->driver = NULL;
7097 ibm->flags.acpi_driver_registered = 0;
7100 if (ibm->flags.init_called && ibm->exit) {
7101 ibm->exit();
7102 ibm->flags.init_called = 0;
7105 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
7108 static int __init ibm_init(struct ibm_init_struct *iibm)
7110 int ret;
7111 struct ibm_struct *ibm = iibm->data;
7112 struct proc_dir_entry *entry;
7114 BUG_ON(ibm == NULL);
7116 INIT_LIST_HEAD(&ibm->all_drivers);
7118 if (ibm->flags.experimental && !experimental)
7119 return 0;
7121 dbg_printk(TPACPI_DBG_INIT,
7122 "probing for %s\n", ibm->name);
7124 if (iibm->init) {
7125 ret = iibm->init(iibm);
7126 if (ret > 0)
7127 return 0; /* probe failed */
7128 if (ret)
7129 return ret;
7131 ibm->flags.init_called = 1;
7134 if (ibm->acpi) {
7135 if (ibm->acpi->hid) {
7136 ret = register_tpacpi_subdriver(ibm);
7137 if (ret)
7138 goto err_out;
7141 if (ibm->acpi->notify) {
7142 ret = setup_acpi_notify(ibm);
7143 if (ret == -ENODEV) {
7144 printk(TPACPI_NOTICE "disabling subdriver %s\n",
7145 ibm->name);
7146 ret = 0;
7147 goto err_out;
7149 if (ret < 0)
7150 goto err_out;
7154 dbg_printk(TPACPI_DBG_INIT,
7155 "%s installed\n", ibm->name);
7157 if (ibm->read) {
7158 entry = create_proc_entry(ibm->name,
7159 S_IFREG | S_IRUGO | S_IWUSR,
7160 proc_dir);
7161 if (!entry) {
7162 printk(TPACPI_ERR "unable to create proc entry %s\n",
7163 ibm->name);
7164 ret = -ENODEV;
7165 goto err_out;
7167 entry->owner = THIS_MODULE;
7168 entry->data = ibm;
7169 entry->read_proc = &dispatch_procfs_read;
7170 if (ibm->write)
7171 entry->write_proc = &dispatch_procfs_write;
7172 ibm->flags.proc_created = 1;
7175 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
7177 return 0;
7179 err_out:
7180 dbg_printk(TPACPI_DBG_INIT,
7181 "%s: at error exit path with result %d\n",
7182 ibm->name, ret);
7184 ibm_exit(ibm);
7185 return (ret < 0)? ret : 0;
7188 /* Probing */
7190 /* returns 0 - probe ok, or < 0 - probe error.
7191 * Probe ok doesn't mean thinkpad found.
7192 * On error, kfree() cleanup on tp->* is not performed, caller must do it */
7193 static int __must_check __init get_thinkpad_model_data(
7194 struct thinkpad_id_data *tp)
7196 const struct dmi_device *dev = NULL;
7197 char ec_fw_string[18];
7198 char const *s;
7200 if (!tp)
7201 return -EINVAL;
7203 memset(tp, 0, sizeof(*tp));
7205 if (dmi_name_in_vendors("IBM"))
7206 tp->vendor = PCI_VENDOR_ID_IBM;
7207 else if (dmi_name_in_vendors("LENOVO"))
7208 tp->vendor = PCI_VENDOR_ID_LENOVO;
7209 else
7210 return 0;
7212 s = dmi_get_system_info(DMI_BIOS_VERSION);
7213 tp->bios_version_str = kstrdup(s, GFP_KERNEL);
7214 if (s && !tp->bios_version_str)
7215 return -ENOMEM;
7216 if (!tp->bios_version_str)
7217 return 0;
7218 tp->bios_model = tp->bios_version_str[0]
7219 | (tp->bios_version_str[1] << 8);
7222 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
7223 * X32 or newer, all Z series; Some models must have an
7224 * up-to-date BIOS or they will not be detected.
7226 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7228 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
7229 if (sscanf(dev->name,
7230 "IBM ThinkPad Embedded Controller -[%17c",
7231 ec_fw_string) == 1) {
7232 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
7233 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
7235 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
7236 if (!tp->ec_version_str)
7237 return -ENOMEM;
7238 tp->ec_model = ec_fw_string[0]
7239 | (ec_fw_string[1] << 8);
7240 break;
7244 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
7245 if (s && !strnicmp(s, "ThinkPad", 8)) {
7246 tp->model_str = kstrdup(s, GFP_KERNEL);
7247 if (!tp->model_str)
7248 return -ENOMEM;
7251 s = dmi_get_system_info(DMI_PRODUCT_NAME);
7252 tp->nummodel_str = kstrdup(s, GFP_KERNEL);
7253 if (s && !tp->nummodel_str)
7254 return -ENOMEM;
7256 return 0;
7259 static int __init probe_for_thinkpad(void)
7261 int is_thinkpad;
7263 if (acpi_disabled)
7264 return -ENODEV;
7267 * Non-ancient models have better DMI tagging, but very old models
7268 * don't.
7270 is_thinkpad = (thinkpad_id.model_str != NULL);
7272 /* ec is required because many other handles are relative to it */
7273 TPACPI_ACPIHANDLE_INIT(ec);
7274 if (!ec_handle) {
7275 if (is_thinkpad)
7276 printk(TPACPI_ERR
7277 "Not yet supported ThinkPad detected!\n");
7278 return -ENODEV;
7282 * Risks a regression on very old machines, but reduces potential
7283 * false positives a damn great deal
7285 if (!is_thinkpad)
7286 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
7288 if (!is_thinkpad && !force_load)
7289 return -ENODEV;
7291 return 0;
7295 /* Module init, exit, parameters */
7297 static struct ibm_init_struct ibms_init[] __initdata = {
7299 .init = thinkpad_acpi_driver_init,
7300 .data = &thinkpad_acpi_driver_data,
7303 .init = hotkey_init,
7304 .data = &hotkey_driver_data,
7307 .init = bluetooth_init,
7308 .data = &bluetooth_driver_data,
7311 .init = wan_init,
7312 .data = &wan_driver_data,
7315 .init = uwb_init,
7316 .data = &uwb_driver_data,
7318 #ifdef CONFIG_THINKPAD_ACPI_VIDEO
7320 .init = video_init,
7321 .data = &video_driver_data,
7323 #endif
7325 .init = light_init,
7326 .data = &light_driver_data,
7328 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7330 .init = dock_init,
7331 .data = &dock_driver_data[0],
7334 .init = dock_init2,
7335 .data = &dock_driver_data[1],
7337 #endif
7338 #ifdef CONFIG_THINKPAD_ACPI_BAY
7340 .init = bay_init,
7341 .data = &bay_driver_data,
7343 #endif
7345 .init = cmos_init,
7346 .data = &cmos_driver_data,
7349 .init = led_init,
7350 .data = &led_driver_data,
7353 .init = beep_init,
7354 .data = &beep_driver_data,
7357 .init = thermal_init,
7358 .data = &thermal_driver_data,
7361 .data = &ecdump_driver_data,
7364 .init = brightness_init,
7365 .data = &brightness_driver_data,
7368 .data = &volume_driver_data,
7371 .init = fan_init,
7372 .data = &fan_driver_data,
7376 static int __init set_ibm_param(const char *val, struct kernel_param *kp)
7378 unsigned int i;
7379 struct ibm_struct *ibm;
7381 if (!kp || !kp->name || !val)
7382 return -EINVAL;
7384 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7385 ibm = ibms_init[i].data;
7386 WARN_ON(ibm == NULL);
7388 if (!ibm || !ibm->name)
7389 continue;
7391 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
7392 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
7393 return -ENOSPC;
7394 strcpy(ibms_init[i].param, val);
7395 strcat(ibms_init[i].param, ",");
7396 return 0;
7400 return -EINVAL;
7403 module_param(experimental, int, 0);
7404 MODULE_PARM_DESC(experimental,
7405 "Enables experimental features when non-zero");
7407 module_param_named(debug, dbg_level, uint, 0);
7408 MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
7410 module_param(force_load, bool, 0);
7411 MODULE_PARM_DESC(force_load,
7412 "Attempts to load the driver even on a "
7413 "mis-identified ThinkPad when true");
7415 module_param_named(fan_control, fan_control_allowed, bool, 0);
7416 MODULE_PARM_DESC(fan_control,
7417 "Enables setting fan parameters features when true");
7419 module_param_named(brightness_mode, brightness_mode, uint, 0);
7420 MODULE_PARM_DESC(brightness_mode,
7421 "Selects brightness control strategy: "
7422 "0=auto, 1=EC, 2=UCMS, 3=EC+NVRAM");
7424 module_param(brightness_enable, uint, 0);
7425 MODULE_PARM_DESC(brightness_enable,
7426 "Enables backlight control when 1, disables when 0");
7428 module_param(hotkey_report_mode, uint, 0);
7429 MODULE_PARM_DESC(hotkey_report_mode,
7430 "used for backwards compatibility with userspace, "
7431 "see documentation");
7433 #define TPACPI_PARAM(feature) \
7434 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
7435 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
7436 "at module load, see documentation")
7438 TPACPI_PARAM(hotkey);
7439 TPACPI_PARAM(bluetooth);
7440 TPACPI_PARAM(video);
7441 TPACPI_PARAM(light);
7442 #ifdef CONFIG_THINKPAD_ACPI_DOCK
7443 TPACPI_PARAM(dock);
7444 #endif
7445 #ifdef CONFIG_THINKPAD_ACPI_BAY
7446 TPACPI_PARAM(bay);
7447 #endif /* CONFIG_THINKPAD_ACPI_BAY */
7448 TPACPI_PARAM(cmos);
7449 TPACPI_PARAM(led);
7450 TPACPI_PARAM(beep);
7451 TPACPI_PARAM(ecdump);
7452 TPACPI_PARAM(brightness);
7453 TPACPI_PARAM(volume);
7454 TPACPI_PARAM(fan);
7456 #ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
7457 module_param(dbg_wlswemul, uint, 0);
7458 MODULE_PARM_DESC(dbg_wlswemul, "Enables WLSW emulation");
7459 module_param_named(wlsw_state, tpacpi_wlsw_emulstate, bool, 0);
7460 MODULE_PARM_DESC(wlsw_state,
7461 "Initial state of the emulated WLSW switch");
7463 module_param(dbg_bluetoothemul, uint, 0);
7464 MODULE_PARM_DESC(dbg_bluetoothemul, "Enables bluetooth switch emulation");
7465 module_param_named(bluetooth_state, tpacpi_bluetooth_emulstate, bool, 0);
7466 MODULE_PARM_DESC(bluetooth_state,
7467 "Initial state of the emulated bluetooth switch");
7469 module_param(dbg_wwanemul, uint, 0);
7470 MODULE_PARM_DESC(dbg_wwanemul, "Enables WWAN switch emulation");
7471 module_param_named(wwan_state, tpacpi_wwan_emulstate, bool, 0);
7472 MODULE_PARM_DESC(wwan_state,
7473 "Initial state of the emulated WWAN switch");
7475 module_param(dbg_uwbemul, uint, 0);
7476 MODULE_PARM_DESC(dbg_uwbemul, "Enables UWB switch emulation");
7477 module_param_named(uwb_state, tpacpi_uwb_emulstate, bool, 0);
7478 MODULE_PARM_DESC(uwb_state,
7479 "Initial state of the emulated UWB switch");
7480 #endif
7482 static void thinkpad_acpi_module_exit(void)
7484 struct ibm_struct *ibm, *itmp;
7486 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
7488 list_for_each_entry_safe_reverse(ibm, itmp,
7489 &tpacpi_all_drivers,
7490 all_drivers) {
7491 ibm_exit(ibm);
7494 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
7496 if (tpacpi_inputdev) {
7497 if (tp_features.input_device_registered)
7498 input_unregister_device(tpacpi_inputdev);
7499 else
7500 input_free_device(tpacpi_inputdev);
7503 if (tpacpi_hwmon)
7504 hwmon_device_unregister(tpacpi_hwmon);
7506 if (tp_features.sensors_pdev_attrs_registered)
7507 device_remove_file(&tpacpi_sensors_pdev->dev,
7508 &dev_attr_thinkpad_acpi_pdev_name);
7509 if (tpacpi_sensors_pdev)
7510 platform_device_unregister(tpacpi_sensors_pdev);
7511 if (tpacpi_pdev)
7512 platform_device_unregister(tpacpi_pdev);
7514 if (tp_features.sensors_pdrv_attrs_registered)
7515 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
7516 if (tp_features.platform_drv_attrs_registered)
7517 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
7519 if (tp_features.sensors_pdrv_registered)
7520 platform_driver_unregister(&tpacpi_hwmon_pdriver);
7522 if (tp_features.platform_drv_registered)
7523 platform_driver_unregister(&tpacpi_pdriver);
7525 if (proc_dir)
7526 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
7528 if (tpacpi_wq)
7529 destroy_workqueue(tpacpi_wq);
7531 kfree(thinkpad_id.bios_version_str);
7532 kfree(thinkpad_id.ec_version_str);
7533 kfree(thinkpad_id.model_str);
7537 static int __init thinkpad_acpi_module_init(void)
7539 int ret, i;
7541 tpacpi_lifecycle = TPACPI_LIFE_INIT;
7543 /* Parameter checking */
7544 if (hotkey_report_mode > 2)
7545 return -EINVAL;
7547 /* Driver-level probe */
7549 ret = get_thinkpad_model_data(&thinkpad_id);
7550 if (ret) {
7551 printk(TPACPI_ERR
7552 "unable to get DMI data: %d\n", ret);
7553 thinkpad_acpi_module_exit();
7554 return ret;
7556 ret = probe_for_thinkpad();
7557 if (ret) {
7558 thinkpad_acpi_module_exit();
7559 return ret;
7562 /* Driver initialization */
7564 TPACPI_ACPIHANDLE_INIT(ecrd);
7565 TPACPI_ACPIHANDLE_INIT(ecwr);
7567 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
7568 if (!tpacpi_wq) {
7569 thinkpad_acpi_module_exit();
7570 return -ENOMEM;
7573 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
7574 if (!proc_dir) {
7575 printk(TPACPI_ERR
7576 "unable to create proc dir " TPACPI_PROC_DIR);
7577 thinkpad_acpi_module_exit();
7578 return -ENODEV;
7580 proc_dir->owner = THIS_MODULE;
7582 ret = platform_driver_register(&tpacpi_pdriver);
7583 if (ret) {
7584 printk(TPACPI_ERR
7585 "unable to register main platform driver\n");
7586 thinkpad_acpi_module_exit();
7587 return ret;
7589 tp_features.platform_drv_registered = 1;
7591 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
7592 if (ret) {
7593 printk(TPACPI_ERR
7594 "unable to register hwmon platform driver\n");
7595 thinkpad_acpi_module_exit();
7596 return ret;
7598 tp_features.sensors_pdrv_registered = 1;
7600 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
7601 if (!ret) {
7602 tp_features.platform_drv_attrs_registered = 1;
7603 ret = tpacpi_create_driver_attributes(
7604 &tpacpi_hwmon_pdriver.driver);
7606 if (ret) {
7607 printk(TPACPI_ERR
7608 "unable to create sysfs driver attributes\n");
7609 thinkpad_acpi_module_exit();
7610 return ret;
7612 tp_features.sensors_pdrv_attrs_registered = 1;
7615 /* Device initialization */
7616 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
7617 NULL, 0);
7618 if (IS_ERR(tpacpi_pdev)) {
7619 ret = PTR_ERR(tpacpi_pdev);
7620 tpacpi_pdev = NULL;
7621 printk(TPACPI_ERR "unable to register platform device\n");
7622 thinkpad_acpi_module_exit();
7623 return ret;
7625 tpacpi_sensors_pdev = platform_device_register_simple(
7626 TPACPI_HWMON_DRVR_NAME,
7627 -1, NULL, 0);
7628 if (IS_ERR(tpacpi_sensors_pdev)) {
7629 ret = PTR_ERR(tpacpi_sensors_pdev);
7630 tpacpi_sensors_pdev = NULL;
7631 printk(TPACPI_ERR
7632 "unable to register hwmon platform device\n");
7633 thinkpad_acpi_module_exit();
7634 return ret;
7636 ret = device_create_file(&tpacpi_sensors_pdev->dev,
7637 &dev_attr_thinkpad_acpi_pdev_name);
7638 if (ret) {
7639 printk(TPACPI_ERR
7640 "unable to create sysfs hwmon device attributes\n");
7641 thinkpad_acpi_module_exit();
7642 return ret;
7644 tp_features.sensors_pdev_attrs_registered = 1;
7645 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
7646 if (IS_ERR(tpacpi_hwmon)) {
7647 ret = PTR_ERR(tpacpi_hwmon);
7648 tpacpi_hwmon = NULL;
7649 printk(TPACPI_ERR "unable to register hwmon device\n");
7650 thinkpad_acpi_module_exit();
7651 return ret;
7653 mutex_init(&tpacpi_inputdev_send_mutex);
7654 tpacpi_inputdev = input_allocate_device();
7655 if (!tpacpi_inputdev) {
7656 printk(TPACPI_ERR "unable to allocate input device\n");
7657 thinkpad_acpi_module_exit();
7658 return -ENOMEM;
7659 } else {
7660 /* Prepare input device, but don't register */
7661 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
7662 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
7663 tpacpi_inputdev->id.bustype = BUS_HOST;
7664 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
7665 thinkpad_id.vendor :
7666 PCI_VENDOR_ID_IBM;
7667 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
7668 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
7670 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
7671 ret = ibm_init(&ibms_init[i]);
7672 if (ret >= 0 && *ibms_init[i].param)
7673 ret = ibms_init[i].data->write(ibms_init[i].param);
7674 if (ret < 0) {
7675 thinkpad_acpi_module_exit();
7676 return ret;
7679 ret = input_register_device(tpacpi_inputdev);
7680 if (ret < 0) {
7681 printk(TPACPI_ERR "unable to register input device\n");
7682 thinkpad_acpi_module_exit();
7683 return ret;
7684 } else {
7685 tp_features.input_device_registered = 1;
7688 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
7689 return 0;
7692 MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
7695 * DMI matching for module autoloading
7697 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
7698 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
7700 * Only models listed in thinkwiki will be supported, so add yours
7701 * if it is not there yet.
7703 #define IBM_BIOS_MODULE_ALIAS(__type) \
7704 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW*")
7706 /* Non-ancient thinkpads */
7707 MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
7708 MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
7710 /* Ancient thinkpad BIOSes have to be identified by
7711 * BIOS type or model number, and there are far less
7712 * BIOS types than model numbers... */
7713 IBM_BIOS_MODULE_ALIAS("I[BDHIMNOTWVYZ]");
7714 IBM_BIOS_MODULE_ALIAS("1[0368A-GIKM-PST]");
7715 IBM_BIOS_MODULE_ALIAS("K[UX-Z]");
7717 MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
7718 MODULE_DESCRIPTION(TPACPI_DESC);
7719 MODULE_VERSION(TPACPI_VERSION);
7720 MODULE_LICENSE("GPL");
7722 module_init(thinkpad_acpi_module_init);
7723 module_exit(thinkpad_acpi_module_exit);