asus_acpi.c 39.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/*
 *  asus_acpi.c - Asus Laptop ACPI Extras
 *
 *
K
Karol Kozimor 已提交
5
 *  Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
L
Linus Torvalds 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  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
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *  The development page for this driver is located at
 *  http://sourceforge.net/projects/acpi4asus/
 *
 *  Credits:
 *  Pontus Fuchs   - Helper functions, cleanup
 *  Johann Wiesner - Small compile fixes
 *  John Belmonte  - ACPI code for Toshiba laptop was a good starting point.
29
 *  �ic Burghard  - LED display support for W1N
L
Linus Torvalds 已提交
30 31 32 33 34 35 36 37
 *
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
A
Alexey Dobriyan 已提交
38
#include <linux/seq_file.h>
39
#include <linux/backlight.h>
L
Linus Torvalds 已提交
40 41 42 43
#include <acpi/acpi_drivers.h>
#include <acpi/acpi_bus.h>
#include <asm/uaccess.h>

K
Karol Kozimor 已提交
44
#define ASUS_ACPI_VERSION "0.30"
L
Linus Torvalds 已提交
45

46
#define PROC_ASUS       "asus"	/* The directory */
L
Linus Torvalds 已提交
47 48 49
#define PROC_MLED       "mled"
#define PROC_WLED       "wled"
#define PROC_TLED       "tled"
50
#define PROC_BT         "bluetooth"
51
#define PROC_LEDD       "ledd"
L
Linus Torvalds 已提交
52 53 54 55 56 57 58 59 60 61 62 63
#define PROC_INFO       "info"
#define PROC_LCD        "lcd"
#define PROC_BRN        "brn"
#define PROC_DISP       "disp"

#define ACPI_HOTK_NAME          "Asus Laptop ACPI Extras Driver"
#define ACPI_HOTK_CLASS         "hotkey"
#define ACPI_HOTK_DEVICE_NAME   "Hotkey"

/*
 * Some events we use, same for all Asus
 */
L
Len Brown 已提交
64
#define BR_UP       0x10
L
Linus Torvalds 已提交
65 66 67 68 69
#define BR_DOWN     0x20

/*
 * Flags for hotk status
 */
70 71 72 73
#define MLED_ON     0x01	/* Mail LED */
#define WLED_ON     0x02	/* Wireless LED */
#define TLED_ON     0x04	/* Touchpad LED */
#define BT_ON       0x08	/* Internal Bluetooth */
L
Linus Torvalds 已提交
74 75 76 77 78 79 80 81

MODULE_AUTHOR("Julien Lerouge, Karol Kozimor");
MODULE_DESCRIPTION(ACPI_HOTK_NAME);
MODULE_LICENSE("GPL");

static uid_t asus_uid;
static gid_t asus_gid;
module_param(asus_uid, uint, 0);
82
MODULE_PARM_DESC(asus_uid, "UID for entries in /proc/acpi/asus");
L
Linus Torvalds 已提交
83
module_param(asus_gid, uint, 0);
84
MODULE_PARM_DESC(asus_gid, "GID for entries in /proc/acpi/asus");
L
Linus Torvalds 已提交
85

86
/* For each model, all features implemented,
L
Linus Torvalds 已提交
87 88
 * those marked with R are relative to HOTK, A for absolute */
struct model_data {
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	char *name;		/* name of the laptop________________A */
	char *mt_mled;		/* method to handle mled_____________R */
	char *mled_status;	/* node to handle mled reading_______A */
	char *mt_wled;		/* method to handle wled_____________R */
	char *wled_status;	/* node to handle wled reading_______A */
	char *mt_tled;		/* method to handle tled_____________R */
	char *tled_status;	/* node to handle tled reading_______A */
	char *mt_ledd;		/* method to handle LED display______R */
	char *mt_bt_switch;	/* method to switch Bluetooth on/off_R */
	char *bt_status;	/* no model currently supports this__? */
	char *mt_lcd_switch;	/* method to turn LCD on/off_________A */
	char *lcd_status;	/* node to read LCD panel state______A */
	char *brightness_up;	/* method to set brightness up_______A */
	char *brightness_down;	/* method to set brightness down ____A */
	char *brightness_set;	/* method to set absolute brightness_R */
	char *brightness_get;	/* method to get absolute brightness_R */
	char *brightness_status;/* node to get brightness____________A */
	char *display_set;	/* method to set video output________R */
	char *display_get;	/* method to get video output________R */
L
Linus Torvalds 已提交
108 109 110 111 112 113 114
};

/*
 * This is the main structure, we can use it to store anything interesting
 * about the hotk device
 */
struct asus_hotk {
115 116 117 118 119 120
	struct acpi_device *device;	/* the device we are in */
	acpi_handle handle;		/* the handle of the hotk device */
	char status;			/* status of the hotk, for LEDs */
	u32 ledd_status;		/* status of the LED display */
	struct model_data *methods;	/* methods available on the laptop */
	u8 brightness;			/* brightness level */
L
Linus Torvalds 已提交
121
	enum {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
		A1x = 0,	/* A1340D, A1300F */
		A2x,		/* A2500H */
		A4G,		/* A4700G */
		D1x,		/* D1 */
		L2D,		/* L2000D */
		L3C,		/* L3800C */
		L3D,		/* L3400D */
		L3H,		/* L3H, L2000E, L5D */
		L4R,		/* L4500R */
		L5x,		/* L5800C */
		L8L,		/* L8400L */
		M1A,		/* M1300A */
		M2E,		/* M2400E, L4400L */
		M6N,		/* M6800N, W3400N */
		M6R,		/* M6700R, A3000G */
		P30,		/* Samsung P30 */
		S1x,		/* S1300A, but also L1400B and M2400A (L84F) */
		S2x,		/* S200 (J1 reported), Victor MP-XP7210 */
		W1N,		/* W1000N */
		W5A,		/* W5A */
		W3V,            /* W3030V */
		xxN,		/* M2400N, M3700N, M5200N, M6800N,
							 S1300N, S5200N*/
		A4S,            /* Z81sp */
		F3Sa,		/* (Centrino) */
C
Corentin Chary 已提交
147
		R1F,
L
Linus Torvalds 已提交
148
		END_MODEL
149 150
	} model;		/* Models currently supported */
	u16 event_count[128];	/* Count for each event TODO make this better */
L
Linus Torvalds 已提交
151 152 153 154 155 156 157 158 159 160 161 162
};

/* Here we go */
#define A1x_PREFIX "\\_SB.PCI0.ISA.EC0."
#define L3C_PREFIX "\\_SB.PCI0.PX40.ECD0."
#define M1A_PREFIX "\\_SB.PCI0.PX40.EC0."
#define P30_PREFIX "\\_SB.PCI0.LPCB.EC0."
#define S1x_PREFIX "\\_SB.PCI0.PX40."
#define S2x_PREFIX A1x_PREFIX
#define xxN_PREFIX "\\_SB.PCI0.SBRG.EC0."

static struct model_data model_conf[END_MODEL] = {
L
Len Brown 已提交
163
	/*
L
Linus Torvalds 已提交
164 165 166 167 168
	 * TODO I have seen a SWBX and AIBX method on some models, like L1400B,
	 * it seems to be a kind of switch, but what for ?
	 */

