thinkpad_acpi.c 235 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
2
 *  thinkpad_acpi.c - ThinkPad ACPI Extras
Linus Torvalds's avatar
Linus Torvalds committed
3
4
 *
 *
5
 *  Copyright (C) 2004-2005 Borislav Deianov <borislav@users.sf.net>
6
 *  Copyright (C) 2006-2009 Henrique de Moraes Holschuh <hmh@hmh.eng.br>
Linus Torvalds's avatar
Linus Torvalds committed
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
20
21
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *  02110-1301, USA.
22
23
 */

24
25
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

26
#define TPACPI_VERSION "0.25"
27
#define TPACPI_SYSFS_VERSION 0x020700
28
29

/*
Linus Torvalds's avatar
Linus Torvalds committed
30
 *  Changelog:
31
32
 *  2007-10-20		changelog trimmed down
 *
33
34
 *  2007-03-27  0.14	renamed to thinkpad_acpi and moved to
 *  			drivers/misc.
35
36
37
38
 *
 *  2006-11-22	0.13	new maintainer
 *  			changelog now lives in git commit history, and will
 *  			not be updated further in-file.
39
 *
40
41
 *  2005-03-17	0.11	support for 600e, 770x
 *			    thanks to Jamie Lentin <lentinj@dial.pipex.com>
42
43
 *
 *  2005-01-16	0.9	use MODULE_VERSION
44
45
46
47
48
49
 *			    thanks to Henrik Brix Andersen <brix@gentoo.org>
 *			fix parameter passing on module loading
 *			    thanks to Rusty Russell <rusty@rustcorp.com.au>
 *			    thanks to Jim Radford <radford@blackbean.org>
 *  2004-11-08	0.8	fix init error case, don't return from a macro
 *			    thanks to Chris Wright <chrisw@osdl.org>
Linus Torvalds's avatar
Linus Torvalds committed
50
51
 */

52
53
54
55
56
57
58
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/mutex.h>
59
#include <linux/sched.h>
60
61
62
#include <linux/kthread.h>
#include <linux/freezer.h>
#include <linux/delay.h>
63
#include <linux/slab.h>
64
65
#include <linux/nvram.h>
#include <linux/proc_fs.h>
66
#include <linux/seq_file.h>
67
68
69
70
71
72
73
#include <linux/sysfs.h>
#include <linux/backlight.h>
#include <linux/fb.h>
#include <linux/platform_device.h>
#include <linux/hwmon.h>
#include <linux/hwmon-sysfs.h>
#include <linux/input.h>
74
#include <linux/leds.h>
75
#include <linux/rfkill.h>
76
77
78
#include <linux/dmi.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
79
80
81
#include <linux/acpi.h>
#include <linux/pci_ids.h>
#include <linux/thinkpad_acpi.h>
82
83
84
#include <sound/core.h>
#include <sound/control.h>
#include <sound/initval.h>
85
#include <asm/uaccess.h>
86
87
88
89
90
91
92

/* ThinkPad CMOS commands */
#define TP_CMOS_VOLUME_DOWN	0
#define TP_CMOS_VOLUME_UP	1
#define TP_CMOS_VOLUME_MUTE	2
#define TP_CMOS_BRIGHTNESS_UP	4
#define TP_CMOS_BRIGHTNESS_DOWN	5
93
94
#define TP_CMOS_THINKLIGHT_ON	12
#define TP_CMOS_THINKLIGHT_OFF	13
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

/* NVRAM Addresses */
enum tp_nvram_addr {
	TP_NVRAM_ADDR_HK2		= 0x57,
	TP_NVRAM_ADDR_THINKLIGHT	= 0x58,
	TP_NVRAM_ADDR_VIDEO		= 0x59,
	TP_NVRAM_ADDR_BRIGHTNESS	= 0x5e,
	TP_NVRAM_ADDR_MIXER		= 0x60,
};

/* NVRAM bit masks */
enum {
	TP_NVRAM_MASK_HKT_THINKPAD	= 0x08,
	TP_NVRAM_MASK_HKT_ZOOM		= 0x20,
	TP_NVRAM_MASK_HKT_DISPLAY	= 0x40,
	TP_NVRAM_MASK_HKT_HIBERNATE	= 0x80,
	TP_NVRAM_MASK_THINKLIGHT	= 0x10,
	TP_NVRAM_MASK_HKT_DISPEXPND	= 0x30,
	TP_NVRAM_MASK_HKT_BRIGHTNESS	= 0x20,
	TP_NVRAM_MASK_LEVEL_BRIGHTNESS	= 0x0f,
	TP_NVRAM_POS_LEVEL_BRIGHTNESS	= 0,
	TP_NVRAM_MASK_MUTE		= 0x40,
	TP_NVRAM_MASK_HKT_VOLUME	= 0x80,
	TP_NVRAM_MASK_LEVEL_VOLUME	= 0x0f,
	TP_NVRAM_POS_LEVEL_VOLUME	= 0,
};

