blob: d5c08c01fd11525c3ad1e1575ab432f62dc62ee6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03002 * thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005 * Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
Henrique de Moraes Holschuh6a2e2932008-01-08 13:02:56 -02006 * Copyright (C) 2006-2008 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
12 *
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.
17 *
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
Henrique de Moraes Holschuha62bc912007-03-23 17:33:58 -030020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * 02110-1301, USA.
Borislav Deianov78f81cc2005-08-17 00:00:00 -040022 */
23
Henrique de Moraes Holschuh1cee5cc2008-01-08 13:02:57 -020024#define TPACPI_VERSION "0.19"
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -020025#define TPACPI_SYSFS_VERSION 0x020200
Borislav Deianov78f81cc2005-08-17 00:00:00 -040026
27/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Changelog:
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020029 * 2007-10-20 changelog trimmed down
30 *
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -030031 * 2007-03-27 0.14 renamed to thinkpad_acpi and moved to
32 * drivers/misc.
Henrique de Moraes Holschuhf9ff43a2006-11-25 16:37:38 -020033 *
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.
Henrique de Moraes Holschuh837ca6d2007-03-23 17:33:54 -030037 *
Borislav Deianov78f81cc2005-08-17 00:00:00 -040038 * 2005-03-17 0.11 support for 600e, 770x
39 * thanks to Jamie Lentin <lentinj@dial.pipex.com>
Henrique de Moraes Holschuh4b45cc02008-01-08 13:02:46 -020040 *
41 * 2005-01-16 0.9 use MODULE_VERSION
Borislav Deianov78f81cc2005-08-17 00:00:00 -040042 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020050#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/init.h>
53#include <linux/types.h>
54#include <linux/string.h>
55#include <linux/list.h>
56#include <linux/mutex.h>
57#include <linux/kthread.h>
58#include <linux/freezer.h>
59#include <linux/delay.h>
60
61#include <linux/nvram.h>
62#include <linux/proc_fs.h>
63#include <linux/sysfs.h>
64#include <linux/backlight.h>
65#include <linux/fb.h>
66#include <linux/platform_device.h>
67#include <linux/hwmon.h>
68#include <linux/hwmon-sysfs.h>
69#include <linux/input.h>
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030070#include <linux/leds.h>
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020071#include <asm/uaccess.h>
72
73#include <linux/dmi.h>
74#include <linux/jiffies.h>
75#include <linux/workqueue.h>
76
77#include <acpi/acpi_drivers.h>
78#include <acpi/acnamesp.h>
79
80#include <linux/pci_ids.h>
81
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020082
83/* ThinkPad CMOS commands */
84#define TP_CMOS_VOLUME_DOWN 0
85#define TP_CMOS_VOLUME_UP 1
86#define TP_CMOS_VOLUME_MUTE 2
87#define TP_CMOS_BRIGHTNESS_UP 4
88#define TP_CMOS_BRIGHTNESS_DOWN 5
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -030089#define TP_CMOS_THINKLIGHT_ON 12
90#define TP_CMOS_THINKLIGHT_OFF 13
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -020091
92/* NVRAM Addresses */
93enum tp_nvram_addr {
94 TP_NVRAM_ADDR_HK2 = 0x57,
95 TP_NVRAM_ADDR_THINKLIGHT = 0x58,
96 TP_NVRAM_ADDR_VIDEO = 0x59,
97 TP_NVRAM_ADDR_BRIGHTNESS = 0x5e,
98 TP_NVRAM_ADDR_MIXER = 0x60,
99};
100
101/* NVRAM bit masks */
102enum {
103 TP_NVRAM_MASK_HKT_THINKPAD = 0x08,
104 TP_NVRAM_MASK_HKT_ZOOM = 0x20,
105 TP_NVRAM_MASK_HKT_DISPLAY = 0x40,
106 TP_NVRAM_MASK_HKT_HIBERNATE = 0x80,
107 TP_NVRAM_MASK_THINKLIGHT = 0x10,
108 TP_NVRAM_MASK_HKT_DISPEXPND = 0x30,
109 TP_NVRAM_MASK_HKT_BRIGHTNESS = 0x20,
110 TP_NVRAM_MASK_LEVEL_BRIGHTNESS = 0x0f,
111 TP_NVRAM_POS_LEVEL_BRIGHTNESS = 0,
112 TP_NVRAM_MASK_MUTE = 0x40,
113 TP_NVRAM_MASK_HKT_VOLUME = 0x80,
114 TP_NVRAM_MASK_LEVEL_VOLUME = 0x0f,
115 TP_NVRAM_POS_LEVEL_VOLUME = 0,
116};
117
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200118/* ACPI HIDs */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200119#define TPACPI_ACPI_HKEY_HID "IBM0068"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200120
121/* Input IDs */
122#define TPACPI_HKEY_INPUT_PRODUCT 0x5054 /* "TP" */
123#define TPACPI_HKEY_INPUT_VERSION 0x4101
124
125
126/****************************************************************************
127 * Main driver
128 */
129
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200130#define TPACPI_NAME "thinkpad"
131#define TPACPI_DESC "ThinkPad ACPI Extras"
132#define TPACPI_FILE TPACPI_NAME "_acpi"
133#define TPACPI_URL "http://ibm-acpi.sf.net/"
134#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200135
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200136#define TPACPI_PROC_DIR "ibm"
137#define TPACPI_ACPI_EVENT_PREFIX "ibm"
138#define TPACPI_DRVR_NAME TPACPI_FILE
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300139#define TPACPI_DRVR_SHORTNAME "tpacpi"
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200140#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200141
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300142#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300143#define TPACPI_WORKQUEUE_NAME "ktpacpid"
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -0300144
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200145#define TPACPI_MAX_ACPI_ARGS 3
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200146
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200147/* Debugging */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200148#define TPACPI_LOG TPACPI_FILE ": "
149#define TPACPI_ERR KERN_ERR TPACPI_LOG
150#define TPACPI_NOTICE KERN_NOTICE TPACPI_LOG
151#define TPACPI_INFO KERN_INFO TPACPI_LOG
152#define TPACPI_DEBUG KERN_DEBUG TPACPI_LOG
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200153
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200154#define TPACPI_DBG_ALL 0xffff
155#define TPACPI_DBG_ALL 0xffff
156#define TPACPI_DBG_INIT 0x0001
157#define TPACPI_DBG_EXIT 0x0002
158#define dbg_printk(a_dbg_level, format, arg...) \
159 do { if (dbg_level & a_dbg_level) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200160 printk(TPACPI_DEBUG "%s: " format, __func__ , ## arg); \
161 } while (0)
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200162#ifdef CONFIG_THINKPAD_ACPI_DEBUG
163#define vdbg_printk(a_dbg_level, format, arg...) \
164 dbg_printk(a_dbg_level, format, ## arg)
165static const char *str_supported(int is_supported);
166#else
167#define vdbg_printk(a_dbg_level, format, arg...)
168#endif
169
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200170#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
171#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
172#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200173
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200174
175/****************************************************************************
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200176 * Driver-wide structs and misc. variables
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200177 */
178
179struct ibm_struct;
180
181struct tp_acpi_drv_struct {
182 const struct acpi_device_id *hid;
183 struct acpi_driver *driver;
184
185 void (*notify) (struct ibm_struct *, u32);
186 acpi_handle *handle;
187 u32 type;
188 struct acpi_device *device;
189};
190
191struct ibm_struct {
192 char *name;
193
194 int (*read) (char *);
195 int (*write) (char *);
196 void (*exit) (void);
197 void (*resume) (void);
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200198 void (*suspend) (pm_message_t state);
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200199
200 struct list_head all_drivers;
201
202 struct tp_acpi_drv_struct *acpi;
203
204 struct {
205 u8 acpi_driver_registered:1;
206 u8 acpi_notify_installed:1;
207 u8 proc_created:1;
208 u8 init_called:1;
209 u8 experimental:1;
210 } flags;
211};
212
213struct ibm_init_struct {
214 char param[32];
215
216 int (*init) (struct ibm_init_struct *);
217 struct ibm_struct *data;
218};
219
220static struct {
221#ifdef CONFIG_THINKPAD_ACPI_BAY
222 u32 bay_status:1;
223 u32 bay_eject:1;
224 u32 bay_status2:1;
225 u32 bay_eject2:1;
226#endif
227 u32 bluetooth:1;
228 u32 hotkey:1;
229 u32 hotkey_mask:1;
230 u32 hotkey_wlsw:1;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -0200231 u32 hotkey_tablet:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200232 u32 light:1;
233 u32 light_status:1;
234 u32 bright_16levels:1;
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300235 u32 bright_acpimode:1;
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200236 u32 wan:1;
237 u32 fan_ctrl_status_undef:1;
238 u32 input_device_registered:1;
239 u32 platform_drv_registered:1;
240 u32 platform_drv_attrs_registered:1;
241 u32 sensors_pdrv_registered:1;
242 u32 sensors_pdrv_attrs_registered:1;
243 u32 sensors_pdev_attrs_registered:1;
244 u32 hotkey_poll_active:1;
245} tp_features;
246
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300247static struct {
248 u16 hotkey_mask_ff:1;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -0300249 u16 bright_cmos_ec_unsync:1;
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -0300250} tp_warned;
251
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200252struct thinkpad_id_data {
253 unsigned int vendor; /* ThinkPad vendor:
254 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */
255
256 char *bios_version_str; /* Something like 1ZET51WW (1.03z) */
257 char *ec_version_str; /* Something like 1ZHT51WW-1.04a */
258
259 u16 bios_model; /* Big Endian, TP-1Y = 0x5931, 0 = unknown */
260 u16 ec_model;
261
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -0300262 char *model_str; /* ThinkPad T43 */
263 char *nummodel_str; /* 9384A9C for a 9384-A9C model */
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200264};
Henrique de Moraes Holschuh0c780392008-01-08 13:02:43 -0200265static struct thinkpad_id_data thinkpad_id;
266
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300267static enum {
268 TPACPI_LIFE_INIT = 0,
269 TPACPI_LIFE_RUNNING,
270 TPACPI_LIFE_EXITING,
271} tpacpi_lifecycle;
272
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200273static int experimental;
274static u32 dbg_level;
275
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -0300276static struct workqueue_struct *tpacpi_wq;
277
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300278/* Special LED class that can defer work */
279struct tpacpi_led_classdev {
280 struct led_classdev led_classdev;
281 struct work_struct work;
282 enum led_brightness new_brightness;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -0300283 unsigned int led;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -0300284};
285
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300286/****************************************************************************
287 ****************************************************************************
288 *
289 * ACPI Helpers and device model
290 *
291 ****************************************************************************
292 ****************************************************************************/
293
294/*************************************************************************
295 * ACPI basic handles
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300298static acpi_handle root_handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200300#define TPACPI_HANDLE(object, parent, paths...) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 static acpi_handle object##_handle; \
302 static acpi_handle *object##_parent = &parent##_handle; \
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400303 static char *object##_path; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 static char *object##_paths[] = { paths }
305
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200306TPACPI_HANDLE(ec, root, "\\_SB.PCI0.ISA.EC0", /* 240, 240x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400307 "\\_SB.PCI.ISA.EC", /* 570 */
308 "\\_SB.PCI0.ISA0.EC0", /* 600e/x, 770e, 770x */
309 "\\_SB.PCI0.ISA.EC", /* A21e, A2xm/p, T20-22, X20-21 */
310 "\\_SB.PCI0.AD4S.EC0", /* i1400, R30 */
311 "\\_SB.PCI0.ICH3.EC0", /* R31 */
312 "\\_SB.PCI0.LPC.EC", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300313 );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200315TPACPI_HANDLE(ecrd, ec, "ECRD"); /* 570 */
316TPACPI_HANDLE(ecwr, ec, "ECWR"); /* 570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200318TPACPI_HANDLE(cmos, root, "\\UCMS", /* R50, R50e, R50p, R51, */
319 /* T4x, X31, X40 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400320 "\\CMOS", /* A3x, G4x, R32, T23, T30, X22-24, X30 */
321 "\\CMS", /* R40, R40e */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300322 ); /* all others */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400323
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200324TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY", /* 600e/x, 770e, 770x */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400325 "^HKEY", /* R30, R31 */
326 "HKEY", /* all others */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300327 ); /* 570 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400328
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -0200329TPACPI_HANDLE(vid, root, "\\_SB.PCI.AGP.VGA", /* 570 */
330 "\\_SB.PCI0.AGP0.VID0", /* 600e/x, 770x */
331 "\\_SB.PCI0.VID0", /* 770e */
332 "\\_SB.PCI0.VID", /* A21e, G4x, R50e, X30, X40 */
333 "\\_SB.PCI0.AGP.VID", /* all others */
334 ); /* R30, R31 */
335
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400336
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300337/*************************************************************************
338 * ACPI helpers
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -0200339 */
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341static int acpi_evalf(acpi_handle handle,
342 void *res, char *method, char *fmt, ...)
343{
344 char *fmt0 = fmt;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400345 struct acpi_object_list params;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200346 union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400347 struct acpi_buffer result, *resultp;
348 union acpi_object out_obj;
349 acpi_status status;
350 va_list ap;
351 char res_type;
352 int success;
353 int quiet;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (!*fmt) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200356 printk(TPACPI_ERR "acpi_evalf() called with empty format\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358 }
359
360 if (*fmt == 'q') {
361 quiet = 1;
362 fmt++;
363 } else
364 quiet = 0;
365
366 res_type = *(fmt++);
367
368 params.count = 0;
369 params.pointer = &in_objs[0];
370
371 va_start(ap, fmt);
372 while (*fmt) {
373 char c = *(fmt++);
374 switch (c) {
375 case 'd': /* int */
376 in_objs[params.count].integer.value = va_arg(ap, int);
377 in_objs[params.count++].type = ACPI_TYPE_INTEGER;
378 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400379 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200381 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 "with invalid format character '%c'\n", c);
383 return 0;
384 }
385 }
386 va_end(ap);
387
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400388 if (res_type != 'v') {
389 result.length = sizeof(out_obj);
390 result.pointer = &out_obj;
391 resultp = &result;
392 } else
393 resultp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400395 status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 switch (res_type) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400398 case 'd': /* int */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 if (res)
400 *(int *)res = out_obj.integer.value;
401 success = status == AE_OK && out_obj.type == ACPI_TYPE_INTEGER;
402 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400403 case 'v': /* void */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 success = status == AE_OK;
405 break;
Borislav Deianov78f81cc2005-08-17 00:00:00 -0400406 /* add more types as needed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 default:
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200408 printk(TPACPI_ERR "acpi_evalf() called "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 "with invalid format character '%c'\n", res_type);
410 return 0;
411 }
412
413 if (!success && !quiet)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200414 printk(TPACPI_ERR "acpi_evalf(%s, %s, ...) failed: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 method, fmt0, status);
416
417 return success;
418}
419
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200420static int acpi_ec_read(int i, u8 *p)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300421{
422 int v;
423
424 if (ecrd_handle) {
425 if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
426 return 0;
427 *p = v;
428 } else {
429 if (ec_read(i, p) < 0)
430 return 0;
431 }
432
433 return 1;
434}
435
436static int acpi_ec_write(int i, u8 v)
437{
438 if (ecwr_handle) {
439 if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
440 return 0;
441 } else {
442 if (ec_write(i, v) < 0)
443 return 0;
444 }
445
446 return 1;
447}
448
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200449#if defined(CONFIG_THINKPAD_ACPI_DOCK) || defined(CONFIG_THINKPAD_ACPI_BAY)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300450static int _sta(acpi_handle handle)
451{
452 int status;
453
454 if (!handle || !acpi_evalf(handle, &status, "_STA", "d"))
455 status = 0;
456
457 return status;
458}
Henrique de Moraes Holschuh013c40e2008-01-08 13:02:54 -0200459#endif
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300460
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -0300461static int issue_thinkpad_cmos_command(int cmos_cmd)
462{
463 if (!cmos_handle)
464 return -ENXIO;
465
466 if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
467 return -EIO;
468
469 return 0;
470}
471
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300472/*************************************************************************
473 * ACPI device model
474 */
475
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200476#define TPACPI_ACPIHANDLE_INIT(object) \
477 drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200478 object##_paths, ARRAY_SIZE(object##_paths), &object##_path)
479
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300480static void drv_acpi_handle_init(char *name,
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300481 acpi_handle *handle, acpi_handle parent,
482 char **paths, int num_paths, char **path)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300483{
484 int i;
485 acpi_status status;
486
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300487 vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
488 name);
489
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300490 for (i = 0; i < num_paths; i++) {
491 status = acpi_get_handle(parent, paths[i], handle);
492 if (ACPI_SUCCESS(status)) {
493 *path = paths[i];
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300494 dbg_printk(TPACPI_DBG_INIT,
495 "Found ACPI handle %s for %s\n",
496 *path, name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300497 return;
498 }
499 }
500
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300501 vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
502 name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300503 *handle = NULL;
504}
505
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300506static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300507{
508 struct ibm_struct *ibm = data;
509
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -0300510 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
511 return;
512
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300513 if (!ibm || !ibm->acpi || !ibm->acpi->notify)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300514 return;
515
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300516 ibm->acpi->notify(ibm, event);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300517}
518
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300519static int __init setup_acpi_notify(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300520{
521 acpi_status status;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300522 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300523
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300524 BUG_ON(!ibm->acpi);
525
526 if (!*ibm->acpi->handle)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300527 return 0;
528
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300529 vdbg_printk(TPACPI_DBG_INIT,
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300530 "setting up ACPI notify for %s\n", ibm->name);
531
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300532 rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
533 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200534 printk(TPACPI_ERR "acpi_bus_get_device(%s) failed: %d\n",
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300535 ibm->name, rc);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300536 return -ENODEV;
537 }
538
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300539 acpi_driver_data(ibm->acpi->device) = ibm;
540 sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200541 TPACPI_ACPI_EVENT_PREFIX,
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -0300542 ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300543
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300544 status = acpi_install_notify_handler(*ibm->acpi->handle,
545 ibm->acpi->type, dispatch_acpi_notify, ibm);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300546 if (ACPI_FAILURE(status)) {
547 if (status == AE_ALREADY_EXISTS) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200548 printk(TPACPI_NOTICE
549 "another device driver is already "
550 "handling %s events\n", ibm->name);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300551 } else {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200552 printk(TPACPI_ERR
553 "acpi_install_notify_handler(%s) failed: %d\n",
554 ibm->name, status);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300555 }
556 return -ENODEV;
557 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300558 ibm->flags.acpi_notify_installed = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300559 return 0;
560}
561
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300562static int __init tpacpi_device_add(struct acpi_device *device)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300563{
564 return 0;
565}
566
Henrique de Moraes Holschuh67001212007-04-21 11:08:25 -0300567static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300568{
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300569 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300570
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -0300571 dbg_printk(TPACPI_DBG_INIT,
572 "registering %s as an ACPI driver\n", ibm->name);
573
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300574 BUG_ON(!ibm->acpi);
575
576 ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
577 if (!ibm->acpi->driver) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200578 printk(TPACPI_ERR "kzalloc(ibm->driver) failed\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -0300579 return -ENOMEM;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300580 }
581
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200582 sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300583 ibm->acpi->driver->ids = ibm->acpi->hid;
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200584
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300585 ibm->acpi->driver->ops.add = &tpacpi_device_add;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300586
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300587 rc = acpi_bus_register_driver(ibm->acpi->driver);
588 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200589 printk(TPACPI_ERR "acpi_bus_register_driver(%s) failed: %d\n",
Thomas Renninger1ba90e32007-07-23 14:44:41 +0200590 ibm->name, rc);
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300591 kfree(ibm->acpi->driver);
592 ibm->acpi->driver = NULL;
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300593 } else if (!rc)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300594 ibm->flags.acpi_driver_registered = 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300595
Henrique de Moraes Holschuh5ae930e2007-04-27 22:00:14 -0300596 return rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300597}
598
599
600/****************************************************************************
601 ****************************************************************************
602 *
603 * Procfs Helpers
604 *
605 ****************************************************************************
606 ****************************************************************************/
607
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300608static int dispatch_procfs_read(char *page, char **start, off_t off,
609 int count, int *eof, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300610{
611 struct ibm_struct *ibm = data;
612 int len;
613
614 if (!ibm || !ibm->read)
615 return -EINVAL;
616
617 len = ibm->read(page);
618 if (len < 0)
619 return len;
620
621 if (len <= off + count)
622 *eof = 1;
623 *start = page + off;
624 len -= off;
625 if (len > count)
626 len = count;
627 if (len < 0)
628 len = 0;
629
630 return len;
631}
632
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300633static int dispatch_procfs_write(struct file *file,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200634 const char __user *userbuf,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -0300635 unsigned long count, void *data)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300636{
637 struct ibm_struct *ibm = data;
638 char *kernbuf;
639 int ret;
640
641 if (!ibm || !ibm->write)
642 return -EINVAL;
643
644 kernbuf = kmalloc(count + 2, GFP_KERNEL);
645 if (!kernbuf)
646 return -ENOMEM;
647
648 if (copy_from_user(kernbuf, userbuf, count)) {
649 kfree(kernbuf);
650 return -EFAULT;
651 }
652
653 kernbuf[count] = 0;
654 strcat(kernbuf, ",");
655 ret = ibm->write(kernbuf);
656 if (ret == 0)
657 ret = count;
658
659 kfree(kernbuf);
660
661 return ret;
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664static char *next_cmd(char **cmds)
665{
666 char *start = *cmds;
667 char *end;
668
669 while ((end = strchr(start, ',')) && end == start)
670 start = end + 1;
671
672 if (!end)
673 return NULL;
674
675 *end = 0;
676 *cmds = end + 1;
677 return start;
678}
679
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300680
681/****************************************************************************
682 ****************************************************************************
683 *
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300684 * Device model: input, hwmon and platform
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300685 *
686 ****************************************************************************
687 ****************************************************************************/
688
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -0300689static struct platform_device *tpacpi_pdev;
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300690static struct platform_device *tpacpi_sensors_pdev;
Tony Jones1beeffe2007-08-20 13:46:20 -0700691static struct device *tpacpi_hwmon;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -0300692static struct input_dev *tpacpi_inputdev;
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -0300693static struct mutex tpacpi_inputdev_send_mutex;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200694static LIST_HEAD(tpacpi_all_drivers);
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300695
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200696static int tpacpi_suspend_handler(struct platform_device *pdev,
697 pm_message_t state)
698{
699 struct ibm_struct *ibm, *itmp;
700
701 list_for_each_entry_safe(ibm, itmp,
702 &tpacpi_all_drivers,
703 all_drivers) {
704 if (ibm->suspend)
705 (ibm->suspend)(state);
706 }
707
708 return 0;
709}
710
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300711static int tpacpi_resume_handler(struct platform_device *pdev)
712{
713 struct ibm_struct *ibm, *itmp;
714
715 list_for_each_entry_safe(ibm, itmp,
716 &tpacpi_all_drivers,
717 all_drivers) {
718 if (ibm->resume)
719 (ibm->resume)();
720 }
721
722 return 0;
723}
724
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300725static struct platform_driver tpacpi_pdriver = {
726 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200727 .name = TPACPI_DRVR_NAME,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300728 .owner = THIS_MODULE,
729 },
Henrique de Moraes Holschuh083f1762008-01-08 13:02:50 -0200730 .suspend = tpacpi_suspend_handler,
Henrique de Moraes Holschuhe295e852007-07-18 23:45:37 -0300731 .resume = tpacpi_resume_handler,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300732};
733
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300734static struct platform_driver tpacpi_hwmon_pdriver = {
735 .driver = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -0200736 .name = TPACPI_HWMON_DRVR_NAME,
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -0300737 .owner = THIS_MODULE,
738 },
739};
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300740
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -0300741/*************************************************************************
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300742 * sysfs support helpers
743 */
744
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200745struct attribute_set {
746 unsigned int members, max_members;
747 struct attribute_group group;
748};
749
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300750struct attribute_set_obj {
751 struct attribute_set s;
752 struct attribute *a;
753} __attribute__((packed));
754
755static struct attribute_set *create_attr_set(unsigned int max_members,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200756 const char *name)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300757{
758 struct attribute_set_obj *sobj;
759
760 if (max_members == 0)
761 return NULL;
762
763 /* Allocates space for implicit NULL at the end too */
764 sobj = kzalloc(sizeof(struct attribute_set_obj) +
765 max_members * sizeof(struct attribute *),
766 GFP_KERNEL);
767 if (!sobj)
768 return NULL;
769 sobj->s.max_members = max_members;
770 sobj->s.group.attrs = &sobj->a;
771 sobj->s.group.name = name;
772
773 return &sobj->s;
774}
775
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200776#define destroy_attr_set(_set) \
777 kfree(_set);
778
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300779/* not multi-threaded safe, use it in a single thread per set */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200780static int add_to_attr_set(struct attribute_set *s, struct attribute *attr)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300781{
782 if (!s || !attr)
783 return -EINVAL;
784
785 if (s->members >= s->max_members)
786 return -ENOMEM;
787
788 s->group.attrs[s->members] = attr;
789 s->members++;
790
791 return 0;
792}
793
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200794static int add_many_to_attr_set(struct attribute_set *s,
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300795 struct attribute **attr,
796 unsigned int count)
797{
798 int i, res;
799
800 for (i = 0; i < count; i++) {
801 res = add_to_attr_set(s, attr[i]);
802 if (res)
803 return res;
804 }
805
806 return 0;
807}
808
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200809static void delete_attr_set(struct attribute_set *s, struct kobject *kobj)
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300810{
811 sysfs_remove_group(kobj, &s->group);
812 destroy_attr_set(s);
813}
814
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -0200815#define register_attr_set_with_sysfs(_attr_set, _kobj) \
816 sysfs_create_group(_kobj, &_attr_set->group)
817
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300818static int parse_strtoul(const char *buf,
819 unsigned long max, unsigned long *value)
820{
821 char *endp;
822
Henrique de Moraes Holschuh32afbf02007-10-08 10:12:56 -0300823 while (*buf && isspace(*buf))
824 buf++;
Henrique de Moraes Holschuh72523742007-04-24 11:48:14 -0300825 *value = simple_strtoul(buf, &endp, 0);
826 while (*endp && isspace(*endp))
827 endp++;
828 if (*endp || *value > max)
829 return -EINVAL;
830
831 return 0;
832}
833
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -0300834static int __init tpacpi_query_bcl_levels(acpi_handle handle)
835{
836 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
837 union acpi_object *obj;
838 int rc;
839
840 if (ACPI_SUCCESS(acpi_evaluate_object(handle, NULL, NULL, &buffer))) {
841 obj = (union acpi_object *)buffer.pointer;
842 if (!obj || (obj->type != ACPI_TYPE_PACKAGE)) {
843 printk(TPACPI_ERR "Unknown _BCL data, "
844 "please report this to %s\n", TPACPI_MAIL);
845 rc = 0;
846 } else {
847 rc = obj->package.count;
848 }
849 } else {
850 return 0;
851 }
852
853 kfree(buffer.pointer);
854 return rc;
855}
856
857static acpi_status __init tpacpi_acpi_walk_find_bcl(acpi_handle handle,
858 u32 lvl, void *context, void **rv)
859{
860 char name[ACPI_PATH_SEGMENT_LENGTH];
861 struct acpi_buffer buffer = { sizeof(name), &name };
862
863 if (ACPI_SUCCESS(acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer)) &&
864 !strncmp("_BCL", name, sizeof(name) - 1)) {
865 BUG_ON(!rv || !*rv);
866 **(int **)rv = tpacpi_query_bcl_levels(handle);
867 return AE_CTRL_TERMINATE;
868 } else {
869 return AE_OK;
870 }
871}
872
873/*
874 * Returns 0 (no ACPI _BCL or _BCL invalid), or size of brightness map
875 */
876static int __init tpacpi_check_std_acpi_brightness_support(void)
877{
878 int status;
879 int bcl_levels = 0;
880 void *bcl_ptr = &bcl_levels;
881
882 if (!vid_handle) {
883 TPACPI_ACPIHANDLE_INIT(vid);
884 }
885 if (!vid_handle)
886 return 0;
887
888 /*
889 * Search for a _BCL method, and execute it. This is safe on all
890 * ThinkPads, and as a side-effect, _BCL will place a Lenovo Vista
891 * BIOS in ACPI backlight control mode. We do NOT have to care
892 * about calling the _BCL method in an enabled video device, any
893 * will do for our purposes.
894 */
895
896 status = acpi_walk_namespace(ACPI_TYPE_METHOD, vid_handle, 3,
897 tpacpi_acpi_walk_find_bcl, NULL,
898 &bcl_ptr);
899
900 if (ACPI_SUCCESS(status) && bcl_levels > 2) {
901 tp_features.bright_acpimode = 1;
902 return (bcl_levels - 2);
903 }
904
905 return 0;
906}
907
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200908/*************************************************************************
909 * thinkpad-acpi driver attributes
910 */
911
912/* interface_version --------------------------------------------------- */
913static ssize_t tpacpi_driver_interface_version_show(
914 struct device_driver *drv,
915 char *buf)
916{
917 return snprintf(buf, PAGE_SIZE, "0x%08x\n", TPACPI_SYSFS_VERSION);
918}
919
920static DRIVER_ATTR(interface_version, S_IRUGO,
921 tpacpi_driver_interface_version_show, NULL);
922
923/* debug_level --------------------------------------------------------- */
924static ssize_t tpacpi_driver_debug_show(struct device_driver *drv,
925 char *buf)
926{
927 return snprintf(buf, PAGE_SIZE, "0x%04x\n", dbg_level);
928}
929
930static ssize_t tpacpi_driver_debug_store(struct device_driver *drv,
931 const char *buf, size_t count)
932{
933 unsigned long t;
934
935 if (parse_strtoul(buf, 0xffff, &t))
936 return -EINVAL;
937
938 dbg_level = t;
939
940 return count;
941}
942
943static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
944 tpacpi_driver_debug_show, tpacpi_driver_debug_store);
945
946/* version ------------------------------------------------------------- */
947static ssize_t tpacpi_driver_version_show(struct device_driver *drv,
948 char *buf)
949{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200950 return snprintf(buf, PAGE_SIZE, "%s v%s\n",
951 TPACPI_DESC, TPACPI_VERSION);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200952}
953
954static DRIVER_ATTR(version, S_IRUGO,
955 tpacpi_driver_version_show, NULL);
956
957/* --------------------------------------------------------------------- */
958
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200959static struct driver_attribute *tpacpi_driver_attributes[] = {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200960 &driver_attr_debug_level, &driver_attr_version,
961 &driver_attr_interface_version,
962};
963
964static int __init tpacpi_create_driver_attributes(struct device_driver *drv)
965{
966 int i, res;
967
968 i = 0;
969 res = 0;
970 while (!res && i < ARRAY_SIZE(tpacpi_driver_attributes)) {
971 res = driver_create_file(drv, tpacpi_driver_attributes[i]);
972 i++;
973 }
974
975 return res;
976}
977
978static void tpacpi_remove_driver_attributes(struct device_driver *drv)
979{
980 int i;
981
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -0200982 for (i = 0; i < ARRAY_SIZE(tpacpi_driver_attributes); i++)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -0200983 driver_remove_file(drv, tpacpi_driver_attributes[i]);
984}
985
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -0300986/****************************************************************************
987 ****************************************************************************
988 *
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300989 * Subdrivers
990 *
991 ****************************************************************************
992 ****************************************************************************/
993
994/*************************************************************************
Henrique de Moraes Holschuh142cfc92007-04-21 11:08:26 -0300995 * thinkpad-acpi init subdriver
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -0300996 */
997
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -0300998static int __init thinkpad_acpi_driver_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001000 printk(TPACPI_INFO "%s v%s\n", TPACPI_DESC, TPACPI_VERSION);
1001 printk(TPACPI_INFO "%s\n", TPACPI_URL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001003 printk(TPACPI_INFO "ThinkPad BIOS %s, EC %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001004 (thinkpad_id.bios_version_str) ?
1005 thinkpad_id.bios_version_str : "unknown",
1006 (thinkpad_id.ec_version_str) ?
1007 thinkpad_id.ec_version_str : "unknown");
1008
1009 if (thinkpad_id.vendor && thinkpad_id.model_str)
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001010 printk(TPACPI_INFO "%s %s, model %s\n",
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03001011 (thinkpad_id.vendor == PCI_VENDOR_ID_IBM) ?
1012 "IBM" : ((thinkpad_id.vendor ==
1013 PCI_VENDOR_ID_LENOVO) ?
1014 "Lenovo" : "Unknown vendor"),
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03001015 thinkpad_id.model_str,
1016 (thinkpad_id.nummodel_str) ?
1017 thinkpad_id.nummodel_str : "unknown");
Henrique de Moraes Holschuh3945ac32007-02-06 19:13:44 -02001018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020}
1021
Henrique de Moraes Holschuh643f12d2007-03-29 01:58:43 -03001022static int thinkpad_acpi_driver_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 int len = 0;
1025
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001026 len += sprintf(p + len, "driver:\t\t%s\n", TPACPI_DESC);
1027 len += sprintf(p + len, "version:\t%s\n", TPACPI_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 return len;
1030}
1031
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001032static struct ibm_struct thinkpad_acpi_driver_data = {
1033 .name = "driver",
1034 .read = thinkpad_acpi_driver_read,
1035};
1036
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001037/*************************************************************************
1038 * Hotkey subdriver
1039 */
1040
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001041enum { /* hot key scan codes (derived from ACPI DSDT) */
1042 TP_ACPI_HOTKEYSCAN_FNF1 = 0,
1043 TP_ACPI_HOTKEYSCAN_FNF2,
1044 TP_ACPI_HOTKEYSCAN_FNF3,
1045 TP_ACPI_HOTKEYSCAN_FNF4,
1046 TP_ACPI_HOTKEYSCAN_FNF5,
1047 TP_ACPI_HOTKEYSCAN_FNF6,
1048 TP_ACPI_HOTKEYSCAN_FNF7,
1049 TP_ACPI_HOTKEYSCAN_FNF8,
1050 TP_ACPI_HOTKEYSCAN_FNF9,
1051 TP_ACPI_HOTKEYSCAN_FNF10,
1052 TP_ACPI_HOTKEYSCAN_FNF11,
1053 TP_ACPI_HOTKEYSCAN_FNF12,
1054 TP_ACPI_HOTKEYSCAN_FNBACKSPACE,
1055 TP_ACPI_HOTKEYSCAN_FNINSERT,
1056 TP_ACPI_HOTKEYSCAN_FNDELETE,
1057 TP_ACPI_HOTKEYSCAN_FNHOME,
1058 TP_ACPI_HOTKEYSCAN_FNEND,
1059 TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1060 TP_ACPI_HOTKEYSCAN_FNPAGEDOWN,
1061 TP_ACPI_HOTKEYSCAN_FNSPACE,
1062 TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1063 TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1064 TP_ACPI_HOTKEYSCAN_MUTE,
1065 TP_ACPI_HOTKEYSCAN_THINKPAD,
1066};
1067
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001068enum { /* Keys available through NVRAM polling */
1069 TPACPI_HKEY_NVRAM_KNOWN_MASK = 0x00fb88c0U,
1070 TPACPI_HKEY_NVRAM_GOOD_MASK = 0x00fb8000U,
1071};
1072
1073enum { /* Positions of some of the keys in hotkey masks */
1074 TP_ACPI_HKEY_DISPSWTCH_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF7,
1075 TP_ACPI_HKEY_DISPXPAND_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF8,
1076 TP_ACPI_HKEY_HIBERNATE_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNF12,
1077 TP_ACPI_HKEY_BRGHTUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNHOME,
1078 TP_ACPI_HKEY_BRGHTDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNEND,
1079 TP_ACPI_HKEY_THNKLGHT_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNPAGEUP,
1080 TP_ACPI_HKEY_ZOOM_MASK = 1 << TP_ACPI_HOTKEYSCAN_FNSPACE,
1081 TP_ACPI_HKEY_VOLUP_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEUP,
1082 TP_ACPI_HKEY_VOLDWN_MASK = 1 << TP_ACPI_HOTKEYSCAN_VOLUMEDOWN,
1083 TP_ACPI_HKEY_MUTE_MASK = 1 << TP_ACPI_HOTKEYSCAN_MUTE,
1084 TP_ACPI_HKEY_THINKPAD_MASK = 1 << TP_ACPI_HOTKEYSCAN_THINKPAD,
1085};
1086
1087enum { /* NVRAM to ACPI HKEY group map */
1088 TP_NVRAM_HKEY_GROUP_HK2 = TP_ACPI_HKEY_THINKPAD_MASK |
1089 TP_ACPI_HKEY_ZOOM_MASK |
1090 TP_ACPI_HKEY_DISPSWTCH_MASK |
1091 TP_ACPI_HKEY_HIBERNATE_MASK,
1092 TP_NVRAM_HKEY_GROUP_BRIGHTNESS = TP_ACPI_HKEY_BRGHTUP_MASK |
1093 TP_ACPI_HKEY_BRGHTDWN_MASK,
1094 TP_NVRAM_HKEY_GROUP_VOLUME = TP_ACPI_HKEY_VOLUP_MASK |
1095 TP_ACPI_HKEY_VOLDWN_MASK |
1096 TP_ACPI_HKEY_MUTE_MASK,
1097};
1098
1099#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1100struct tp_nvram_state {
1101 u16 thinkpad_toggle:1;
1102 u16 zoom_toggle:1;
1103 u16 display_toggle:1;
1104 u16 thinklight_toggle:1;
1105 u16 hibernate_toggle:1;
1106 u16 displayexp_toggle:1;
1107 u16 display_state:1;
1108 u16 brightness_toggle:1;
1109 u16 volume_toggle:1;
1110 u16 mute:1;
1111
1112 u8 brightness_level;
1113 u8 volume_level;
1114};
1115
1116static struct task_struct *tpacpi_hotkey_task;
1117static u32 hotkey_source_mask; /* bit mask 0=ACPI,1=NVRAM */
1118static int hotkey_poll_freq = 10; /* Hz */
1119static struct mutex hotkey_thread_mutex;
1120static struct mutex hotkey_thread_data_mutex;
1121static unsigned int hotkey_config_change;
1122
1123#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1124
1125#define hotkey_source_mask 0U
1126
1127#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1128
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02001129static struct mutex hotkey_mutex;
1130
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001131static enum { /* Reasons for waking up */
1132 TP_ACPI_WAKEUP_NONE = 0, /* None or unknown */
1133 TP_ACPI_WAKEUP_BAYEJ, /* Bay ejection request */
1134 TP_ACPI_WAKEUP_UNDOCK, /* Undock request */
1135} hotkey_wakeup_reason;
1136
1137static int hotkey_autosleep_ack;
1138
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001139static int hotkey_orig_status;
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001140static u32 hotkey_orig_mask;
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001141static u32 hotkey_all_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001142static u32 hotkey_reserved_mask;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001143static u32 hotkey_mask;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03001144
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02001145static unsigned int hotkey_report_mode;
1146
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001147static u16 *hotkey_keycode_map;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04001148
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03001149static struct attribute_set *hotkey_dev_attributes;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001150
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001151#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1152#define HOTKEY_CONFIG_CRITICAL_START \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001153 do { \
1154 mutex_lock(&hotkey_thread_data_mutex); \
1155 hotkey_config_change++; \
1156 } while (0);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001157#define HOTKEY_CONFIG_CRITICAL_END \
1158 mutex_unlock(&hotkey_thread_data_mutex);
1159#else
1160#define HOTKEY_CONFIG_CRITICAL_START
1161#define HOTKEY_CONFIG_CRITICAL_END
1162#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1163
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001164/* HKEY.MHKG() return bits */
1165#define TP_HOTKEY_TABLET_MASK (1 << 3)
1166
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001167static int hotkey_get_wlsw(int *status)
1168{
1169 if (!acpi_evalf(hkey_handle, status, "WLSW", "d"))
1170 return -EIO;
1171 return 0;
1172}
1173
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001174static int hotkey_get_tablet_mode(int *status)
1175{
1176 int s;
1177
1178 if (!acpi_evalf(hkey_handle, &s, "MHKG", "d"))
1179 return -EIO;
1180
Henrique de Moraes Holschuhcee47f52008-03-04 14:29:21 -08001181 *status = ((s & TP_HOTKEY_TABLET_MASK) != 0);
1182 return 0;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001183}
1184
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001185/*
1186 * Call with hotkey_mutex held
1187 */
1188static int hotkey_mask_get(void)
1189{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001190 u32 m = 0;
1191
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001192 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001193 if (!acpi_evalf(hkey_handle, &m, "DHKN", "d"))
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001194 return -EIO;
1195 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001196 hotkey_mask = m | (hotkey_source_mask & hotkey_mask);
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001197
1198 return 0;
1199}
1200
1201/*
1202 * Call with hotkey_mutex held
1203 */
1204static int hotkey_mask_set(u32 mask)
1205{
1206 int i;
1207 int rc = 0;
1208
1209 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh92889022008-04-26 01:02:18 -03001210 if (!tp_warned.hotkey_mask_ff &&
1211 (mask == 0xffff || mask == 0xffffff ||
1212 mask == 0xffffffff)) {
1213 tp_warned.hotkey_mask_ff = 1;
1214 printk(TPACPI_NOTICE
1215 "setting the hotkey mask to 0x%08x is likely "
1216 "not the best way to go about it\n", mask);
1217 printk(TPACPI_NOTICE
1218 "please consider using the driver defaults, "
1219 "and refer to up-to-date thinkpad-acpi "
1220 "documentation\n");
1221 }
1222
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001223 HOTKEY_CONFIG_CRITICAL_START
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001224 for (i = 0; i < 32; i++) {
1225 u32 m = 1 << i;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001226 /* enable in firmware mask only keys not in NVRAM
1227 * mode, but enable the key in the cached hotkey_mask
1228 * regardless of mode, or the key will end up
1229 * disabled by hotkey_mask_get() */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001230 if (!acpi_evalf(hkey_handle,
1231 NULL, "MHKM", "vdd", i + 1,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001232 !!((mask & ~hotkey_source_mask) & m))) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001233 rc = -EIO;
1234 break;
1235 } else {
1236 hotkey_mask = (hotkey_mask & ~m) | (mask & m);
1237 }
1238 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001239 HOTKEY_CONFIG_CRITICAL_END
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001240
1241 /* hotkey_mask_get must be called unconditionally below */
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001242 if (!hotkey_mask_get() && !rc &&
1243 (hotkey_mask & ~hotkey_source_mask) !=
1244 (mask & ~hotkey_source_mask)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001245 printk(TPACPI_NOTICE
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001246 "requested hot key mask 0x%08x, but "
1247 "firmware forced it to 0x%08x\n",
1248 mask, hotkey_mask);
1249 }
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001250 } else {
1251#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1252 HOTKEY_CONFIG_CRITICAL_START
1253 hotkey_mask = mask & hotkey_source_mask;
1254 HOTKEY_CONFIG_CRITICAL_END
1255 hotkey_mask_get();
1256 if (hotkey_mask != mask) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02001257 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001258 "requested hot key mask 0x%08x, "
1259 "forced to 0x%08x (NVRAM poll mask is "
1260 "0x%08x): no firmware mask support\n",
1261 mask, hotkey_mask, hotkey_source_mask);
1262 }
1263#else
1264 hotkey_mask_get();
1265 rc = -ENXIO;
1266#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001267 }
1268
1269 return rc;
1270}
1271
1272static int hotkey_status_get(int *status)
1273{
1274 if (!acpi_evalf(hkey_handle, status, "DHKC", "d"))
1275 return -EIO;
1276
1277 return 0;
1278}
1279
1280static int hotkey_status_set(int status)
1281{
1282 if (!acpi_evalf(hkey_handle, NULL, "MHKC", "vd", status))
1283 return -EIO;
1284
1285 return 0;
1286}
1287
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001288static void tpacpi_input_send_radiosw(void)
1289{
1290 int wlsw;
1291
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001292 if (tp_features.hotkey_wlsw && !hotkey_get_wlsw(&wlsw)) {
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001293 mutex_lock(&tpacpi_inputdev_send_mutex);
1294
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001295 input_report_switch(tpacpi_inputdev,
1296 SW_RADIO, !!wlsw);
1297 input_sync(tpacpi_inputdev);
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001298
Henrique de Moraes Holschuhd147da72008-02-16 02:17:57 -02001299 mutex_unlock(&tpacpi_inputdev_send_mutex);
1300 }
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001301}
1302
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001303static void tpacpi_input_send_tabletsw(void)
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001304{
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001305 int state;
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001306
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001307 if (tp_features.hotkey_tablet &&
1308 !hotkey_get_tablet_mode(&state)) {
1309 mutex_lock(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001310
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001311 input_report_switch(tpacpi_inputdev,
1312 SW_TABLET_MODE, !!state);
1313 input_sync(tpacpi_inputdev);
1314
1315 mutex_unlock(&tpacpi_inputdev_send_mutex);
1316 }
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02001317}
1318
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02001319static void tpacpi_input_send_key(unsigned int scancode)
1320{
1321 unsigned int keycode;
1322
1323 keycode = hotkey_keycode_map[scancode];
1324
1325 if (keycode != KEY_RESERVED) {
1326 mutex_lock(&tpacpi_inputdev_send_mutex);
1327
1328 input_report_key(tpacpi_inputdev, keycode, 1);
1329 if (keycode == KEY_UNKNOWN)
1330 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1331 scancode);
1332 input_sync(tpacpi_inputdev);
1333
1334 input_report_key(tpacpi_inputdev, keycode, 0);
1335 if (keycode == KEY_UNKNOWN)
1336 input_event(tpacpi_inputdev, EV_MSC, MSC_SCAN,
1337 scancode);
1338 input_sync(tpacpi_inputdev);
1339
1340 mutex_unlock(&tpacpi_inputdev_send_mutex);
1341 }
1342}
1343
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001344#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1345static struct tp_acpi_drv_struct ibm_hotkey_acpidriver;
1346
1347static void tpacpi_hotkey_send_key(unsigned int scancode)
1348{
1349 tpacpi_input_send_key(scancode);
1350 if (hotkey_report_mode < 2) {
1351 acpi_bus_generate_proc_event(ibm_hotkey_acpidriver.device,
1352 0x80, 0x1001 + scancode);
1353 }
1354}
1355
1356static void hotkey_read_nvram(struct tp_nvram_state *n, u32 m)
1357{
1358 u8 d;
1359
1360 if (m & TP_NVRAM_HKEY_GROUP_HK2) {
1361 d = nvram_read_byte(TP_NVRAM_ADDR_HK2);
1362 n->thinkpad_toggle = !!(d & TP_NVRAM_MASK_HKT_THINKPAD);
1363 n->zoom_toggle = !!(d & TP_NVRAM_MASK_HKT_ZOOM);
1364 n->display_toggle = !!(d & TP_NVRAM_MASK_HKT_DISPLAY);
1365 n->hibernate_toggle = !!(d & TP_NVRAM_MASK_HKT_HIBERNATE);
1366 }
1367 if (m & TP_ACPI_HKEY_THNKLGHT_MASK) {
1368 d = nvram_read_byte(TP_NVRAM_ADDR_THINKLIGHT);
1369 n->thinklight_toggle = !!(d & TP_NVRAM_MASK_THINKLIGHT);
1370 }
1371 if (m & TP_ACPI_HKEY_DISPXPAND_MASK) {
1372 d = nvram_read_byte(TP_NVRAM_ADDR_VIDEO);
1373 n->displayexp_toggle =
1374 !!(d & TP_NVRAM_MASK_HKT_DISPEXPND);
1375 }
1376 if (m & TP_NVRAM_HKEY_GROUP_BRIGHTNESS) {
1377 d = nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS);
1378 n->brightness_level = (d & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
1379 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
1380 n->brightness_toggle =
1381 !!(d & TP_NVRAM_MASK_HKT_BRIGHTNESS);
1382 }
1383 if (m & TP_NVRAM_HKEY_GROUP_VOLUME) {
1384 d = nvram_read_byte(TP_NVRAM_ADDR_MIXER);
1385 n->volume_level = (d & TP_NVRAM_MASK_LEVEL_VOLUME)
1386 >> TP_NVRAM_POS_LEVEL_VOLUME;
1387 n->mute = !!(d & TP_NVRAM_MASK_MUTE);
1388 n->volume_toggle = !!(d & TP_NVRAM_MASK_HKT_VOLUME);
1389 }
1390}
1391
1392#define TPACPI_COMPARE_KEY(__scancode, __member) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001393 do { \
1394 if ((mask & (1 << __scancode)) && \
1395 oldn->__member != newn->__member) \
1396 tpacpi_hotkey_send_key(__scancode); \
1397 } while (0)
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001398
1399#define TPACPI_MAY_SEND_KEY(__scancode) \
1400 do { if (mask & (1 << __scancode)) \
1401 tpacpi_hotkey_send_key(__scancode); } while (0)
1402
1403static void hotkey_compare_and_issue_event(struct tp_nvram_state *oldn,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001404 struct tp_nvram_state *newn,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001405 u32 mask)
1406{
1407 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_THINKPAD, thinkpad_toggle);
1408 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNSPACE, zoom_toggle);
1409 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF7, display_toggle);
1410 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF12, hibernate_toggle);
1411
1412 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNPAGEUP, thinklight_toggle);
1413
1414 TPACPI_COMPARE_KEY(TP_ACPI_HOTKEYSCAN_FNF8, displayexp_toggle);
1415
1416 /* handle volume */
1417 if (oldn->volume_toggle != newn->volume_toggle) {
1418 if (oldn->mute != newn->mute) {
1419 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1420 }
1421 if (oldn->volume_level > newn->volume_level) {
1422 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1423 } else if (oldn->volume_level < newn->volume_level) {
1424 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1425 } else if (oldn->mute == newn->mute) {
1426 /* repeated key presses that didn't change state */
1427 if (newn->mute) {
1428 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_MUTE);
1429 } else if (newn->volume_level != 0) {
1430 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEUP);
1431 } else {
1432 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_VOLUMEDOWN);
1433 }
1434 }
1435 }
1436
1437 /* handle brightness */
1438 if (oldn->brightness_toggle != newn->brightness_toggle) {
1439 if (oldn->brightness_level < newn->brightness_level) {
1440 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1441 } else if (oldn->brightness_level > newn->brightness_level) {
1442 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1443 } else {
1444 /* repeated key presses that didn't change state */
1445 if (newn->brightness_level != 0) {
1446 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNHOME);
1447 } else {
1448 TPACPI_MAY_SEND_KEY(TP_ACPI_HOTKEYSCAN_FNEND);
1449 }
1450 }
1451 }
1452}
1453
1454#undef TPACPI_COMPARE_KEY
1455#undef TPACPI_MAY_SEND_KEY
1456
1457static int hotkey_kthread(void *data)
1458{
1459 struct tp_nvram_state s[2];
1460 u32 mask;
1461 unsigned int si, so;
1462 unsigned long t;
1463 unsigned int change_detector, must_reset;
1464
1465 mutex_lock(&hotkey_thread_mutex);
1466
1467 if (tpacpi_lifecycle == TPACPI_LIFE_EXITING)
1468 goto exit;
1469
1470 set_freezable();
1471
1472 so = 0;
1473 si = 1;
1474 t = 0;
1475
1476 /* Initial state for compares */
1477 mutex_lock(&hotkey_thread_data_mutex);
1478 change_detector = hotkey_config_change;
1479 mask = hotkey_source_mask & hotkey_mask;
1480 mutex_unlock(&hotkey_thread_data_mutex);
1481 hotkey_read_nvram(&s[so], mask);
1482
1483 while (!kthread_should_stop() && hotkey_poll_freq) {
1484 if (t == 0)
1485 t = 1000/hotkey_poll_freq;
1486 t = msleep_interruptible(t);
1487 if (unlikely(kthread_should_stop()))
1488 break;
1489 must_reset = try_to_freeze();
1490 if (t > 0 && !must_reset)
1491 continue;
1492
1493 mutex_lock(&hotkey_thread_data_mutex);
1494 if (must_reset || hotkey_config_change != change_detector) {
1495 /* forget old state on thaw or config change */
1496 si = so;
1497 t = 0;
1498 change_detector = hotkey_config_change;
1499 }
1500 mask = hotkey_source_mask & hotkey_mask;
1501 mutex_unlock(&hotkey_thread_data_mutex);
1502
1503 if (likely(mask)) {
1504 hotkey_read_nvram(&s[si], mask);
1505 if (likely(si != so)) {
1506 hotkey_compare_and_issue_event(&s[so], &s[si],
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001507 mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001508 }
1509 }
1510
1511 so = si;
1512 si ^= 1;
1513 }
1514
1515exit:
1516 mutex_unlock(&hotkey_thread_mutex);
1517 return 0;
1518}
1519
1520static void hotkey_poll_stop_sync(void)
1521{
1522 if (tpacpi_hotkey_task) {
1523 if (frozen(tpacpi_hotkey_task) ||
1524 freezing(tpacpi_hotkey_task))
1525 thaw_process(tpacpi_hotkey_task);
1526
1527 kthread_stop(tpacpi_hotkey_task);
1528 tpacpi_hotkey_task = NULL;
1529 mutex_lock(&hotkey_thread_mutex);
1530 /* at this point, the thread did exit */
1531 mutex_unlock(&hotkey_thread_mutex);
1532 }
1533}
1534
1535/* call with hotkey_mutex held */
1536static void hotkey_poll_setup(int may_warn)
1537{
1538 if ((hotkey_source_mask & hotkey_mask) != 0 &&
1539 hotkey_poll_freq > 0 &&
1540 (tpacpi_inputdev->users > 0 || hotkey_report_mode < 2)) {
1541 if (!tpacpi_hotkey_task) {
1542 tpacpi_hotkey_task = kthread_run(hotkey_kthread,
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03001543 NULL, TPACPI_NVRAM_KTHREAD_NAME);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001544 if (IS_ERR(tpacpi_hotkey_task)) {
1545 tpacpi_hotkey_task = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001546 printk(TPACPI_ERR
1547 "could not create kernel thread "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001548 "for hotkey polling\n");
1549 }
1550 }
1551 } else {
1552 hotkey_poll_stop_sync();
1553 if (may_warn &&
1554 hotkey_source_mask != 0 && hotkey_poll_freq == 0) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02001555 printk(TPACPI_NOTICE
1556 "hot keys 0x%08x require polling, "
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001557 "which is currently disabled\n",
1558 hotkey_source_mask);
1559 }
1560 }
1561}
1562
1563static void hotkey_poll_setup_safe(int may_warn)
1564{
1565 mutex_lock(&hotkey_mutex);
1566 hotkey_poll_setup(may_warn);
1567 mutex_unlock(&hotkey_mutex);
1568}
1569
Henrique de Moraes Holschuh1bc6b9c2008-02-16 02:17:52 -02001570#else /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1571
1572static void hotkey_poll_setup_safe(int __unused)
1573{
1574}
1575
1576#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1577
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001578static int hotkey_inputdev_open(struct input_dev *dev)
1579{
1580 switch (tpacpi_lifecycle) {
1581 case TPACPI_LIFE_INIT:
1582 /*
1583 * hotkey_init will call hotkey_poll_setup_safe
1584 * at the appropriate moment
1585 */
1586 return 0;
1587 case TPACPI_LIFE_EXITING:
1588 return -EBUSY;
1589 case TPACPI_LIFE_RUNNING:
1590 hotkey_poll_setup_safe(0);
1591 return 0;
1592 }
1593
1594 /* Should only happen if tpacpi_lifecycle is corrupt */
1595 BUG();
1596 return -EBUSY;
1597}
1598
1599static void hotkey_inputdev_close(struct input_dev *dev)
1600{
1601 /* disable hotkey polling when possible */
1602 if (tpacpi_lifecycle == TPACPI_LIFE_RUNNING)
1603 hotkey_poll_setup_safe(0);
1604}
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001605
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001606/* sysfs hotkey enable ------------------------------------------------- */
1607static ssize_t hotkey_enable_show(struct device *dev,
1608 struct device_attribute *attr,
1609 char *buf)
1610{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001611 int res, status;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001612
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001613 res = hotkey_status_get(&status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001614 if (res)
1615 return res;
1616
1617 return snprintf(buf, PAGE_SIZE, "%d\n", status);
1618}
1619
1620static ssize_t hotkey_enable_store(struct device *dev,
1621 struct device_attribute *attr,
1622 const char *buf, size_t count)
1623{
1624 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001625 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001626
1627 if (parse_strtoul(buf, 1, &t))
1628 return -EINVAL;
1629
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001630 res = hotkey_status_set(t);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001631
1632 return (res) ? res : count;
1633}
1634
1635static struct device_attribute dev_attr_hotkey_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001636 __ATTR(hotkey_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001637 hotkey_enable_show, hotkey_enable_store);
1638
1639/* sysfs hotkey mask --------------------------------------------------- */
1640static ssize_t hotkey_mask_show(struct device *dev,
1641 struct device_attribute *attr,
1642 char *buf)
1643{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001644 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001645
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001646 if (mutex_lock_interruptible(&hotkey_mutex))
1647 return -ERESTARTSYS;
1648 res = hotkey_mask_get();
1649 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001650
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001651 return (res)?
1652 res : snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001653}
1654
1655static ssize_t hotkey_mask_store(struct device *dev,
1656 struct device_attribute *attr,
1657 const char *buf, size_t count)
1658{
1659 unsigned long t;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001660 int res;
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001661
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001662 if (parse_strtoul(buf, 0xffffffffUL, &t))
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001663 return -EINVAL;
1664
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001665 if (mutex_lock_interruptible(&hotkey_mutex))
1666 return -ERESTARTSYS;
1667
1668 res = hotkey_mask_set(t);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001669
1670#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1671 hotkey_poll_setup(1);
1672#endif
1673
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02001674 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001675
1676 return (res) ? res : count;
1677}
1678
1679static struct device_attribute dev_attr_hotkey_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001680 __ATTR(hotkey_mask, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001681 hotkey_mask_show, hotkey_mask_store);
1682
1683/* sysfs hotkey bios_enabled ------------------------------------------- */
1684static ssize_t hotkey_bios_enabled_show(struct device *dev,
1685 struct device_attribute *attr,
1686 char *buf)
1687{
1688 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_orig_status);
1689}
1690
1691static struct device_attribute dev_attr_hotkey_bios_enabled =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001692 __ATTR(hotkey_bios_enabled, S_IRUGO, hotkey_bios_enabled_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001693
1694/* sysfs hotkey bios_mask ---------------------------------------------- */
1695static ssize_t hotkey_bios_mask_show(struct device *dev,
1696 struct device_attribute *attr,
1697 char *buf)
1698{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03001699 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_orig_mask);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001700}
1701
1702static struct device_attribute dev_attr_hotkey_bios_mask =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03001703 __ATTR(hotkey_bios_mask, S_IRUGO, hotkey_bios_mask_show, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001704
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001705/* sysfs hotkey all_mask ----------------------------------------------- */
1706static ssize_t hotkey_all_mask_show(struct device *dev,
1707 struct device_attribute *attr,
1708 char *buf)
1709{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001710 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
1711 hotkey_all_mask | hotkey_source_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001712}
1713
1714static struct device_attribute dev_attr_hotkey_all_mask =
1715 __ATTR(hotkey_all_mask, S_IRUGO, hotkey_all_mask_show, NULL);
1716
1717/* sysfs hotkey recommended_mask --------------------------------------- */
1718static ssize_t hotkey_recommended_mask_show(struct device *dev,
1719 struct device_attribute *attr,
1720 char *buf)
1721{
1722 return snprintf(buf, PAGE_SIZE, "0x%08x\n",
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001723 (hotkey_all_mask | hotkey_source_mask)
1724 & ~hotkey_reserved_mask);
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001725}
1726
1727static struct device_attribute dev_attr_hotkey_recommended_mask =
1728 __ATTR(hotkey_recommended_mask, S_IRUGO,
1729 hotkey_recommended_mask_show, NULL);
1730
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001731#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1732
1733/* sysfs hotkey hotkey_source_mask ------------------------------------- */
1734static ssize_t hotkey_source_mask_show(struct device *dev,
1735 struct device_attribute *attr,
1736 char *buf)
1737{
1738 return snprintf(buf, PAGE_SIZE, "0x%08x\n", hotkey_source_mask);
1739}
1740
1741static ssize_t hotkey_source_mask_store(struct device *dev,
1742 struct device_attribute *attr,
1743 const char *buf, size_t count)
1744{
1745 unsigned long t;
1746
1747 if (parse_strtoul(buf, 0xffffffffUL, &t) ||
1748 ((t & ~TPACPI_HKEY_NVRAM_KNOWN_MASK) != 0))
1749 return -EINVAL;
1750
1751 if (mutex_lock_interruptible(&hotkey_mutex))
1752 return -ERESTARTSYS;
1753
1754 HOTKEY_CONFIG_CRITICAL_START
1755 hotkey_source_mask = t;
1756 HOTKEY_CONFIG_CRITICAL_END
1757
1758 hotkey_poll_setup(1);
1759
1760 mutex_unlock(&hotkey_mutex);
1761
1762 return count;
1763}
1764
1765static struct device_attribute dev_attr_hotkey_source_mask =
1766 __ATTR(hotkey_source_mask, S_IWUSR | S_IRUGO,
1767 hotkey_source_mask_show, hotkey_source_mask_store);
1768
1769/* sysfs hotkey hotkey_poll_freq --------------------------------------- */
1770static ssize_t hotkey_poll_freq_show(struct device *dev,
1771 struct device_attribute *attr,
1772 char *buf)
1773{
1774 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_poll_freq);
1775}
1776
1777static ssize_t hotkey_poll_freq_store(struct device *dev,
1778 struct device_attribute *attr,
1779 const char *buf, size_t count)
1780{
1781 unsigned long t;
1782
1783 if (parse_strtoul(buf, 25, &t))
1784 return -EINVAL;
1785
1786 if (mutex_lock_interruptible(&hotkey_mutex))
1787 return -ERESTARTSYS;
1788
1789 hotkey_poll_freq = t;
1790
1791 hotkey_poll_setup(1);
1792 mutex_unlock(&hotkey_mutex);
1793
1794 return count;
1795}
1796
1797static struct device_attribute dev_attr_hotkey_poll_freq =
1798 __ATTR(hotkey_poll_freq, S_IWUSR | S_IRUGO,
1799 hotkey_poll_freq_show, hotkey_poll_freq_store);
1800
1801#endif /* CONFIG_THINKPAD_ACPI_HOTKEY_POLL */
1802
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001803/* sysfs hotkey radio_sw (pollable) ------------------------------------ */
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03001804static ssize_t hotkey_radio_sw_show(struct device *dev,
1805 struct device_attribute *attr,
1806 char *buf)
1807{
1808 int res, s;
1809 res = hotkey_get_wlsw(&s);
1810 if (res < 0)
1811 return res;
1812
1813 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1814}
1815
1816static struct device_attribute dev_attr_hotkey_radio_sw =
1817 __ATTR(hotkey_radio_sw, S_IRUGO, hotkey_radio_sw_show, NULL);
1818
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001819static void hotkey_radio_sw_notify_change(void)
1820{
1821 if (tp_features.hotkey_wlsw)
1822 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1823 "hotkey_radio_sw");
1824}
1825
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02001826/* sysfs hotkey tablet mode (pollable) --------------------------------- */
1827static ssize_t hotkey_tablet_mode_show(struct device *dev,
1828 struct device_attribute *attr,
1829 char *buf)
1830{
1831 int res, s;
1832 res = hotkey_get_tablet_mode(&s);
1833 if (res < 0)
1834 return res;
1835
1836 return snprintf(buf, PAGE_SIZE, "%d\n", !!s);
1837}
1838
1839static struct device_attribute dev_attr_hotkey_tablet_mode =
1840 __ATTR(hotkey_tablet_mode, S_IRUGO, hotkey_tablet_mode_show, NULL);
1841
1842static void hotkey_tablet_mode_notify_change(void)
1843{
1844 if (tp_features.hotkey_tablet)
1845 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1846 "hotkey_tablet_mode");
1847}
1848
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001849/* sysfs hotkey report_mode -------------------------------------------- */
1850static ssize_t hotkey_report_mode_show(struct device *dev,
1851 struct device_attribute *attr,
1852 char *buf)
1853{
1854 return snprintf(buf, PAGE_SIZE, "%d\n",
1855 (hotkey_report_mode != 0) ? hotkey_report_mode : 1);
1856}
1857
1858static struct device_attribute dev_attr_hotkey_report_mode =
1859 __ATTR(hotkey_report_mode, S_IRUGO, hotkey_report_mode_show, NULL);
1860
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001861/* sysfs wakeup reason (pollable) -------------------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001862static ssize_t hotkey_wakeup_reason_show(struct device *dev,
1863 struct device_attribute *attr,
1864 char *buf)
1865{
1866 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_wakeup_reason);
1867}
1868
1869static struct device_attribute dev_attr_hotkey_wakeup_reason =
1870 __ATTR(wakeup_reason, S_IRUGO, hotkey_wakeup_reason_show, NULL);
1871
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001872static void hotkey_wakeup_reason_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001873{
1874 if (tp_features.hotkey_mask)
1875 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1876 "wakeup_reason");
1877}
1878
1879/* sysfs wakeup hotunplug_complete (pollable) -------------------------- */
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001880static ssize_t hotkey_wakeup_hotunplug_complete_show(struct device *dev,
1881 struct device_attribute *attr,
1882 char *buf)
1883{
1884 return snprintf(buf, PAGE_SIZE, "%d\n", hotkey_autosleep_ack);
1885}
1886
1887static struct device_attribute dev_attr_hotkey_wakeup_hotunplug_complete =
1888 __ATTR(wakeup_hotunplug_complete, S_IRUGO,
1889 hotkey_wakeup_hotunplug_complete_show, NULL);
1890
Adrian Bunk1d5a2b52008-02-13 23:30:06 +02001891static void hotkey_wakeup_hotunplug_complete_notify_change(void)
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02001892{
1893 if (tp_features.hotkey_mask)
1894 sysfs_notify(&tpacpi_pdev->dev.kobj, NULL,
1895 "wakeup_hotunplug_complete");
1896}
1897
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001898/* --------------------------------------------------------------------- */
1899
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001900static struct attribute *hotkey_attributes[] __initdata = {
1901 &dev_attr_hotkey_enable.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001902 &dev_attr_hotkey_bios_enabled.attr,
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001903 &dev_attr_hotkey_report_mode.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001904#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1905 &dev_attr_hotkey_mask.attr,
1906 &dev_attr_hotkey_all_mask.attr,
1907 &dev_attr_hotkey_recommended_mask.attr,
1908 &dev_attr_hotkey_source_mask.attr,
1909 &dev_attr_hotkey_poll_freq.attr,
1910#endif
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03001911};
1912
1913static struct attribute *hotkey_mask_attributes[] __initdata = {
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001914 &dev_attr_hotkey_bios_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001915#ifndef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
1916 &dev_attr_hotkey_mask.attr,
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03001917 &dev_attr_hotkey_all_mask.attr,
1918 &dev_attr_hotkey_recommended_mask.attr,
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02001919#endif
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02001920 &dev_attr_hotkey_wakeup_reason.attr,
1921 &dev_attr_hotkey_wakeup_hotunplug_complete.attr,
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03001922};
1923
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03001924static int __init hotkey_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03001925{
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001926 /* Requirements for changing the default keymaps:
1927 *
1928 * 1. Many of the keys are mapped to KEY_RESERVED for very
1929 * good reasons. Do not change them unless you have deep
1930 * knowledge on the IBM and Lenovo ThinkPad firmware for
1931 * the various ThinkPad models. The driver behaves
1932 * differently for KEY_RESERVED: such keys have their
1933 * hot key mask *unset* in mask_recommended, and also
1934 * in the initial hot key mask programmed into the
1935 * firmware at driver load time, which means the firm-
1936 * ware may react very differently if you change them to
1937 * something else;
1938 *
1939 * 2. You must be subscribed to the linux-thinkpad and
1940 * ibm-acpi-devel mailing lists, and you should read the
1941 * list archives since 2007 if you want to change the
1942 * keymaps. This requirement exists so that you will
1943 * know the past history of problems with the thinkpad-
1944 * acpi driver keymaps, and also that you will be
1945 * listening to any bug reports;
1946 *
1947 * 3. Do not send thinkpad-acpi specific patches directly to
1948 * for merging, *ever*. Send them to the linux-acpi
1949 * mailinglist for comments. Merging is to be done only
1950 * through acpi-test and the ACPI maintainer.
1951 *
1952 * If the above is too much to ask, don't change the keymap.
1953 * Ask the thinkpad-acpi maintainer to do it, instead.
1954 */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001955 static u16 ibm_keycode_map[] __initdata = {
1956 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1957 KEY_FN_F1, KEY_FN_F2, KEY_COFFEE, KEY_SLEEP,
1958 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1959 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001960
1961 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001962 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1963 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
1964 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001965
1966 /* brightness: firmware always reacts to them, unless
1967 * X.org did some tricks in the radeon BIOS scratch
1968 * registers of *some* models */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001969 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001970 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001971
1972 /* Thinklight: firmware always react to it */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001973 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001974
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001975 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
1976 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001977
1978 /* Volume: firmware always react to it and reprograms
1979 * the built-in *extra* mixer. Never map it to control
1980 * another mixer by default. */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02001981 KEY_RESERVED, /* 0x14: VOLUME UP */
1982 KEY_RESERVED, /* 0x15: VOLUME DOWN */
1983 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001984
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001985 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001986
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001987 /* (assignments unknown, please report if found) */
1988 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1989 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
1990 };
1991 static u16 lenovo_keycode_map[] __initdata = {
1992 /* Scan Codes 0x00 to 0x0B: ACPI HKEY FN+F1..F12 */
1993 KEY_FN_F1, KEY_COFFEE, KEY_BATTERY, KEY_SLEEP,
1994 KEY_WLAN, KEY_FN_F6, KEY_SWITCHVIDEOMODE, KEY_FN_F8,
1995 KEY_FN_F9, KEY_FN_F10, KEY_FN_F11, KEY_SUSPEND,
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02001996
1997 /* Scan codes 0x0C to 0x1F: Other ACPI HKEY hot keys */
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03001998 KEY_UNKNOWN, /* 0x0C: FN+BACKSPACE */
1999 KEY_UNKNOWN, /* 0x0D: FN+INSERT */
2000 KEY_UNKNOWN, /* 0x0E: FN+DELETE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002001
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002002 /* These either have to go through ACPI video, or
2003 * act like in the IBM ThinkPads, so don't ever
2004 * enable them by default */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002005 KEY_RESERVED, /* 0x0F: FN+HOME (brightness up) */
Henrique de Moraes Holschuh56a185b2007-12-13 12:14:09 -02002006 KEY_RESERVED, /* 0x10: FN+END (brightness down) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002007
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002008 KEY_RESERVED, /* 0x11: FN+PGUP (thinklight toggle) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002009
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002010 KEY_UNKNOWN, /* 0x12: FN+PGDOWN */
2011 KEY_ZOOM, /* 0x13: FN+SPACE (zoom) */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002012
2013 /* Volume: z60/z61, T60 (BIOS version?): firmware always
2014 * react to it and reprograms the built-in *extra* mixer.
2015 * Never map it to control another mixer by default.
2016 *
2017 * T60?, T61, R60?, R61: firmware and EC tries to send
2018 * these over the regular keyboard, so these are no-ops,
2019 * but there are still weird bugs re. MUTE, so do not
2020 * change unless you get test reports from all Lenovo
2021 * models. May cause the BIOS to interfere with the
2022 * HDA mixer.
2023 */
Henrique de Moraes Holschuhe927c082007-10-30 17:46:19 -02002024 KEY_RESERVED, /* 0x14: VOLUME UP */
2025 KEY_RESERVED, /* 0x15: VOLUME DOWN */
2026 KEY_RESERVED, /* 0x16: MUTE */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002027
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002028 KEY_VENDOR, /* 0x17: Thinkpad/AccessIBM/Lenovo */
Henrique de Moraes Holschuh0f089142008-01-08 13:02:38 -02002029
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002030 /* (assignments unknown, please report if found) */
2031 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2032 KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
2033 };
2034
2035#define TPACPI_HOTKEY_MAP_LEN ARRAY_SIZE(ibm_keycode_map)
2036#define TPACPI_HOTKEY_MAP_SIZE sizeof(ibm_keycode_map)
2037#define TPACPI_HOTKEY_MAP_TYPESIZE sizeof(ibm_keycode_map[0])
2038
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002039 int res, i;
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002040 int status;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002041 int hkeyv;
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002042
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002043 vdbg_printk(TPACPI_DBG_INIT, "initializing hotkey subdriver\n");
2044
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002045 BUG_ON(!tpacpi_inputdev);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002046 BUG_ON(tpacpi_inputdev->open != NULL ||
2047 tpacpi_inputdev->close != NULL);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002048
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002049 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002050 mutex_init(&hotkey_mutex);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002051
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002052#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2053 mutex_init(&hotkey_thread_mutex);
2054 mutex_init(&hotkey_thread_data_mutex);
2055#endif
2056
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002057 /* hotkey not supported on 570 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002058 tp_features.hotkey = hkey_handle != NULL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002059
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002060 vdbg_printk(TPACPI_DBG_INIT, "hotkeys are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002061 str_supported(tp_features.hotkey));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002062
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002063 if (tp_features.hotkey) {
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002064 hotkey_dev_attributes = create_attr_set(13, NULL);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002065 if (!hotkey_dev_attributes)
2066 return -ENOMEM;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002067 res = add_many_to_attr_set(hotkey_dev_attributes,
2068 hotkey_attributes,
2069 ARRAY_SIZE(hotkey_attributes));
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002070 if (res)
2071 return res;
2072
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002073 /* mask not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002074 A30, R30, R31, T20-22, X20-21, X22-24. Detected by checking
2075 for HKEY interface version 0x100 */
2076 if (acpi_evalf(hkey_handle, &hkeyv, "MHKV", "qd")) {
2077 if ((hkeyv >> 8) != 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002078 printk(TPACPI_ERR "unknown version of the "
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002079 "HKEY interface: 0x%x\n", hkeyv);
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002080 printk(TPACPI_ERR "please report this to %s\n",
2081 TPACPI_MAIL);
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002082 } else {
2083 /*
2084 * MHKV 0x100 in A31, R40, R40e,
2085 * T4x, X31, and later
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002086 */
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002087 tp_features.hotkey_mask = 1;
2088 }
2089 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002090
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002091 vdbg_printk(TPACPI_DBG_INIT, "hotkey masks are %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002092 str_supported(tp_features.hotkey_mask));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002093
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002094 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002095 if (!acpi_evalf(hkey_handle, &hotkey_all_mask,
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002096 "MHKA", "qd")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002097 printk(TPACPI_ERR
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002098 "missing MHKA handler, "
2099 "please report this to %s\n",
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002100 TPACPI_MAIL);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002101 /* FN+F12, FN+F4, FN+F3 */
2102 hotkey_all_mask = 0x080cU;
Henrique de Moraes Holschuh1b6521d2007-09-23 11:39:03 -03002103 }
Henrique de Moraes Holschuh9b010de2007-07-18 23:45:30 -03002104 }
2105
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002106 /* hotkey_source_mask *must* be zero for
2107 * the first hotkey_mask_get */
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002108 res = hotkey_status_get(&hotkey_orig_status);
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002109 if (!res && tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002110 res = hotkey_mask_get();
2111 hotkey_orig_mask = hotkey_mask;
2112 if (!res) {
2113 res = add_many_to_attr_set(
2114 hotkey_dev_attributes,
2115 hotkey_mask_attributes,
2116 ARRAY_SIZE(hotkey_mask_attributes));
2117 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002118 }
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002119
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002120#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2121 if (tp_features.hotkey_mask) {
2122 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK
2123 & ~hotkey_all_mask;
2124 } else {
2125 hotkey_source_mask = TPACPI_HKEY_NVRAM_GOOD_MASK;
2126 }
2127
2128 vdbg_printk(TPACPI_DBG_INIT,
2129 "hotkey source mask 0x%08x, polling freq %d\n",
2130 hotkey_source_mask, hotkey_poll_freq);
2131#endif
2132
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002133 /* Not all thinkpads have a hardware radio switch */
2134 if (!res && acpi_evalf(hkey_handle, &status, "WLSW", "qd")) {
2135 tp_features.hotkey_wlsw = 1;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002136 printk(TPACPI_INFO
Henrique de Moraes Holschuh74941a62007-07-18 23:45:31 -03002137 "radio switch found; radios are %s\n",
2138 enabled(status, 0));
2139 res = add_to_attr_set(hotkey_dev_attributes,
2140 &dev_attr_hotkey_radio_sw.attr);
2141 }
2142
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002143 /* For X41t, X60t, X61t Tablets... */
2144 if (!res && acpi_evalf(hkey_handle, &status, "MHKG", "qd")) {
2145 tp_features.hotkey_tablet = 1;
2146 printk(TPACPI_INFO
2147 "possible tablet mode switch found; "
2148 "ThinkPad in %s mode\n",
2149 (status & TP_HOTKEY_TABLET_MASK)?
2150 "tablet" : "laptop");
2151 res = add_to_attr_set(hotkey_dev_attributes,
2152 &dev_attr_hotkey_tablet_mode.attr);
2153 }
2154
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002155 if (!res)
2156 res = register_attr_set_with_sysfs(
2157 hotkey_dev_attributes,
2158 &tpacpi_pdev->dev.kobj);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002159 if (res)
2160 return res;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002161
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002162 /* Set up key map */
2163
2164 hotkey_keycode_map = kmalloc(TPACPI_HOTKEY_MAP_SIZE,
2165 GFP_KERNEL);
2166 if (!hotkey_keycode_map) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002167 printk(TPACPI_ERR
2168 "failed to allocate memory for key map\n");
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002169 return -ENOMEM;
2170 }
2171
2172 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
2173 dbg_printk(TPACPI_DBG_INIT,
2174 "using Lenovo default hot key map\n");
2175 memcpy(hotkey_keycode_map, &lenovo_keycode_map,
2176 TPACPI_HOTKEY_MAP_SIZE);
2177 } else {
2178 dbg_printk(TPACPI_DBG_INIT,
2179 "using IBM default hot key map\n");
2180 memcpy(hotkey_keycode_map, &ibm_keycode_map,
2181 TPACPI_HOTKEY_MAP_SIZE);
2182 }
2183
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002184 set_bit(EV_KEY, tpacpi_inputdev->evbit);
2185 set_bit(EV_MSC, tpacpi_inputdev->evbit);
2186 set_bit(MSC_SCAN, tpacpi_inputdev->mscbit);
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03002187 tpacpi_inputdev->keycodesize = TPACPI_HOTKEY_MAP_TYPESIZE;
2188 tpacpi_inputdev->keycodemax = TPACPI_HOTKEY_MAP_LEN;
2189 tpacpi_inputdev->keycode = hotkey_keycode_map;
2190 for (i = 0; i < TPACPI_HOTKEY_MAP_LEN; i++) {
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002191 if (hotkey_keycode_map[i] != KEY_RESERVED) {
2192 set_bit(hotkey_keycode_map[i],
2193 tpacpi_inputdev->keybit);
2194 } else {
2195 if (i < sizeof(hotkey_reserved_mask)*8)
2196 hotkey_reserved_mask |= 1 << i;
2197 }
2198 }
2199
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002200 if (tp_features.hotkey_wlsw) {
2201 set_bit(EV_SW, tpacpi_inputdev->evbit);
2202 set_bit(SW_RADIO, tpacpi_inputdev->swbit);
2203 }
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002204 if (tp_features.hotkey_tablet) {
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002205 set_bit(EV_SW, tpacpi_inputdev->evbit);
2206 set_bit(SW_TABLET_MODE, tpacpi_inputdev->swbit);
2207 }
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002208
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03002209 /* Do not issue duplicate brightness change events to
2210 * userspace */
2211 if (!tp_features.bright_acpimode)
2212 /* update bright_acpimode... */
2213 tpacpi_check_std_acpi_brightness_support();
2214
2215 if (tp_features.bright_acpimode) {
2216 printk(TPACPI_INFO
2217 "This ThinkPad has standard ACPI backlight "
2218 "brightness control, supported by the ACPI "
2219 "video driver\n");
2220 printk(TPACPI_NOTICE
2221 "Disabling thinkpad-acpi brightness events "
2222 "by default...\n");
2223
2224 /* The hotkey_reserved_mask change below is not
2225 * necessary while the keys are at KEY_RESERVED in the
2226 * default map, but better safe than sorry, leave it
2227 * here as a marker of what we have to do, especially
2228 * when we finally become able to set this at runtime
2229 * on response to X.org requests */
2230 hotkey_reserved_mask |=
2231 (1 << TP_ACPI_HOTKEYSCAN_FNHOME)
2232 | (1 << TP_ACPI_HOTKEYSCAN_FNEND);
2233 }
2234
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002235 dbg_printk(TPACPI_DBG_INIT,
2236 "enabling hot key handling\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002237 res = hotkey_status_set(1);
2238 if (res)
2239 return res;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002240 res = hotkey_mask_set(((hotkey_all_mask | hotkey_source_mask)
2241 & ~hotkey_reserved_mask)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002242 | hotkey_orig_mask);
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002243 if (res < 0 && res != -ENXIO)
Henrique de Moraes Holschuh1a343762007-07-18 23:45:36 -03002244 return res;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002245
2246 dbg_printk(TPACPI_DBG_INIT,
2247 "legacy hot key reporting over procfs %s\n",
2248 (hotkey_report_mode < 2) ?
2249 "enabled" : "disabled");
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002250
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002251 tpacpi_inputdev->open = &hotkey_inputdev_open;
2252 tpacpi_inputdev->close = &hotkey_inputdev_close;
2253
2254 hotkey_poll_setup_safe(1);
Henrique de Moraes Holschuh75266962008-02-16 02:17:53 -02002255 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002256 tpacpi_input_send_tabletsw();
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002257 }
2258
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002259 return (tp_features.hotkey)? 0 : 1;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002260}
2261
2262static void hotkey_exit(void)
2263{
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002264#ifdef CONFIG_THINKPAD_ACPI_HOTKEY_POLL
2265 hotkey_poll_stop_sync();
2266#endif
2267
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002268 if (tp_features.hotkey) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002269 dbg_printk(TPACPI_DBG_EXIT,
2270 "restoring original hot key mask\n");
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002271 /* no short-circuit boolean operator below! */
2272 if ((hotkey_mask_set(hotkey_orig_mask) |
2273 hotkey_status_set(hotkey_orig_status)) != 0)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002274 printk(TPACPI_ERR
2275 "failed to restore hot key mask "
2276 "to BIOS defaults\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002277 }
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002278
2279 if (hotkey_dev_attributes) {
2280 delete_attr_set(hotkey_dev_attributes, &tpacpi_pdev->dev.kobj);
2281 hotkey_dev_attributes = NULL;
2282 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002283}
2284
2285static void hotkey_notify(struct ibm_struct *ibm, u32 event)
2286{
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002287 u32 hkey;
Henrique de Moraes Holschuhb7c8c202008-01-08 13:02:40 -02002288 unsigned int scancode;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002289 int send_acpi_ev;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002290 int ignore_acpi_ev;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002291 int unk_ev;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002292
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002293 if (event != 0x80) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002294 printk(TPACPI_ERR
2295 "unknown HKEY notification event %d\n", event);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002296 /* forward it to userspace, maybe it knows how to handle it */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002297 acpi_bus_generate_netlink_event(
2298 ibm->acpi->device->pnp.device_class,
2299 ibm->acpi->device->dev.bus_id,
2300 event, 0);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002301 return;
2302 }
2303
2304 while (1) {
2305 if (!acpi_evalf(hkey_handle, &hkey, "MHKP", "d")) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002306 printk(TPACPI_ERR "failed to retrieve HKEY event\n");
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002307 return;
2308 }
2309
2310 if (hkey == 0) {
2311 /* queue empty */
2312 return;
2313 }
2314
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002315 send_acpi_ev = 1;
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002316 ignore_acpi_ev = 0;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002317 unk_ev = 0;
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002318
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002319 switch (hkey >> 12) {
2320 case 1:
2321 /* 0x1000-0x1FFF: key presses */
2322 scancode = hkey & 0xfff;
2323 if (scancode > 0 && scancode < 0x21) {
2324 scancode--;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002325 if (!(hotkey_source_mask & (1 << scancode))) {
2326 tpacpi_input_send_key(scancode);
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002327 send_acpi_ev = 0;
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002328 } else {
2329 ignore_acpi_ev = 1;
2330 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002331 } else {
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002332 unk_ev = 1;
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002333 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002334 break;
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002335 case 2:
2336 /* Wakeup reason */
2337 switch (hkey) {
2338 case 0x2304: /* suspend, undock */
2339 case 0x2404: /* hibernation, undock */
2340 hotkey_wakeup_reason = TP_ACPI_WAKEUP_UNDOCK;
2341 ignore_acpi_ev = 1;
2342 break;
2343 case 0x2305: /* suspend, bay eject */
2344 case 0x2405: /* hibernation, bay eject */
2345 hotkey_wakeup_reason = TP_ACPI_WAKEUP_BAYEJ;
2346 ignore_acpi_ev = 1;
2347 break;
2348 default:
2349 unk_ev = 1;
2350 }
2351 if (hotkey_wakeup_reason != TP_ACPI_WAKEUP_NONE) {
2352 printk(TPACPI_INFO
2353 "woke up due to a hot-unplug "
2354 "request...\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002355 hotkey_wakeup_reason_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002356 }
2357 break;
2358 case 3:
2359 /* bay-related wakeups */
2360 if (hkey == 0x3003) {
2361 hotkey_autosleep_ack = 1;
2362 printk(TPACPI_INFO
2363 "bay ejected\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002364 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002365 } else {
2366 unk_ev = 1;
2367 }
2368 break;
2369 case 4:
2370 /* dock-related wakeups */
2371 if (hkey == 0x4003) {
2372 hotkey_autosleep_ack = 1;
2373 printk(TPACPI_INFO
2374 "undocked\n");
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002375 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002376 } else {
2377 unk_ev = 1;
2378 }
2379 break;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002380 case 5:
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002381 /* 0x5000-0x5FFF: human interface helpers */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002382 switch (hkey) {
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002383 case 0x5010: /* Lenovo new BIOS: brightness changed */
Henrique de Moraes Holschuhd1edb2b2008-01-08 13:02:53 -02002384 case 0x500b: /* X61t: tablet pen inserted into bay */
2385 case 0x500c: /* X61t: tablet pen removed from bay */
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002386 break;
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002387 case 0x5009: /* X41t-X61t: swivel up (tablet mode) */
2388 case 0x500a: /* X41t-X61t: swivel down (normal mode) */
2389 tpacpi_input_send_tabletsw();
2390 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuhb3ec6f92008-02-16 02:17:55 -02002391 send_acpi_ev = 0;
2392 break;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002393 case 0x5001:
2394 case 0x5002:
2395 /* LID switch events. Do not propagate */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002396 ignore_acpi_ev = 1;
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002397 break;
2398 default:
2399 unk_ev = 1;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002400 }
2401 break;
2402 case 7:
2403 /* 0x7000-0x7FFF: misc */
2404 if (tp_features.hotkey_wlsw && hkey == 0x7000) {
2405 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002406 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002407 send_acpi_ev = 0;
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002408 break;
2409 }
2410 /* fallthrough to default */
2411 default:
Henrique de Moraes Holschuh3b64b512008-01-08 13:02:51 -02002412 unk_ev = 1;
2413 }
2414 if (unk_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002415 printk(TPACPI_NOTICE
2416 "unhandled HKEY event 0x%04x\n", hkey);
Henrique de Moraes Holschuh6a38abb2007-07-18 23:45:35 -03002417 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002418
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002419 /* Legacy events */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002420 if (!ignore_acpi_ev &&
2421 (send_acpi_ev || hotkey_report_mode < 2)) {
2422 acpi_bus_generate_proc_event(ibm->acpi->device,
2423 event, hkey);
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002424 }
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03002425
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002426 /* netlink events */
Henrique de Moraes Holschuh3e5ce912007-09-23 11:39:05 -03002427 if (!ignore_acpi_ev && send_acpi_ev) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002428 acpi_bus_generate_netlink_event(
2429 ibm->acpi->device->pnp.device_class,
2430 ibm->acpi->device->dev.bus_id,
2431 event, hkey);
Henrique de Moraes Holschuh3eea1232007-09-23 11:39:04 -03002432 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002433 }
2434}
2435
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002436static void hotkey_suspend(pm_message_t state)
2437{
2438 /* Do these on suspend, we get the events on early resume! */
2439 hotkey_wakeup_reason = TP_ACPI_WAKEUP_NONE;
2440 hotkey_autosleep_ack = 0;
2441}
2442
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002443static void hotkey_resume(void)
2444{
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002445 if (hotkey_mask_get())
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02002446 printk(TPACPI_ERR
2447 "error while trying to read hot key mask "
2448 "from firmware\n");
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002449 tpacpi_input_send_radiosw();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002450 hotkey_radio_sw_notify_change();
Henrique de Moraes Holschuh6c231bd2008-02-16 02:17:58 -02002451 hotkey_tablet_mode_notify_change();
Henrique de Moraes Holschuh50ebec02008-01-08 13:02:55 -02002452 hotkey_wakeup_reason_notify_change();
2453 hotkey_wakeup_hotunplug_complete_notify_change();
Henrique de Moraes Holschuh01e88f22008-01-08 13:02:41 -02002454 hotkey_poll_setup_safe(0);
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002455}
2456
Henrique de Moraes Holschuha0416422007-04-27 22:00:16 -03002457/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002458static int hotkey_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002460 int res, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 int len = 0;
2462
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002463 if (!tp_features.hotkey) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002464 len += sprintf(p + len, "status:\t\tnot supported\n");
2465 return len;
2466 }
2467
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002468 if (mutex_lock_interruptible(&hotkey_mutex))
2469 return -ERESTARTSYS;
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002470 res = hotkey_status_get(&status);
2471 if (!res)
2472 res = hotkey_mask_get();
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002473 mutex_unlock(&hotkey_mutex);
Henrique de Moraes Holschuhb86c4722007-04-21 11:08:39 -03002474 if (res)
2475 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 len += sprintf(p + len, "status:\t\t%s\n", enabled(status, 0));
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002478 if (tp_features.hotkey_mask) {
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002479 len += sprintf(p + len, "mask:\t\t0x%08x\n", hotkey_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 len += sprintf(p + len,
2481 "commands:\tenable, disable, reset, <mask>\n");
2482 } else {
2483 len += sprintf(p + len, "mask:\t\tnot supported\n");
2484 len += sprintf(p + len, "commands:\tenable, disable, reset\n");
2485 }
2486
2487 return len;
2488}
2489
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002490static int hotkey_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Henrique de Moraes Holschuhae92bd12007-07-18 23:45:29 -03002492 int res, status;
2493 u32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002496 if (!tp_features.hotkey)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return -ENODEV;
2498
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02002499 if (mutex_lock_interruptible(&hotkey_mutex))
2500 return -ERESTARTSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002501
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002502 status = -1;
2503 mask = hotkey_mask;
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002504
2505 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 while ((cmd = next_cmd(&buf))) {
2507 if (strlencmp(cmd, "enable") == 0) {
2508 status = 1;
2509 } else if (strlencmp(cmd, "disable") == 0) {
2510 status = 0;
2511 } else if (strlencmp(cmd, "reset") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002512 status = hotkey_orig_status;
2513 mask = hotkey_orig_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 } else if (sscanf(cmd, "0x%x", &mask) == 1) {
2515 /* mask set */
2516 } else if (sscanf(cmd, "%x", &mask) == 1) {
2517 /* mask set */
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002518 } else {
2519 res = -EINVAL;
2520 goto errexit;
2521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002523 if (status != -1)
2524 res = hotkey_status_set(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Henrique de Moraes Holschuhb2c985e2008-01-08 13:02:39 -02002526 if (!res && mask != hotkey_mask)
2527 res = hotkey_mask_set(mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03002529errexit:
2530 mutex_unlock(&hotkey_mutex);
2531 return res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002534static const struct acpi_device_id ibm_htk_device_ids[] = {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002535 {TPACPI_ACPI_HKEY_HID, 0},
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002536 {"", 0},
2537};
2538
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002539static struct tp_acpi_drv_struct ibm_hotkey_acpidriver = {
Thomas Renninger1ba90e32007-07-23 14:44:41 +02002540 .hid = ibm_htk_device_ids,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002541 .notify = hotkey_notify,
2542 .handle = &hkey_handle,
2543 .type = ACPI_DEVICE_NOTIFY,
2544};
2545
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002546static struct ibm_struct hotkey_driver_data = {
2547 .name = "hotkey",
2548 .read = hotkey_read,
2549 .write = hotkey_write,
2550 .exit = hotkey_exit,
Henrique de Moraes Holschuh5c29d582007-07-18 23:45:38 -03002551 .resume = hotkey_resume,
Henrique de Moraes Holschuha713b4d2008-01-08 13:02:52 -02002552 .suspend = hotkey_suspend,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03002553 .acpi = &ibm_hotkey_acpidriver,
2554};
2555
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002556/*************************************************************************
2557 * Bluetooth subdriver
2558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002560enum {
2561 /* ACPI GBDC/SBDC bits */
2562 TP_ACPI_BLUETOOTH_HWPRESENT = 0x01, /* Bluetooth hw available */
2563 TP_ACPI_BLUETOOTH_RADIOSSW = 0x02, /* Bluetooth radio enabled */
2564 TP_ACPI_BLUETOOTH_UNK = 0x04, /* unknown function */
2565};
2566
2567static int bluetooth_get_radiosw(void);
2568static int bluetooth_set_radiosw(int radio_on);
2569
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002570/* sysfs bluetooth enable ---------------------------------------------- */
2571static ssize_t bluetooth_enable_show(struct device *dev,
2572 struct device_attribute *attr,
2573 char *buf)
2574{
2575 int status;
2576
2577 status = bluetooth_get_radiosw();
2578 if (status < 0)
2579 return status;
2580
2581 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2582}
2583
2584static ssize_t bluetooth_enable_store(struct device *dev,
2585 struct device_attribute *attr,
2586 const char *buf, size_t count)
2587{
2588 unsigned long t;
2589 int res;
2590
2591 if (parse_strtoul(buf, 1, &t))
2592 return -EINVAL;
2593
2594 res = bluetooth_set_radiosw(t);
2595
2596 return (res) ? res : count;
2597}
2598
2599static struct device_attribute dev_attr_bluetooth_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002600 __ATTR(bluetooth_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002601 bluetooth_enable_show, bluetooth_enable_store);
2602
2603/* --------------------------------------------------------------------- */
2604
2605static struct attribute *bluetooth_attributes[] = {
2606 &dev_attr_bluetooth_enable.attr,
2607 NULL
2608};
2609
2610static const struct attribute_group bluetooth_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002611 .attrs = bluetooth_attributes,
2612};
2613
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002614static int __init bluetooth_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002616 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002617 int status = 0;
2618
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002619 vdbg_printk(TPACPI_DBG_INIT, "initializing bluetooth subdriver\n");
2620
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002621 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002622
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002623 /* bluetooth not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
2624 G4x, R30, R31, R40e, R50e, T20-22, X20-21 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002625 tp_features.bluetooth = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002626 acpi_evalf(hkey_handle, &status, "GBDC", "qd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002628 vdbg_printk(TPACPI_DBG_INIT, "bluetooth is %s, status 0x%02x\n",
2629 str_supported(tp_features.bluetooth),
2630 status);
2631
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002632 if (tp_features.bluetooth) {
2633 if (!(status & TP_ACPI_BLUETOOTH_HWPRESENT)) {
2634 /* no bluetooth hardware present in system */
2635 tp_features.bluetooth = 0;
2636 dbg_printk(TPACPI_DBG_INIT,
2637 "bluetooth hardware not installed\n");
2638 } else {
2639 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2640 &bluetooth_attr_group);
2641 if (res)
2642 return res;
2643 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002644 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002645
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002646 return (tp_features.bluetooth)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002649static void bluetooth_exit(void)
2650{
2651 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2652 &bluetooth_attr_group);
2653}
2654
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002655static int bluetooth_get_radiosw(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 int status;
2658
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002659 if (!tp_features.bluetooth)
2660 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002662 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2663 return -EIO;
2664
2665 return ((status & TP_ACPI_BLUETOOTH_RADIOSSW) != 0);
2666}
2667
2668static int bluetooth_set_radiosw(int radio_on)
2669{
2670 int status;
2671
2672 if (!tp_features.bluetooth)
2673 return -ENODEV;
2674
2675 if (!acpi_evalf(hkey_handle, &status, "GBDC", "d"))
2676 return -EIO;
2677 if (radio_on)
2678 status |= TP_ACPI_BLUETOOTH_RADIOSSW;
2679 else
2680 status &= ~TP_ACPI_BLUETOOTH_RADIOSSW;
2681 if (!acpi_evalf(hkey_handle, NULL, "SBDC", "vd", status))
2682 return -EIO;
2683
2684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002687/* procfs -------------------------------------------------------------- */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002688static int bluetooth_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002691 int status = bluetooth_get_radiosw();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002693 if (!tp_features.bluetooth)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 len += sprintf(p + len, "status:\t\tnot supported\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002696 len += sprintf(p + len, "status:\t\t%s\n",
2697 (status)? "enabled" : "disabled");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 len += sprintf(p + len, "commands:\tenable, disable\n");
2699 }
2700
2701 return len;
2702}
2703
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002704static int bluetooth_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 char *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002708 if (!tp_features.bluetooth)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002709 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 while ((cmd = next_cmd(&buf))) {
2712 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002713 bluetooth_set_radiosw(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002715 bluetooth_set_radiosw(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 } else
2717 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 }
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return 0;
2721}
2722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002723static struct ibm_struct bluetooth_driver_data = {
2724 .name = "bluetooth",
2725 .read = bluetooth_read,
2726 .write = bluetooth_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002727 .exit = bluetooth_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002728};
2729
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002730/*************************************************************************
2731 * Wan subdriver
2732 */
2733
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002734enum {
2735 /* ACPI GWAN/SWAN bits */
2736 TP_ACPI_WANCARD_HWPRESENT = 0x01, /* Wan hw available */
2737 TP_ACPI_WANCARD_RADIOSSW = 0x02, /* Wan radio enabled */
2738 TP_ACPI_WANCARD_UNK = 0x04, /* unknown function */
2739};
2740
2741static int wan_get_radiosw(void);
2742static int wan_set_radiosw(int radio_on);
2743
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002744/* sysfs wan enable ---------------------------------------------------- */
2745static ssize_t wan_enable_show(struct device *dev,
2746 struct device_attribute *attr,
2747 char *buf)
2748{
2749 int status;
2750
2751 status = wan_get_radiosw();
2752 if (status < 0)
2753 return status;
2754
2755 return snprintf(buf, PAGE_SIZE, "%d\n", status ? 1 : 0);
2756}
2757
2758static ssize_t wan_enable_store(struct device *dev,
2759 struct device_attribute *attr,
2760 const char *buf, size_t count)
2761{
2762 unsigned long t;
2763 int res;
2764
2765 if (parse_strtoul(buf, 1, &t))
2766 return -EINVAL;
2767
2768 res = wan_set_radiosw(t);
2769
2770 return (res) ? res : count;
2771}
2772
2773static struct device_attribute dev_attr_wan_enable =
Henrique de Moraes Holschuhcc4c24e2007-05-30 20:50:14 -03002774 __ATTR(wwan_enable, S_IWUSR | S_IRUGO,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002775 wan_enable_show, wan_enable_store);
2776
2777/* --------------------------------------------------------------------- */
2778
2779static struct attribute *wan_attributes[] = {
2780 &dev_attr_wan_enable.attr,
2781 NULL
2782};
2783
2784static const struct attribute_group wan_attr_group = {
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002785 .attrs = wan_attributes,
2786};
2787
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002788static int __init wan_init(struct ibm_init_struct *iibm)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002789{
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002790 int res;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002791 int status = 0;
2792
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002793 vdbg_printk(TPACPI_DBG_INIT, "initializing wan subdriver\n");
2794
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002795 TPACPI_ACPIHANDLE_INIT(hkey);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002796
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002797 tp_features.wan = hkey_handle &&
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002798 acpi_evalf(hkey_handle, &status, "GWAN", "qd");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002799
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002800 vdbg_printk(TPACPI_DBG_INIT, "wan is %s, status 0x%02x\n",
2801 str_supported(tp_features.wan),
2802 status);
2803
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002804 if (tp_features.wan) {
2805 if (!(status & TP_ACPI_WANCARD_HWPRESENT)) {
2806 /* no wan hardware present in system */
2807 tp_features.wan = 0;
2808 dbg_printk(TPACPI_DBG_INIT,
2809 "wan hardware not installed\n");
2810 } else {
2811 res = sysfs_create_group(&tpacpi_pdev->dev.kobj,
2812 &wan_attr_group);
2813 if (res)
2814 return res;
2815 }
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002816 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002817
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002818 return (tp_features.wan)? 0 : 1;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002819}
2820
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002821static void wan_exit(void)
2822{
2823 sysfs_remove_group(&tpacpi_pdev->dev.kobj,
2824 &wan_attr_group);
2825}
2826
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002827static int wan_get_radiosw(void)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002828{
2829 int status;
2830
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002831 if (!tp_features.wan)
2832 return -ENODEV;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002833
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002834 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2835 return -EIO;
2836
2837 return ((status & TP_ACPI_WANCARD_RADIOSSW) != 0);
2838}
2839
2840static int wan_set_radiosw(int radio_on)
2841{
2842 int status;
2843
2844 if (!tp_features.wan)
2845 return -ENODEV;
2846
2847 if (!acpi_evalf(hkey_handle, &status, "GWAN", "d"))
2848 return -EIO;
2849 if (radio_on)
2850 status |= TP_ACPI_WANCARD_RADIOSSW;
2851 else
2852 status &= ~TP_ACPI_WANCARD_RADIOSSW;
2853 if (!acpi_evalf(hkey_handle, NULL, "SWAN", "vd", status))
2854 return -EIO;
2855
2856 return 0;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002857}
2858
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002859/* procfs -------------------------------------------------------------- */
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002860static int wan_read(char *p)
2861{
2862 int len = 0;
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002863 int status = wan_get_radiosw();
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002864
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002865 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002866 len += sprintf(p + len, "status:\t\tnot supported\n");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002867 else {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002868 len += sprintf(p + len, "status:\t\t%s\n",
2869 (status)? "enabled" : "disabled");
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002870 len += sprintf(p + len, "commands:\tenable, disable\n");
2871 }
2872
2873 return len;
2874}
2875
2876static int wan_write(char *buf)
2877{
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002878 char *cmd;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002879
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03002880 if (!tp_features.wan)
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002881 return -ENODEV;
2882
2883 while ((cmd = next_cmd(&buf))) {
2884 if (strlencmp(cmd, "enable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002885 wan_set_radiosw(1);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002886 } else if (strlencmp(cmd, "disable") == 0) {
Henrique de Moraes Holschuhd6fdd1e2007-04-21 11:08:40 -03002887 wan_set_radiosw(0);
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002888 } else
2889 return -EINVAL;
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002890 }
2891
Jeremy Fitzhardinge42adb532006-06-01 17:41:00 -04002892 return 0;
2893}
2894
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002895static struct ibm_struct wan_driver_data = {
2896 .name = "wan",
2897 .read = wan_read,
2898 .write = wan_write,
Henrique de Moraes Holschuhd3a6ade2007-04-27 22:00:17 -03002899 .exit = wan_exit,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03002900 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002901};
2902
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002903/*************************************************************************
2904 * Video subdriver
2905 */
2906
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02002907#ifdef CONFIG_THINKPAD_ACPI_VIDEO
2908
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002909enum video_access_mode {
2910 TPACPI_VIDEO_NONE = 0,
2911 TPACPI_VIDEO_570, /* 570 */
2912 TPACPI_VIDEO_770, /* 600e/x, 770e, 770x */
2913 TPACPI_VIDEO_NEW, /* all others */
2914};
2915
2916enum { /* video status flags, based on VIDEO_570 */
2917 TP_ACPI_VIDEO_S_LCD = 0x01, /* LCD output enabled */
2918 TP_ACPI_VIDEO_S_CRT = 0x02, /* CRT output enabled */
2919 TP_ACPI_VIDEO_S_DVI = 0x08, /* DVI output enabled */
2920};
2921
2922enum { /* TPACPI_VIDEO_570 constants */
2923 TP_ACPI_VIDEO_570_PHSCMD = 0x87, /* unknown magic constant :( */
2924 TP_ACPI_VIDEO_570_PHSMASK = 0x03, /* PHS bits that map to
2925 * video_status_flags */
2926 TP_ACPI_VIDEO_570_PHS2CMD = 0x8b, /* unknown magic constant :( */
2927 TP_ACPI_VIDEO_570_PHS2SET = 0x80, /* unknown magic constant :( */
2928};
2929
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02002930static enum video_access_mode video_supported;
2931static int video_orig_autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002932
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02002933static int video_autosw_get(void);
2934static int video_autosw_set(int enable);
2935
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002936TPACPI_HANDLE(vid2, root, "\\_SB.PCI0.AGPB.VID"); /* G41 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002937
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03002938static int __init video_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002940 int ivga;
2941
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002942 vdbg_printk(TPACPI_DBG_INIT, "initializing video subdriver\n");
2943
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002944 TPACPI_ACPIHANDLE_INIT(vid);
2945 TPACPI_ACPIHANDLE_INIT(vid2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002946
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002947 if (vid2_handle && acpi_evalf(NULL, &ivga, "\\IVGA", "d") && ivga)
2948 /* G41, assume IVGA doesn't change */
2949 vid_handle = vid2_handle;
2950
2951 if (!vid_handle)
2952 /* video switching not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002953 video_supported = TPACPI_VIDEO_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002954 else if (acpi_evalf(vid_handle, &video_orig_autosw, "SWIT", "qd"))
2955 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002956 video_supported = TPACPI_VIDEO_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002957 else if (acpi_evalf(vid_handle, &video_orig_autosw, "^VADL", "qd"))
2958 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002959 video_supported = TPACPI_VIDEO_770;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04002960 else
2961 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03002962 video_supported = TPACPI_VIDEO_NEW;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03002964 vdbg_printk(TPACPI_DBG_INIT, "video is %s, mode %d\n",
2965 str_supported(video_supported != TPACPI_VIDEO_NONE),
2966 video_supported);
2967
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03002968 return (video_supported != TPACPI_VIDEO_NONE)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002971static void video_exit(void)
2972{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002973 dbg_printk(TPACPI_DBG_EXIT,
2974 "restoring original video autoswitch mode\n");
2975 if (video_autosw_set(video_orig_autosw))
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02002976 printk(TPACPI_ERR "error while trying to restore original "
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002977 "video autoswitch mode\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03002978}
2979
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002980static int video_outputsw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 int status = 0;
2983 int i;
2984
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03002985 switch (video_supported) {
2986 case TPACPI_VIDEO_570:
2987 if (!acpi_evalf(NULL, &i, "\\_SB.PHS", "dd",
2988 TP_ACPI_VIDEO_570_PHSCMD))
2989 return -EIO;
2990 status = i & TP_ACPI_VIDEO_570_PHSMASK;
2991 break;
2992 case TPACPI_VIDEO_770:
2993 if (!acpi_evalf(NULL, &i, "\\VCDL", "d"))
2994 return -EIO;
2995 if (i)
2996 status |= TP_ACPI_VIDEO_S_LCD;
2997 if (!acpi_evalf(NULL, &i, "\\VCDC", "d"))
2998 return -EIO;
2999 if (i)
3000 status |= TP_ACPI_VIDEO_S_CRT;
3001 break;
3002 case TPACPI_VIDEO_NEW:
3003 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 1) ||
3004 !acpi_evalf(NULL, &i, "\\VCDC", "d"))
3005 return -EIO;
3006 if (i)
3007 status |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003009 if (!acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0) ||
3010 !acpi_evalf(NULL, &i, "\\VCDL", "d"))
3011 return -EIO;
3012 if (i)
3013 status |= TP_ACPI_VIDEO_S_LCD;
3014 if (!acpi_evalf(NULL, &i, "\\VCDD", "d"))
3015 return -EIO;
3016 if (i)
3017 status |= TP_ACPI_VIDEO_S_DVI;
3018 break;
3019 default:
3020 return -ENOSYS;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 return status;
3024}
3025
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003026static int video_outputsw_set(int status)
3027{
3028 int autosw;
3029 int res = 0;
3030
3031 switch (video_supported) {
3032 case TPACPI_VIDEO_570:
3033 res = acpi_evalf(NULL, NULL,
3034 "\\_SB.PHS2", "vdd",
3035 TP_ACPI_VIDEO_570_PHS2CMD,
3036 status | TP_ACPI_VIDEO_570_PHS2SET);
3037 break;
3038 case TPACPI_VIDEO_770:
3039 autosw = video_autosw_get();
3040 if (autosw < 0)
3041 return autosw;
3042
3043 res = video_autosw_set(1);
3044 if (res)
3045 return res;
3046 res = acpi_evalf(vid_handle, NULL,
3047 "ASWT", "vdd", status * 0x100, 0);
3048 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003049 printk(TPACPI_ERR
3050 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003051 return -EIO;
3052 }
3053 break;
3054 case TPACPI_VIDEO_NEW:
3055 res = acpi_evalf(NULL, NULL, "\\VUPS", "vd", 0x80) &&
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003056 acpi_evalf(NULL, NULL, "\\VSDS", "vdd", status, 1);
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003057 break;
3058 default:
3059 return -ENOSYS;
3060 }
3061
3062 return (res)? 0 : -EIO;
3063}
3064
3065static int video_autosw_get(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003067 int autosw = 0;
3068
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003069 switch (video_supported) {
3070 case TPACPI_VIDEO_570:
3071 if (!acpi_evalf(vid_handle, &autosw, "SWIT", "d"))
3072 return -EIO;
3073 break;
3074 case TPACPI_VIDEO_770:
3075 case TPACPI_VIDEO_NEW:
3076 if (!acpi_evalf(vid_handle, &autosw, "^VDEE", "d"))
3077 return -EIO;
3078 break;
3079 default:
3080 return -ENOSYS;
3081 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003082
3083 return autosw & 1;
3084}
3085
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003086static int video_autosw_set(int enable)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003087{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003088 if (!acpi_evalf(vid_handle, NULL, "_DOS", "vd", (enable)? 1 : 0))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003089 return -EIO;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003090 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003091}
3092
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003093static int video_outputsw_cycle(void)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003094{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003095 int autosw = video_autosw_get();
3096 int res;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003097
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003098 if (autosw < 0)
3099 return autosw;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003100
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003101 switch (video_supported) {
3102 case TPACPI_VIDEO_570:
3103 res = video_autosw_set(1);
3104 if (res)
3105 return res;
3106 res = acpi_evalf(ec_handle, NULL, "_Q16", "v");
3107 break;
3108 case TPACPI_VIDEO_770:
3109 case TPACPI_VIDEO_NEW:
3110 res = video_autosw_set(1);
3111 if (res)
3112 return res;
3113 res = acpi_evalf(vid_handle, NULL, "VSWT", "v");
3114 break;
3115 default:
3116 return -ENOSYS;
3117 }
3118 if (!autosw && video_autosw_set(autosw)) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003119 printk(TPACPI_ERR
3120 "video auto-switch left enabled due to error\n");
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003121 return -EIO;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003122 }
3123
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003124 return (res)? 0 : -EIO;
3125}
3126
3127static int video_expand_toggle(void)
3128{
3129 switch (video_supported) {
3130 case TPACPI_VIDEO_570:
3131 return acpi_evalf(ec_handle, NULL, "_Q17", "v")?
3132 0 : -EIO;
3133 case TPACPI_VIDEO_770:
3134 return acpi_evalf(vid_handle, NULL, "VEXP", "v")?
3135 0 : -EIO;
3136 case TPACPI_VIDEO_NEW:
3137 return acpi_evalf(NULL, NULL, "\\VEXP", "v")?
3138 0 : -EIO;
3139 default:
3140 return -ENOSYS;
3141 }
3142 /* not reached */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003143}
3144
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003145static int video_read(char *p)
3146{
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003147 int status, autosw;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003148 int len = 0;
3149
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003150 if (video_supported == TPACPI_VIDEO_NONE) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003151 len += sprintf(p + len, "status:\t\tnot supported\n");
3152 return len;
3153 }
3154
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003155 status = video_outputsw_get();
3156 if (status < 0)
3157 return status;
3158
3159 autosw = video_autosw_get();
3160 if (autosw < 0)
3161 return autosw;
3162
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003163 len += sprintf(p + len, "status:\t\tsupported\n");
3164 len += sprintf(p + len, "lcd:\t\t%s\n", enabled(status, 0));
3165 len += sprintf(p + len, "crt:\t\t%s\n", enabled(status, 1));
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003166 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003167 len += sprintf(p + len, "dvi:\t\t%s\n", enabled(status, 3));
3168 len += sprintf(p + len, "auto:\t\t%s\n", enabled(autosw, 0));
3169 len += sprintf(p + len, "commands:\tlcd_enable, lcd_disable\n");
3170 len += sprintf(p + len, "commands:\tcrt_enable, crt_disable\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003171 if (video_supported == TPACPI_VIDEO_NEW)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003172 len += sprintf(p + len, "commands:\tdvi_enable, dvi_disable\n");
3173 len += sprintf(p + len, "commands:\tauto_enable, auto_disable\n");
3174 len += sprintf(p + len, "commands:\tvideo_switch, expand_toggle\n");
3175
3176 return len;
3177}
3178
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003179static int video_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180{
3181 char *cmd;
3182 int enable, disable, status;
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003183 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003185 if (video_supported == TPACPI_VIDEO_NONE)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003186 return -ENODEV;
3187
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003188 enable = 0;
3189 disable = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
3191 while ((cmd = next_cmd(&buf))) {
3192 if (strlencmp(cmd, "lcd_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003193 enable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 } else if (strlencmp(cmd, "lcd_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003195 disable |= TP_ACPI_VIDEO_S_LCD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 } else if (strlencmp(cmd, "crt_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003197 enable |= TP_ACPI_VIDEO_S_CRT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 } else if (strlencmp(cmd, "crt_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003199 disable |= TP_ACPI_VIDEO_S_CRT;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003200 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003201 strlencmp(cmd, "dvi_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003202 enable |= TP_ACPI_VIDEO_S_DVI;
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003203 } else if (video_supported == TPACPI_VIDEO_NEW &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003204 strlencmp(cmd, "dvi_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003205 disable |= TP_ACPI_VIDEO_S_DVI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 } else if (strlencmp(cmd, "auto_enable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003207 res = video_autosw_set(1);
3208 if (res)
3209 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 } else if (strlencmp(cmd, "auto_disable") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003211 res = video_autosw_set(0);
3212 if (res)
3213 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 } else if (strlencmp(cmd, "video_switch") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003215 res = video_outputsw_cycle();
3216 if (res)
3217 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 } else if (strlencmp(cmd, "expand_toggle") == 0) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003219 res = video_expand_toggle();
3220 if (res)
3221 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 } else
3223 return -EINVAL;
3224 }
3225
3226 if (enable || disable) {
Henrique de Moraes Holschuh83f34722007-04-21 11:08:41 -03003227 status = video_outputsw_get();
3228 if (status < 0)
3229 return status;
3230 res = video_outputsw_set((status & ~disable) | enable);
3231 if (res)
3232 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 }
3234
3235 return 0;
3236}
3237
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003238static struct ibm_struct video_driver_data = {
3239 .name = "video",
3240 .read = video_read,
3241 .write = video_write,
3242 .exit = video_exit,
3243};
3244
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02003245#endif /* CONFIG_THINKPAD_ACPI_VIDEO */
3246
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003247/*************************************************************************
3248 * Light (thinklight) subdriver
3249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003251TPACPI_HANDLE(lght, root, "\\LGHT"); /* A21e, A2xm/p, T20-22, X20-21 */
3252TPACPI_HANDLE(ledb, ec, "LEDB"); /* G4x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003253
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003254static int light_get_status(void)
3255{
3256 int status = 0;
3257
3258 if (tp_features.light_status) {
3259 if (!acpi_evalf(ec_handle, &status, "KBLT", "d"))
3260 return -EIO;
3261 return (!!status);
3262 }
3263
3264 return -ENXIO;
3265}
3266
3267static int light_set_status(int status)
3268{
3269 int rc;
3270
3271 if (tp_features.light) {
3272 if (cmos_handle) {
3273 rc = acpi_evalf(cmos_handle, NULL, NULL, "vd",
3274 (status)?
3275 TP_CMOS_THINKLIGHT_ON :
3276 TP_CMOS_THINKLIGHT_OFF);
3277 } else {
3278 rc = acpi_evalf(lght_handle, NULL, NULL, "vd",
3279 (status)? 1 : 0);
3280 }
3281 return (rc)? 0 : -EIO;
3282 }
3283
3284 return -ENXIO;
3285}
3286
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003287static void light_set_status_worker(struct work_struct *work)
3288{
3289 struct tpacpi_led_classdev *data =
3290 container_of(work, struct tpacpi_led_classdev, work);
3291
3292 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3293 light_set_status((data->new_brightness != LED_OFF));
3294}
3295
3296static void light_sysfs_set(struct led_classdev *led_cdev,
3297 enum led_brightness brightness)
3298{
3299 struct tpacpi_led_classdev *data =
3300 container_of(led_cdev,
3301 struct tpacpi_led_classdev,
3302 led_classdev);
3303 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003304 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003305}
3306
3307static enum led_brightness light_sysfs_get(struct led_classdev *led_cdev)
3308{
3309 return (light_get_status() == 1)? LED_FULL : LED_OFF;
3310}
3311
3312static struct tpacpi_led_classdev tpacpi_led_thinklight = {
3313 .led_classdev = {
3314 .name = "tpacpi::thinklight",
3315 .brightness_set = &light_sysfs_set,
3316 .brightness_get = &light_sysfs_get,
3317 }
3318};
3319
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003320static int __init light_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321{
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003322 int rc = 0;
3323
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003324 vdbg_printk(TPACPI_DBG_INIT, "initializing light subdriver\n");
3325
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003326 TPACPI_ACPIHANDLE_INIT(ledb);
3327 TPACPI_ACPIHANDLE_INIT(lght);
3328 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003329 INIT_WORK(&tpacpi_led_thinklight.work, light_set_status_worker);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003330
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003331 /* light not supported on 570, 600e/x, 770e, 770x, G4x, R30, R31 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003332 tp_features.light = (cmos_handle || lght_handle) && !ledb_handle;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003333
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003334 if (tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003335 /* light status not supported on
3336 570, 600e/x, 770e, 770x, G4x, R30, R31, R32, X20 */
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003337 tp_features.light_status =
3338 acpi_evalf(ec_handle, NULL, "KBLT", "qv");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003340 vdbg_printk(TPACPI_DBG_INIT, "light is %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003341 str_supported(tp_features.light));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003342
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003343 if (tp_features.light) {
3344 rc = led_classdev_register(&tpacpi_pdev->dev,
3345 &tpacpi_led_thinklight.led_classdev);
3346 }
3347
3348 if (rc < 0) {
3349 tp_features.light = 0;
3350 tp_features.light_status = 0;
3351 } else {
3352 rc = (tp_features.light)? 0 : 1;
3353 }
3354 return rc;
3355}
3356
3357static void light_exit(void)
3358{
3359 led_classdev_unregister(&tpacpi_led_thinklight.led_classdev);
3360 if (work_pending(&tpacpi_led_thinklight.work))
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003361 flush_workqueue(tpacpi_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362}
3363
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003364static int light_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 int len = 0;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003367 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003369 if (!tp_features.light) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003370 len += sprintf(p + len, "status:\t\tnot supported\n");
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003371 } else if (!tp_features.light_status) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003372 len += sprintf(p + len, "status:\t\tunknown\n");
3373 len += sprintf(p + len, "commands:\ton, off\n");
3374 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003375 status = light_get_status();
3376 if (status < 0)
3377 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 len += sprintf(p + len, "status:\t\t%s\n", onoff(status, 0));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003379 len += sprintf(p + len, "commands:\ton, off\n");
3380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381
3382 return len;
3383}
3384
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003385static int light_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003388 int newstatus = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003389
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003390 if (!tp_features.light)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003391 return -ENODEV;
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 while ((cmd = next_cmd(&buf))) {
3394 if (strlencmp(cmd, "on") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003395 newstatus = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 } else if (strlencmp(cmd, "off") == 0) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003397 newstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 } else
3399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
3401
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003402 return light_set_status(newstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403}
3404
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003405static struct ibm_struct light_driver_data = {
3406 .name = "light",
3407 .read = light_read,
3408 .write = light_write,
Henrique de Moraes Holschuhe3065012008-04-26 01:02:24 -03003409 .exit = light_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003410};
3411
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003412/*************************************************************************
3413 * Dock subdriver
3414 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003416#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003417
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003418static void dock_notify(struct ibm_struct *ibm, u32 event);
3419static int dock_read(char *p);
3420static int dock_write(char *buf);
3421
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003422TPACPI_HANDLE(dock, root, "\\_SB.GDCK", /* X30, X31, X40 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003423 "\\_SB.PCI0.DOCK", /* 600e/x,770e,770x,A2xm/p,T20-22,X20-21 */
3424 "\\_SB.PCI0.PCI1.DOCK", /* all others */
3425 "\\_SB.PCI.ISA.SLCE", /* 570 */
3426 ); /* A21e,G4x,R30,R31,R32,R40,R40e,R50e */
3427
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003428/* don't list other alternatives as we install a notify handler on the 570 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003429TPACPI_HANDLE(pci, root, "\\_SB.PCI"); /* 570 */
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003430
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003431static const struct acpi_device_id ibm_pci_device_ids[] = {
3432 {PCI_ROOT_HID_STRING, 0},
3433 {"", 0},
3434};
3435
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003436static struct tp_acpi_drv_struct ibm_dock_acpidriver[2] = {
3437 {
3438 .notify = dock_notify,
3439 .handle = &dock_handle,
3440 .type = ACPI_SYSTEM_NOTIFY,
3441 },
3442 {
Henrique de Moraes Holschuh996fba02007-07-18 23:45:40 -03003443 /* THIS ONE MUST NEVER BE USED FOR DRIVER AUTOLOADING.
3444 * We just use it to get notifications of dock hotplug
3445 * in very old thinkpads */
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003446 .hid = ibm_pci_device_ids,
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003447 .notify = dock_notify,
3448 .handle = &pci_handle,
3449 .type = ACPI_SYSTEM_NOTIFY,
3450 },
3451};
3452
3453static struct ibm_struct dock_driver_data[2] = {
3454 {
3455 .name = "dock",
3456 .read = dock_read,
3457 .write = dock_write,
3458 .acpi = &ibm_dock_acpidriver[0],
3459 },
3460 {
3461 .name = "dock",
3462 .acpi = &ibm_dock_acpidriver[1],
3463 },
3464};
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466#define dock_docked() (_sta(dock_handle) & 1)
3467
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003468static int __init dock_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003469{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003470 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver\n");
3471
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003472 TPACPI_ACPIHANDLE_INIT(dock);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003473
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003474 vdbg_printk(TPACPI_DBG_INIT, "dock is %s\n",
3475 str_supported(dock_handle != NULL));
3476
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003477 return (dock_handle)? 0 : 1;
3478}
3479
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003480static int __init dock_init2(struct ibm_init_struct *iibm)
3481{
3482 int dock2_needed;
3483
3484 vdbg_printk(TPACPI_DBG_INIT, "initializing dock subdriver part 2\n");
3485
3486 if (dock_driver_data[0].flags.acpi_driver_registered &&
3487 dock_driver_data[0].flags.acpi_notify_installed) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003488 TPACPI_ACPIHANDLE_INIT(pci);
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03003489 dock2_needed = (pci_handle != NULL);
3490 vdbg_printk(TPACPI_DBG_INIT,
3491 "dock PCI handler for the TP 570 is %s\n",
3492 str_supported(dock2_needed));
3493 } else {
3494 vdbg_printk(TPACPI_DBG_INIT,
3495 "dock subdriver part 2 not required\n");
3496 dock2_needed = 0;
3497 }
3498
3499 return (dock2_needed)? 0 : 1;
3500}
3501
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003502static void dock_notify(struct ibm_struct *ibm, u32 event)
3503{
3504 int docked = dock_docked();
Thomas Renninger1ba90e32007-07-23 14:44:41 +02003505 int pci = ibm->acpi->hid && ibm->acpi->device &&
3506 acpi_match_device_ids(ibm->acpi->device, ibm_pci_device_ids);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003507 int data;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003508
3509 if (event == 1 && !pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003510 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003511 else if (event == 1 && pci) /* 570 */
Zhang Rui962ce8c2007-08-23 01:24:31 +08003512 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003513 else if (event == 3 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003514 data = 1; /* button */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003515 else if (event == 3 && !docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003516 data = 2; /* undock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003517 else if (event == 0 && docked)
Zhang Rui962ce8c2007-08-23 01:24:31 +08003518 data = 3; /* dock */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003519 else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003520 printk(TPACPI_ERR "unknown dock event %d, status %d\n",
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003521 event, _sta(dock_handle));
Zhang Rui962ce8c2007-08-23 01:24:31 +08003522 data = 0; /* unknown */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003523 }
Len Brown14e04fb32007-08-23 15:20:26 -04003524 acpi_bus_generate_proc_event(ibm->acpi->device, event, data);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003525 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3526 ibm->acpi->device->dev.bus_id,
3527 event, data);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003528}
3529
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003530static int dock_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531{
3532 int len = 0;
3533 int docked = dock_docked();
3534
3535 if (!dock_handle)
3536 len += sprintf(p + len, "status:\t\tnot supported\n");
3537 else if (!docked)
3538 len += sprintf(p + len, "status:\t\tundocked\n");
3539 else {
3540 len += sprintf(p + len, "status:\t\tdocked\n");
3541 len += sprintf(p + len, "commands:\tdock, undock\n");
3542 }
3543
3544 return len;
3545}
3546
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003547static int dock_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 char *cmd;
3550
3551 if (!dock_docked())
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003552 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 while ((cmd = next_cmd(&buf))) {
3555 if (strlencmp(cmd, "undock") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003556 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 0) ||
3557 !acpi_evalf(dock_handle, NULL, "_EJ0", "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return -EIO;
3559 } else if (strlencmp(cmd, "dock") == 0) {
3560 if (!acpi_evalf(dock_handle, NULL, "_DCK", "vd", 1))
3561 return -EIO;
3562 } else
3563 return -EINVAL;
3564 }
3565
3566 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003567}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003569#endif /* CONFIG_THINKPAD_ACPI_DOCK */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003571/*************************************************************************
3572 * Bay subdriver
3573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003575#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003576
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003577TPACPI_HANDLE(bay, root, "\\_SB.PCI.IDE.SECN.MAST", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003578 "\\_SB.PCI0.IDE0.IDES.IDSM", /* 600e/x, 770e, 770x */
3579 "\\_SB.PCI0.SATA.SCND.MSTR", /* T60, X60, Z60 */
3580 "\\_SB.PCI0.IDE0.SCND.MSTR", /* all others */
3581 ); /* A21e, R30, R31 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003582TPACPI_HANDLE(bay_ej, bay, "_EJ3", /* 600e/x, A2xm/p, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003583 "_EJ0", /* all others */
3584 ); /* 570,A21e,G4x,R30,R31,R32,R40e,R50e */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003585TPACPI_HANDLE(bay2, root, "\\_SB.PCI0.IDE0.PRIM.SLAV", /* A3x, R32 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003586 "\\_SB.PCI0.IDE0.IDEP.IDPS", /* 600e/x, 770e, 770x */
3587 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003588TPACPI_HANDLE(bay2_ej, bay2, "_EJ3", /* 600e/x, 770e, A3x */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003589 "_EJ0", /* 770x */
3590 ); /* all others */
3591
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003592static int __init bay_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003594 vdbg_printk(TPACPI_DBG_INIT, "initializing bay subdriver\n");
3595
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003596 TPACPI_ACPIHANDLE_INIT(bay);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003597 if (bay_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003598 TPACPI_ACPIHANDLE_INIT(bay_ej);
3599 TPACPI_ACPIHANDLE_INIT(bay2);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003600 if (bay2_handle)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003601 TPACPI_ACPIHANDLE_INIT(bay2_ej);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003602
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003603 tp_features.bay_status = bay_handle &&
3604 acpi_evalf(bay_handle, NULL, "_STA", "qv");
3605 tp_features.bay_status2 = bay2_handle &&
3606 acpi_evalf(bay2_handle, NULL, "_STA", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003607
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003608 tp_features.bay_eject = bay_handle && bay_ej_handle &&
3609 (strlencmp(bay_ej_path, "_EJ0") == 0 || experimental);
3610 tp_features.bay_eject2 = bay2_handle && bay2_ej_handle &&
3611 (strlencmp(bay2_ej_path, "_EJ0") == 0 || experimental);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003613 vdbg_printk(TPACPI_DBG_INIT,
3614 "bay 1: status %s, eject %s; bay 2: status %s, eject %s\n",
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003615 str_supported(tp_features.bay_status),
3616 str_supported(tp_features.bay_eject),
3617 str_supported(tp_features.bay_status2),
3618 str_supported(tp_features.bay_eject2));
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003619
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003620 return (tp_features.bay_status || tp_features.bay_eject ||
3621 tp_features.bay_status2 || tp_features.bay_eject2)? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003624static void bay_notify(struct ibm_struct *ibm, u32 event)
3625{
Len Brown14e04fb32007-08-23 15:20:26 -04003626 acpi_bus_generate_proc_event(ibm->acpi->device, event, 0);
Zhang Rui962ce8c2007-08-23 01:24:31 +08003627 acpi_bus_generate_netlink_event(ibm->acpi->device->pnp.device_class,
3628 ibm->acpi->device->dev.bus_id,
3629 event, 0);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003630}
3631
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003632#define bay_occupied(b) (_sta(b##_handle) & 1)
3633
3634static int bay_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635{
3636 int len = 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003637 int occupied = bay_occupied(bay);
3638 int occupied2 = bay_occupied(bay2);
3639 int eject, eject2;
3640
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003641 len += sprintf(p + len, "status:\t\t%s\n",
3642 tp_features.bay_status ?
3643 (occupied ? "occupied" : "unoccupied") :
3644 "not supported");
3645 if (tp_features.bay_status2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003646 len += sprintf(p + len, "status2:\t%s\n", occupied2 ?
3647 "occupied" : "unoccupied");
3648
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003649 eject = tp_features.bay_eject && occupied;
3650 eject2 = tp_features.bay_eject2 && occupied2;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003651
3652 if (eject && eject2)
3653 len += sprintf(p + len, "commands:\teject, eject2\n");
3654 else if (eject)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 len += sprintf(p + len, "commands:\teject\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003656 else if (eject2)
3657 len += sprintf(p + len, "commands:\teject2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
3659 return len;
3660}
3661
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003662static int bay_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663{
3664 char *cmd;
3665
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003666 if (!tp_features.bay_eject && !tp_features.bay_eject2)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003667 return -ENODEV;
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003670 if (tp_features.bay_eject && strlencmp(cmd, "eject") == 0) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003671 if (!acpi_evalf(bay_ej_handle, NULL, NULL, "vd", 1))
3672 return -EIO;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03003673 } else if (tp_features.bay_eject2 &&
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003674 strlencmp(cmd, "eject2") == 0) {
3675 if (!acpi_evalf(bay2_ej_handle, NULL, NULL, "vd", 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return -EIO;
3677 } else
3678 return -EINVAL;
3679 }
3680
3681 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003682}
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003683
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003684static struct tp_acpi_drv_struct ibm_bay_acpidriver = {
3685 .notify = bay_notify,
3686 .handle = &bay_handle,
3687 .type = ACPI_SYSTEM_NOTIFY,
3688};
3689
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003690static struct ibm_struct bay_driver_data = {
3691 .name = "bay",
3692 .read = bay_read,
3693 .write = bay_write,
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03003694 .acpi = &ibm_bay_acpidriver,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003695};
3696
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03003697#endif /* CONFIG_THINKPAD_ACPI_BAY */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003699/*************************************************************************
3700 * CMOS subdriver
3701 */
3702
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003703/* sysfs cmos_command -------------------------------------------------- */
3704static ssize_t cmos_command_store(struct device *dev,
3705 struct device_attribute *attr,
3706 const char *buf, size_t count)
3707{
3708 unsigned long cmos_cmd;
3709 int res;
3710
3711 if (parse_strtoul(buf, 21, &cmos_cmd))
3712 return -EINVAL;
3713
3714 res = issue_thinkpad_cmos_command(cmos_cmd);
3715 return (res)? res : count;
3716}
3717
3718static struct device_attribute dev_attr_cmos_command =
3719 __ATTR(cmos_command, S_IWUSR, NULL, cmos_command_store);
3720
3721/* --------------------------------------------------------------------- */
3722
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003723static int __init cmos_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003724{
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003725 int res;
3726
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003727 vdbg_printk(TPACPI_DBG_INIT,
3728 "initializing cmos commands subdriver\n");
3729
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003730 TPACPI_ACPIHANDLE_INIT(cmos);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003731
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003732 vdbg_printk(TPACPI_DBG_INIT, "cmos commands are %s\n",
3733 str_supported(cmos_handle != NULL));
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003734
3735 res = device_create_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3736 if (res)
3737 return res;
3738
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003739 return (cmos_handle)? 0 : 1;
3740}
3741
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003742static void cmos_exit(void)
3743{
3744 device_remove_file(&tpacpi_pdev->dev, &dev_attr_cmos_command);
3745}
3746
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003747static int cmos_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
3749 int len = 0;
3750
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003751 /* cmos not supported on 570, 600e/x, 770e, 770x, A21e, A2xm/p,
3752 R30, R31, T20-22, X20-21 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if (!cmos_handle)
3754 len += sprintf(p + len, "status:\t\tnot supported\n");
3755 else {
3756 len += sprintf(p + len, "status:\t\tsupported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003757 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-21)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
3759
3760 return len;
3761}
3762
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003763static int cmos_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
3765 char *cmd;
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003766 int cmos_cmd, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
3768 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003769 if (sscanf(cmd, "%u", &cmos_cmd) == 1 &&
3770 cmos_cmd >= 0 && cmos_cmd <= 21) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 /* cmos_cmd set */
3772 } else
3773 return -EINVAL;
3774
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03003775 res = issue_thinkpad_cmos_command(cmos_cmd);
3776 if (res)
3777 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779
3780 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003781}
3782
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003783static struct ibm_struct cmos_driver_data = {
3784 .name = "cmos",
3785 .read = cmos_read,
3786 .write = cmos_write,
Henrique de Moraes Holschuhb6160042007-04-24 11:48:19 -03003787 .exit = cmos_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003788};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003789
3790/*************************************************************************
3791 * LED subdriver
3792 */
3793
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02003794enum led_access_mode {
3795 TPACPI_LED_NONE = 0,
3796 TPACPI_LED_570, /* 570 */
3797 TPACPI_LED_OLD, /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
3798 TPACPI_LED_NEW, /* all others */
3799};
3800
3801enum { /* For TPACPI_LED_OLD */
3802 TPACPI_LED_EC_HLCL = 0x0c, /* EC reg to get led to power on */
3803 TPACPI_LED_EC_HLBL = 0x0d, /* EC reg to blink a lit led */
3804 TPACPI_LED_EC_HLMS = 0x0e, /* EC reg to select led to command */
3805};
3806
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003807enum led_status_t {
3808 TPACPI_LED_OFF = 0,
3809 TPACPI_LED_ON,
3810 TPACPI_LED_BLINK,
3811};
3812
Henrique de Moraes Holschuh9a8e1732006-11-25 16:36:00 -02003813static enum led_access_mode led_supported;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003814
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003815TPACPI_HANDLE(led, ec, "SLED", /* 570 */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02003816 "SYSL", /* 600e/x, 770e, 770x, A21e, A2xm/p, */
3817 /* T20-22, X20-21 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03003818 "LED", /* all others */
3819 ); /* R30, R31 */
3820
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003821#define TPACPI_LED_NUMLEDS 8
3822static struct tpacpi_led_classdev *tpacpi_leds;
3823static enum led_status_t tpacpi_led_state_cache[TPACPI_LED_NUMLEDS];
3824static const char const *tpacpi_led_names[TPACPI_LED_NUMLEDS] = {
3825 /* there's a limit of 19 chars + NULL before 2.6.26 */
3826 "tpacpi::power",
3827 "tpacpi:orange:batt",
3828 "tpacpi:green:batt",
3829 "tpacpi::dock_active",
3830 "tpacpi::bay_active",
3831 "tpacpi::dock_batt",
3832 "tpacpi::unknown_led",
3833 "tpacpi::standby",
3834};
3835
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003836static int led_get_status(unsigned int led)
3837{
3838 int status;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003839 enum led_status_t led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003840
3841 switch (led_supported) {
3842 case TPACPI_LED_570:
3843 if (!acpi_evalf(ec_handle,
3844 &status, "GLED", "dd", 1 << led))
3845 return -EIO;
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003846 led_s = (status == 0)?
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003847 TPACPI_LED_OFF :
3848 ((status == 1)?
3849 TPACPI_LED_ON :
3850 TPACPI_LED_BLINK);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003851 tpacpi_led_state_cache[led] = led_s;
3852 return led_s;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003853 default:
3854 return -ENXIO;
3855 }
3856
3857 /* not reached */
3858}
3859
3860static int led_set_status(unsigned int led, enum led_status_t ledstatus)
3861{
3862 /* off, on, blink. Index is led_status_t */
3863 static const int const led_sled_arg1[] = { 0, 1, 3 };
3864 static const int const led_exp_hlbl[] = { 0, 0, 1 }; /* led# * */
3865 static const int const led_exp_hlcl[] = { 0, 1, 1 }; /* led# * */
3866 static const int const led_led_arg1[] = { 0, 0x80, 0xc0 };
3867
3868 int rc = 0;
3869
3870 switch (led_supported) {
3871 case TPACPI_LED_570:
3872 /* 570 */
3873 led = 1 << led;
3874 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3875 led, led_sled_arg1[ledstatus]))
3876 rc = -EIO;
3877 break;
3878 case TPACPI_LED_OLD:
3879 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20 */
3880 led = 1 << led;
3881 rc = ec_write(TPACPI_LED_EC_HLMS, led);
3882 if (rc >= 0)
3883 rc = ec_write(TPACPI_LED_EC_HLBL,
3884 led * led_exp_hlbl[ledstatus]);
3885 if (rc >= 0)
3886 rc = ec_write(TPACPI_LED_EC_HLCL,
Henrique de Moraes Holschuh10cc9272008-04-26 01:02:27 -03003887 led * led_exp_hlcl[ledstatus]);
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003888 break;
3889 case TPACPI_LED_NEW:
3890 /* all others */
3891 if (!acpi_evalf(led_handle, NULL, NULL, "vdd",
3892 led, led_led_arg1[ledstatus]))
3893 rc = -EIO;
3894 break;
3895 default:
3896 rc = -ENXIO;
3897 }
3898
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003899 if (!rc)
3900 tpacpi_led_state_cache[led] = ledstatus;
3901
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03003902 return rc;
3903}
3904
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003905static void led_sysfs_set_status(unsigned int led,
3906 enum led_brightness brightness)
3907{
3908 led_set_status(led,
3909 (brightness == LED_OFF) ?
3910 TPACPI_LED_OFF :
3911 (tpacpi_led_state_cache[led] == TPACPI_LED_BLINK) ?
3912 TPACPI_LED_BLINK : TPACPI_LED_ON);
3913}
3914
3915static void led_set_status_worker(struct work_struct *work)
3916{
3917 struct tpacpi_led_classdev *data =
3918 container_of(work, struct tpacpi_led_classdev, work);
3919
3920 if (likely(tpacpi_lifecycle == TPACPI_LIFE_RUNNING))
3921 led_sysfs_set_status(data->led, data->new_brightness);
3922}
3923
3924static void led_sysfs_set(struct led_classdev *led_cdev,
3925 enum led_brightness brightness)
3926{
3927 struct tpacpi_led_classdev *data = container_of(led_cdev,
3928 struct tpacpi_led_classdev, led_classdev);
3929
3930 data->new_brightness = brightness;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003931 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003932}
3933
3934static int led_sysfs_blink_set(struct led_classdev *led_cdev,
3935 unsigned long *delay_on, unsigned long *delay_off)
3936{
3937 struct tpacpi_led_classdev *data = container_of(led_cdev,
3938 struct tpacpi_led_classdev, led_classdev);
3939
3940 /* Can we choose the flash rate? */
3941 if (*delay_on == 0 && *delay_off == 0) {
3942 /* yes. set them to the hardware blink rate (1 Hz) */
3943 *delay_on = 500; /* ms */
3944 *delay_off = 500; /* ms */
3945 } else if ((*delay_on != 500) || (*delay_off != 500))
3946 return -EINVAL;
3947
3948 data->new_brightness = TPACPI_LED_BLINK;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03003949 queue_work(tpacpi_wq, &data->work);
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003950
3951 return 0;
3952}
3953
3954static enum led_brightness led_sysfs_get(struct led_classdev *led_cdev)
3955{
3956 int rc;
3957
3958 struct tpacpi_led_classdev *data = container_of(led_cdev,
3959 struct tpacpi_led_classdev, led_classdev);
3960
3961 rc = led_get_status(data->led);
3962
3963 if (rc == TPACPI_LED_OFF || rc < 0)
3964 rc = LED_OFF; /* no error handling in led class :( */
3965 else
3966 rc = LED_FULL;
3967
3968 return rc;
3969}
3970
3971static void led_exit(void)
3972{
3973 unsigned int i;
3974
3975 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
3976 if (tpacpi_leds[i].led_classdev.name)
3977 led_classdev_unregister(&tpacpi_leds[i].led_classdev);
3978 }
3979
3980 kfree(tpacpi_leds);
3981 tpacpi_leds = NULL;
3982}
3983
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03003984static int __init led_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003985{
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03003986 unsigned int i;
3987 int rc;
3988
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03003989 vdbg_printk(TPACPI_DBG_INIT, "initializing LED subdriver\n");
3990
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02003991 TPACPI_ACPIHANDLE_INIT(led);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03003992
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003993 if (!led_handle)
3994 /* led not supported on R30, R31 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003995 led_supported = TPACPI_LED_NONE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003996 else if (strlencmp(led_path, "SLED") == 0)
3997 /* 570 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03003998 led_supported = TPACPI_LED_570;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04003999 else if (strlencmp(led_path, "SYSL") == 0)
4000 /* 600e/x, 770e, 770x, A21e, A2xm/p, T20-22, X20-21 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004001 led_supported = TPACPI_LED_OLD;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004002 else
4003 /* all others */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004004 led_supported = TPACPI_LED_NEW;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004005
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004006 vdbg_printk(TPACPI_DBG_INIT, "LED commands are %s, mode %d\n",
4007 str_supported(led_supported), led_supported);
4008
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004009 tpacpi_leds = kzalloc(sizeof(*tpacpi_leds) * TPACPI_LED_NUMLEDS,
4010 GFP_KERNEL);
4011 if (!tpacpi_leds) {
4012 printk(TPACPI_ERR "Out of memory for LED data\n");
4013 return -ENOMEM;
4014 }
4015
4016 for (i = 0; i < TPACPI_LED_NUMLEDS; i++) {
4017 tpacpi_leds[i].led = i;
4018
4019 tpacpi_leds[i].led_classdev.brightness_set = &led_sysfs_set;
4020 tpacpi_leds[i].led_classdev.blink_set = &led_sysfs_blink_set;
4021 if (led_supported == TPACPI_LED_570)
4022 tpacpi_leds[i].led_classdev.brightness_get =
4023 &led_sysfs_get;
4024
4025 tpacpi_leds[i].led_classdev.name = tpacpi_led_names[i];
4026
4027 INIT_WORK(&tpacpi_leds[i].work, led_set_status_worker);
4028
4029 rc = led_classdev_register(&tpacpi_pdev->dev,
4030 &tpacpi_leds[i].led_classdev);
4031 if (rc < 0) {
4032 tpacpi_leds[i].led_classdev.name = NULL;
4033 led_exit();
4034 return rc;
4035 }
4036 }
4037
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004038 return (led_supported != TPACPI_LED_NONE)? 0 : 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004039}
4040
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004041#define str_led_status(s) \
4042 ((s) == TPACPI_LED_OFF ? "off" : \
4043 ((s) == TPACPI_LED_ON ? "on" : "blinking"))
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004044
4045static int led_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046{
4047 int len = 0;
4048
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004049 if (!led_supported) {
4050 len += sprintf(p + len, "status:\t\tnot supported\n");
4051 return len;
4052 }
4053 len += sprintf(p + len, "status:\t\tsupported\n");
4054
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004055 if (led_supported == TPACPI_LED_570) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004056 /* 570 */
4057 int i, status;
4058 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004059 status = led_get_status(i);
4060 if (status < 0)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004061 return -EIO;
4062 len += sprintf(p + len, "%d:\t\t%s\n",
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004063 i, str_led_status(status));
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004064 }
4065 }
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 len += sprintf(p + len, "commands:\t"
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004068 "<led> on, <led> off, <led> blink (<led> is 0-7)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
4070 return len;
4071}
4072
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004073static int led_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
4075 char *cmd;
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004076 int led, rc;
4077 enum led_status_t s;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004078
4079 if (!led_supported)
4080 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
4082 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004083 if (sscanf(cmd, "%d", &led) != 1 || led < 0 || led > 7)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 return -EINVAL;
4085
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004086 if (strstr(cmd, "off")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004087 s = TPACPI_LED_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 } else if (strstr(cmd, "on")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004089 s = TPACPI_LED_ON;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004090 } else if (strstr(cmd, "blink")) {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004091 s = TPACPI_LED_BLINK;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004092 } else {
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004093 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004094 }
Henrique de Moraes Holschuh4fa68112008-04-26 01:02:23 -03004095
4096 rc = led_set_status(led, s);
4097 if (rc < 0)
4098 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
4100
4101 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004102}
4103
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004104static struct ibm_struct led_driver_data = {
4105 .name = "led",
4106 .read = led_read,
4107 .write = led_write,
Henrique de Moraes Holschuhaf116102008-04-26 01:02:25 -03004108 .exit = led_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004109};
4110
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004111/*************************************************************************
4112 * Beep subdriver
4113 */
4114
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004115TPACPI_HANDLE(beep, ec, "BEEP"); /* all except R30, R31 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004116
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004117static int __init beep_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004118{
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004119 vdbg_printk(TPACPI_DBG_INIT, "initializing beep subdriver\n");
4120
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004121 TPACPI_ACPIHANDLE_INIT(beep);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004122
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004123 vdbg_printk(TPACPI_DBG_INIT, "beep is %s\n",
4124 str_supported(beep_handle != NULL));
4125
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004126 return (beep_handle)? 0 : 1;
4127}
4128
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004129static int beep_read(char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130{
4131 int len = 0;
4132
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004133 if (!beep_handle)
4134 len += sprintf(p + len, "status:\t\tnot supported\n");
4135 else {
4136 len += sprintf(p + len, "status:\t\tsupported\n");
4137 len += sprintf(p + len, "commands:\t<cmd> (<cmd> is 0-17)\n");
4138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139
4140 return len;
4141}
4142
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004143static int beep_write(char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
4145 char *cmd;
4146 int beep_cmd;
4147
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004148 if (!beep_handle)
4149 return -ENODEV;
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 while ((cmd = next_cmd(&buf))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004152 if (sscanf(cmd, "%u", &beep_cmd) == 1 &&
4153 beep_cmd >= 0 && beep_cmd <= 17) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 /* beep_cmd set */
4155 } else
4156 return -EINVAL;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004157 if (!acpi_evalf(beep_handle, NULL, NULL, "vdd", beep_cmd, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return -EIO;
4159 }
4160
4161 return 0;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004162}
4163
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004164static struct ibm_struct beep_driver_data = {
4165 .name = "beep",
4166 .read = beep_read,
4167 .write = beep_write,
4168};
4169
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004170/*************************************************************************
4171 * Thermal subdriver
4172 */
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004173
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004174enum thermal_access_mode {
4175 TPACPI_THERMAL_NONE = 0, /* No thermal support */
4176 TPACPI_THERMAL_ACPI_TMP07, /* Use ACPI TMP0-7 */
4177 TPACPI_THERMAL_ACPI_UPDT, /* Use ACPI TMP0-7 with UPDT */
4178 TPACPI_THERMAL_TPEC_8, /* Use ACPI EC regs, 8 sensors */
4179 TPACPI_THERMAL_TPEC_16, /* Use ACPI EC regs, 16 sensors */
4180};
4181
4182enum { /* TPACPI_THERMAL_TPEC_* */
4183 TP_EC_THERMAL_TMP0 = 0x78, /* ACPI EC regs TMP 0..7 */
4184 TP_EC_THERMAL_TMP8 = 0xC0, /* ACPI EC regs TMP 8..15 */
4185 TP_EC_THERMAL_TMP_NA = -128, /* ACPI EC sensor not available */
4186};
4187
4188#define TPACPI_MAX_THERMAL_SENSORS 16 /* Max thermal sensors supported */
4189struct ibm_thermal_sensors_struct {
4190 s32 temp[TPACPI_MAX_THERMAL_SENSORS];
4191};
4192
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004193static enum thermal_access_mode thermal_read_mode;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004194
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004195/* idx is zero-based */
4196static int thermal_get_sensor(int idx, s32 *value)
4197{
4198 int t;
4199 s8 tmp;
4200 char tmpi[5];
4201
4202 t = TP_EC_THERMAL_TMP0;
4203
4204 switch (thermal_read_mode) {
4205#if TPACPI_MAX_THERMAL_SENSORS >= 16
4206 case TPACPI_THERMAL_TPEC_16:
4207 if (idx >= 8 && idx <= 15) {
4208 t = TP_EC_THERMAL_TMP8;
4209 idx -= 8;
4210 }
4211 /* fallthrough */
4212#endif
4213 case TPACPI_THERMAL_TPEC_8:
4214 if (idx <= 7) {
4215 if (!acpi_ec_read(t + idx, &tmp))
4216 return -EIO;
4217 *value = tmp * 1000;
4218 return 0;
4219 }
4220 break;
4221
4222 case TPACPI_THERMAL_ACPI_UPDT:
4223 if (idx <= 7) {
4224 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4225 if (!acpi_evalf(ec_handle, NULL, "UPDT", "v"))
4226 return -EIO;
4227 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4228 return -EIO;
4229 *value = (t - 2732) * 100;
4230 return 0;
4231 }
4232 break;
4233
4234 case TPACPI_THERMAL_ACPI_TMP07:
4235 if (idx <= 7) {
4236 snprintf(tmpi, sizeof(tmpi), "TMP%c", '0' + idx);
4237 if (!acpi_evalf(ec_handle, &t, tmpi, "d"))
4238 return -EIO;
4239 if (t > 127 || t < -127)
4240 t = TP_EC_THERMAL_TMP_NA;
4241 *value = t * 1000;
4242 return 0;
4243 }
4244 break;
4245
4246 case TPACPI_THERMAL_NONE:
4247 default:
4248 return -ENOSYS;
4249 }
4250
4251 return -EINVAL;
4252}
4253
4254static int thermal_get_sensors(struct ibm_thermal_sensors_struct *s)
4255{
4256 int res, i;
4257 int n;
4258
4259 n = 8;
4260 i = 0;
4261
4262 if (!s)
4263 return -EINVAL;
4264
4265 if (thermal_read_mode == TPACPI_THERMAL_TPEC_16)
4266 n = 16;
4267
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004268 for (i = 0 ; i < n; i++) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004269 res = thermal_get_sensor(i, &s->temp[i]);
4270 if (res)
4271 return res;
4272 }
4273
4274 return n;
4275}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004276
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004277/* sysfs temp##_input -------------------------------------------------- */
4278
4279static ssize_t thermal_temp_input_show(struct device *dev,
4280 struct device_attribute *attr,
4281 char *buf)
4282{
4283 struct sensor_device_attribute *sensor_attr =
4284 to_sensor_dev_attr(attr);
4285 int idx = sensor_attr->index;
4286 s32 value;
4287 int res;
4288
4289 res = thermal_get_sensor(idx, &value);
4290 if (res)
4291 return res;
4292 if (value == TP_EC_THERMAL_TMP_NA * 1000)
4293 return -ENXIO;
4294
4295 return snprintf(buf, PAGE_SIZE, "%d\n", value);
4296}
4297
4298#define THERMAL_SENSOR_ATTR_TEMP(_idxA, _idxB) \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004299 SENSOR_ATTR(temp##_idxA##_input, S_IRUGO, \
4300 thermal_temp_input_show, NULL, _idxB)
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004301
4302static struct sensor_device_attribute sensor_dev_attr_thermal_temp_input[] = {
4303 THERMAL_SENSOR_ATTR_TEMP(1, 0),
4304 THERMAL_SENSOR_ATTR_TEMP(2, 1),
4305 THERMAL_SENSOR_ATTR_TEMP(3, 2),
4306 THERMAL_SENSOR_ATTR_TEMP(4, 3),
4307 THERMAL_SENSOR_ATTR_TEMP(5, 4),
4308 THERMAL_SENSOR_ATTR_TEMP(6, 5),
4309 THERMAL_SENSOR_ATTR_TEMP(7, 6),
4310 THERMAL_SENSOR_ATTR_TEMP(8, 7),
4311 THERMAL_SENSOR_ATTR_TEMP(9, 8),
4312 THERMAL_SENSOR_ATTR_TEMP(10, 9),
4313 THERMAL_SENSOR_ATTR_TEMP(11, 10),
4314 THERMAL_SENSOR_ATTR_TEMP(12, 11),
4315 THERMAL_SENSOR_ATTR_TEMP(13, 12),
4316 THERMAL_SENSOR_ATTR_TEMP(14, 13),
4317 THERMAL_SENSOR_ATTR_TEMP(15, 14),
4318 THERMAL_SENSOR_ATTR_TEMP(16, 15),
4319};
4320
4321#define THERMAL_ATTRS(X) \
4322 &sensor_dev_attr_thermal_temp_input[X].dev_attr.attr
4323
4324static struct attribute *thermal_temp_input_attr[] = {
4325 THERMAL_ATTRS(8),
4326 THERMAL_ATTRS(9),
4327 THERMAL_ATTRS(10),
4328 THERMAL_ATTRS(11),
4329 THERMAL_ATTRS(12),
4330 THERMAL_ATTRS(13),
4331 THERMAL_ATTRS(14),
4332 THERMAL_ATTRS(15),
4333 THERMAL_ATTRS(0),
4334 THERMAL_ATTRS(1),
4335 THERMAL_ATTRS(2),
4336 THERMAL_ATTRS(3),
4337 THERMAL_ATTRS(4),
4338 THERMAL_ATTRS(5),
4339 THERMAL_ATTRS(6),
4340 THERMAL_ATTRS(7),
4341 NULL
4342};
4343
4344static const struct attribute_group thermal_temp_input16_group = {
4345 .attrs = thermal_temp_input_attr
4346};
4347
4348static const struct attribute_group thermal_temp_input8_group = {
4349 .attrs = &thermal_temp_input_attr[8]
4350};
4351
4352#undef THERMAL_SENSOR_ATTR_TEMP
4353#undef THERMAL_ATTRS
4354
4355/* --------------------------------------------------------------------- */
4356
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004357static int __init thermal_init(struct ibm_init_struct *iibm)
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004358{
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004359 u8 t, ta1, ta2;
4360 int i;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004361 int acpi_tmp7;
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004362 int res;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004363
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004364 vdbg_printk(TPACPI_DBG_INIT, "initializing thermal subdriver\n");
4365
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03004366 acpi_tmp7 = acpi_evalf(ec_handle, NULL, "TMP7", "qv");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004367
Henrique de Moraes Holschuh3d6f99c2007-07-18 23:45:46 -03004368 if (thinkpad_id.ec_model) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004369 /*
4370 * Direct EC access mode: sensors at registers
4371 * 0x78-0x7F, 0xC0-0xC7. Registers return 0x00 for
4372 * non-implemented, thermal sensors return 0x80 when
4373 * not available
4374 */
4375
4376 ta1 = ta2 = 0;
4377 for (i = 0; i < 8; i++) {
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004378 if (acpi_ec_read(TP_EC_THERMAL_TMP0 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004379 ta1 |= t;
4380 } else {
4381 ta1 = 0;
4382 break;
4383 }
Henrique de Moraes Holschuh04cc8622007-04-21 11:08:43 -03004384 if (acpi_ec_read(TP_EC_THERMAL_TMP8 + i, &t)) {
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004385 ta2 |= t;
4386 } else {
4387 ta1 = 0;
4388 break;
4389 }
4390 }
4391 if (ta1 == 0) {
4392 /* This is sheer paranoia, but we handle it anyway */
4393 if (acpi_tmp7) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004394 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004395 "ThinkPad ACPI EC access misbehaving, "
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004396 "falling back to ACPI TMPx access "
4397 "mode\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004398 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004399 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004400 printk(TPACPI_ERR
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004401 "ThinkPad ACPI EC access misbehaving, "
4402 "disabling thermal sensors access\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004403 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004404 }
4405 } else {
4406 thermal_read_mode =
4407 (ta2 != 0) ?
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004408 TPACPI_THERMAL_TPEC_16 : TPACPI_THERMAL_TPEC_8;
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02004409 }
4410 } else if (acpi_tmp7) {
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004411 if (acpi_evalf(ec_handle, NULL, "UPDT", "qv")) {
4412 /* 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004413 thermal_read_mode = TPACPI_THERMAL_ACPI_UPDT;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004414 } else {
4415 /* Standard ACPI TMPx access, max 8 sensors */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004416 thermal_read_mode = TPACPI_THERMAL_ACPI_TMP07;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004417 }
4418 } else {
4419 /* temperatures not supported on 570, G4x, R30, R31, R32 */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004420 thermal_read_mode = TPACPI_THERMAL_NONE;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004421 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004422
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004423 vdbg_printk(TPACPI_DBG_INIT, "thermal is %s, mode %d\n",
4424 str_supported(thermal_read_mode != TPACPI_THERMAL_NONE),
4425 thermal_read_mode);
4426
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004427 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004428 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004429 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004430 &thermal_temp_input16_group);
4431 if (res)
4432 return res;
4433 break;
4434 case TPACPI_THERMAL_TPEC_8:
4435 case TPACPI_THERMAL_ACPI_TMP07:
4436 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004437 res = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004438 &thermal_temp_input8_group);
4439 if (res)
4440 return res;
4441 break;
4442 case TPACPI_THERMAL_NONE:
4443 default:
4444 return 1;
4445 }
4446
4447 return 0;
4448}
4449
4450static void thermal_exit(void)
4451{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004452 switch (thermal_read_mode) {
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004453 case TPACPI_THERMAL_TPEC_16:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004454 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004455 &thermal_temp_input16_group);
4456 break;
4457 case TPACPI_THERMAL_TPEC_8:
4458 case TPACPI_THERMAL_ACPI_TMP07:
4459 case TPACPI_THERMAL_ACPI_UPDT:
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03004460 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004461 &thermal_temp_input16_group);
4462 break;
4463 case TPACPI_THERMAL_NONE:
4464 default:
4465 break;
4466 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004467}
4468
4469static int thermal_read(char *p)
4470{
4471 int len = 0;
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004472 int n, i;
4473 struct ibm_thermal_sensors_struct t;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004474
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004475 n = thermal_get_sensors(&t);
4476 if (unlikely(n < 0))
4477 return n;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004478
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004479 len += sprintf(p + len, "temperatures:\t");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004480
Henrique de Moraes Holschuha26f8782006-11-24 11:47:08 -02004481 if (n > 0) {
4482 for (i = 0; i < (n - 1); i++)
4483 len += sprintf(p + len, "%d ", t.temp[i] / 1000);
4484 len += sprintf(p + len, "%d\n", t.temp[i] / 1000);
4485 } else
4486 len += sprintf(p + len, "not supported\n");
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004487
4488 return len;
4489}
4490
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004491static struct ibm_struct thermal_driver_data = {
4492 .name = "thermal",
4493 .read = thermal_read,
Henrique de Moraes Holschuh2c37aa42007-04-24 11:48:16 -03004494 .exit = thermal_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004495};
4496
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004497/*************************************************************************
4498 * EC Dump subdriver
4499 */
4500
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004501static u8 ecdump_regs[256];
4502
4503static int ecdump_read(char *p)
4504{
4505 int len = 0;
4506 int i, j;
4507 u8 v;
4508
4509 len += sprintf(p + len, "EC "
4510 " +00 +01 +02 +03 +04 +05 +06 +07"
4511 " +08 +09 +0a +0b +0c +0d +0e +0f\n");
4512 for (i = 0; i < 256; i += 16) {
4513 len += sprintf(p + len, "EC 0x%02x:", i);
4514 for (j = 0; j < 16; j++) {
4515 if (!acpi_ec_read(i + j, &v))
4516 break;
4517 if (v != ecdump_regs[i + j])
4518 len += sprintf(p + len, " *%02x", v);
4519 else
4520 len += sprintf(p + len, " %02x", v);
4521 ecdump_regs[i + j] = v;
4522 }
4523 len += sprintf(p + len, "\n");
4524 if (j != 16)
4525 break;
4526 }
4527
4528 /* These are way too dangerous to advertise openly... */
4529#if 0
4530 len += sprintf(p + len, "commands:\t0x<offset> 0x<value>"
4531 " (<offset> is 00-ff, <value> is 00-ff)\n");
4532 len += sprintf(p + len, "commands:\t0x<offset> <value> "
4533 " (<offset> is 00-ff, <value> is 0-255)\n");
4534#endif
4535 return len;
4536}
4537
4538static int ecdump_write(char *buf)
4539{
4540 char *cmd;
4541 int i, v;
4542
4543 while ((cmd = next_cmd(&buf))) {
4544 if (sscanf(cmd, "0x%x 0x%x", &i, &v) == 2) {
4545 /* i and v set */
4546 } else if (sscanf(cmd, "0x%x %u", &i, &v) == 2) {
4547 /* i and v set */
4548 } else
4549 return -EINVAL;
4550 if (i >= 0 && i < 256 && v >= 0 && v < 256) {
4551 if (!acpi_ec_write(i, v))
4552 return -EIO;
4553 } else
4554 return -EINVAL;
4555 }
4556
4557 return 0;
4558}
4559
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004560static struct ibm_struct ecdump_driver_data = {
4561 .name = "ecdump",
4562 .read = ecdump_read,
4563 .write = ecdump_write,
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03004564 .flags.experimental = 1,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004565};
4566
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004567/*************************************************************************
4568 * Backlight/brightness subdriver
4569 */
Holger Macht8acb0252006-10-20 14:30:28 -07004570
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004571#define TPACPI_BACKLIGHT_DEV_NAME "thinkpad_screen"
4572
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004573enum {
4574 TP_EC_BACKLIGHT = 0x31,
4575
4576 /* TP_EC_BACKLIGHT bitmasks */
4577 TP_EC_BACKLIGHT_LVLMSK = 0x1F,
4578 TP_EC_BACKLIGHT_CMDMSK = 0xE0,
4579 TP_EC_BACKLIGHT_MAPSW = 0x20,
4580};
4581
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03004582static struct backlight_device *ibm_backlight_device;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004583static int brightness_mode;
4584static unsigned int brightness_enable = 2; /* 2 = auto, 0 = no, 1 = yes */
4585
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004586static struct mutex brightness_mutex;
4587
4588/*
4589 * ThinkPads can read brightness from two places: EC 0x31, or
4590 * CMOS NVRAM byte 0x5E, bits 0-3.
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004591 *
4592 * EC 0x31 has the following layout
4593 * Bit 7: unknown function
4594 * Bit 6: unknown function
4595 * Bit 5: Z: honour scale changes, NZ: ignore scale changes
4596 * Bit 4: must be set to zero to avoid problems
4597 * Bit 3-0: backlight brightness level
4598 *
4599 * brightness_get_raw returns status data in the EC 0x31 layout
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004600 */
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004601static int brightness_get_raw(int *status)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004602{
4603 u8 lec = 0, lcmos = 0, level = 0;
4604
4605 if (brightness_mode & 1) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004606 if (!acpi_ec_read(TP_EC_BACKLIGHT, &lec))
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004607 return -EIO;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004608 level = lec & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004609 };
4610 if (brightness_mode & 2) {
4611 lcmos = (nvram_read_byte(TP_NVRAM_ADDR_BRIGHTNESS)
4612 & TP_NVRAM_MASK_LEVEL_BRIGHTNESS)
4613 >> TP_NVRAM_POS_LEVEL_BRIGHTNESS;
4614 lcmos &= (tp_features.bright_16levels)? 0x0f : 0x07;
4615 level = lcmos;
4616 }
4617
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004618 if (brightness_mode == 3) {
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004619 *status = lec; /* Prefer EC, CMOS is just a backing store */
4620 lec &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuh2d5e94d2008-04-26 01:02:20 -03004621 if (lec == lcmos)
4622 tp_warned.bright_cmos_ec_unsync = 0;
4623 else {
4624 if (!tp_warned.bright_cmos_ec_unsync) {
4625 printk(TPACPI_ERR
4626 "CMOS NVRAM (%u) and EC (%u) do not "
4627 "agree on display brightness level\n",
4628 (unsigned int) lcmos,
4629 (unsigned int) lec);
4630 tp_warned.bright_cmos_ec_unsync = 1;
4631 }
4632 return -EIO;
4633 }
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004634 } else {
4635 *status = level;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004636 }
4637
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004638 return 0;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004639}
4640
4641/* May return EINTR which can always be mapped to ERESTARTSYS */
4642static int brightness_set(int value)
4643{
4644 int cmos_cmd, inc, i, res;
4645 int current_value;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004646 int command_bits;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004647
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004648 if (value > ((tp_features.bright_16levels)? 15 : 7) ||
4649 value < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004650 return -EINVAL;
4651
4652 res = mutex_lock_interruptible(&brightness_mutex);
4653 if (res < 0)
4654 return res;
4655
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004656 res = brightness_get_raw(&current_value);
4657 if (res < 0)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004658 goto errout;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004659
4660 command_bits = current_value & TP_EC_BACKLIGHT_CMDMSK;
4661 current_value &= TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004662
4663 cmos_cmd = value > current_value ?
4664 TP_CMOS_BRIGHTNESS_UP :
4665 TP_CMOS_BRIGHTNESS_DOWN;
4666 inc = (value > current_value)? 1 : -1;
4667
4668 res = 0;
4669 for (i = current_value; i != value; i += inc) {
4670 if ((brightness_mode & 2) &&
4671 issue_thinkpad_cmos_command(cmos_cmd)) {
4672 res = -EIO;
4673 goto errout;
4674 }
4675 if ((brightness_mode & 1) &&
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004676 !acpi_ec_write(TP_EC_BACKLIGHT,
4677 (i + inc) | command_bits)) {
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004678 res = -EIO;
4679 goto errout;;
4680 }
4681 }
4682
4683errout:
4684 mutex_unlock(&brightness_mutex);
4685 return res;
4686}
4687
4688/* sysfs backlight class ----------------------------------------------- */
4689
4690static int brightness_update_status(struct backlight_device *bd)
4691{
4692 /* it is the backlight class's job (caller) to handle
4693 * EINTR and other errors properly */
4694 return brightness_set(
4695 (bd->props.fb_blank == FB_BLANK_UNBLANK &&
4696 bd->props.power == FB_BLANK_UNBLANK) ?
4697 bd->props.brightness : 0);
4698}
Holger Macht8acb0252006-10-20 14:30:28 -07004699
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004700static int brightness_get(struct backlight_device *bd)
4701{
4702 int status, res;
4703
4704 res = brightness_get_raw(&status);
4705 if (res < 0)
4706 return 0; /* FIXME: teach backlight about error handling */
4707
4708 return status & TP_EC_BACKLIGHT_LVLMSK;
4709}
4710
Richard Purdie599a52d2007-02-10 23:07:48 +00004711static struct backlight_ops ibm_backlight_data = {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004712 .get_brightness = brightness_get,
4713 .update_status = brightness_update_status,
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004714};
4715
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02004716/* --------------------------------------------------------------------- */
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004717
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004718static int __init brightness_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004719{
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004720 int b;
4721
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004722 vdbg_printk(TPACPI_DBG_INIT, "initializing brightness subdriver\n");
4723
Henrique de Moraes Holschuhf4322552007-07-18 23:45:48 -03004724 mutex_init(&brightness_mutex);
4725
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004726 /*
4727 * We always attempt to detect acpi support, so as to switch
4728 * Lenovo Vista BIOS to ACPI brightness mode even if we are not
4729 * going to publish a backlight interface
4730 */
4731 b = tpacpi_check_std_acpi_brightness_support();
4732 if (b > 0) {
4733 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO) {
4734 printk(TPACPI_NOTICE
4735 "Lenovo BIOS switched to ACPI backlight "
4736 "control mode\n");
4737 }
4738 if (brightness_enable > 1) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004739 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004740 "standard ACPI backlight interface "
4741 "available, not loading native one...\n");
Henrique de Moraes Holschuhe11e2112007-10-30 17:46:22 -02004742 return 1;
4743 }
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02004744 }
4745
Henrique de Moraes Holschuhb5972792008-04-26 01:02:17 -03004746 if (!brightness_enable) {
4747 dbg_printk(TPACPI_DBG_INIT,
4748 "brightness support disabled by "
4749 "module parameter\n");
4750 return 1;
4751 }
4752
4753 if (b > 16) {
4754 printk(TPACPI_ERR
4755 "Unsupported brightness interface, "
4756 "please contact %s\n", TPACPI_MAIL);
4757 return 1;
4758 }
4759 if (b == 16)
4760 tp_features.bright_16levels = 1;
4761
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004762 if (!brightness_mode) {
4763 if (thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO)
4764 brightness_mode = 2;
4765 else
4766 brightness_mode = 3;
4767
4768 dbg_printk(TPACPI_DBG_INIT, "selected brightness_mode=%d\n",
4769 brightness_mode);
4770 }
4771
4772 if (brightness_mode > 3)
4773 return -EINVAL;
4774
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004775 if (brightness_get_raw(&b) < 0)
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03004776 return 1;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004777
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004778 if (tp_features.bright_16levels)
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004779 printk(TPACPI_INFO
4780 "detected a 16-level brightness capable ThinkPad\n");
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004781
Henrique de Moraes Holschuh7d5a0152007-04-24 11:48:20 -03004782 ibm_backlight_device = backlight_device_register(
4783 TPACPI_BACKLIGHT_DEV_NAME, NULL, NULL,
4784 &ibm_backlight_data);
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004785 if (IS_ERR(ibm_backlight_device)) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02004786 printk(TPACPI_ERR "Could not register backlight device\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004787 return PTR_ERR(ibm_backlight_device);
4788 }
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004789 vdbg_printk(TPACPI_DBG_INIT, "brightness is supported\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004790
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004791 ibm_backlight_device->props.max_brightness =
4792 (tp_features.bright_16levels)? 15 : 7;
Henrique de Moraes Holschuhe11aecf2008-04-26 01:02:21 -03004793 ibm_backlight_device->props.brightness = b & TP_EC_BACKLIGHT_LVLMSK;
Henrique de Moraes Holschuhadb00582007-02-22 16:04:55 -02004794 backlight_update_status(ibm_backlight_device);
Richard Purdie599a52d2007-02-10 23:07:48 +00004795
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004796 return 0;
4797}
4798
4799static void brightness_exit(void)
4800{
4801 if (ibm_backlight_device) {
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03004802 vdbg_printk(TPACPI_DBG_EXIT,
4803 "calling backlight_device_unregister()\n");
Henrique de Moraes Holschuhfb87a812006-11-25 16:35:09 -02004804 backlight_device_unregister(ibm_backlight_device);
4805 ibm_backlight_device = NULL;
4806 }
4807}
4808
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004809static int brightness_read(char *p)
4810{
4811 int len = 0;
4812 int level;
4813
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004814 level = brightness_get(NULL);
4815 if (level < 0) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004816 len += sprintf(p + len, "level:\t\tunreadable\n");
4817 } else {
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004818 len += sprintf(p + len, "level:\t\t%d\n", level);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004819 len += sprintf(p + len, "commands:\tup, down\n");
4820 len += sprintf(p + len, "commands:\tlevel <level>"
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004821 " (<level> is 0-%d)\n",
4822 (tp_features.bright_16levels) ? 15 : 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004823 }
4824
4825 return len;
4826}
4827
4828static int brightness_write(char *buf)
4829{
4830 int level;
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004831 int rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004832 char *cmd;
Henrique de Moraes Holschuha3f104c2007-10-30 17:46:20 -02004833 int max_level = (tp_features.bright_16levels) ? 15 : 7;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004834
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004835 level = brightness_get(NULL);
4836 if (level < 0)
4837 return level;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004838
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004839 while ((cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004840 if (strlencmp(cmd, "up") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004841 if (level < max_level)
4842 level++;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004843 } else if (strlencmp(cmd, "down") == 0) {
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004844 if (level > 0)
4845 level--;
4846 } else if (sscanf(cmd, "level %d", &level) == 1 &&
4847 level >= 0 && level <= max_level) {
4848 /* new level set */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004849 } else
4850 return -EINVAL;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004851 }
4852
Henrique de Moraes Holschuh4273af82007-10-30 17:46:25 -02004853 /*
4854 * Now we know what the final level should be, so we try to set it.
4855 * Doing it this way makes the syscall restartable in case of EINTR
4856 */
4857 rc = brightness_set(level);
4858 return (rc == -EINTR)? ERESTARTSYS : rc;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004859}
4860
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004861static struct ibm_struct brightness_driver_data = {
4862 .name = "brightness",
4863 .read = brightness_read,
4864 .write = brightness_write,
4865 .exit = brightness_exit,
4866};
4867
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004868/*************************************************************************
4869 * Volume subdriver
4870 */
4871
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02004872static int volume_offset = 0x30;
4873
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004874static int volume_read(char *p)
4875{
4876 int len = 0;
4877 u8 level;
4878
4879 if (!acpi_ec_read(volume_offset, &level)) {
4880 len += sprintf(p + len, "level:\t\tunreadable\n");
4881 } else {
4882 len += sprintf(p + len, "level:\t\t%d\n", level & 0xf);
4883 len += sprintf(p + len, "mute:\t\t%s\n", onoff(level, 6));
4884 len += sprintf(p + len, "commands:\tup, down, mute\n");
4885 len += sprintf(p + len, "commands:\tlevel <level>"
4886 " (<level> is 0-15)\n");
4887 }
4888
4889 return len;
4890}
4891
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004892static int volume_write(char *buf)
4893{
4894 int cmos_cmd, inc, i;
4895 u8 level, mute;
4896 int new_level, new_mute;
4897 char *cmd;
4898
4899 while ((cmd = next_cmd(&buf))) {
4900 if (!acpi_ec_read(volume_offset, &level))
4901 return -EIO;
4902 new_mute = mute = level & 0x40;
4903 new_level = level = level & 0xf;
4904
4905 if (strlencmp(cmd, "up") == 0) {
4906 if (mute)
4907 new_mute = 0;
4908 else
4909 new_level = level == 15 ? 15 : level + 1;
4910 } else if (strlencmp(cmd, "down") == 0) {
4911 if (mute)
4912 new_mute = 0;
4913 else
4914 new_level = level == 0 ? 0 : level - 1;
4915 } else if (sscanf(cmd, "level %d", &new_level) == 1 &&
4916 new_level >= 0 && new_level <= 15) {
4917 /* new_level set */
4918 } else if (strlencmp(cmd, "mute") == 0) {
4919 new_mute = 0x40;
4920 } else
4921 return -EINVAL;
4922
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004923 if (new_level != level) {
4924 /* mute doesn't change */
4925
4926 cmos_cmd = (new_level > level) ?
4927 TP_CMOS_VOLUME_UP : TP_CMOS_VOLUME_DOWN;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004928 inc = new_level > level ? 1 : -1;
4929
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004930 if (mute && (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004931 !acpi_ec_write(volume_offset, level)))
4932 return -EIO;
4933
4934 for (i = level; i != new_level; i += inc)
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004935 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004936 !acpi_ec_write(volume_offset, i + inc))
4937 return -EIO;
4938
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004939 if (mute &&
4940 (issue_thinkpad_cmos_command(TP_CMOS_VOLUME_MUTE) ||
4941 !acpi_ec_write(volume_offset, new_level + mute))) {
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004942 return -EIO;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004943 }
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004944 }
4945
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02004946 if (new_mute != mute) {
4947 /* level doesn't change */
4948
4949 cmos_cmd = (new_mute) ?
4950 TP_CMOS_VOLUME_MUTE : TP_CMOS_VOLUME_UP;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004951
Henrique de Moraes Holschuhc9bea992007-04-21 11:08:42 -03004952 if (issue_thinkpad_cmos_command(cmos_cmd) ||
Borislav Deianov78f81cc2005-08-17 00:00:00 -04004953 !acpi_ec_write(volume_offset, level + new_mute))
4954 return -EIO;
4955 }
4956 }
4957
4958 return 0;
4959}
4960
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03004961static struct ibm_struct volume_driver_data = {
4962 .name = "volume",
4963 .read = volume_read,
4964 .write = volume_write,
4965};
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004966
4967/*************************************************************************
4968 * Fan subdriver
4969 */
4970
4971/*
4972 * FAN ACCESS MODES
4973 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004974 * TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004975 * ACPI GFAN method: returns fan level
4976 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004977 * see TPACPI_FAN_WR_ACPI_SFAN
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004978 * EC 0x2f (HFSP) not available if GFAN exists
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004979 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004980 * TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004981 * ACPI SFAN method: sets fan level, 0 (stop) to 7 (max)
4982 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004983 * EC 0x2f (HFSP) might be available *for reading*, but do not use
4984 * it for writing.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004985 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03004986 * TPACPI_FAN_WR_TPEC:
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03004987 * ThinkPad EC register 0x2f (HFSP): fan control loop mode
4988 * Supported on almost all ThinkPads
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03004989 *
4990 * Fan speed changes of any sort (including those caused by the
4991 * disengaged mode) are usually done slowly by the firmware as the
4992 * maximum ammount of fan duty cycle change per second seems to be
4993 * limited.
4994 *
4995 * Reading is not available if GFAN exists.
4996 * Writing is not available if SFAN exists.
4997 *
4998 * Bits
4999 * 7 automatic mode engaged;
5000 * (default operation mode of the ThinkPad)
5001 * fan level is ignored in this mode.
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005002 * 6 full speed mode (takes precedence over bit 7);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005003 * not available on all thinkpads. May disable
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005004 * the tachometer while the fan controller ramps up
5005 * the speed (which can take up to a few *minutes*).
5006 * Speeds up fan to 100% duty-cycle, which is far above
5007 * the standard RPM levels. It is not impossible that
5008 * it could cause hardware damage.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005009 * 5-3 unused in some models. Extra bits for fan level
5010 * in others, but still useless as all values above
5011 * 7 map to the same speed as level 7 in these models.
5012 * 2-0 fan level (0..7 usually)
5013 * 0x00 = stop
5014 * 0x07 = max (set when temperatures critical)
5015 * Some ThinkPads may have other levels, see
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005016 * TPACPI_FAN_WR_ACPI_FANS (X31/X40/X41)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005017 *
5018 * FIRMWARE BUG: on some models, EC 0x2f might not be initialized at
5019 * boot. Apparently the EC does not intialize it, so unless ACPI DSDT
5020 * does so, its initial value is meaningless (0x07).
5021 *
5022 * For firmware bugs, refer to:
5023 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5024 *
5025 * ----
5026 *
5027 * ThinkPad EC register 0x84 (LSB), 0x85 (MSB):
5028 * Main fan tachometer reading (in RPM)
5029 *
5030 * This register is present on all ThinkPads with a new-style EC, and
5031 * it is known not to be present on the A21m/e, and T22, as there is
5032 * something else in offset 0x84 according to the ACPI DSDT. Other
5033 * ThinkPads from this same time period (and earlier) probably lack the
5034 * tachometer as well.
5035 *
5036 * Unfortunately a lot of ThinkPads with new-style ECs but whose firwmare
5037 * was never fixed by IBM to report the EC firmware version string
5038 * probably support the tachometer (like the early X models), so
5039 * detecting it is quite hard. We need more data to know for sure.
5040 *
5041 * FIRMWARE BUG: always read 0x84 first, otherwise incorrect readings
5042 * might result.
5043 *
Henrique de Moraes Holschuhf51d1a32007-04-21 11:08:29 -03005044 * FIRMWARE BUG: may go stale while the EC is switching to full speed
5045 * mode.
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005046 *
5047 * For firmware bugs, refer to:
5048 * http://thinkwiki.org/wiki/Embedded_Controller_Firmware#Firmware_Issues
5049 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005050 * TPACPI_FAN_WR_ACPI_FANS:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005051 * ThinkPad X31, X40, X41. Not available in the X60.
5052 *
5053 * FANS ACPI handle: takes three arguments: low speed, medium speed,
5054 * high speed. ACPI DSDT seems to map these three speeds to levels
5055 * as follows: STOP LOW LOW MED MED HIGH HIGH HIGH HIGH
5056 * (this map is stored on FAN0..FAN8 as "0,1,1,2,2,3,3,3,3")
5057 *
5058 * The speeds are stored on handles
5059 * (FANA:FAN9), (FANC:FANB), (FANE:FAND).
5060 *
5061 * There are three default speed sets, acessible as handles:
5062 * FS1L,FS1M,FS1H; FS2L,FS2M,FS2H; FS3L,FS3M,FS3H
5063 *
5064 * ACPI DSDT switches which set is in use depending on various
5065 * factors.
5066 *
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005067 * TPACPI_FAN_WR_TPEC is also available and should be used to
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005068 * command the fan. The X31/X40/X41 seems to have 8 fan levels,
5069 * but the ACPI tables just mention level 7.
5070 */
5071
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005072enum { /* Fan control constants */
5073 fan_status_offset = 0x2f, /* EC register 0x2f */
5074 fan_rpm_offset = 0x84, /* EC register 0x84: LSB, 0x85 MSB (RPM)
5075 * 0x84 must be read before 0x85 */
5076
5077 TP_EC_FAN_FULLSPEED = 0x40, /* EC fan mode: full speed */
5078 TP_EC_FAN_AUTO = 0x80, /* EC fan mode: auto fan control */
5079
5080 TPACPI_FAN_LAST_LEVEL = 0x100, /* Use cached last-seen fan level */
5081};
5082
5083enum fan_status_access_mode {
5084 TPACPI_FAN_NONE = 0, /* No fan status or control */
5085 TPACPI_FAN_RD_ACPI_GFAN, /* Use ACPI GFAN */
5086 TPACPI_FAN_RD_TPEC, /* Use ACPI EC regs 0x2f, 0x84-0x85 */
5087};
5088
5089enum fan_control_access_mode {
5090 TPACPI_FAN_WR_NONE = 0, /* No fan control */
5091 TPACPI_FAN_WR_ACPI_SFAN, /* Use ACPI SFAN */
5092 TPACPI_FAN_WR_TPEC, /* Use ACPI EC reg 0x2f */
5093 TPACPI_FAN_WR_ACPI_FANS, /* Use ACPI FANS and EC reg 0x2f */
5094};
5095
5096enum fan_control_commands {
5097 TPACPI_FAN_CMD_SPEED = 0x0001, /* speed command */
5098 TPACPI_FAN_CMD_LEVEL = 0x0002, /* level command */
5099 TPACPI_FAN_CMD_ENABLE = 0x0004, /* enable/disable cmd,
5100 * and also watchdog cmd */
5101};
5102
5103static int fan_control_allowed;
5104
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005105static enum fan_status_access_mode fan_status_access_mode;
5106static enum fan_control_access_mode fan_control_access_mode;
5107static enum fan_control_commands fan_control_commands;
5108
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005109static u8 fan_control_initial_status;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005110static u8 fan_control_desired_level;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005111static int fan_watchdog_maxinterval;
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005112
5113static struct mutex fan_mutex;
5114
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02005115static void fan_watchdog_fire(struct work_struct *ignored);
Len Brown25c68a32006-12-08 04:43:41 -05005116static DECLARE_DELAYED_WORK(fan_watchdog_task, fan_watchdog_fire);
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005117
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005118TPACPI_HANDLE(fans, ec, "FANS"); /* X31, X40, X41 */
5119TPACPI_HANDLE(gfan, ec, "GFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005120 "\\FSPD", /* 600e/x, 770e, 770x */
5121 ); /* all others */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005122TPACPI_HANDLE(sfan, ec, "SFAN", /* 570 */
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005123 "JFNS", /* 770x-JL */
5124 ); /* all others */
5125
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005126/*
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005127 * Call with fan_mutex held
5128 */
5129static void fan_update_desired_level(u8 status)
5130{
5131 if ((status &
5132 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5133 if (status > 7)
5134 fan_control_desired_level = 7;
5135 else
5136 fan_control_desired_level = status;
5137 }
5138}
5139
5140static int fan_get_status(u8 *status)
5141{
5142 u8 s;
5143
5144 /* TODO:
5145 * Add TPACPI_FAN_RD_ACPI_FANS ? */
5146
5147 switch (fan_status_access_mode) {
5148 case TPACPI_FAN_RD_ACPI_GFAN:
5149 /* 570, 600e/x, 770e, 770x */
5150
5151 if (unlikely(!acpi_evalf(gfan_handle, &s, NULL, "d")))
5152 return -EIO;
5153
5154 if (likely(status))
5155 *status = s & 0x07;
5156
5157 break;
5158
5159 case TPACPI_FAN_RD_TPEC:
5160 /* all except 570, 600e/x, 770e, 770x */
5161 if (unlikely(!acpi_ec_read(fan_status_offset, &s)))
5162 return -EIO;
5163
5164 if (likely(status))
5165 *status = s;
5166
5167 break;
5168
5169 default:
5170 return -ENXIO;
5171 }
5172
5173 return 0;
5174}
5175
5176static int fan_get_status_safe(u8 *status)
5177{
5178 int rc;
5179 u8 s;
5180
5181 if (mutex_lock_interruptible(&fan_mutex))
5182 return -ERESTARTSYS;
5183 rc = fan_get_status(&s);
5184 if (!rc)
5185 fan_update_desired_level(s);
5186 mutex_unlock(&fan_mutex);
5187
5188 if (status)
5189 *status = s;
5190
5191 return rc;
5192}
5193
5194static int fan_get_speed(unsigned int *speed)
5195{
5196 u8 hi, lo;
5197
5198 switch (fan_status_access_mode) {
5199 case TPACPI_FAN_RD_TPEC:
5200 /* all except 570, 600e/x, 770e, 770x */
5201 if (unlikely(!acpi_ec_read(fan_rpm_offset, &lo) ||
5202 !acpi_ec_read(fan_rpm_offset + 1, &hi)))
5203 return -EIO;
5204
5205 if (likely(speed))
5206 *speed = (hi << 8) | lo;
5207
5208 break;
5209
5210 default:
5211 return -ENXIO;
5212 }
5213
5214 return 0;
5215}
5216
5217static int fan_set_level(int level)
5218{
5219 if (!fan_control_allowed)
5220 return -EPERM;
5221
5222 switch (fan_control_access_mode) {
5223 case TPACPI_FAN_WR_ACPI_SFAN:
5224 if (level >= 0 && level <= 7) {
5225 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", level))
5226 return -EIO;
5227 } else
5228 return -EINVAL;
5229 break;
5230
5231 case TPACPI_FAN_WR_ACPI_FANS:
5232 case TPACPI_FAN_WR_TPEC:
5233 if ((level != TP_EC_FAN_AUTO) &&
5234 (level != TP_EC_FAN_FULLSPEED) &&
5235 ((level < 0) || (level > 7)))
5236 return -EINVAL;
5237
5238 /* safety net should the EC not support AUTO
5239 * or FULLSPEED mode bits and just ignore them */
5240 if (level & TP_EC_FAN_FULLSPEED)
5241 level |= 7; /* safety min speed 7 */
Roel Kluin547266e2008-02-05 00:24:56 +01005242 else if (level & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005243 level |= 4; /* safety min speed 4 */
5244
5245 if (!acpi_ec_write(fan_status_offset, level))
5246 return -EIO;
5247 else
5248 tp_features.fan_ctrl_status_undef = 0;
5249 break;
5250
5251 default:
5252 return -ENXIO;
5253 }
5254 return 0;
5255}
5256
5257static int fan_set_level_safe(int level)
5258{
5259 int rc;
5260
5261 if (!fan_control_allowed)
5262 return -EPERM;
5263
5264 if (mutex_lock_interruptible(&fan_mutex))
5265 return -ERESTARTSYS;
5266
5267 if (level == TPACPI_FAN_LAST_LEVEL)
5268 level = fan_control_desired_level;
5269
5270 rc = fan_set_level(level);
5271 if (!rc)
5272 fan_update_desired_level(level);
5273
5274 mutex_unlock(&fan_mutex);
5275 return rc;
5276}
5277
5278static int fan_set_enable(void)
5279{
5280 u8 s;
5281 int rc;
5282
5283 if (!fan_control_allowed)
5284 return -EPERM;
5285
5286 if (mutex_lock_interruptible(&fan_mutex))
5287 return -ERESTARTSYS;
5288
5289 switch (fan_control_access_mode) {
5290 case TPACPI_FAN_WR_ACPI_FANS:
5291 case TPACPI_FAN_WR_TPEC:
5292 rc = fan_get_status(&s);
5293 if (rc < 0)
5294 break;
5295
5296 /* Don't go out of emergency fan mode */
5297 if (s != 7) {
5298 s &= 0x07;
5299 s |= TP_EC_FAN_AUTO | 4; /* min fan speed 4 */
5300 }
5301
5302 if (!acpi_ec_write(fan_status_offset, s))
5303 rc = -EIO;
5304 else {
5305 tp_features.fan_ctrl_status_undef = 0;
5306 rc = 0;
5307 }
5308 break;
5309
5310 case TPACPI_FAN_WR_ACPI_SFAN:
5311 rc = fan_get_status(&s);
5312 if (rc < 0)
5313 break;
5314
5315 s &= 0x07;
5316
5317 /* Set fan to at least level 4 */
5318 s |= 4;
5319
5320 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", s))
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005321 rc = -EIO;
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005322 else
5323 rc = 0;
5324 break;
5325
5326 default:
5327 rc = -ENXIO;
5328 }
5329
5330 mutex_unlock(&fan_mutex);
5331 return rc;
5332}
5333
5334static int fan_set_disable(void)
5335{
5336 int rc;
5337
5338 if (!fan_control_allowed)
5339 return -EPERM;
5340
5341 if (mutex_lock_interruptible(&fan_mutex))
5342 return -ERESTARTSYS;
5343
5344 rc = 0;
5345 switch (fan_control_access_mode) {
5346 case TPACPI_FAN_WR_ACPI_FANS:
5347 case TPACPI_FAN_WR_TPEC:
5348 if (!acpi_ec_write(fan_status_offset, 0x00))
5349 rc = -EIO;
5350 else {
5351 fan_control_desired_level = 0;
5352 tp_features.fan_ctrl_status_undef = 0;
5353 }
5354 break;
5355
5356 case TPACPI_FAN_WR_ACPI_SFAN:
5357 if (!acpi_evalf(sfan_handle, NULL, NULL, "vd", 0x00))
5358 rc = -EIO;
5359 else
5360 fan_control_desired_level = 0;
5361 break;
5362
5363 default:
5364 rc = -ENXIO;
5365 }
5366
5367
5368 mutex_unlock(&fan_mutex);
5369 return rc;
5370}
5371
5372static int fan_set_speed(int speed)
5373{
5374 int rc;
5375
5376 if (!fan_control_allowed)
5377 return -EPERM;
5378
5379 if (mutex_lock_interruptible(&fan_mutex))
5380 return -ERESTARTSYS;
5381
5382 rc = 0;
5383 switch (fan_control_access_mode) {
5384 case TPACPI_FAN_WR_ACPI_FANS:
5385 if (speed >= 0 && speed <= 65535) {
5386 if (!acpi_evalf(fans_handle, NULL, NULL, "vddd",
5387 speed, speed, speed))
5388 rc = -EIO;
5389 } else
5390 rc = -EINVAL;
5391 break;
5392
5393 default:
5394 rc = -ENXIO;
5395 }
5396
5397 mutex_unlock(&fan_mutex);
5398 return rc;
5399}
5400
5401static void fan_watchdog_reset(void)
5402{
5403 static int fan_watchdog_active;
5404
5405 if (fan_control_access_mode == TPACPI_FAN_WR_NONE)
5406 return;
5407
5408 if (fan_watchdog_active)
5409 cancel_delayed_work(&fan_watchdog_task);
5410
5411 if (fan_watchdog_maxinterval > 0 &&
5412 tpacpi_lifecycle != TPACPI_LIFE_EXITING) {
5413 fan_watchdog_active = 1;
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005414 if (!queue_delayed_work(tpacpi_wq, &fan_watchdog_task,
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005415 msecs_to_jiffies(fan_watchdog_maxinterval
5416 * 1000))) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005417 printk(TPACPI_ERR
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005418 "failed to queue the fan watchdog, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005419 "watchdog will not trigger\n");
5420 }
5421 } else
5422 fan_watchdog_active = 0;
5423}
5424
5425static void fan_watchdog_fire(struct work_struct *ignored)
5426{
5427 int rc;
5428
5429 if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
5430 return;
5431
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005432 printk(TPACPI_NOTICE "fan watchdog: enabling fan\n");
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005433 rc = fan_set_enable();
5434 if (rc < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005435 printk(TPACPI_ERR "fan watchdog: error %d while enabling fan, "
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02005436 "will try again later...\n", -rc);
5437 /* reschedule for later */
5438 fan_watchdog_reset();
5439 }
5440}
5441
5442/*
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005443 * SYSFS fan layout: hwmon compatible (device)
5444 *
5445 * pwm*_enable:
5446 * 0: "disengaged" mode
5447 * 1: manual mode
5448 * 2: native EC "auto" mode (recommended, hardware default)
5449 *
5450 * pwm*: set speed in manual mode, ignored otherwise.
5451 * 0 is level 0; 255 is level 7. Intermediate points done with linear
5452 * interpolation.
5453 *
5454 * fan*_input: tachometer reading, RPM
5455 *
5456 *
5457 * SYSFS fan layout: extensions
5458 *
5459 * fan_watchdog (driver):
5460 * fan watchdog interval in seconds, 0 disables (default), max 120
5461 */
5462
5463/* sysfs fan pwm1_enable ----------------------------------------------- */
5464static ssize_t fan_pwm1_enable_show(struct device *dev,
5465 struct device_attribute *attr,
5466 char *buf)
5467{
5468 int res, mode;
5469 u8 status;
5470
5471 res = fan_get_status_safe(&status);
5472 if (res)
5473 return res;
5474
5475 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5476 if (status != fan_control_initial_status) {
5477 tp_features.fan_ctrl_status_undef = 0;
5478 } else {
5479 /* Return most likely status. In fact, it
5480 * might be the only possible status */
5481 status = TP_EC_FAN_AUTO;
5482 }
5483 }
5484
5485 if (status & TP_EC_FAN_FULLSPEED) {
5486 mode = 0;
5487 } else if (status & TP_EC_FAN_AUTO) {
5488 mode = 2;
5489 } else
5490 mode = 1;
5491
5492 return snprintf(buf, PAGE_SIZE, "%d\n", mode);
5493}
5494
5495static ssize_t fan_pwm1_enable_store(struct device *dev,
5496 struct device_attribute *attr,
5497 const char *buf, size_t count)
5498{
5499 unsigned long t;
5500 int res, level;
5501
5502 if (parse_strtoul(buf, 2, &t))
5503 return -EINVAL;
5504
5505 switch (t) {
5506 case 0:
5507 level = TP_EC_FAN_FULLSPEED;
5508 break;
5509 case 1:
5510 level = TPACPI_FAN_LAST_LEVEL;
5511 break;
5512 case 2:
5513 level = TP_EC_FAN_AUTO;
5514 break;
5515 case 3:
5516 /* reserved for software-controlled auto mode */
5517 return -ENOSYS;
5518 default:
5519 return -EINVAL;
5520 }
5521
5522 res = fan_set_level_safe(level);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005523 if (res == -ENXIO)
5524 return -EINVAL;
5525 else if (res < 0)
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005526 return res;
5527
5528 fan_watchdog_reset();
5529
5530 return count;
5531}
5532
5533static struct device_attribute dev_attr_fan_pwm1_enable =
5534 __ATTR(pwm1_enable, S_IWUSR | S_IRUGO,
5535 fan_pwm1_enable_show, fan_pwm1_enable_store);
5536
5537/* sysfs fan pwm1 ------------------------------------------------------ */
5538static ssize_t fan_pwm1_show(struct device *dev,
5539 struct device_attribute *attr,
5540 char *buf)
5541{
5542 int res;
5543 u8 status;
5544
5545 res = fan_get_status_safe(&status);
5546 if (res)
5547 return res;
5548
5549 if (unlikely(tp_features.fan_ctrl_status_undef)) {
5550 if (status != fan_control_initial_status) {
5551 tp_features.fan_ctrl_status_undef = 0;
5552 } else {
5553 status = TP_EC_FAN_AUTO;
5554 }
5555 }
5556
5557 if ((status &
5558 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) != 0)
5559 status = fan_control_desired_level;
5560
5561 if (status > 7)
5562 status = 7;
5563
5564 return snprintf(buf, PAGE_SIZE, "%u\n", (status * 255) / 7);
5565}
5566
5567static ssize_t fan_pwm1_store(struct device *dev,
5568 struct device_attribute *attr,
5569 const char *buf, size_t count)
5570{
5571 unsigned long s;
5572 int rc;
5573 u8 status, newlevel;
5574
5575 if (parse_strtoul(buf, 255, &s))
5576 return -EINVAL;
5577
5578 /* scale down from 0-255 to 0-7 */
5579 newlevel = (s >> 5) & 0x07;
5580
Henrique de Moraes Holschuhfc589a32007-10-30 17:46:24 -02005581 if (mutex_lock_interruptible(&fan_mutex))
5582 return -ERESTARTSYS;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005583
5584 rc = fan_get_status(&status);
5585 if (!rc && (status &
5586 (TP_EC_FAN_AUTO | TP_EC_FAN_FULLSPEED)) == 0) {
5587 rc = fan_set_level(newlevel);
Henrique de Moraes Holschuhc573ddb2007-04-27 22:00:12 -03005588 if (rc == -ENXIO)
5589 rc = -EINVAL;
5590 else if (!rc) {
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005591 fan_update_desired_level(newlevel);
Henrique de Moraes Holschuhca4ac2f2007-04-27 22:00:11 -03005592 fan_watchdog_reset();
5593 }
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005594 }
5595
5596 mutex_unlock(&fan_mutex);
5597 return (rc)? rc : count;
5598}
5599
5600static struct device_attribute dev_attr_fan_pwm1 =
5601 __ATTR(pwm1, S_IWUSR | S_IRUGO,
5602 fan_pwm1_show, fan_pwm1_store);
5603
5604/* sysfs fan fan1_input ------------------------------------------------ */
5605static ssize_t fan_fan1_input_show(struct device *dev,
5606 struct device_attribute *attr,
5607 char *buf)
5608{
5609 int res;
5610 unsigned int speed;
5611
5612 res = fan_get_speed(&speed);
5613 if (res < 0)
5614 return res;
5615
5616 return snprintf(buf, PAGE_SIZE, "%u\n", speed);
5617}
5618
5619static struct device_attribute dev_attr_fan_fan1_input =
5620 __ATTR(fan1_input, S_IRUGO,
5621 fan_fan1_input_show, NULL);
5622
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005623/* sysfs fan fan_watchdog (hwmon driver) ------------------------------- */
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005624static ssize_t fan_fan_watchdog_show(struct device_driver *drv,
5625 char *buf)
5626{
5627 return snprintf(buf, PAGE_SIZE, "%u\n", fan_watchdog_maxinterval);
5628}
5629
5630static ssize_t fan_fan_watchdog_store(struct device_driver *drv,
5631 const char *buf, size_t count)
5632{
5633 unsigned long t;
5634
5635 if (parse_strtoul(buf, 120, &t))
5636 return -EINVAL;
5637
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005638 if (!fan_control_allowed)
5639 return -EPERM;
5640
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005641 fan_watchdog_maxinterval = t;
5642 fan_watchdog_reset();
5643
5644 return count;
5645}
5646
5647static DRIVER_ATTR(fan_watchdog, S_IWUSR | S_IRUGO,
5648 fan_fan_watchdog_show, fan_fan_watchdog_store);
5649
5650/* --------------------------------------------------------------------- */
5651static struct attribute *fan_attributes[] = {
5652 &dev_attr_fan_pwm1_enable.attr, &dev_attr_fan_pwm1.attr,
5653 &dev_attr_fan_fan1_input.attr,
5654 NULL
5655};
5656
5657static const struct attribute_group fan_attr_group = {
5658 .attrs = fan_attributes,
5659};
5660
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005661static int __init fan_init(struct ibm_init_struct *iibm)
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005662{
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005663 int rc;
5664
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005665 vdbg_printk(TPACPI_DBG_INIT, "initializing fan subdriver\n");
5666
Henrique de Moraes Holschuh40ca9fd2007-04-24 11:48:15 -03005667 mutex_init(&fan_mutex);
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005668 fan_status_access_mode = TPACPI_FAN_NONE;
5669 fan_control_access_mode = TPACPI_FAN_WR_NONE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005670 fan_control_commands = 0;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005671 fan_watchdog_maxinterval = 0;
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005672 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005673 fan_control_desired_level = 7;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005674
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005675 TPACPI_ACPIHANDLE_INIT(fans);
5676 TPACPI_ACPIHANDLE_INIT(gfan);
5677 TPACPI_ACPIHANDLE_INIT(sfan);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005678
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005679 if (gfan_handle) {
5680 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005681 fan_status_access_mode = TPACPI_FAN_RD_ACPI_GFAN;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005682 } else {
5683 /* all other ThinkPads: note that even old-style
5684 * ThinkPad ECs supports the fan control register */
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005685 if (likely(acpi_ec_read(fan_status_offset,
5686 &fan_control_initial_status))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005687 fan_status_access_mode = TPACPI_FAN_RD_TPEC;
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005688
5689 /* In some ThinkPads, neither the EC nor the ACPI
5690 * DSDT initialize the fan status, and it ends up
5691 * being set to 0x07 when it *could* be either
5692 * 0x07 or 0x80.
5693 *
5694 * Enable for TP-1Y (T43), TP-78 (R51e),
5695 * TP-76 (R52), TP-70 (T43, R52), which are known
5696 * to be buggy. */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005697 if (fan_control_initial_status == 0x07) {
5698 switch (thinkpad_id.ec_model) {
5699 case 0x5931: /* TP-1Y */
5700 case 0x3837: /* TP-78 */
5701 case 0x3637: /* TP-76 */
5702 case 0x3037: /* TP-70 */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005703 printk(TPACPI_NOTICE
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005704 "fan_init: initial fan status "
5705 "is unknown, assuming it is "
5706 "in auto mode\n");
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03005707 tp_features.fan_ctrl_status_undef = 1;
5708 ;;
5709 }
Henrique de Moraes Holschuh778b4d72006-11-24 11:47:14 -02005710 }
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005711 } else {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005712 printk(TPACPI_ERR
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005713 "ThinkPad ACPI EC access misbehaving, "
5714 "fan status and control unavailable\n");
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03005715 return 1;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005716 }
5717 }
5718
5719 if (sfan_handle) {
5720 /* 570, 770x-JL */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005721 fan_control_access_mode = TPACPI_FAN_WR_ACPI_SFAN;
Henrique de Moraes Holschuh1c6a3342006-11-24 11:47:12 -02005722 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005723 TPACPI_FAN_CMD_LEVEL | TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005724 } else {
5725 if (!gfan_handle) {
5726 /* gfan without sfan means no fan control */
5727 /* all other models implement TP EC 0x2f control */
5728
5729 if (fans_handle) {
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005730 /* X31, X40, X41 */
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005731 fan_control_access_mode =
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005732 TPACPI_FAN_WR_ACPI_FANS;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005733 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005734 TPACPI_FAN_CMD_SPEED |
5735 TPACPI_FAN_CMD_LEVEL |
5736 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005737 } else {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005738 fan_control_access_mode = TPACPI_FAN_WR_TPEC;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005739 fan_control_commands |=
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005740 TPACPI_FAN_CMD_LEVEL |
5741 TPACPI_FAN_CMD_ENABLE;
Henrique de Moraes Holschuh69ba91c2006-11-24 11:47:09 -02005742 }
5743 }
5744 }
5745
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03005746 vdbg_printk(TPACPI_DBG_INIT, "fan is %s, modes %d, %d\n",
5747 str_supported(fan_status_access_mode != TPACPI_FAN_NONE ||
5748 fan_control_access_mode != TPACPI_FAN_WR_NONE),
5749 fan_status_access_mode, fan_control_access_mode);
5750
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03005751 /* fan control master switch */
5752 if (!fan_control_allowed) {
5753 fan_control_access_mode = TPACPI_FAN_WR_NONE;
5754 fan_control_commands = 0;
5755 dbg_printk(TPACPI_DBG_INIT,
5756 "fan control features disabled by parameter\n");
5757 }
5758
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005759 /* update fan_control_desired_level */
5760 if (fan_status_access_mode != TPACPI_FAN_NONE)
5761 fan_get_status_safe(NULL);
5762
5763 if (fan_status_access_mode != TPACPI_FAN_NONE ||
5764 fan_control_access_mode != TPACPI_FAN_WR_NONE) {
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005765 rc = sysfs_create_group(&tpacpi_sensors_pdev->dev.kobj,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005766 &fan_attr_group);
5767 if (!(rc < 0))
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005768 rc = driver_create_file(&tpacpi_hwmon_pdriver.driver,
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005769 &driver_attr_fan_watchdog);
5770 if (rc < 0)
5771 return rc;
5772 return 0;
5773 } else
5774 return 1;
5775}
5776
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005777static void fan_exit(void)
5778{
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005779 vdbg_printk(TPACPI_DBG_EXIT,
5780 "cancelling any pending fan watchdog tasks\n");
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005781
5782 /* FIXME: can we really do this unconditionally? */
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005783 sysfs_remove_group(&tpacpi_sensors_pdev->dev.kobj, &fan_attr_group);
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005784 driver_remove_file(&tpacpi_hwmon_pdriver.driver,
5785 &driver_attr_fan_watchdog);
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005786
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005787 cancel_delayed_work(&fan_watchdog_task);
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03005788 flush_workqueue(tpacpi_wq);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005789}
5790
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005791static int fan_read(char *p)
5792{
5793 int len = 0;
5794 int rc;
5795 u8 status;
5796 unsigned int speed = 0;
5797
5798 switch (fan_status_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005799 case TPACPI_FAN_RD_ACPI_GFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005800 /* 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005801 rc = fan_get_status_safe(&status);
5802 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005803 return rc;
5804
5805 len += sprintf(p + len, "status:\t\t%s\n"
5806 "level:\t\t%d\n",
5807 (status != 0) ? "enabled" : "disabled", status);
5808 break;
5809
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005810 case TPACPI_FAN_RD_TPEC:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005811 /* all except 570, 600e/x, 770e, 770x */
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005812 rc = fan_get_status_safe(&status);
5813 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005814 return rc;
5815
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005816 if (unlikely(tp_features.fan_ctrl_status_undef)) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005817 if (status != fan_control_initial_status)
Henrique de Moraes Holschuhd8fd94d2007-04-21 11:08:36 -03005818 tp_features.fan_ctrl_status_undef = 0;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005819 else
5820 /* Return most likely status. In fact, it
5821 * might be the only possible status */
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005822 status = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005823 }
5824
5825 len += sprintf(p + len, "status:\t\t%s\n",
5826 (status != 0) ? "enabled" : "disabled");
5827
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005828 rc = fan_get_speed(&speed);
5829 if (rc < 0)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005830 return rc;
5831
5832 len += sprintf(p + len, "speed:\t\t%d\n", speed);
5833
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005834 if (status & TP_EC_FAN_FULLSPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005835 /* Disengaged mode takes precedence */
5836 len += sprintf(p + len, "level:\t\tdisengaged\n");
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005837 else if (status & TP_EC_FAN_AUTO)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005838 len += sprintf(p + len, "level:\t\tauto\n");
5839 else
5840 len += sprintf(p + len, "level:\t\t%d\n", status);
5841 break;
5842
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005843 case TPACPI_FAN_NONE:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005844 default:
5845 len += sprintf(p + len, "status:\t\tnot supported\n");
5846 }
5847
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005848 if (fan_control_commands & TPACPI_FAN_CMD_LEVEL) {
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005849 len += sprintf(p + len, "commands:\tlevel <level>");
5850
5851 switch (fan_control_access_mode) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005852 case TPACPI_FAN_WR_ACPI_SFAN:
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005853 len += sprintf(p + len, " (<level> is 0-7)\n");
5854 break;
5855
5856 default:
5857 len += sprintf(p + len, " (<level> is 0-7, "
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005858 "auto, disengaged, full-speed)\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005859 break;
5860 }
5861 }
5862
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005863 if (fan_control_commands & TPACPI_FAN_CMD_ENABLE)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005864 len += sprintf(p + len, "commands:\tenable, disable\n"
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005865 "commands:\twatchdog <timeout> (<timeout> "
5866 "is 0 (off), 1-120 (seconds))\n");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005867
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005868 if (fan_control_commands & TPACPI_FAN_CMD_SPEED)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005869 len += sprintf(p + len, "commands:\tspeed <speed>"
5870 " (<speed> is 0-65535)\n");
5871
5872 return len;
5873}
5874
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005875static int fan_write_cmd_level(const char *cmd, int *rc)
5876{
5877 int level;
5878
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005879 if (strlencmp(cmd, "level auto") == 0)
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005880 level = TP_EC_FAN_AUTO;
Henrique de Moraes Holschuhfe98a522007-04-24 11:48:17 -03005881 else if ((strlencmp(cmd, "level disengaged") == 0) |
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005882 (strlencmp(cmd, "level full-speed") == 0))
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005883 level = TP_EC_FAN_FULLSPEED;
Henrique de Moraes Holschuha12095c2006-11-24 11:47:13 -02005884 else if (sscanf(cmd, "level %d", &level) != 1)
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005885 return 0;
5886
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005887 *rc = fan_set_level_safe(level);
5888 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005889 printk(TPACPI_ERR "level command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005890 "access mode %d", fan_control_access_mode);
5891
5892 return 1;
5893}
5894
5895static int fan_write_cmd_enable(const char *cmd, int *rc)
5896{
5897 if (strlencmp(cmd, "enable") != 0)
5898 return 0;
5899
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005900 *rc = fan_set_enable();
5901 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005902 printk(TPACPI_ERR "enable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005903 "access mode %d", fan_control_access_mode);
5904
5905 return 1;
5906}
5907
5908static int fan_write_cmd_disable(const char *cmd, int *rc)
5909{
5910 if (strlencmp(cmd, "disable") != 0)
5911 return 0;
5912
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005913 *rc = fan_set_disable();
5914 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005915 printk(TPACPI_ERR "disable command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005916 "access mode %d", fan_control_access_mode);
5917
5918 return 1;
5919}
5920
5921static int fan_write_cmd_speed(const char *cmd, int *rc)
5922{
5923 int speed;
5924
Henrique de Moraes Holschuha8b7a662006-11-24 11:47:11 -02005925 /* TODO:
5926 * Support speed <low> <medium> <high> ? */
5927
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005928 if (sscanf(cmd, "speed %d", &speed) != 1)
5929 return 0;
5930
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02005931 *rc = fan_set_speed(speed);
5932 if (*rc == -ENXIO)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005933 printk(TPACPI_ERR "speed command accepted for unsupported "
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005934 "access mode %d", fan_control_access_mode);
5935
5936 return 1;
5937}
5938
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005939static int fan_write_cmd_watchdog(const char *cmd, int *rc)
5940{
5941 int interval;
5942
5943 if (sscanf(cmd, "watchdog %d", &interval) != 1)
5944 return 0;
5945
5946 if (interval < 0 || interval > 120)
5947 *rc = -EINVAL;
5948 else
5949 fan_watchdog_maxinterval = interval;
5950
5951 return 1;
5952}
5953
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005954static int fan_write(char *buf)
5955{
5956 char *cmd;
5957 int rc = 0;
5958
5959 while (!rc && (cmd = next_cmd(&buf))) {
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005960 if (!((fan_control_commands & TPACPI_FAN_CMD_LEVEL) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005961 fan_write_cmd_level(cmd, &rc)) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005962 !((fan_control_commands & TPACPI_FAN_CMD_ENABLE) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005963 (fan_write_cmd_enable(cmd, &rc) ||
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005964 fan_write_cmd_disable(cmd, &rc) ||
5965 fan_write_cmd_watchdog(cmd, &rc))) &&
Henrique de Moraes Holschuhefa27142007-04-21 11:08:28 -03005966 !((fan_control_commands & TPACPI_FAN_CMD_SPEED) &&
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005967 fan_write_cmd_speed(cmd, &rc))
5968 )
5969 rc = -EINVAL;
Henrique de Moraes Holschuh16663a82006-11-24 11:47:14 -02005970 else if (!rc)
5971 fan_watchdog_reset();
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005972 }
5973
Henrique de Moraes Holschuh18ad7992006-11-24 11:47:11 -02005974 return rc;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04005975}
5976
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005977static struct ibm_struct fan_driver_data = {
5978 .name = "fan",
5979 .read = fan_read,
5980 .write = fan_write,
5981 .exit = fan_exit,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03005982};
5983
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03005984/****************************************************************************
5985 ****************************************************************************
5986 *
5987 * Infrastructure
5988 *
5989 ****************************************************************************
5990 ****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005992/* sysfs name ---------------------------------------------------------- */
5993static ssize_t thinkpad_acpi_pdev_name_show(struct device *dev,
5994 struct device_attribute *attr,
5995 char *buf)
5996{
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02005997 return snprintf(buf, PAGE_SIZE, "%s\n", TPACPI_NAME);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03005998}
5999
6000static struct device_attribute dev_attr_thinkpad_acpi_pdev_name =
6001 __ATTR(name, S_IRUGO, thinkpad_acpi_pdev_name_show, NULL);
6002
6003/* --------------------------------------------------------------------- */
6004
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006005/* /proc support */
Henrique de Moraes Holschuh94954cc62007-07-18 23:45:27 -03006006static struct proc_dir_entry *proc_dir;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006007
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006008/*
6009 * Module and infrastructure proble, init and exit handling
6010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006012static int force_load;
6013
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006014#ifdef CONFIG_THINKPAD_ACPI_DEBUG
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006015static const char * __init str_supported(int is_supported)
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006016{
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006017 static char text_unsupported[] __initdata = "not supported";
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006018
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006019 return (is_supported)? &text_unsupported[4] : &text_unsupported[0];
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006020}
6021#endif /* CONFIG_THINKPAD_ACPI_DEBUG */
6022
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006023static void ibm_exit(struct ibm_struct *ibm)
6024{
6025 dbg_printk(TPACPI_DBG_EXIT, "removing %s\n", ibm->name);
6026
6027 list_del_init(&ibm->all_drivers);
6028
6029 if (ibm->flags.acpi_notify_installed) {
6030 dbg_printk(TPACPI_DBG_EXIT,
6031 "%s: acpi_remove_notify_handler\n", ibm->name);
6032 BUG_ON(!ibm->acpi);
6033 acpi_remove_notify_handler(*ibm->acpi->handle,
6034 ibm->acpi->type,
6035 dispatch_acpi_notify);
6036 ibm->flags.acpi_notify_installed = 0;
6037 ibm->flags.acpi_notify_installed = 0;
6038 }
6039
6040 if (ibm->flags.proc_created) {
6041 dbg_printk(TPACPI_DBG_EXIT,
6042 "%s: remove_proc_entry\n", ibm->name);
6043 remove_proc_entry(ibm->name, proc_dir);
6044 ibm->flags.proc_created = 0;
6045 }
6046
6047 if (ibm->flags.acpi_driver_registered) {
6048 dbg_printk(TPACPI_DBG_EXIT,
6049 "%s: acpi_bus_unregister_driver\n", ibm->name);
6050 BUG_ON(!ibm->acpi);
6051 acpi_bus_unregister_driver(ibm->acpi->driver);
6052 kfree(ibm->acpi->driver);
6053 ibm->acpi->driver = NULL;
6054 ibm->flags.acpi_driver_registered = 0;
6055 }
6056
6057 if (ibm->flags.init_called && ibm->exit) {
6058 ibm->exit();
6059 ibm->flags.init_called = 0;
6060 }
6061
6062 dbg_printk(TPACPI_DBG_INIT, "finished removing %s\n", ibm->name);
6063}
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006064
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006065static int __init ibm_init(struct ibm_init_struct *iibm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066{
6067 int ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006068 struct ibm_struct *ibm = iibm->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 struct proc_dir_entry *entry;
6070
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006071 BUG_ON(ibm == NULL);
6072
6073 INIT_LIST_HEAD(&ibm->all_drivers);
6074
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006075 if (ibm->flags.experimental && !experimental)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 return 0;
6077
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006078 dbg_printk(TPACPI_DBG_INIT,
6079 "probing for %s\n", ibm->name);
6080
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006081 if (iibm->init) {
6082 ret = iibm->init(iibm);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006083 if (ret > 0)
6084 return 0; /* probe failed */
6085 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 return ret;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006087
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006088 ibm->flags.init_called = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 }
6090
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006091 if (ibm->acpi) {
6092 if (ibm->acpi->hid) {
6093 ret = register_tpacpi_subdriver(ibm);
6094 if (ret)
6095 goto err_out;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006096 }
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006097
6098 if (ibm->acpi->notify) {
6099 ret = setup_acpi_notify(ibm);
6100 if (ret == -ENODEV) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006101 printk(TPACPI_NOTICE "disabling subdriver %s\n",
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006102 ibm->name);
6103 ret = 0;
6104 goto err_out;
6105 }
6106 if (ret < 0)
6107 goto err_out;
6108 }
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006109 }
6110
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006111 dbg_printk(TPACPI_DBG_INIT,
6112 "%s installed\n", ibm->name);
6113
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006114 if (ibm->read) {
6115 entry = create_proc_entry(ibm->name,
6116 S_IFREG | S_IRUGO | S_IWUSR,
6117 proc_dir);
6118 if (!entry) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006119 printk(TPACPI_ERR "unable to create proc entry %s\n",
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006120 ibm->name);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006121 ret = -ENODEV;
6122 goto err_out;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006123 }
6124 entry->owner = THIS_MODULE;
6125 entry->data = ibm;
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006126 entry->read_proc = &dispatch_procfs_read;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006127 if (ibm->write)
Henrique de Moraes Holschuh8d376cd2007-04-21 11:08:37 -03006128 entry->write_proc = &dispatch_procfs_write;
Henrique de Moraes Holschuh92641172007-04-21 11:08:35 -03006129 ibm->flags.proc_created = 1;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006132 list_add_tail(&ibm->all_drivers, &tpacpi_all_drivers);
6133
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 return 0;
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006135
6136err_out:
Henrique de Moraes Holschuhfe08bc42007-04-21 11:08:32 -03006137 dbg_printk(TPACPI_DBG_INIT,
6138 "%s: at error exit path with result %d\n",
6139 ibm->name, ret);
6140
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006141 ibm_exit(ibm);
6142 return (ret < 0)? ret : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143}
6144
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006145/* Probing */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006147static void __init get_thinkpad_model_data(struct thinkpad_id_data *tp)
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006148{
Jeff Garzik18552562007-10-03 15:15:40 -04006149 const struct dmi_device *dev = NULL;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006150 char ec_fw_string[18];
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006151
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006152 if (!tp)
6153 return;
6154
6155 memset(tp, 0, sizeof(*tp));
6156
6157 if (dmi_name_in_vendors("IBM"))
6158 tp->vendor = PCI_VENDOR_ID_IBM;
6159 else if (dmi_name_in_vendors("LENOVO"))
6160 tp->vendor = PCI_VENDOR_ID_LENOVO;
6161 else
6162 return;
6163
6164 tp->bios_version_str = kstrdup(dmi_get_system_info(DMI_BIOS_VERSION),
6165 GFP_KERNEL);
6166 if (!tp->bios_version_str)
6167 return;
6168 tp->bios_model = tp->bios_version_str[0]
6169 | (tp->bios_version_str[1] << 8);
6170
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006171 /*
6172 * ThinkPad T23 or newer, A31 or newer, R50e or newer,
6173 * X32 or newer, all Z series; Some models must have an
6174 * up-to-date BIOS or they will not be detected.
6175 *
6176 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6177 */
6178 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) {
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006179 if (sscanf(dev->name,
6180 "IBM ThinkPad Embedded Controller -[%17c",
6181 ec_fw_string) == 1) {
6182 ec_fw_string[sizeof(ec_fw_string) - 1] = 0;
6183 ec_fw_string[strcspn(ec_fw_string, " ]")] = 0;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006184
6185 tp->ec_version_str = kstrdup(ec_fw_string, GFP_KERNEL);
6186 tp->ec_model = ec_fw_string[0]
6187 | (ec_fw_string[1] << 8);
6188 break;
Henrique de Moraes Holschuh49a13cd2006-11-24 11:47:13 -02006189 }
Henrique de Moraes Holschuh60eb0b32006-11-24 11:47:08 -02006190 }
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006191
6192 tp->model_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_VERSION),
6193 GFP_KERNEL);
6194 if (strnicmp(tp->model_str, "ThinkPad", 8) != 0) {
6195 kfree(tp->model_str);
6196 tp->model_str = NULL;
6197 }
Henrique de Moraes Holschuh8c74adb2008-04-26 01:02:19 -03006198
6199 tp->nummodel_str = kstrdup(dmi_get_system_info(DMI_PRODUCT_NAME),
6200 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201}
6202
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006203static int __init probe_for_thinkpad(void)
6204{
6205 int is_thinkpad;
6206
6207 if (acpi_disabled)
6208 return -ENODEV;
6209
6210 /*
6211 * Non-ancient models have better DMI tagging, but very old models
6212 * don't.
6213 */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006214 is_thinkpad = (thinkpad_id.model_str != NULL);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006215
6216 /* ec is required because many other handles are relative to it */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006217 TPACPI_ACPIHANDLE_INIT(ec);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006218 if (!ec_handle) {
6219 if (is_thinkpad)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006220 printk(TPACPI_ERR
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006221 "Not yet supported ThinkPad detected!\n");
6222 return -ENODEV;
6223 }
6224
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006225 /*
6226 * Risks a regression on very old machines, but reduces potential
6227 * false positives a damn great deal
6228 */
6229 if (!is_thinkpad)
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006230 is_thinkpad = (thinkpad_id.vendor == PCI_VENDOR_ID_IBM);
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006231
6232 if (!is_thinkpad && !force_load)
6233 return -ENODEV;
6234
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006235 return 0;
6236}
6237
6238
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006239/* Module init, exit, parameters */
6240
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006241static struct ibm_init_struct ibms_init[] __initdata = {
6242 {
6243 .init = thinkpad_acpi_driver_init,
6244 .data = &thinkpad_acpi_driver_data,
6245 },
6246 {
6247 .init = hotkey_init,
6248 .data = &hotkey_driver_data,
6249 },
6250 {
6251 .init = bluetooth_init,
6252 .data = &bluetooth_driver_data,
6253 },
6254 {
6255 .init = wan_init,
6256 .data = &wan_driver_data,
6257 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006258#ifdef CONFIG_THINKPAD_ACPI_VIDEO
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006259 {
6260 .init = video_init,
6261 .data = &video_driver_data,
6262 },
Henrique de Moraes Holschuhd7c1d172008-02-16 02:17:54 -02006263#endif
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006264 {
6265 .init = light_init,
6266 .data = &light_driver_data,
6267 },
6268#ifdef CONFIG_THINKPAD_ACPI_DOCK
6269 {
6270 .init = dock_init,
6271 .data = &dock_driver_data[0],
6272 },
6273 {
Henrique de Moraes Holschuhd94a7f12007-04-27 22:00:15 -03006274 .init = dock_init2,
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006275 .data = &dock_driver_data[1],
6276 },
6277#endif
6278#ifdef CONFIG_THINKPAD_ACPI_BAY
6279 {
6280 .init = bay_init,
6281 .data = &bay_driver_data,
6282 },
6283#endif
6284 {
6285 .init = cmos_init,
6286 .data = &cmos_driver_data,
6287 },
6288 {
6289 .init = led_init,
6290 .data = &led_driver_data,
6291 },
6292 {
6293 .init = beep_init,
6294 .data = &beep_driver_data,
6295 },
6296 {
6297 .init = thermal_init,
6298 .data = &thermal_driver_data,
6299 },
6300 {
6301 .data = &ecdump_driver_data,
6302 },
6303 {
6304 .init = brightness_init,
6305 .data = &brightness_driver_data,
6306 },
6307 {
6308 .data = &volume_driver_data,
6309 },
6310 {
6311 .init = fan_init,
6312 .data = &fan_driver_data,
6313 },
6314};
6315
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006316static int __init set_ibm_param(const char *val, struct kernel_param *kp)
6317{
6318 unsigned int i;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006319 struct ibm_struct *ibm;
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006320
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006321 if (!kp || !kp->name || !val)
6322 return -EINVAL;
6323
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006324 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6325 ibm = ibms_init[i].data;
Henrique de Moraes Holschuh59f91ff2007-11-18 09:18:29 -02006326 WARN_ON(ibm == NULL);
6327
6328 if (!ibm || !ibm->name)
6329 continue;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006330
6331 if (strcmp(ibm->name, kp->name) == 0 && ibm->write) {
6332 if (strlen(val) > sizeof(ibms_init[i].param) - 2)
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006333 return -ENOSPC;
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006334 strcpy(ibms_init[i].param, val);
6335 strcat(ibms_init[i].param, ",");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006336 return 0;
6337 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006338 }
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006339
6340 return -EINVAL;
6341}
6342
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006343module_param(experimental, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006344MODULE_PARM_DESC(experimental,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006345 "Enables experimental features when non-zero");
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006346
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006347module_param_named(debug, dbg_level, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006348MODULE_PARM_DESC(debug, "Sets debug level bit-mask");
Henrique de Moraes Holschuh132ce092007-04-21 11:08:30 -03006349
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006350module_param(force_load, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006351MODULE_PARM_DESC(force_load,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006352 "Attempts to load the driver even on a "
6353 "mis-identified ThinkPad when true");
Henrique de Moraes Holschuh0dcef772007-04-21 11:08:34 -03006354
Henrique de Moraes Holschuh86cc9442007-07-18 23:45:41 -03006355module_param_named(fan_control, fan_control_allowed, bool, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006356MODULE_PARM_DESC(fan_control,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006357 "Enables setting fan parameters features when true");
Henrique de Moraes Holschuhecf2a802007-04-27 22:00:09 -03006358
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006359module_param_named(brightness_mode, brightness_mode, int, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006360MODULE_PARM_DESC(brightness_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006361 "Selects brightness control strategy: "
6362 "0=auto, 1=EC, 2=CMOS, 3=both");
Henrique de Moraes Holschuh24d3b772007-07-18 23:45:43 -03006363
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006364module_param(brightness_enable, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006365MODULE_PARM_DESC(brightness_enable,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006366 "Enables backlight control when 1, disables when 0");
Henrique de Moraes Holschuh87cc5372007-10-30 18:02:07 -02006367
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006368module_param(hotkey_report_mode, uint, 0);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006369MODULE_PARM_DESC(hotkey_report_mode,
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006370 "used for backwards compatibility with userspace, "
6371 "see documentation");
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006372
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006373#define TPACPI_PARAM(feature) \
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006374 module_param_call(feature, set_ibm_param, NULL, NULL, 0); \
Henrique de Moraes Holschuhcbb14842008-02-16 02:17:50 -02006375 MODULE_PARM_DESC(feature, "Simulates thinkpad-acpi procfs command " \
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006376 "at module load, see documentation")
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006377
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006378TPACPI_PARAM(hotkey);
6379TPACPI_PARAM(bluetooth);
6380TPACPI_PARAM(video);
6381TPACPI_PARAM(light);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006382#ifdef CONFIG_THINKPAD_ACPI_DOCK
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006383TPACPI_PARAM(dock);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006384#endif
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006385#ifdef CONFIG_THINKPAD_ACPI_BAY
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006386TPACPI_PARAM(bay);
Henrique de Moraes Holschuh85998242007-03-29 01:58:41 -03006387#endif /* CONFIG_THINKPAD_ACPI_BAY */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006388TPACPI_PARAM(cmos);
6389TPACPI_PARAM(led);
6390TPACPI_PARAM(beep);
6391TPACPI_PARAM(ecdump);
6392TPACPI_PARAM(brightness);
6393TPACPI_PARAM(volume);
6394TPACPI_PARAM(fan);
Henrique de Moraes Holschuh56b6aeb2007-03-23 17:33:57 -03006395
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006396static void thinkpad_acpi_module_exit(void)
6397{
6398 struct ibm_struct *ibm, *itmp;
6399
6400 tpacpi_lifecycle = TPACPI_LIFE_EXITING;
6401
6402 list_for_each_entry_safe_reverse(ibm, itmp,
6403 &tpacpi_all_drivers,
6404 all_drivers) {
6405 ibm_exit(ibm);
6406 }
6407
6408 dbg_printk(TPACPI_DBG_INIT, "finished subdriver exit path...\n");
6409
6410 if (tpacpi_inputdev) {
6411 if (tp_features.input_device_registered)
6412 input_unregister_device(tpacpi_inputdev);
6413 else
6414 input_free_device(tpacpi_inputdev);
6415 }
6416
6417 if (tpacpi_hwmon)
6418 hwmon_device_unregister(tpacpi_hwmon);
6419
6420 if (tp_features.sensors_pdev_attrs_registered)
6421 device_remove_file(&tpacpi_sensors_pdev->dev,
6422 &dev_attr_thinkpad_acpi_pdev_name);
6423 if (tpacpi_sensors_pdev)
6424 platform_device_unregister(tpacpi_sensors_pdev);
6425 if (tpacpi_pdev)
6426 platform_device_unregister(tpacpi_pdev);
6427
6428 if (tp_features.sensors_pdrv_attrs_registered)
6429 tpacpi_remove_driver_attributes(&tpacpi_hwmon_pdriver.driver);
6430 if (tp_features.platform_drv_attrs_registered)
6431 tpacpi_remove_driver_attributes(&tpacpi_pdriver.driver);
6432
6433 if (tp_features.sensors_pdrv_registered)
6434 platform_driver_unregister(&tpacpi_hwmon_pdriver);
6435
6436 if (tp_features.platform_drv_registered)
6437 platform_driver_unregister(&tpacpi_pdriver);
6438
6439 if (proc_dir)
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006440 remove_proc_entry(TPACPI_PROC_DIR, acpi_root_dir);
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006441
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006442 if (tpacpi_wq)
6443 destroy_workqueue(tpacpi_wq);
6444
Henrique de Moraes Holschuhb21a15f2008-01-08 13:02:45 -02006445 kfree(thinkpad_id.bios_version_str);
6446 kfree(thinkpad_id.ec_version_str);
6447 kfree(thinkpad_id.model_str);
6448}
6449
Henrique de Moraes Holschuhf74a27d2008-01-08 13:02:44 -02006450
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006451static int __init thinkpad_acpi_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006452{
6453 int ret, i;
6454
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006455 tpacpi_lifecycle = TPACPI_LIFE_INIT;
6456
Henrique de Moraes Holschuhff80f132007-09-04 11:13:15 -03006457 /* Parameter checking */
6458 if (hotkey_report_mode > 2)
6459 return -EINVAL;
6460
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006461 /* Driver-level probe */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006462
6463 get_thinkpad_model_data(&thinkpad_id);
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006464 ret = probe_for_thinkpad();
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006465 if (ret) {
6466 thinkpad_acpi_module_exit();
Henrique de Moraes Holschuh5fba3442007-04-21 11:08:31 -03006467 return ret;
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006470 /* Driver initialization */
Henrique de Moraes Holschuhd5a2f2f2007-07-18 23:45:42 -03006471
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006472 TPACPI_ACPIHANDLE_INIT(ecrd);
6473 TPACPI_ACPIHANDLE_INIT(ecwr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474
Henrique de Moraes Holschuhe0e3c062008-04-26 01:02:28 -03006475 tpacpi_wq = create_singlethread_workqueue(TPACPI_WORKQUEUE_NAME);
6476 if (!tpacpi_wq) {
6477 thinkpad_acpi_module_exit();
6478 return -ENOMEM;
6479 }
6480
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006481 proc_dir = proc_mkdir(TPACPI_PROC_DIR, acpi_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 if (!proc_dir) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006483 printk(TPACPI_ERR
6484 "unable to create proc dir " TPACPI_PROC_DIR);
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006485 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006486 return -ENODEV;
6487 }
6488 proc_dir->owner = THIS_MODULE;
Borislav Deianov78f81cc2005-08-17 00:00:00 -04006489
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006490 ret = platform_driver_register(&tpacpi_pdriver);
6491 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006492 printk(TPACPI_ERR
6493 "unable to register main platform driver\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006494 thinkpad_acpi_module_exit();
6495 return ret;
6496 }
Henrique de Moraes Holschuhac363932007-07-27 17:04:40 -03006497 tp_features.platform_drv_registered = 1;
6498
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006499 ret = platform_driver_register(&tpacpi_hwmon_pdriver);
6500 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006501 printk(TPACPI_ERR
6502 "unable to register hwmon platform driver\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006503 thinkpad_acpi_module_exit();
6504 return ret;
6505 }
6506 tp_features.sensors_pdrv_registered = 1;
6507
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006508 ret = tpacpi_create_driver_attributes(&tpacpi_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006509 if (!ret) {
6510 tp_features.platform_drv_attrs_registered = 1;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006511 ret = tpacpi_create_driver_attributes(
6512 &tpacpi_hwmon_pdriver.driver);
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006513 }
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006514 if (ret) {
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006515 printk(TPACPI_ERR
6516 "unable to create sysfs driver attributes\n");
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006517 thinkpad_acpi_module_exit();
6518 return ret;
6519 }
Henrique de Moraes Holschuh2369cc92007-09-23 11:39:07 -03006520 tp_features.sensors_pdrv_attrs_registered = 1;
Henrique de Moraes Holschuh176750d2007-04-24 11:48:13 -03006521
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006522
6523 /* Device initialization */
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006524 tpacpi_pdev = platform_device_register_simple(TPACPI_DRVR_NAME, -1,
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006525 NULL, 0);
6526 if (IS_ERR(tpacpi_pdev)) {
6527 ret = PTR_ERR(tpacpi_pdev);
6528 tpacpi_pdev = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006529 printk(TPACPI_ERR "unable to register platform device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006530 thinkpad_acpi_module_exit();
6531 return ret;
6532 }
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006533 tpacpi_sensors_pdev = platform_device_register_simple(
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006534 TPACPI_HWMON_DRVR_NAME,
6535 -1, NULL, 0);
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006536 if (IS_ERR(tpacpi_sensors_pdev)) {
6537 ret = PTR_ERR(tpacpi_sensors_pdev);
6538 tpacpi_sensors_pdev = NULL;
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006539 printk(TPACPI_ERR
6540 "unable to register hwmon platform device\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006541 thinkpad_acpi_module_exit();
6542 return ret;
6543 }
6544 ret = device_create_file(&tpacpi_sensors_pdev->dev,
6545 &dev_attr_thinkpad_acpi_pdev_name);
6546 if (ret) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006547 printk(TPACPI_ERR
Henrique de Moraes Holschuh35ff8b92008-01-08 13:02:49 -02006548 "unable to create sysfs hwmon device attributes\n");
Henrique de Moraes Holschuh7fd40022007-09-25 06:38:03 -03006549 thinkpad_acpi_module_exit();
6550 return ret;
6551 }
6552 tp_features.sensors_pdev_attrs_registered = 1;
6553 tpacpi_hwmon = hwmon_device_register(&tpacpi_sensors_pdev->dev);
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006554 if (IS_ERR(tpacpi_hwmon)) {
6555 ret = PTR_ERR(tpacpi_hwmon);
6556 tpacpi_hwmon = NULL;
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006557 printk(TPACPI_ERR "unable to register hwmon device\n");
Henrique de Moraes Holschuh54ae1502007-04-24 11:48:12 -03006558 thinkpad_acpi_module_exit();
6559 return ret;
6560 }
Henrique de Moraes Holschuh8523ed62007-09-23 11:39:01 -03006561 mutex_init(&tpacpi_inputdev_send_mutex);
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006562 tpacpi_inputdev = input_allocate_device();
6563 if (!tpacpi_inputdev) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006564 printk(TPACPI_ERR "unable to allocate input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006565 thinkpad_acpi_module_exit();
6566 return -ENOMEM;
6567 } else {
6568 /* Prepare input device, but don't register */
6569 tpacpi_inputdev->name = "ThinkPad Extra Buttons";
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006570 tpacpi_inputdev->phys = TPACPI_DRVR_NAME "/input0";
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006571 tpacpi_inputdev->id.bustype = BUS_HOST;
Henrique de Moraes Holschuhedf0e0e2007-07-18 23:45:44 -03006572 tpacpi_inputdev->id.vendor = (thinkpad_id.vendor) ?
6573 thinkpad_id.vendor :
6574 PCI_VENDOR_ID_IBM;
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006575 tpacpi_inputdev->id.product = TPACPI_HKEY_INPUT_PRODUCT;
6576 tpacpi_inputdev->id.version = TPACPI_HKEY_INPUT_VERSION;
6577 }
Henrique de Moraes Holschuha5763f22007-04-21 11:08:33 -03006578 for (i = 0; i < ARRAY_SIZE(ibms_init); i++) {
6579 ret = ibm_init(&ibms_init[i]);
6580 if (ret >= 0 && *ibms_init[i].param)
6581 ret = ibms_init[i].data->write(ibms_init[i].param);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 if (ret < 0) {
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006583 thinkpad_acpi_module_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 return ret;
6585 }
6586 }
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006587 ret = input_register_device(tpacpi_inputdev);
6588 if (ret < 0) {
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006589 printk(TPACPI_ERR "unable to register input device\n");
Henrique de Moraes Holschuh7f5d1cd2007-07-18 23:45:34 -03006590 thinkpad_acpi_module_exit();
6591 return ret;
6592 } else {
6593 tp_features.input_device_registered = 1;
6594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006595
Henrique de Moraes Holschuh8fef5022007-09-23 11:39:02 -03006596 tpacpi_lifecycle = TPACPI_LIFE_RUNNING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 return 0;
6598}
6599
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006600/* Please remove this in year 2009 */
6601MODULE_ALIAS("ibm_acpi");
6602
Henrique de Moraes Holschuh95e57ab2008-04-26 01:02:22 -03006603MODULE_ALIAS(TPACPI_DRVR_SHORTNAME);
6604
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006605/*
6606 * DMI matching for module autoloading
6607 *
6608 * See http://thinkwiki.org/wiki/List_of_DMI_IDs
6609 * See http://thinkwiki.org/wiki/BIOS_Upgrade_Downloads
6610 *
6611 * Only models listed in thinkwiki will be supported, so add yours
6612 * if it is not there yet.
6613 */
6614#define IBM_BIOS_MODULE_ALIAS(__type) \
6615 MODULE_ALIAS("dmi:bvnIBM:bvr" __type "ET??WW")
6616
6617/* Non-ancient thinkpads */
6618MODULE_ALIAS("dmi:bvnIBM:*:svnIBM:*:pvrThinkPad*:rvnIBM:*");
6619MODULE_ALIAS("dmi:bvnLENOVO:*:svnLENOVO:*:pvrThinkPad*:rvnLENOVO:*");
6620
6621/* Ancient thinkpad BIOSes have to be identified by
6622 * BIOS type or model number, and there are far less
6623 * BIOS types than model numbers... */
6624IBM_BIOS_MODULE_ALIAS("I[B,D,H,I,M,N,O,T,W,V,Y,Z]");
6625IBM_BIOS_MODULE_ALIAS("1[0,3,6,8,A-G,I,K,M-P,S,T]");
6626IBM_BIOS_MODULE_ALIAS("K[U,X-Z]");
6627
6628MODULE_AUTHOR("Borislav Deianov, Henrique de Moraes Holschuh");
Henrique de Moraes Holschuhe0c7dfe2008-01-08 13:02:48 -02006629MODULE_DESCRIPTION(TPACPI_DESC);
6630MODULE_VERSION(TPACPI_VERSION);
Henrique de Moraes Holschuhf68080f2008-01-08 13:02:47 -02006631MODULE_LICENSE("GPL");
6632
Henrique de Moraes Holschuh1def7112007-04-21 11:08:27 -03006633module_init(thinkpad_acpi_module_init);
6634module_exit(thinkpad_acpi_module_exit);