	{
L
Len Brown 已提交
169 170 171 172 173 174 175
	 .name = "A1x",
	 .mt_mled = "MLED",
	 .mled_status = "\\MAIL",
	 .mt_lcd_switch = A1x_PREFIX "_Q10",
	 .lcd_status = "\\BKLI",
	 .brightness_up = A1x_PREFIX "_Q0E",
	 .brightness_down = A1x_PREFIX "_Q0F"},
L
Linus Torvalds 已提交
176 177

	{
L
Len Brown 已提交
178 179 180 181 182 183 184 185 186 187
	 .name = "A2x",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .wled_status = "\\SG66",
	 .mt_lcd_switch = "\\Q10",
	 .lcd_status = "\\BAOF",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
188

K
Karol Kozimor 已提交
189 190 191 192 193 194 195 196 197 198
	{
	 .name = "A4G",
	 .mt_mled = "MLED",
/* WLED present, but not controlled by ACPI */
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\ADVG"},

L
Linus Torvalds 已提交
199
	{
L
Len Brown 已提交
200 201 202 203 204 205 206 207 208
	 .name = "D1x",
	 .mt_mled = "MLED",
	 .mt_lcd_switch = "\\Q0D",
	 .lcd_status = "\\GP11",
	 .brightness_up = "\\Q0C",
	 .brightness_down = "\\Q0B",
	 .brightness_status = "\\BLVL",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
209 210

	{
L
Len Brown 已提交
211 212 213 214 215 216 217 218 219 220 221
	 .name = "L2D",
	 .mt_mled = "MLED",
	 .mled_status = "\\SGP6",
	 .mt_wled = "WLED",
	 .wled_status = "\\RCP3",
	 .mt_lcd_switch = "\\Q10",
	 .lcd_status = "\\SGP0",
	 .brightness_up = "\\Q0E",
	 .brightness_down = "\\Q0F",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
222 223

	{
L
Len Brown 已提交
224 225 226 227 228 229 230 231 232
	 .name = "L3C",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = L3C_PREFIX "_Q10",
	 .lcd_status = "\\GL32",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\_SB.PCI0.PCI1.VGAC.NMAP"},
L
Linus Torvalds 已提交
233 234

	{
L
Len Brown 已提交
235 236 237 238 239 240 241 242 243 244
	 .name = "L3D",
	 .mt_mled = "MLED",
	 .mled_status = "\\MALD",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = "\\Q10",
	 .lcd_status = "\\BKLG",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
245 246

	{
L
Len Brown 已提交
247 248 249 250 251 252 253 254 255
	 .name = "L3H",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = "EHK",
	 .lcd_status = "\\_SB.PCI0.PM.PBC",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
256 257

	{
L
Len Brown 已提交
258 259 260 261 262 263 264 265 266 267
	 .name = "L4R",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .wled_status = "\\_SB.PCI0.SBRG.SG13",
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
L
Linus Torvalds 已提交
268 269

	{
L
Len Brown 已提交
270 271
	 .name = "L5x",
	 .mt_mled = "MLED",
L
Linus Torvalds 已提交
272
/* WLED present, but not controlled by ACPI */
L
Len Brown 已提交
273 274 275 276 277 278 279
	 .mt_tled = "TLED",
	 .mt_lcd_switch = "\\Q0D",
	 .lcd_status = "\\BAOF",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
280 281

	{
L
Len Brown 已提交
282
	 .name = "L8L"
L
Linus Torvalds 已提交
283
/* No features, but at least support the hotkeys */
L
Len Brown 已提交
284
	 },
L
Linus Torvalds 已提交
285 286

	{
L
Len Brown 已提交
287 288 289 290 291 292 293 294 295
	 .name = "M1A",
	 .mt_mled = "MLED",
	 .mt_lcd_switch = M1A_PREFIX "Q10",
	 .lcd_status = "\\PNOF",
	 .brightness_up = M1A_PREFIX "Q0E",
	 .brightness_down = M1A_PREFIX "Q0F",
	 .brightness_status = "\\BRIT",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
296 297

	{
L
Len Brown 已提交
298 299 300 301 302 303 304 305 306
	 .name = "M2E",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = "\\Q10",
	 .lcd_status = "\\GP06",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},
L
Linus Torvalds 已提交
307 308

	{
L
Len Brown 已提交
309 310 311 312 313 314 315 316 317
	 .name = "M6N",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .wled_status = "\\_SB.PCI0.SBRG.SG13",
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\_SB.BKLT",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
318 319
	 .display_get = "\\SSTE"},

L
Linus Torvalds 已提交
320
	{
L
Len Brown 已提交
321 322 323 324 325 326 327 328
	 .name = "M6R",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\_SB.PCI0.SBSM.SEO4",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
329
	 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
L
Linus Torvalds 已提交
330 331

	{
L
Len Brown 已提交
332 333 334 335 336 337 338 339 340
	 .name = "P30",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = P30_PREFIX "_Q0E",
	 .lcd_status = "\\BKLT",
	 .brightness_up = P30_PREFIX "_Q68",
	 .brightness_down = P30_PREFIX "_Q69",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\DNXT"},
L
Linus Torvalds 已提交
341 342

	{
L
Len Brown 已提交
343 344 345 346 347 348 349 350
	 .name = "S1x",
	 .mt_mled = "MLED",
	 .mled_status = "\\EMLE",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = S1x_PREFIX "Q10",
	 .lcd_status = "\\PNOF",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV"},
L
Linus Torvalds 已提交
351 352

	{
L
Len Brown 已提交
353 354 355 356 357 358 359
	 .name = "S2x",
	 .mt_mled = "MLED",
	 .mled_status = "\\MAIL",
	 .mt_lcd_switch = S2x_PREFIX "_Q10",
	 .lcd_status = "\\BKLI",
	 .brightness_up = S2x_PREFIX "_Q0B",
	 .brightness_down = S2x_PREFIX "_Q0A"},
L
Linus Torvalds 已提交
360

361 362 363 364 365 366 367 368 369 370 371 372
	{
	 .name = "W1N",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .mt_ledd = "SLCM",
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\BKLT",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\ADVG"},

373 374 375 376 377 378 379 380 381 382
	{
	 .name = "W5A",
	 .mt_bt_switch = "BLED",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\ADVG"},

L
Linus Torvalds 已提交
383
	{
M
Marek W 已提交
384 385 386 387 388 389 390 391 392 393 394
	 .name = "W3V",
	 .mt_mled = "MLED",
	 .mt_wled = "WLED",
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\BKLT",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\INFB"},

       {
L
Len Brown 已提交
395 396
	 .name = "xxN",
	 .mt_mled = "MLED",
L
Linus Torvalds 已提交
397
/* WLED present, but not controlled by ACPI */
L
Len Brown 已提交
398 399 400 401 402
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\BKLT",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
403 404 405 406 407 408 409 410
	.display_get = "\\ADVG"},

	{
		.name              = "A4S",
		.brightness_set    = "SPLV",
		.brightness_get    = "GPLV",
		.mt_bt_switch      = "BLED",
		.mt_wled           = "WLED"
411 412 413 414 415 416 417 418 419 420 421 422 423 424
	},

	{
		.name		= "F3Sa",
		.mt_bt_switch	= "BLED",
		.mt_wled	= "WLED",
		.mt_mled	= "MLED",
		.brightness_get	= "GPLV",
		.brightness_set	= "SPLV",
		.mt_lcd_switch	= "\\_SB.PCI0.SBRG.EC0._Q10",
		.lcd_status	= "\\_SB.PCI0.SBRG.EC0.RPIN",
		.display_get	= "\\ADVG",
		.display_set	= "SDSP",
	},
C
Corentin Chary 已提交
425 426 427 428 429 430 431 432 433 434 435 436
	{
		.name = "R1F",
		.mt_bt_switch = "BLED",
		.mt_mled = "MLED",
		.mt_wled = "WLED",
		.mt_lcd_switch = "\\Q10",
		.lcd_status = "\\GP06",
		.brightness_set = "SPLV",
		.brightness_get = "GPLV",
		.display_set = "SDSP",
		.display_get = "\\INFB"
	}
L
Linus Torvalds 已提交
437 438 439 440 441
};

/* procdir we use */
static struct proc_dir_entry *asus_proc_dir;

442 443
static struct backlight_device *asus_backlight_device;

L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454
/*
 * This header is made available to allow proper configuration given model,
 * revision number , ... this info cannot go in struct asus_hotk because it is
 * available before the hotk
 */
static struct acpi_table_header *asus_info;

/* The actual device the driver binds to */
static struct asus_hotk *hotk;

/*
455
 * The hotkey driver and autoloading declaration
L
Linus Torvalds 已提交
456 457 458
 */
static int asus_hotk_add(struct acpi_device *device);
static int asus_hotk_remove(struct acpi_device *device, int type);
459 460
static void asus_hotk_notify(struct acpi_device *device, u32 event);

461 462 463 464 465 466
static const struct acpi_device_id asus_device_ids[] = {
	{"ATK0100", 0},
	{"", 0},
};
MODULE_DEVICE_TABLE(acpi, asus_device_ids);

L
Linus Torvalds 已提交
467
static struct acpi_driver asus_hotk_driver = {
L
Len Brown 已提交
468
	.name = "asus_acpi",
L
Len Brown 已提交
469
	.class = ACPI_HOTK_CLASS,
A
Alan Jenkins 已提交
470
	.owner = THIS_MODULE,
471
	.ids = asus_device_ids,
472
	.flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
L
Len Brown 已提交
473 474 475
	.ops = {
		.add = asus_hotk_add,
		.remove = asus_hotk_remove,
476
		.notify = asus_hotk_notify,
L
Len Brown 已提交
477
		},
L
Linus Torvalds 已提交
478 479
};

480
/*
L
Linus Torvalds 已提交
481 482 483 484
 * This function evaluates an ACPI method, given an int as parameter, the
 * method is searched within the scope of the handle, can be NULL. The output
 * of the method is written is output, which can also be NULL
 *
485
 * returns 1 if write is successful, 0 else.
L
Linus Torvalds 已提交
486 487 488 489
 */
static int write_acpi_int(acpi_handle handle, const char *method, int val,
			  struct acpi_buffer *output)
{
490 491
	struct acpi_object_list params;	/* list of input parameters (int) */
	union acpi_object in_obj;	/* the only param we use */
L
Linus Torvalds 已提交
492 493 494 495 496 497 498
	acpi_status status;

	params.count = 1;
	params.pointer = &in_obj;
	in_obj.type = ACPI_TYPE_INTEGER;
	in_obj.integer.value = val;

L
Len Brown 已提交
499
	status = acpi_evaluate_object(handle, (char *)method, &params, output);
L
Linus Torvalds 已提交
500 501 502 503 504 505 506 507 508 509 510 511
	return (status == AE_OK);
}

static int read_acpi_int(acpi_handle handle, const char *method, int *val)
{
	struct acpi_buffer output;
	union acpi_object out_obj;
	acpi_status status;

	output.length = sizeof(out_obj);
	output.pointer = &out_obj;

L
Len Brown 已提交
512
	status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
L
Linus Torvalds 已提交
513 514 515 516
	*val = out_obj.integer.value;
	return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
}

A
Alexey Dobriyan 已提交
517
static int asus_info_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
518 519 520
{
	int temp;

A
Alexey Dobriyan 已提交
521 522
	seq_printf(m, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
	seq_printf(m, "Model reference    : %s\n", hotk->methods->name);
523 524 525
	/*
	 * The SFUN method probably allows the original driver to get the list
	 * of features supported by a given model. For now, 0x0100 or 0x0800
L
Linus Torvalds 已提交
526 527 528 529
	 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
	 * The significance of others is yet to be found.
	 */
	if (read_acpi_int(hotk->handle, "SFUN", &temp))
A
Alexey Dobriyan 已提交
530
		seq_printf(m, "SFUN value         : 0x%04x\n", temp);
L
Linus Torvalds 已提交
531 532 533
	/*
	 * Another value for userspace: the ASYM method returns 0x02 for
	 * battery low and 0x04 for battery critical, its readings tend to be
534
	 * more accurate than those provided by _BST.
L
Linus Torvalds 已提交
535 536 537 538
	 * Note: since not all the laptops provide this method, errors are
	 * silently ignored.
	 */
	if (read_acpi_int(hotk->handle, "ASYM", &temp))
A
Alexey Dobriyan 已提交
539
		seq_printf(m, "ASYM value         : 0x%04x\n", temp);
L
Linus Torvalds 已提交
540
	if (asus_info) {
A
Alexey Dobriyan 已提交
541 542 543 544 545 546 547 548
		seq_printf(m, "DSDT length        : %d\n", asus_info->length);
		seq_printf(m, "DSDT checksum      : %d\n", asus_info->checksum);
		seq_printf(m, "DSDT revision      : %d\n", asus_info->revision);
		seq_printf(m, "OEM id             : %.*s\n", ACPI_OEM_ID_SIZE, asus_info->oem_id);
		seq_printf(m, "OEM table id       : %.*s\n", ACPI_OEM_TABLE_ID_SIZE, asus_info->oem_table_id);
		seq_printf(m, "OEM revision       : 0x%x\n", asus_info->oem_revision);
		seq_printf(m, "ASL comp vendor id : %.*s\n", ACPI_NAME_SIZE, asus_info->asl_compiler_id);
		seq_printf(m, "ASL comp revision  : 0x%x\n", asus_info->asl_compiler_revision);
L
Linus Torvalds 已提交
549 550
	}

A
Alexey Dobriyan 已提交
551 552 553 554 555 556
	return 0;
}

static int asus_info_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, asus_info_proc_show, NULL);
L
Linus Torvalds 已提交
557 558
}

A
Alexey Dobriyan 已提交
559 560 561 562 563 564 565 566
static const struct file_operations asus_info_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= asus_info_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

L
Linus Torvalds 已提交
567 568 569 570 571 572 573 574
/*
 * /proc handlers
 * We write our info in page, we begin at offset off and cannot write more
 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
 * number of bytes written in page
 */

/* Generic LED functions */
L
Len Brown 已提交
575
static int read_led(const char *ledname, int ledmask)
L
Linus Torvalds 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588
{
	if (ledname) {
		int led_status;

		if (read_acpi_int(NULL, ledname, &led_status))
			return led_status;
		else
			printk(KERN_WARNING "Asus ACPI: Error reading LED "
			       "status\n");
	}
	return (hotk->status & ledmask) ? 1 : 0;
}

589
static int parse_arg(const char __user *buf, unsigned long count, int *val)
L
Linus Torvalds 已提交
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
{
	char s[32];
	if (!count)
		return 0;
	if (count > 31)
		return -EINVAL;
	if (copy_from_user(s, buf, count))
		return -EFAULT;
	s[count] = 0;
	if (sscanf(s, "%i", val) != 1)
		return -EINVAL;
	return count;
}

/* FIXME: kill extraneous args so it can be called independently */
static int
606
write_led(const char __user *buffer, unsigned long count,
L
Len Brown 已提交
607
	  char *ledname, int ledmask, int invert)
L
Linus Torvalds 已提交
608
{
609
	int rv, value;
L
Linus Torvalds 已提交
610 611
	int led_out = 0;

612 613
	rv = parse_arg(buffer, count, &value);
	if (rv > 0)
L
Linus Torvalds 已提交
614 615 616 617 618
		led_out = value ? 1 : 0;

	hotk->status =
	    (led_out) ? (hotk->status | ledmask) : (hotk->status & ~ledmask);

L
Len Brown 已提交
619
	if (invert)		/* invert target value */
J
Julia Lawall 已提交
620
		led_out = !led_out;
L
Linus Torvalds 已提交
621 622

	if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
L
Len Brown 已提交
623 624
		printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
		       ledname);
L
Linus Torvalds 已提交
625

626
	return rv;
L
Linus Torvalds 已提交
627 628 629 630 631
}

/*
 * Proc handlers for MLED
 */
A
Alexey Dobriyan 已提交
632
static int mled_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
633
{
A
Alexey Dobriyan 已提交
634 635
	seq_printf(m, "%d\n", read_led(hotk->methods->mled_status, MLED_ON));
	return 0;
L
Linus Torvalds 已提交
636 637
}

A
Alexey Dobriyan 已提交
638 639 640 641 642 643 644
static int mled_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, mled_proc_show, NULL);
}

static ssize_t mled_proc_write(struct file *file, const char __user *buffer,
		size_t count, loff_t *pos)
L
Linus Torvalds 已提交
645 646 647 648
{
	return write_led(buffer, count, hotk->methods->mt_mled, MLED_ON, 1);
}

A
Alexey Dobriyan 已提交
649 650 651 652 653 654 655 656 657
static const struct file_operations mled_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= mled_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= mled_proc_write,
};

658 659 660
/*
 * Proc handlers for LED display
 */
A
Alexey Dobriyan 已提交
661
static int ledd_proc_show(struct seq_file *m, void *v)
662
{
A
Alexey Dobriyan 已提交
663 664
	seq_printf(m, "0x%08x\n", hotk->ledd_status);
	return 0;
665 666
}

A
Alexey Dobriyan 已提交
667 668 669 670 671 672 673
static int ledd_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, ledd_proc_show, NULL);
}

static ssize_t ledd_proc_write(struct file *file, const char __user *buffer,
		size_t count, loff_t *pos)
674
{
675
	int rv, value;
676

677 678
	rv = parse_arg(buffer, count, &value);
	if (rv > 0) {
679 680 681 682 683 684
		if (!write_acpi_int
		    (hotk->handle, hotk->methods->mt_ledd, value, NULL))
			printk(KERN_WARNING
			       "Asus ACPI: LED display write failed\n");
		else
			hotk->ledd_status = (u32) value;
685
	}
686
	return rv;
687 688
}

A
Alexey Dobriyan 已提交
689 690 691 692 693 694 695 696 697
static const struct file_operations ledd_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= ledd_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= ledd_proc_write,
};

L
Linus Torvalds 已提交
698 699 700
/*
 * Proc handlers for WLED
 */
A
Alexey Dobriyan 已提交
701
static int wled_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
702
{
A
Alexey Dobriyan 已提交
703 704
	seq_printf(m, "%d\n", read_led(hotk->methods->wled_status, WLED_ON));
	return 0;
L
Linus Torvalds 已提交
705 706
}

A
Alexey Dobriyan 已提交
707 708 709 710 711 712 713
static int wled_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, wled_proc_show, NULL);
}

static ssize_t wled_proc_write(struct file *file, const char __user *buffer,
		size_t count, loff_t *pos)
L
Linus Torvalds 已提交
714 715 716 717
{
	return write_led(buffer, count, hotk->methods->mt_wled, WLED_ON, 0);
}

A
Alexey Dobriyan 已提交
718 719 720 721 722 723 724 725 726
static const struct file_operations wled_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= wled_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= wled_proc_write,
};

727 728 729
/*
 * Proc handlers for Bluetooth
 */
A
Alexey Dobriyan 已提交
730
static int bluetooth_proc_show(struct seq_file *m, void *v)
731
{
A
Alexey Dobriyan 已提交
732 733
	seq_printf(m, "%d\n", read_led(hotk->methods->bt_status, BT_ON));
	return 0;
734 735
}

A
Alexey Dobriyan 已提交
736 737 738 739 740 741 742
static int bluetooth_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, bluetooth_proc_show, NULL);
}

static ssize_t bluetooth_proc_write(struct file *file,
		const char __user *buffer, size_t count, loff_t *pos)
743
{
744
	/* Note: mt_bt_switch controls both internal Bluetooth adapter's
745 746 747 748
	   presence and its LED */
	return write_led(buffer, count, hotk->methods->mt_bt_switch, BT_ON, 0);
}

A
Alexey Dobriyan 已提交
749 750 751 752 753 754 755 756 757
static const struct file_operations bluetooth_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= bluetooth_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= bluetooth_proc_write,
};