122
123
124
125
126
/* Misc NVRAM-related */
enum {
	TP_NVRAM_LEVEL_VOLUME_MAX = 14,
};

127
/* ACPI HIDs */
128
129
#define TPACPI_ACPI_IBM_HKEY_HID	"IBM0068"
#define TPACPI_ACPI_LENOVO_HKEY_HID	"LEN0068"
130
#define TPACPI_ACPI_EC_HID		"PNP0C09"
131
132
133
134
135

/* Input IDs */
#define TPACPI_HKEY_INPUT_PRODUCT	0x5054 /* "TP" */
#define TPACPI_HKEY_INPUT_VERSION	0x4101

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* ACPI \WGSV commands */
enum {
	TP_ACPI_WGSV_GET_STATE		= 0x01, /* Get state information */
	TP_ACPI_WGSV_PWR_ON_ON_RESUME	= 0x02, /* Resume WWAN powered on */
	TP_ACPI_WGSV_PWR_OFF_ON_RESUME	= 0x03,	/* Resume WWAN powered off */
	TP_ACPI_WGSV_SAVE_STATE		= 0x04, /* Save state for S4/S5 */
};

/* TP_ACPI_WGSV_GET_STATE bits */
enum {
	TP_ACPI_WGSV_STATE_WWANEXIST	= 0x0001, /* WWAN hw available */
	TP_ACPI_WGSV_STATE_WWANPWR	= 0x0002, /* WWAN radio enabled */
	TP_ACPI_WGSV_STATE_WWANPWRRES	= 0x0004, /* WWAN state at resume */
	TP_ACPI_WGSV_STATE_WWANBIOSOFF	= 0x0008, /* WWAN disabled in BIOS */
	TP_ACPI_WGSV_STATE_BLTHEXIST	= 0x0001, /* BLTH hw available */
	TP_ACPI_WGSV_STATE_BLTHPWR	= 0x0002, /* BLTH radio enabled */
	TP_ACPI_WGSV_STATE_BLTHPWRRES	= 0x0004, /* BLTH state at resume */
	TP_ACPI_WGSV_STATE_BLTHBIOSOFF	= 0x0008, /* BLTH disabled in BIOS */
	TP_ACPI_WGSV_STATE_UWBEXIST	= 0x0010, /* UWB hw available */
	TP_ACPI_WGSV_STATE_UWBPWR	= 0x0020, /* UWB radio enabled */
};
157

158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* HKEY events */
enum tpacpi_hkey_event_t {
	/* Hotkey-related */
	TP_HKEY_EV_HOTKEY_BASE		= 0x1001, /* first hotkey (FN+F1) */
	TP_HKEY_EV_BRGHT_UP		= 0x1010, /* Brightness up */
	TP_HKEY_EV_BRGHT_DOWN		= 0x1011, /* Brightness down */
	TP_HKEY_EV_VOL_UP		= 0x1015, /* Volume up or unmute */
	TP_HKEY_EV_VOL_DOWN		= 0x1016, /* Volume down or unmute */
	TP_HKEY_EV_VOL_MUTE		= 0x1017, /* Mixer output mute */

	/* Reasons for waking up from S3/S4 */
	TP_HKEY_EV_WKUP_S3_UNDOCK	= 0x2304, /* undock requested, S3 */
	TP_HKEY_EV_WKUP_S4_UNDOCK	= 0x2404, /* undock requested, S4 */
	TP_HKEY_EV_WKUP_S3_BAYEJ	= 0x2305, /* bay ejection req, S3 */
	TP_HKEY_EV_WKUP_S4_BAYEJ	= 0x2405, /* bay ejection req, S4 */
	TP_HKEY_EV_WKUP_S3_BATLOW	= 0x2313, /* battery empty, S3 */
	TP_HKEY_EV_WKUP_S4_BATLOW	= 0x2413, /* battery empty, S4 */

	/* Auto-sleep after eject request */
	TP_HKEY_EV_BAYEJ_ACK		= 0x3003, /* bay ejection complete */
	TP_HKEY_EV_UNDOCK_ACK		= 0x4003, /* undock complete */

	/* Misc bay events */
	TP_HKEY_EV_OPTDRV_EJ		= 0x3006, /* opt. drive tray ejected */
182
183
184
185
	TP_HKEY_EV_HOTPLUG_DOCK		= 0x4010, /* docked into hotplug dock
						     or port replicator */
	TP_HKEY_EV_HOTPLUG_UNDOCK	= 0x4011, /* undocked from hotplug
						     dock or port replicator */
186
187
188
189
190
191
192
193
194
195