L
Linus Torvalds 已提交
758 759 760
/*
 * Proc handlers for TLED
 */
A
Alexey Dobriyan 已提交
761
static int tled_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
762
{
A
Alexey Dobriyan 已提交
763 764
	seq_printf(m, "%d\n", read_led(hotk->methods->tled_status, TLED_ON));
	return 0;
L
Linus Torvalds 已提交
765 766
}

A
Alexey Dobriyan 已提交
767 768 769 770 771 772 773
static int tled_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, tled_proc_show, NULL);
}

static ssize_t tled_proc_write(struct file *file, const char __user *buffer,
		size_t count, loff_t *pos)
L
Linus Torvalds 已提交
774 775 776 777
{
	return write_led(buffer, count, hotk->methods->mt_tled, TLED_ON, 0);
}

A
Alexey Dobriyan 已提交
778 779 780 781 782 783 784 785 786
static const struct file_operations tled_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= tled_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= tled_proc_write,
};

L
Linus Torvalds 已提交
787 788 789 790
static int get_lcd_state(void)
{
	int lcd = 0;

791 792
	if (hotk->model == L3H) {
		/* L3H and the like have to be handled differently */
L
Linus Torvalds 已提交
793 794 795 796 797
		acpi_status status = 0;
		struct acpi_object_list input;
		union acpi_object mt_params[2];
		struct acpi_buffer output;
		union acpi_object out_obj;
L
Len Brown 已提交
798

L
Linus Torvalds 已提交
799 800
		input.count = 2;
		input.pointer = mt_params;
801
		/* Note: the following values are partly guessed up, but
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809
		   otherwise they seem to work */
		mt_params[0].type = ACPI_TYPE_INTEGER;
		mt_params[0].integer.value = 0x02;
		mt_params[1].type = ACPI_TYPE_INTEGER;
		mt_params[1].integer.value = 0x02;

		output.length = sizeof(out_obj);
		output.pointer = &out_obj;
L
Len Brown 已提交
810 811 812 813

		status =
		    acpi_evaluate_object(NULL, hotk->methods->lcd_status,
					 &input, &output);
L
Linus Torvalds 已提交
814 815 816 817 818
		if (status != AE_OK)
			return -1;
		if (out_obj.type == ACPI_TYPE_INTEGER)
			/* That's what the AML code does */
			lcd = out_obj.integer.value >> 8;
819
	} else if (hotk->model == F3Sa) {
820
		unsigned long long tmp;
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
		union acpi_object param;
		struct acpi_object_list input;
		acpi_status status;

		/* Read pin 11 */
		param.type = ACPI_TYPE_INTEGER;
		param.integer.value = 0x11;
		input.count = 1;
		input.pointer = &param;

		status = acpi_evaluate_integer(NULL, hotk->methods->lcd_status,
						&input, &tmp);
		if (status != AE_OK)
			return -1;

		lcd = tmp;
	} else {
		/* We don't have to check anything if we are here */
		if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
			printk(KERN_WARNING
			       "Asus ACPI: Error reading LCD status\n");

		if (hotk->model == L2D)
			lcd = ~lcd;
L
Linus Torvalds 已提交
845
	}
L
Len Brown 已提交
846

L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859
	return (lcd & 1);
}