	/* User-interface events */
	TP_HKEY_EV_LID_CLOSE		= 0x5001, /* laptop lid closed */
	TP_HKEY_EV_LID_OPEN		= 0x5002, /* laptop lid opened */
	TP_HKEY_EV_TABLET_TABLET	= 0x5009, /* tablet swivel up */
	TP_HKEY_EV_TABLET_NOTEBOOK	= 0x500a, /* tablet swivel down */
	TP_HKEY_EV_PEN_INSERTED		= 0x500b, /* tablet pen inserted */
	TP_HKEY_EV_PEN_REMOVED		= 0x500c, /* tablet pen removed */
	TP_HKEY_EV_BRGHT_CHANGED	= 0x5010, /* backlight control event */

196
197
198
199
	/* Key-related user-interface events */
	TP_HKEY_EV_KEY_NUMLOCK		= 0x6000, /* NumLock key pressed */
	TP_HKEY_EV_KEY_FN		= 0x6005, /* Fn key pressed? E420 */

200
201
202
203
204
205
206
	/* Thermal events */
	TP_HKEY_EV_ALARM_BAT_HOT	= 0x6011, /* battery too hot */
	TP_HKEY_EV_ALARM_BAT_XHOT	= 0x6012, /* battery critically hot */
	TP_HKEY_EV_ALARM_SENSOR_HOT	= 0x6021, /* sensor too hot */
	TP_HKEY_EV_ALARM_SENSOR_XHOT	= 0x6022, /* sensor critically hot */
	TP_HKEY_EV_THM_TABLE_CHANGED	= 0x6030, /* thermal table changed */

207
208
	/* AC-related events */
	TP_HKEY_EV_AC_CHANGED		= 0x6040, /* AC status changed */
209

210
211
212
213
	/* Misc */
	TP_HKEY_EV_RFKILL_CHANGED	= 0x7000, /* rfkill switch changed */
};

214
215
216
217
/****************************************************************************
 * Main driver
 */

218
219
220
221
222
#define TPACPI_NAME "thinkpad"
#define TPACPI_DESC "ThinkPad ACPI Extras"
#define TPACPI_FILE TPACPI_NAME "_acpi"
#define TPACPI_URL "http://ibm-acpi.sf.net/"
#define TPACPI_MAIL "ibm-acpi-devel@lists.sourceforge.net"
223

224
225
226
#define TPACPI_PROC_DIR "ibm"
#define TPACPI_ACPI_EVENT_PREFIX "ibm"
#define TPACPI_DRVR_NAME TPACPI_FILE
227
#define TPACPI_DRVR_SHORTNAME "tpacpi"
228
#define TPACPI_HWMON_DRVR_NAME TPACPI_NAME "_hwmon"
229

230
#define TPACPI_NVRAM_KTHREAD_NAME "ktpacpi_nvramd"
231
#define TPACPI_WORKQUEUE_NAME "ktpacpid"
232

233
#define TPACPI_MAX_ACPI_ARGS 3
234

235
/* Debugging printk groups */
236
#define TPACPI_DBG_ALL		0xffff
237
#define TPACPI_DBG_DISCLOSETASK	0x8000
238
239
#define TPACPI_DBG_INIT		0x0001
#define TPACPI_DBG_EXIT		0x0002
240
#define TPACPI_DBG_RFKILL	0x0004
241
#define TPACPI_DBG_HKEY		0x0008
242
#define TPACPI_DBG_FAN		0x0010
243
#define TPACPI_DBG_BRGHT	0x0020
244
#define TPACPI_DBG_MIXER	0x0040
245

246
247
248
#define onoff(status, bit) ((status) & (1 << (bit)) ? "on" : "off")
#define enabled(status, bit) ((status) & (1 << (bit)) ? "enabled" : "disabled")
#define strlencmp(a, b) (strncmp((a), (b), strlen(b)))
249
250
251


/****************************************************************************
252
 * Driver-wide structs and misc. variables
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
 */

struct ibm_struct;

struct tp_acpi_drv_struct {
	const struct acpi_device_id *hid;
	struct acpi_driver *driver;

	void (*notify) (struct ibm_struct *, u32);
	acpi_handle *handle;
	u32 type;
	struct acpi_device *device;
};

struct ibm_struct {
	char *name;

270
	int (*read) (struct seq_file *);
271
272
273
	int (*write) (char *);
	void (*exit) (void);
	void (*resume) (void);
274
	void (*suspend) (void);
275
	void (*shutdown) (void);
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

	struct list_head all_drivers;

	struct tp_acpi_drv_struct *acpi;

	struct {
		u8 acpi_driver_registered:1;
		u8 acpi_notify_installed:1;
		u8 proc_created:1;
		u8 init_called:1;
		u8 experimental:1;
	} flags;
};

struct ibm_init_struct {
	char param[32];

	int (*init) (struct ibm_init_struct *);
Al Viro's avatar
Al Viro committed
294
	umode_t base_procfs_mode;
295
296
297
298
299
300
301
302
	struct ibm_struct *data;
};

static struct {
	u32 bluetooth:1;
	u32 hotkey:1;
	u32 hotkey_mask:1;
	u32 hotkey_wlsw:1;
303
	u32 hotkey_tablet:1;
304
305
	u32 light:1;
	u32 light_status:1;
306
	u32 bright_acpimode:1;
307
	u32 bright_unkfw:1;
308
	u32 wan:1;
309
	u32 uwb:1;
310
	u32 fan_ctrl_status_undef:1;
311
	u32 second_fan:1;
312
	u32 beep_needs_two_args:1;
313
	u32 mixer_no_level_control:1;
314
315
316
317
318
319
320
321
322
	u32 input_device_registered:1;
	u32 platform_drv_registered:1;
	u32 platform_drv_attrs_registered:1;
	u32 sensors_pdrv_registered:1;
	u32 sensors_pdrv_attrs_registered:1;
	u32 sensors_pdev_attrs_registered:1;
	u32 hotkey_poll_active:1;
} tp_features;

323
324
static struct {
	u16 hotkey_mask_ff:1;
325
	u16 volume_ctrl_forbidden:1;
326
327
} tp_warned;

328
329
330
331
332
333
334
struct thinkpad_id_data {
	unsigned int vendor;	/* ThinkPad vendor:
				 * PCI_VENDOR_ID_IBM/PCI_VENDOR_ID_LENOVO */

	char *bios_version_str;	/* Something like 1ZET51WW (1.03z) */
	char *ec_version_str;	/* Something like 1ZHT51WW-1.04a */

335
	u16 bios_model;		/* 1Y = 0x5931, 0 = unknown */
336
	u16 ec_model;
337
338
	u16 bios_release;	/* 1ZETK1WW = 0x314b, 0 = unknown */
	u16 ec_release;
339

340
341
	char *model_str;	/* ThinkPad T43 */
	char *nummodel_str;	/* 9384A9C for a 9384-A9C model */
342
343
344
};
static struct thinkpad_id_data thinkpad_id;

345
346
347
348
349
350
static enum {
	TPACPI_LIFE_INIT = 0,
	TPACPI_LIFE_RUNNING,
	TPACPI_LIFE_EXITING,
} tpacpi_lifecycle;

351
352
353
static int experimental;
static u32 dbg_level;

354
355
static struct workqueue_struct *tpacpi_wq;

356
357
358
359
360
361
enum led_status_t {
	TPACPI_LED_OFF = 0,
	TPACPI_LED_ON,
	TPACPI_LED_BLINK,
};

362
363
364
365
/* Special LED class that can defer work */
struct tpacpi_led_classdev {
	struct led_classdev led_classdev;
	struct work_struct work;
366
	enum led_status_t new_state;
367
	int led;
368
369
};

370
371
372
/* brightness level capabilities */
static unsigned int bright_maxlvl;	/* 0 = unknown */

373
374
#ifdef CONFIG_THINKPAD_ACPI_DEBUGFACILITIES
static int dbg_wlswemul;
375
static bool tpacpi_wlsw_emulstate;
376
static int dbg_bluetoothemul;
377
static bool tpacpi_bluetooth_emulstate;
378
static int dbg_wwanemul;
379
static bool tpacpi_wwan_emulstate;
380
static int dbg_uwbemul;
381
static bool tpacpi_uwb_emulstate;
382
383
384
#endif


385
386
387
388
/*************************************************************************
 *  Debugging helpers
 */