static int set_lcd_state(int value)
{
	int lcd = 0;
	acpi_status status = 0;

	lcd = value ? 1 : 0;
	if (lcd != get_lcd_state()) {
		/* switch */
		if (hotk->model != L3H) {
			status =
L
Len Brown 已提交
860 861
			    acpi_evaluate_object(NULL,
						 hotk->methods->mt_lcd_switch,
L
Linus Torvalds 已提交
862
						 NULL, NULL);
863 864
		} else {
			/* L3H and the like must be handled differently */
L
Len Brown 已提交
865 866 867
			if (!write_acpi_int
			    (hotk->handle, hotk->methods->mt_lcd_switch, 0x07,
			     NULL))
L
Linus Torvalds 已提交
868
				status = AE_ERROR;
869
			/* L3H's AML executes EHK (0x07) upon Fn+F7 keypress,
L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877 878
			   the exact behaviour is simulated here */
		}
		if (ACPI_FAILURE(status))
			printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
	}
	return 0;

}

A
Alexey Dobriyan 已提交
879
static int lcd_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
880
{
A
Alexey Dobriyan 已提交
881 882
	seq_printf(m, "%d\n", get_lcd_state());
	return 0;
L
Linus Torvalds 已提交
883 884
}

A
Alexey Dobriyan 已提交
885 886 887 888 889 890 891
static int lcd_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, lcd_proc_show, NULL);
}