389
390
391
392
393
394
#define dbg_printk(a_dbg_level, format, arg...)				\
do {									\
	if (dbg_level & (a_dbg_level))					\
		printk(KERN_DEBUG pr_fmt("%s: " format),		\
		       __func__, ##arg);				\
} while (0)
395
396
397
398
399

#ifdef CONFIG_THINKPAD_ACPI_DEBUG
#define vdbg_printk dbg_printk
static const char *str_supported(int is_supported);
#else
400
401
402
static inline const char *str_supported(int is_supported) { return ""; }
#define vdbg_printk(a_dbg_level, format, arg...)	\
	no_printk(format, ##arg)
403
404
#endif

405
406
static void tpacpi_log_usertask(const char * const what)
{
407
408
	printk(KERN_DEBUG pr_fmt("%s: access by process with PID %d\n"),
	       what, task_tgid_vnr(current));
409
410
}

411
412
413
414
415
416
417
418
#define tpacpi_disclose_usertask(what, format, arg...)			\
do {									\
	if (unlikely((dbg_level & TPACPI_DBG_DISCLOSETASK) &&		\
		     (tpacpi_lifecycle == TPACPI_LIFE_RUNNING))) {	\
		printk(KERN_DEBUG pr_fmt("%s: PID %d: " format),	\
		       what, task_tgid_vnr(current), ## arg);		\
	}								\
} while (0)
419

420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
 * Quirk handling helpers
 *
 * ThinkPad IDs and versions seen in the field so far
 * are two-characters from the set [0-9A-Z], i.e. base 36.
 *
 * We use values well outside that range as specials.
 */

#define TPACPI_MATCH_ANY		0xffffU
#define TPACPI_MATCH_UNKNOWN		0U

/* TPID('1', 'Y') == 0x5931 */
#define TPID(__c1, __c2) (((__c2) << 8) | (__c1))

#define TPACPI_Q_IBM(__id1, __id2, __quirk)	\
	{ .vendor = PCI_VENDOR_ID_IBM,		\
	  .bios = TPID(__id1, __id2),		\
	  .ec = TPACPI_MATCH_ANY,		\
	  .quirks = (__quirk) }

#define TPACPI_Q_LNV(__id1, __id2, __quirk)	\
	{ .vendor = PCI_VENDOR_ID_LENOVO,	\
	  .bios = TPID(__id1, __id2),		\
	  .ec = TPACPI_MATCH_ANY,		\
	  .quirks = (__quirk) }

447
448
449
450
451
452
#define TPACPI_QEC_LNV(__id1, __id2, __quirk)	\
	{ .vendor = PCI_VENDOR_ID_LENOVO,	\
	  .bios = TPACPI_MATCH_ANY,		\
	  .ec = TPID(__id1, __id2),		\
	  .quirks = (__quirk) }

453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
struct tpacpi_quirk {
	unsigned int vendor;
	u16 bios;
	u16 ec;
	unsigned long quirks;
};

/**
 * tpacpi_check_quirks() - search BIOS/EC version on a list
 * @qlist:		array of &struct tpacpi_quirk
 * @qlist_size:		number of elements in @qlist
 *
 * Iterates over a quirks list until one is found that matches the
 * ThinkPad's vendor, BIOS and EC model.
 *
 * Returns 0 if nothing matches, otherwise returns the quirks field of
 * the matching &struct tpacpi_quirk entry.
 *
 * The match criteria is: vendor, ec and bios much match.
 */
static unsigned long __init tpacpi_check_quirks(
			const struct tpacpi_quirk *qlist,
			unsigned int qlist_size)
{
	while (qlist_size) {
		if ((qlist->vendor == thinkpad_id.vendor ||
				qlist->vendor == TPACPI_MATCH_ANY) &&
		    (qlist->bios == thinkpad_id.bios_model ||
				qlist->bios == TPACPI_MATCH_ANY) &&
		    (qlist->ec == thinkpad_id.ec_model ||
				qlist->ec == TPACPI_MATCH_ANY))
			return qlist->quirks;

		qlist_size--;
		qlist++;
	}
	return 0;
}

492
493
494
495
496
497
498
499
500
static inline bool __pure __init tpacpi_is_lenovo(void)
{
	return thinkpad_id.vendor == PCI_VENDOR_ID_LENOVO;
}

static inline bool __pure __init tpacpi_is_ibm(void)
{
	return thinkpad_id.vendor == PCI_VENDOR_ID_IBM;
}
501

502
503
504
505
506
507
508
509
510
511
512
/****************************************************************************
 ****************************************************************************
 *
 * ACPI Helpers and device model
 *
 ****************************************************************************
 ****************************************************************************/

/*************************************************************************
 * ACPI basic handles
 */
Linus Torvalds's avatar
Linus Torvalds committed
513

514
static acpi_handle root_handle;
515
static acpi_handle ec_handle;
Linus Torvalds's avatar
Linus Torvalds committed
516

517
#define TPACPI_HANDLE(object, parent, paths...)			\
Linus Torvalds's avatar
Linus Torvalds committed
518
	static acpi_handle  object##_handle;			\
519
	static const acpi_handle * const object##_parent __initconst =	\
520
521
						&parent##_handle; \
	static char *object##_paths[] __initdata = { paths }
Linus Torvalds's avatar
Linus Torvalds committed
522

523
524
TPACPI_HANDLE(ecrd, ec, "ECRD");	/* 570 */
TPACPI_HANDLE(ecwr, ec, "ECWR");	/* 570 */
525

526
527
TPACPI_HANDLE(cmos, root, "\\UCMS",	/* R50, R50e, R50p, R51, */
					/* T4x, X31, X40 */
528
529
	   "\\CMOS",		/* A3x, G4x, R32, T23, T30, X22-24, X30 */
	   "\\CMS",		/* R40, R40e */
530
	   );			/* all others */
531

532
TPACPI_HANDLE(hkey, ec, "\\_SB.HKEY",	/* 600e/x, 770e, 770x */
533
534
	   "^HKEY",		/* R30, R31 */
	   "HKEY",		/* all others */
535
	   );			/* 570 */
536

537
538
/*************************************************************************
 * ACPI helpers
539
540
 */

Linus Torvalds's avatar
Linus Torvalds committed
541
static int acpi_evalf(acpi_handle handle,
542
		      int *res, char *method, char *fmt, ...)
Linus Torvalds's avatar
Linus Torvalds committed
543
544
{
	char *fmt0 = fmt;
545
	struct acpi_object_list params;
546
	union acpi_object in_objs[TPACPI_MAX_ACPI_ARGS];
547
548
549
550
551
552
553
	struct acpi_buffer result, *resultp;
	union acpi_object out_obj;
	acpi_status status;
	va_list ap;
	char res_type;
	int success;
	int quiet;
Linus Torvalds's avatar
Linus Torvalds committed
554
555

	if (!*fmt) {
556
		pr_err("acpi_evalf() called with empty format\n");
Linus Torvalds's avatar
Linus Torvalds committed
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
		return 0;
	}

	if (*fmt == 'q') {
		quiet = 1;
		fmt++;
	} else
		quiet = 0;

	res_type = *(fmt++);

	params.count = 0;
	params.pointer = &in_objs[0];

	va_start(ap, fmt);
	while (*fmt) {
		char c = *(fmt++);
		switch (c) {
		case 'd':	/* int */
			in_objs[params.count].integer.value = va_arg(ap, int);
			in_objs[params.count++].type = ACPI_TYPE_INTEGER;
			break;
579
			/* add more types as needed */
Linus Torvalds's avatar
Linus Torvalds committed
580
		default:
581
			pr_err("acpi_evalf() called "
Linus Torvalds's avatar
Linus Torvalds committed
582
			       "with invalid format character '%c'\n", c);
583
			va_end(ap);
Linus Torvalds's avatar
Linus Torvalds committed
584
585
586
587
588
			return 0;
		}
	}
	va_end(ap);

589
590
591
592
593
594
	if (res_type != 'v') {
		result.length = sizeof(out_obj);
		result.pointer = &out_obj;
		resultp = &result;
	} else
		resultp = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
595

596
	status = acpi_evaluate_object(handle, method, &params, resultp);
Linus Torvalds's avatar
Linus Torvalds committed
597
598

	switch (res_type) {
599
	case 'd':		/* int */
600
601
602
		success = (status == AE_OK &&
			   out_obj.type == ACPI_TYPE_INTEGER);
		if (success && res)
603
			*res = out_obj.integer.value;
Linus Torvalds's avatar
Linus Torvalds committed
604
		break;
605
	case 'v':		/* void */
Linus Torvalds's avatar
Linus Torvalds committed
606
607
		success = status == AE_OK;
		break;
608
		/* add more types as needed */
Linus Torvalds's avatar
Linus Torvalds committed
609
	default:
610
		pr_err("acpi_evalf() called "
Linus Torvalds's avatar
Linus Torvalds committed
611
612
613
614
		       "with invalid format character '%c'\n", res_type);
		return 0;
	}

615
	if (!success && !quiet)
616
		pr_err("acpi_evalf(%s, %s, ...) failed: %s\n",
617
		       method, fmt0, acpi_format_exception(status));
618
619
620
621

	return success;
}

622
static int acpi_ec_read(int i, u8 *p)
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
{
	int v;

	if (ecrd_handle) {
		if (!acpi_evalf(ecrd_handle, &v, NULL, "dd", i))
			return 0;
		*p = v;
	} else {
		if (ec_read(i, p) < 0)
			return 0;
	}

	return 1;
}

static int acpi_ec_write(int i, u8 v)
{
	if (ecwr_handle) {
		if (!acpi_evalf(ecwr_handle, NULL, NULL, "vdd", i, v))
			return 0;
	} else {
		if (ec_write(i, v) < 0)
			return 0;
	}

	return 1;
}

651
652
653
654
655
656
657
658
659
660
661
static int issue_thinkpad_cmos_command(int cmos_cmd)
{
	if (!cmos_handle)
		return -ENXIO;

	if (!acpi_evalf(cmos_handle, NULL, NULL, "vd", cmos_cmd))
		return -EIO;

	return 0;
}

662
663
664
665
/*************************************************************************
 * ACPI device model
 */

666
667
#define TPACPI_ACPIHANDLE_INIT(object) \
	drv_acpi_handle_init(#object, &object##_handle, *object##_parent, \
668
		object##_paths, ARRAY_SIZE(object##_paths))
669

670
671
672
static void __init drv_acpi_handle_init(const char *name,
			   acpi_handle *handle, const acpi_handle parent,
			   char **paths, const int num_paths)
673
674
675
676
{
	int i;
	acpi_status status;

677
678
679
	vdbg_printk(TPACPI_DBG_INIT, "trying to locate ACPI handle for %s\n",
		name);

680
681
682
	for (i = 0; i < num_paths; i++) {
		status = acpi_get_handle(parent, paths[i], handle);
		if (ACPI_SUCCESS(status)) {
683
684
			dbg_printk(TPACPI_DBG_INIT,
				   "Found ACPI handle %s for %s\n",
685
				   paths[i], name);
686
687
688
689
			return;
		}
	}

690
691
	vdbg_printk(TPACPI_DBG_INIT, "ACPI handle for %s not found\n",
		    name);
692
693
694
	*handle = NULL;
}

695
696
697
static acpi_status __init tpacpi_acpi_handle_locate_callback(acpi_handle handle,
			u32 level, void *context, void **return_value)
{
698
699
700
701
702
703
704
705
	struct acpi_device *dev;
	if (!strcmp(context, "video")) {
		if (acpi_bus_get_device(handle, &dev))
			return AE_OK;
		if (strcmp(ACPI_VIDEO_HID, acpi_device_hid(dev)))
			return AE_OK;
	}

706
707
708
709
710
711
712
713
714
715
716
717
	*(acpi_handle *)return_value = handle;

	return AE_CTRL_TERMINATE;
}

static void __init tpacpi_acpi_handle_locate(const char *name,
		const char *hid,
		acpi_handle *handle)
{
	acpi_status status;
	acpi_handle device_found;

718
	BUG_ON(!name || !handle);
719
720
	vdbg_printk(TPACPI_DBG_INIT,
			"trying to locate ACPI handle for %s, using HID %s\n",
721
			name, hid ? hid : "NULL");
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739

	memset(&device_found, 0, sizeof(device_found));
	status = acpi_get_devices(hid, tpacpi_acpi_handle_locate_callback,
				  (void *)name, &device_found);

	*handle = NULL;

	if (ACPI_SUCCESS(status)) {
		*handle = device_found;
		dbg_printk(TPACPI_DBG_INIT,
			   "Found ACPI handle for %s\n", name);
	} else {
		vdbg_printk(TPACPI_DBG_INIT,
			    "Could not locate an ACPI handle for %s: %s\n",
			    name, acpi_format_exception(status));
	}
}

740
static void dispatch_acpi_notify(acpi_handle handle, u32 event, void *data)
741
742
743
{
	struct ibm_struct *ibm = data;

744
745
746
	if (tpacpi_lifecycle != TPACPI_LIFE_RUNNING)
		return;

747
	if (!ibm || !ibm->acpi || !ibm->acpi->notify)
748
749
		return;

750
	ibm->acpi->notify(ibm, event);
751
752
}

753
static int __init setup_acpi_notify(struct ibm_struct *ibm)
754
755
{
	acpi_status status;
756
	int rc;
757

758
759
760
	BUG_ON(!ibm->acpi);

	if (!*ibm->acpi->handle)
761
762
		return 0;

763
	vdbg_printk(TPACPI_DBG_INIT,
764
765
		"setting up ACPI notify for %s\n", ibm->name);

766
767
	rc = acpi_bus_get_device(*ibm->acpi->handle, &ibm->acpi->device);
	if (rc < 0) {
768
		pr_err("acpi_bus_get_device(%s) failed: %d\n", ibm->name, rc);
769
770
771
		return -ENODEV;
	}

772
	ibm->acpi->device->driver_data = ibm;
773
	sprintf(acpi_device_class(ibm->acpi->device), "%s/%s",
774
		TPACPI_ACPI_EVENT_PREFIX,
775
		ibm->name);
776

777
778
	status = acpi_install_notify_handler(*ibm->acpi->handle,
			ibm->acpi->type, dispatch_acpi_notify, ibm);
779
780
	if (ACPI_FAILURE(status)) {
		if (status == AE_ALREADY_EXISTS) {
781
782
			pr_notice("another device driver is already "
				  "handling %s events\n", ibm->name);
783
		} else {
784
			pr_err("acpi_install_notify_handler(%s) failed: %s\n",
785
			       ibm->name, acpi_format_exception(status));
786
787
788
		}
		return -ENODEV;
	}
789
	ibm->flags.acpi_notify_installed = 1;
790
791
792
	return 0;
}

793
static int __init tpacpi_device_add(struct acpi_device *device)
794
795
796
797
{
	return 0;
}

798
static int __init register_tpacpi_subdriver(struct ibm_struct *ibm)
799
{
800
	int rc;
801

802
803
804
	dbg_printk(TPACPI_DBG_INIT,
		"registering %s as an ACPI driver\n", ibm->name);

805
806
807
808
	BUG_ON(!ibm->acpi);

	ibm->acpi->driver = kzalloc(sizeof(struct acpi_driver), GFP_KERNEL);
	if (!ibm->acpi->driver) {
809
		pr_err("failed to allocate memory for ibm->acpi->driver\n");
810
		return -ENOMEM;
811
812
	}

813
	sprintf(ibm->acpi->driver->name, "%s_%s", TPACPI_NAME, ibm->name);
814
	ibm->acpi->driver->ids = ibm->acpi->hid;
815

816
	ibm->acpi->driver->ops.add = &tpacpi_device_add;
817

818
819
	rc = acpi_bus_register_driver(ibm->acpi->driver);
	if (rc < 0) {
820
		pr_err("acpi_bus_register_driver(%s) failed: %d\n",
821
		       ibm->name, rc);
822
823
		kfree(ibm->acpi->driver);
		ibm->acpi->driver = NULL;
824
	} else if (!rc)
825
		ibm->flags.acpi_driver_registered = 1;
826

827
	return rc;
828
829
830
831
832
833
834
835
836
837
838
}


/****************************************************************************
 ****************************************************************************
 *
 * Procfs Helpers
 *
 ****************************************************************************
 ****************************************************************************/

839
static int dispatch_proc_show(struct seq_file *m, void *v)
840
{
841
	struct ibm_struct *ibm = m->private;
842
843
844

	if (!ibm || !ibm->read)
		return -EINVAL;
845
846
	return ibm->read(m);
}
847

848
849
static int dispatch_proc_open(struct inode *inode, struct file *file)
{
Al Viro's avatar
Al Viro committed
850
	return single_open(file, dispatch_proc_show, PDE_DATA(inode));
851
852
}

853
static ssize_t dispatch_proc_write(struct file *file,
854
			const char __user *userbuf,
855
			size_t count, loff_t *pos)
856
{
Al Viro's avatar
Al Viro committed
857
	struct ibm_struct *ibm = PDE_DATA(file_inode(file));
858
859
860
861
862
	char *kernbuf;
	int ret;

	if (!ibm || !ibm->write)
		return -EINVAL;
863
864
	if (count > PAGE_SIZE - 2)
		return -EINVAL;
865
866
867
868

	kernbuf = kmalloc(count + 2, GFP_KERNEL);
	if (!kernbuf)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
869

870
871
872
873
	if (copy_from_user(kernbuf, userbuf, count)) {
		kfree(kernbuf);
		return -EFAULT;
	}
Linus Torvalds's avatar
Linus Torvalds committed
874

875
876
877
878
879
	kernbuf[count] = 0;
	strcat(kernbuf, ",");
	ret = ibm->write(kernbuf);
	if (ret == 0)
		ret = count;
Linus Torvalds's avatar
Linus Torvalds committed
880

881
882
883
	kfree(kernbuf);

	return ret;
Linus Torvalds's avatar
Linus Torvalds committed
884
885
}

886
887
888
889
890
891
892
893
894
static const struct file_operations dispatch_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= dispatch_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= dispatch_proc_write,
};

Linus Torvalds's avatar
Linus Torvalds committed
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
static char *next_cmd(char **cmds)
{
	char *start = *cmds;
	char *end;

	while ((end = strchr(start, ',')) && end == start)
		start = end + 1;

	if (!end)
		return NULL;

	*end = 0;
	*cmds = end + 1;
	return start;
}

911

912
913
914
/****************************************************************************
 ****************************************************************************
 *
915
 * Device model: input, hwmon and platform
916
917
918
919
 *
 ****************************************************************************
 ****************************************************************************/

920
static struct platform_device *tpacpi_pdev;
921
static struct platform_device *tpacpi_sensors_pdev;
922
static struct device *tpacpi_hwmon;
923
static struct input_dev *tpacpi_inputdev;
924
static struct mutex tpacpi_inputdev_send_mutex;
925
static LIST_HEAD(tpacpi_all_drivers);
926

927
#ifdef CONFIG_PM_SLEEP
928
static int tpacpi_suspend_handler(struct device *dev)
929
930
931
932
933
934
935
{
	struct ibm_struct *ibm, *itmp;

	list_for_each_entry_safe(ibm, itmp,
				 &tpacpi_all_drivers,
				 all_drivers) {
		if (ibm->suspend)
936
			(ibm->suspend)();
937
938
939
940
941
	}

	return 0;
}

942
static int tpacpi_resume_handler(struct device *dev)
943
944
945
946
947
948
949
950
951
952
953
954
{
	struct ibm_struct *ibm, *itmp;

	list_for_each_entry_safe(ibm, itmp,
				 &tpacpi_all_drivers,
				 all_drivers) {
		if (ibm->resume)
			(ibm->resume)();
	}

	return 0;
}
955
#endif
956

957
958
959
static SIMPLE_DEV_PM_OPS(tpacpi_pm,
			 tpacpi_suspend_handler, tpacpi_resume_handler);

960
961
962
963
964
965
966
967
968
969
970
971
static void tpacpi_shutdown_handler(struct platform_device *pdev)
{
	struct ibm_struct *ibm, *itmp;

	list_for_each_entry_safe(ibm, itmp,
				 &tpacpi_all_drivers,
				 all_drivers) {
		if (ibm->shutdown)
			(ibm->shutdown)();
	}
}

972
973
static struct platform_driver tpacpi_pdriver = {
	.driver = {
974
		.name = TPACPI_DRVR_NAME,
975
		.owner = THIS_MODULE,
976
		.pm = &tpacpi_pm,
977
	},
978
	.shutdown = tpacpi_shutdown_handler,
979
980
};

981
982
static struct platform_driver tpacpi_hwmon_pdriver = {
	.driver = {
983
		.name = TPACPI_HWMON_DRVR_NAME,
984
985
986
		.owner = THIS_MODULE,
	},
};
987

988
/*************************************************************************
989
 * sysfs support helpers
990
991
 */

992
993
994
struct attribute_set {
	unsigned int members, max_members;
	struct attribute_group group;
995
996
};

997
998
999
1000
struct attribute_set_obj {
	struct attribute_set s;
	struct attribute *a;
} __attribute__((packed));