static ssize_t lcd_proc_write(struct file *file, const char __user *buffer,
	       size_t count, loff_t *pos)
L
Linus Torvalds 已提交
892
{
893
	int rv, value;
L
Len Brown 已提交
894

895 896
	rv = parse_arg(buffer, count, &value);
	if (rv > 0)
L
Linus Torvalds 已提交
897
		set_lcd_state(value);
898
	return rv;
L
Linus Torvalds 已提交
899 900
}

A
Alexey Dobriyan 已提交
901 902 903 904 905 906 907 908 909
static const struct file_operations lcd_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= lcd_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= lcd_proc_write,
};

910
static int read_brightness(struct backlight_device *bd)
L
Linus Torvalds 已提交
911 912
{
	int value;
L
Len Brown 已提交
913 914 915

	if (hotk->methods->brightness_get) {	/* SPLV/GPLV laptop */
		if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get,
L
Linus Torvalds 已提交
916
				   &value))
L
Len Brown 已提交
917 918 919 920
			printk(KERN_WARNING
			       "Asus ACPI: Error reading brightness\n");
	} else if (hotk->methods->brightness_status) {	/* For D1 for example */
		if (!read_acpi_int(NULL, hotk->methods->brightness_status,
L
Linus Torvalds 已提交
921
				   &value))
L
Len Brown 已提交
922 923 924
			printk(KERN_WARNING
			       "Asus ACPI: Error reading brightness\n");
	} else			/* No GPLV method */
L
Linus Torvalds 已提交
925 926 927 928 929 930 931
		value = hotk->brightness;
	return value;
}

/*
 * Change the brightness level
 */
932
static int set_brightness(int value)
L
Linus Torvalds 已提交
933 934
{
	acpi_status status = 0;
935
	int ret = 0;
L
Linus Torvalds 已提交
936 937

	/* SPLV laptop */
L
Len Brown 已提交
938 939
	if (hotk->methods->brightness_set) {
		if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set,
L
Linus Torvalds 已提交
940
				    value, NULL))
L
Len Brown 已提交
941 942
			printk(KERN_WARNING
			       "Asus ACPI: Error changing brightness\n");
943 944
			ret = -EIO;
		goto out;
L
Linus Torvalds 已提交
945 946 947
	}

	/* No SPLV method if we are here, act as appropriate */
948
	value -= read_brightness(NULL);
L
Linus Torvalds 已提交
949
	while (value != 0) {
L
Len Brown 已提交
950 951
		status = acpi_evaluate_object(NULL, (value > 0) ?
					      hotk->methods->brightness_up :
L
Linus Torvalds 已提交
952 953 954 955
					      hotk->methods->brightness_down,
					      NULL, NULL);
		(value > 0) ? value-- : value++;
		if (ACPI_FAILURE(status))
L
Len Brown 已提交
956 957
			printk(KERN_WARNING
			       "Asus ACPI: Error changing brightness\n");
958
			ret = -EIO;
L
Linus Torvalds 已提交
959
	}
960 961 962 963 964 965
out:
	return ret;
}

static int set_brightness_status(struct backlight_device *bd)
{
966
	return set_brightness(bd->props.brightness);
L
Linus Torvalds 已提交
967 968
}

A
Alexey Dobriyan 已提交
969
static int brn_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
970
{
A
Alexey Dobriyan 已提交
971 972
	seq_printf(m, "%d\n", read_brightness(NULL));
	return 0;
L
Linus Torvalds 已提交
973 974
}

A
Alexey Dobriyan 已提交
975 976 977 978 979 980 981
static int brn_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, brn_proc_show, NULL);
}

static ssize_t brn_proc_write(struct file *file, const char __user *buffer,
	       size_t count, loff_t *pos)
L
Linus Torvalds 已提交
982
{
983
	int rv, value;
L
Linus Torvalds 已提交
984

985 986
	rv = parse_arg(buffer, count, &value);
	if (rv > 0) {
L
Linus Torvalds 已提交
987
		value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
L
Len Brown 已提交
988
		/* 0 <= value <= 15 */
L
Linus Torvalds 已提交
989 990
		set_brightness(value);
	}
991
	return rv;
L
Linus Torvalds 已提交
992 993
}

A
Alexey Dobriyan 已提交
994 995 996 997 998 999 1000 1001 1002
static const struct file_operations brn_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= brn_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= brn_proc_write,
};

L
Linus Torvalds 已提交
1003 1004 1005
static void set_display(int value)
{
	/* no sanity check needed for now */
L
Len Brown 已提交
1006
	if (!write_acpi_int(hotk->handle, hotk->methods->display_set,
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012
			    value, NULL))
		printk(KERN_WARNING "Asus ACPI: Error setting display\n");
	return;
}

/*
1013
 * Now, *this* one could be more user-friendly, but so far, no-one has
L
Linus Torvalds 已提交
1014 1015
 * complained. The significance of bits is the same as in proc_write_disp()
 */
A
Alexey Dobriyan 已提交
1016
static int disp_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
1017 1018
{
	int value = 0;
L
Len Brown 已提交
1019

L
Linus Torvalds 已提交
1020
	if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
L
Len Brown 已提交
1021 1022
		printk(KERN_WARNING
		       "Asus ACPI: Error reading display status\n");
1023
	value &= 0x07;	/* needed for some models, shouldn't hurt others */
A
Alexey Dobriyan 已提交
1024 1025 1026 1027 1028 1029 1030
	seq_printf(m, "%d\n", value);
	return 0;
}

static int disp_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, disp_proc_show, NULL);
L
Linus Torvalds 已提交
1031 1032 1033
}

/*
1034 1035 1036 1037
 * Experimental support for display switching. As of now: 1 should activate
 * the LCD output, 2 should do for CRT, and 4 for TV-Out. Any combination
 * (bitwise) of these will suffice. I never actually tested 3 displays hooked
 * up simultaneously, so be warned. See the acpi4asus README for more info.
L
Linus Torvalds 已提交
1038
 */
A
Alexey Dobriyan 已提交
1039 1040
static ssize_t disp_proc_write(struct file *file, const char __user *buffer,
		size_t count, loff_t *pos)
L
Linus Torvalds 已提交
1041
{
1042
	int rv, value;
L
Linus Torvalds 已提交
1043

1044 1045
	rv = parse_arg(buffer, count, &value);
	if (rv > 0)
L
Linus Torvalds 已提交
1046
		set_display(value);
1047
	return rv;
L
Linus Torvalds 已提交
1048 1049
}

A
Alexey Dobriyan 已提交
1050 1051 1052 1053 1054 1055 1056 1057
static const struct file_operations disp_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= disp_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
	.write		= disp_proc_write,
};
L
Linus Torvalds 已提交
1058 1059

static int
A
Alexey Dobriyan 已提交
1060
asus_proc_add(char *name, const struct file_operations *proc_fops, mode_t mode,
L
Len Brown 已提交
1061
		     struct acpi_device *device)
L
Linus Torvalds 已提交
1062
{
A
Alexey Dobriyan 已提交
1063 1064 1065 1066
	struct proc_dir_entry *proc;

	proc = proc_create_data(name, mode, acpi_device_dir(device),
				proc_fops, acpi_driver_data(device));
L
Len Brown 已提交
1067
	if (!proc) {
L
Linus Torvalds 已提交
1068 1069 1070 1071 1072 1073 1074 1075
		printk(KERN_WARNING "  Unable to create %s fs entry\n", name);
		return -1;
	}
	proc->uid = asus_uid;
	proc->gid = asus_gid;
	return 0;
}

1076
static int asus_hotk_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
1077 1078 1079
{
	struct proc_dir_entry *proc;
	mode_t mode;
L
Len Brown 已提交
1080

L
Linus Torvalds 已提交
1081 1082 1083 1084 1085 1086
	/*
	 * If parameter uid or gid is not changed, keep the default setting for
	 * our proc entries (-rw-rw-rw-) else, it means we care about security,
	 * and then set to -rw-rw----
	 */

L
Len Brown 已提交
1087
	if ((asus_uid == 0) && (asus_gid == 0)) {
L
Linus Torvalds 已提交
1088 1089 1090
		mode = S_IFREG | S_IRUGO | S_IWUGO;
	} else {
		mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
1091 1092
		printk(KERN_WARNING "  asus_uid and asus_gid parameters are "
		       "deprecated, use chown and chmod instead!\n");
L
Linus Torvalds 已提交
1093 1094 1095 1096 1097 1098
	}

	acpi_device_dir(device) = asus_proc_dir;
	if (!acpi_device_dir(device))
		return -ENODEV;

A
Alexey Dobriyan 已提交
1099 1100
	proc = proc_create(PROC_INFO, mode, acpi_device_dir(device),
			   &asus_info_proc_fops);
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109
	if (proc) {
		proc->uid = asus_uid;
		proc->gid = asus_gid;
	} else {
		printk(KERN_WARNING "  Unable to create " PROC_INFO
		       " fs entry\n");
	}

	if (hotk->methods->mt_wled) {
A
Alexey Dobriyan 已提交
1110
		asus_proc_add(PROC_WLED, &wled_proc_fops, mode, device);
L
Linus Torvalds 已提交
1111 1112
	}

1113
	if (hotk->methods->mt_ledd) {
A
Alexey Dobriyan 已提交
1114
		asus_proc_add(PROC_LEDD, &ledd_proc_fops, mode, device);
1115 1116
	}

L
Linus Torvalds 已提交
1117
	if (hotk->methods->mt_mled) {
A
Alexey Dobriyan 已提交
1118
		asus_proc_add(PROC_MLED, &mled_proc_fops, mode, device);
L
Linus Torvalds 已提交
1119 1120 1121
	}

	if (hotk->methods->mt_tled) {
A
Alexey Dobriyan 已提交
1122
		asus_proc_add(PROC_TLED, &tled_proc_fops, mode, device);
L
Linus Torvalds 已提交
1123 1124
	}

1125
	if (hotk->methods->mt_bt_switch) {
A
Alexey Dobriyan 已提交
1126
		asus_proc_add(PROC_BT, &bluetooth_proc_fops, mode, device);
1127 1128
	}

1129 1130 1131
	/*
	 * We need both read node and write method as LCD switch is also
	 * accessible from the keyboard
L
Linus Torvalds 已提交
1132 1133
	 */
	if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
A
Alexey Dobriyan 已提交
1134
		asus_proc_add(PROC_LCD, &lcd_proc_fops, mode, device);
L
Linus Torvalds 已提交
1135
	}
L
Len Brown 已提交
1136

L
Linus Torvalds 已提交
1137 1138
	if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
	    (hotk->methods->brightness_get && hotk->methods->brightness_set)) {
A
Alexey Dobriyan 已提交
1139
		asus_proc_add(PROC_BRN, &brn_proc_fops, mode, device);
L
Linus Torvalds 已提交
1140 1141 1142
	}

	if (hotk->methods->display_set) {
A
Alexey Dobriyan 已提交
1143
		asus_proc_add(PROC_DISP, &disp_proc_fops, mode, device);
L
Linus Torvalds 已提交
1144 1145 1146 1147 1148
	}

	return 0;
}

L
Len Brown 已提交
1149
static int asus_hotk_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
1150
{
L
Len Brown 已提交
1151 1152
	if (acpi_device_dir(device)) {
		remove_proc_entry(PROC_INFO, acpi_device_dir(device));
L
Linus Torvalds 已提交
1153
		if (hotk->methods->mt_wled)
L
Len Brown 已提交
1154
			remove_proc_entry(PROC_WLED, acpi_device_dir(device));
L
Linus Torvalds 已提交
1155
		if (hotk->methods->mt_mled)
L
Len Brown 已提交
1156
			remove_proc_entry(PROC_MLED, acpi_device_dir(device));
L
Linus Torvalds 已提交
1157
		if (hotk->methods->mt_tled)
L
Len Brown 已提交
1158
			remove_proc_entry(PROC_TLED, acpi_device_dir(device));
1159 1160
		if (hotk->methods->mt_ledd)
			remove_proc_entry(PROC_LEDD, acpi_device_dir(device));
1161 1162
		if (hotk->methods->mt_bt_switch)
			remove_proc_entry(PROC_BT, acpi_device_dir(device));
L
Linus Torvalds 已提交
1163 1164
		if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status)
			remove_proc_entry(PROC_LCD, acpi_device_dir(device));
L
Len Brown 已提交
1165 1166 1167 1168
		if ((hotk->methods->brightness_up
		     && hotk->methods->brightness_down)
		    || (hotk->methods->brightness_get
			&& hotk->methods->brightness_set))
L
Linus Torvalds 已提交
1169 1170 1171 1172 1173 1174 1175
			remove_proc_entry(PROC_BRN, acpi_device_dir(device));
		if (hotk->methods->display_set)
			remove_proc_entry(PROC_DISP, acpi_device_dir(device));
	}
	return 0;
}

1176
static void asus_hotk_notify(struct acpi_device *device, u32 event)
L
Linus Torvalds 已提交
1177
{
L
Len Brown 已提交
1178
	/* TODO Find a better way to handle events count. */
L
Linus Torvalds 已提交
1179 1180 1181
	if (!hotk)
		return;

1182 1183 1184 1185 1186 1187 1188 1189
	/*
	 * The BIOS *should* be sending us device events, but apparently
	 * Asus uses system events instead, so just ignore any device
	 * events we get.
	 */
	if (event > ACPI_MAX_SYS_NOTIFY)
		return;

1190
	if ((event & ~((u32) BR_UP)) < 16)
L
Linus Torvalds 已提交
1191
		hotk->brightness = (event & ~((u32) BR_UP));
1192
	else if ((event & ~((u32) BR_DOWN)) < 16)
L
Linus Torvalds 已提交
1193 1194
		hotk->brightness = (event & ~((u32) BR_DOWN));

1195
	acpi_bus_generate_proc_event(hotk->device, event,
L
Linus Torvalds 已提交
1196 1197 1198 1199 1200
				hotk->event_count[event % 128]++);

	return;
}

1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
/*
 * Match the model string to the list of supported models. Return END_MODEL if
 * no match or model is NULL.
 */
static int asus_model_match(char *model)
{
	if (model == NULL)
		return END_MODEL;

	if (strncmp(model, "L3D", 3) == 0)
		return L3D;
	else if (strncmp(model, "L2E", 3) == 0 ||
		 strncmp(model, "L3H", 3) == 0 || strncmp(model, "L5D", 3) == 0)
		return L3H;
	else if (strncmp(model, "L3", 2) == 0 || strncmp(model, "L2B", 3) == 0)
		return L3C;
	else if (strncmp(model, "L8L", 3) == 0)
		return L8L;
	else if (strncmp(model, "L4R", 3) == 0)
		return L4R;
	else if (strncmp(model, "M6N", 3) == 0 || strncmp(model, "W3N", 3) == 0)
		return M6N;
	else if (strncmp(model, "M6R", 3) == 0 || strncmp(model, "A3G", 3) == 0)
		return M6R;
	else if (strncmp(model, "M2N", 3) == 0 ||
		 strncmp(model, "M3N", 3) == 0 ||
		 strncmp(model, "M5N", 3) == 0 ||
		 strncmp(model, "M6N", 3) == 0 ||
		 strncmp(model, "S1N", 3) == 0 ||
		 strncmp(model, "S5N", 3) == 0 || strncmp(model, "W1N", 3) == 0)
		return xxN;
	else if (strncmp(model, "M1", 2) == 0)
		return M1A;
	else if (strncmp(model, "M2", 2) == 0 || strncmp(model, "L4E", 3) == 0)
		return M2E;
	else if (strncmp(model, "L2", 2) == 0)
		return L2D;
	else if (strncmp(model, "L8", 2) == 0)
		return S1x;
	else if (strncmp(model, "D1", 2) == 0)
		return D1x;
	else if (strncmp(model, "A1", 2) == 0)
		return A1x;
	else if (strncmp(model, "A2", 2) == 0)
		return A2x;
	else if (strncmp(model, "J1", 2) == 0)
		return S2x;
	else if (strncmp(model, "L5", 2) == 0)
		return L5x;
	else if (strncmp(model, "A4G", 3) == 0)
		return A4G;
	else if (strncmp(model, "W1N", 3) == 0)
		return W1N;
M
Marek W 已提交
1254 1255
	else if (strncmp(model, "W3V", 3) == 0)
		return W3V;
1256 1257
	else if (strncmp(model, "W5A", 3) == 0)
		return W5A;
C
Corentin Chary 已提交
1258 1259
	else if (strncmp(model, "R1F", 3) == 0)
		return R1F;
1260 1261
	else if (strncmp(model, "A4S", 3) == 0)
		return A4S;
1262 1263
	else if (strncmp(model, "F3Sa", 4) == 0)
		return F3Sa;
1264 1265 1266 1267
	else
		return END_MODEL;
}

L
Linus Torvalds 已提交
1268 1269 1270 1271
/*
 * This function is used to initialize the hotk with right values. In this
 * method, we can make all the detection we want, and modify the hotk struct
 */
1272
static int asus_hotk_get_info(void)
L
Linus Torvalds 已提交
1273 1274 1275 1276
{
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *model = NULL;
	int bsts_result;
1277
	char *string = NULL;
L
Linus Torvalds 已提交
1278 1279 1280
	acpi_status status;

	/*
1281 1282
	 * Get DSDT headers early enough to allow for differentiating between
	 * models, but late enough to allow acpi_bus_register_driver() to fail
L
Linus Torvalds 已提交
1283 1284 1285 1286 1287
	 * before doing anything ACPI-specific. Should we encounter a machine,
	 * which needs special handling (i.e. its hotkey device has a different
	 * HID), this bit will be moved. A global variable asus_info contains
	 * the DSDT header.
	 */
1288
	status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
L
Linus Torvalds 已提交
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301
	if (ACPI_FAILURE(status))
		printk(KERN_WARNING "  Couldn't get the DSDT table header\n");

	/* We have to write 0 on init this far for all ASUS models */
	if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
		printk(KERN_ERR "  Hotkey initialization failed\n");
		return -ENODEV;
	}

	/* This needs to be called for some laptops to init properly */
	if (!read_acpi_int(hotk->handle, "BSTS", &bsts_result))
		printk(KERN_WARNING "  Error calling BSTS\n");
	else if (bsts_result)
L
Len Brown 已提交
1302 1303
		printk(KERN_NOTICE "  BSTS called, 0x%02x returned\n",
		       bsts_result);
L
Linus Torvalds 已提交
1304

1305
	/*
1306
	 * Try to match the object returned by INIT to the specific model.
1307 1308
	 * Handle every possible object (or the lack of thereof) the DSDT
	 * writers might throw at us. When in trouble, we pass NULL to
1309
	 * asus_model_match() and try something completely different.
1310
	 */
1311
	if (buffer.pointer) {
1312
		model = buffer.pointer;
1313 1314 1315 1316 1317 1318 1319 1320 1321
		switch (model->type) {
		case ACPI_TYPE_STRING:
			string = model->string.pointer;
			break;
		case ACPI_TYPE_BUFFER:
			string = model->buffer.pointer;
			break;
		default:
			kfree(model);
1322
			model = NULL;
1323 1324 1325 1326 1327 1328
			break;
		}
	}
	hotk->model = asus_model_match(string);
	if (hotk->model == END_MODEL) {	/* match failed */
		if (asus_info &&
L
Linus Torvalds 已提交
1329 1330
		    strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
			hotk->model = P30;
L
Len Brown 已提交
1331 1332
			printk(KERN_NOTICE
			       "  Samsung P30 detected, supported\n");
L
Linus Torvalds 已提交
1333 1334
		} else {
			hotk->model = M2E;
1335 1336 1337 1338
			printk(KERN_NOTICE "  unsupported model %s, trying "
			       "default values\n", string);
			printk(KERN_NOTICE
			       "  send /proc/acpi/dsdt to the developers\n");
1339 1340
			kfree(model);
			return -ENODEV;
L
Linus Torvalds 已提交
1341 1342
		}
		hotk->methods = &model_conf[hotk->model];
1343
		return AE_OK;
L
Linus Torvalds 已提交
1344 1345
	}
	hotk->methods = &model_conf[hotk->model];
1346
	printk(KERN_NOTICE "  %s model detected, supported\n", string);
L
Linus Torvalds 已提交
1347 1348

	/* Sort of per-model blacklist */
1349
	if (strncmp(string, "L2B", 3) == 0)
L
Len Brown 已提交
1350
		hotk->methods->lcd_status = NULL;
1351
	/* L2B is similar enough to L3C to use its settings, with this only
L
Linus Torvalds 已提交
1352
	   exception */
1353
	else if (strncmp(string, "A3G", 3) == 0)
K
Karol Kozimor 已提交
1354 1355
		hotk->methods->lcd_status = "\\BLFG";
	/* A3G is like M6R */
1356 1357 1358
	else if (strncmp(string, "S5N", 3) == 0 ||
		 strncmp(string, "M5N", 3) == 0 ||
		 strncmp(string, "W3N", 3) == 0)
L
Len Brown 已提交
1359
		hotk->methods->mt_mled = NULL;
K
Karol Kozimor 已提交
1360
	/* S5N, M5N and W3N have no MLED */
1361
	else if (strncmp(string, "L5D", 3) == 0)
K
Karol Kozimor 已提交
1362 1363
		hotk->methods->mt_wled = NULL;
	/* L5D's WLED is not controlled by ACPI */
1364
	else if (strncmp(string, "M2N", 3) == 0 ||
M
Marek W 已提交
1365
		 strncmp(string, "W3V", 3) == 0 ||
1366
		 strncmp(string, "S1N", 3) == 0)
L
Len Brown 已提交
1367
		hotk->methods->mt_wled = "WLED";
M
Marek W 已提交
1368
	/* M2N, S1N and W3V have a usable WLED */
L
Linus Torvalds 已提交
1369 1370 1371
	else if (asus_info) {
		if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
			hotk->methods->mled_status = NULL;
L
Len Brown 已提交
1372
		/* S1300A reports L84F, but L1400B too, account for that */
L
Linus Torvalds 已提交
1373 1374
	}

1375
	kfree(model);
L
Linus Torvalds 已提交
1376 1377 1378 1379

	return AE_OK;
}

1380
static int asus_hotk_check(void)
L
Linus Torvalds 已提交
1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
{
	int result = 0;

	result = acpi_bus_get_status(hotk->device);
	if (result)
		return result;

	if (hotk->device->status.present) {
		result = asus_hotk_get_info();
	} else {
		printk(KERN_ERR "  Hotkey device not present, aborting\n");
		return -EINVAL;
	}

	return result;
}

1398 1399
static int asus_hotk_found;

1400
static int asus_hotk_add(struct acpi_device *device)
L
Linus Torvalds 已提交
1401 1402 1403 1404 1405 1406 1407
{
	acpi_status status = AE_OK;
	int result;

	printk(KERN_NOTICE "Asus Laptop ACPI Extras version %s\n",
	       ASUS_ACPI_VERSION);

1408
	hotk = kzalloc(sizeof(struct asus_hotk), GFP_KERNEL);
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414
	if (!hotk)
		return -ENOMEM;

	hotk->handle = device->handle;
	strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
1415
	device->driver_data = hotk;
L
Linus Torvalds 已提交
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
	hotk->device = device;

	result = asus_hotk_check();
	if (result)
		goto end;

	result = asus_hotk_add_fs(device);
	if (result)
		goto end;

	/* For laptops without GPLV: init the hotk->brightness value */
L
Len Brown 已提交
1427 1428
	if ((!hotk->methods->brightness_get)
	    && (!hotk->methods->brightness_status)
K
Karol Kozimor 已提交
1429
	    && (hotk->methods->brightness_up && hotk->methods->brightness_down)) {
L
Len Brown 已提交
1430 1431 1432
		status =
		    acpi_evaluate_object(NULL, hotk->methods->brightness_down,
					 NULL, NULL);
L
Linus Torvalds 已提交
1433 1434 1435
		if (ACPI_FAILURE(status))
			printk(KERN_WARNING "  Error changing brightness\n");
		else {
L
Len Brown 已提交
1436 1437 1438 1439
			status =
			    acpi_evaluate_object(NULL,
						 hotk->methods->brightness_up,
						 NULL, NULL);
L
Linus Torvalds 已提交
1440
			if (ACPI_FAILURE(status))
L
Len Brown 已提交
1441
				printk(KERN_WARNING "  Strange, error changing"
L
Linus Torvalds 已提交
1442 1443 1444 1445
				       " brightness\n");
		}
	}

1446 1447
	asus_hotk_found = 1;

1448 1449 1450
	/* LED display is off by default */
	hotk->ledd_status = 0xFFF;

1451 1452
end:
	if (result)
L
Linus Torvalds 已提交
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
		kfree(hotk);

	return result;
}

static int asus_hotk_remove(struct acpi_device *device, int type)
{
	asus_hotk_remove_fs(device);

	kfree(hotk);

	return 0;
}

1467
static struct backlight_ops asus_backlight_data = {
1468 1469
	.get_brightness = read_brightness,
	.update_status  = set_brightness_status,
1470 1471
};

1472
static void asus_acpi_exit(void)
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
{
	if (asus_backlight_device)
		backlight_device_unregister(asus_backlight_device);

	acpi_bus_unregister_driver(&asus_hotk_driver);
	remove_proc_entry(PROC_ASUS, acpi_root_dir);

	return;
}

L
Linus Torvalds 已提交
1483 1484 1485 1486
static int __init asus_acpi_init(void)
{
	int result;

1487 1488 1489
	result = acpi_bus_register_driver(&asus_hotk_driver);
	if (result < 0)
		return result;
L
Linus Torvalds 已提交
1490 1491 1492 1493

	asus_proc_dir = proc_mkdir(PROC_ASUS, acpi_root_dir);
	if (!asus_proc_dir) {
		printk(KERN_ERR "Asus ACPI: Unable to create /proc entry\n");
1494
		acpi_bus_unregister_driver(&asus_hotk_driver);
L
Linus Torvalds 已提交
1495 1496 1497
		return -ENODEV;
	}

1498 1499 1500 1501 1502 1503 1504 1505
	/*
	 * This is a bit of a kludge.  We only want this module loaded
	 * for ASUS systems, but there's currently no way to probe the
	 * ACPI namespace for ASUS HIDs.  So we just return failure if
	 * we didn't find one, which will cause the module to be
	 * unloaded.
	 */
	if (!asus_hotk_found) {
L
Linus Torvalds 已提交
1506 1507
		acpi_bus_unregister_driver(&asus_hotk_driver);
		remove_proc_entry(PROC_ASUS, acpi_root_dir);
1508
		return -ENODEV;
L
Linus Torvalds 已提交
1509 1510
	}

1511
	asus_backlight_device = backlight_device_register("asus", NULL, NULL,
1512
							  &asus_backlight_data);
1513
	if (IS_ERR(asus_backlight_device)) {
1514 1515 1516
		printk(KERN_ERR "Could not register asus backlight device\n");
		asus_backlight_device = NULL;
		asus_acpi_exit();
1517
		return -ENODEV;
1518
	}
1519
	asus_backlight_device->props.max_brightness = 15;
L
Linus Torvalds 已提交
1520

1521
	return 0;
L
Linus Torvalds 已提交
1522 1523 1524 1525
}

module_init(asus_acpi_init);
module_exit(asus_acpi_exit);