asus_acpi.c 34.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
/*
 *  asus_acpi.c - Asus Laptop ACPI Extras
 *
 *
 *  Copyright (C) 2002, 2003, 2004 Julien Lerouge, Karol Kozimor
 *
 *  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.
 *
 *  TODO:
 *  add Fn key status
 *  Add mode selection on module loading (parameter) -> still necessary?
 *  Complete display switching -- may require dirty hacks or calling _DOS?
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/proc_fs.h>
#include <acpi/acpi_drivers.h>
#include <acpi/acpi_bus.h>
#include <asm/uaccess.h>

#define ASUS_ACPI_VERSION "0.29"

#define PROC_ASUS       "asus"	//the directory
#define PROC_MLED       "mled"
#define PROC_WLED       "wled"
#define PROC_TLED       "tled"
#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"
#define ACPI_HOTK_HID           "ATK0100"

/*
 * Some events we use, same for all Asus
 */
L
Len Brown 已提交
64
#define BR_UP       0x10
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#define BR_DOWN     0x20

/*
 * Flags for hotk status
 */
#define MLED_ON     0x01	//is MLED ON ?
#define WLED_ON     0x02
#define TLED_ON     0x04

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);
81
MODULE_PARM_DESC(asus_uid, "UID for entries in /proc/acpi/asus.\n");
L
Linus Torvalds 已提交
82
module_param(asus_gid, uint, 0);
83
MODULE_PARM_DESC(asus_gid, "GID for entries in /proc/acpi/asus.\n");
L
Linus Torvalds 已提交
84 85 86 87

/* For each model, all features implemented, 
 * those marked with R are relative to HOTK, A for absolute */
struct model_data {
L
Len Brown 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	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_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;	//guess what ?______________________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 已提交
104 105 106 107 108 109 110
};

/*
 * This is the main structure, we can use it to store anything interesting
 * about the hotk device
 */
struct asus_hotk {
L
Len Brown 已提交
111 112 113 114 115
	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, ...
	struct model_data *methods;	//methods available on the laptop
	u8 brightness;		//brightness level
L
Linus Torvalds 已提交
116
	enum {
L
Len Brown 已提交
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		A1x = 0,	//A1340D, A1300F
		A2x,		//A2500H
		D1x,		//D1
		L2D,		//L2000D
		L3C,		//L3800C
		L3D,		//L3400D
		L3H,		//L3H, but also L2000E
		L4R,		//L4500R
		L5x,		//L5800C 
		L8L,		//L8400L
		M1A,		//M1300A
		M2E,		//M2400E, L4400L
		M6N,		//M6800N
		M6R,		//M6700R
		P30,		//Samsung P30
		S1x,		//S1300A, but also L1400B and M2400A (L84F)
		S2x,		//S200 (J1 reported), Victor MP-XP7210
		xxN,		//M2400N, M3700N, M5200N, S1300N, S5200N, W1OOON
		//(Centrino)
L
Linus Torvalds 已提交
136
		END_MODEL
L
Len Brown 已提交
137 138
	} model;		//Models currently supported
	u16 event_count[128];	//count for each event TODO make this better
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146 147 148 149 150
};

/* 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 已提交
151
	/*
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165
	 * Those pathnames are relative to the HOTK / ATKD device :
	 *       - mt_mled
	 *       - mt_wled
	 *       - brightness_set
	 *       - brightness_get
	 *       - display_set
	 *       - display_get
	 *
	 * 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 已提交
166 167 168 169 170 171 172
	 .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 已提交
173 174

	{
L
Len Brown 已提交
175 176 177 178 179 180 181 182 183 184
	 .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 已提交
185 186

	{
L
Len Brown 已提交
187 188 189 190 191 192 193 194 195
	 .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 已提交
196 197

	{
L
Len Brown 已提交
198 199 200 201 202 203 204 205 206 207 208
	 .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 已提交
209 210

	{
L
Len Brown 已提交
211 212 213 214 215 216 217 218 219
	 .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 已提交
220 221

	{
L
Len Brown 已提交
222 223 224 225 226 227 228 229 230 231
	 .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 已提交
232 233

	{
L
Len Brown 已提交
234 235 236 237 238 239 240 241 242
	 .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 已提交
243 244

	{
L
Len Brown 已提交
245 246 247 248 249 250 251 252 253 254
	 .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 已提交
255 256

	{
L
Len Brown 已提交
257 258
	 .name = "L5x",
	 .mt_mled = "MLED",
L
Linus Torvalds 已提交
259
/* WLED present, but not controlled by ACPI */
L
Len Brown 已提交
260 261 262 263 264 265 266
	 .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 已提交
267 268

	{
L
Len Brown 已提交
269
	 .name = "L8L"
L
Linus Torvalds 已提交
270
/* No features, but at least support the hotkeys */
L
Len Brown 已提交
271
	 },
L
Linus Torvalds 已提交
272 273

	{
L
Len Brown 已提交
274 275 276 277 278 279 280 281 282
	 .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 已提交
283 284

	{
L
Len Brown 已提交
285 286 287 288 289 290 291 292 293
	 .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 已提交
294 295

	{
L
Len Brown 已提交
296 297 298 299 300 301 302 303 304
	 .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",
K
Karol Kozimor 已提交
305
	 .display_get = "\\_SB.PCI0.P0P1.VGA.GETD"},
L
Linus Torvalds 已提交
306
	{
L
Len Brown 已提交
307 308 309 310 311 312 313 314 315
	 .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",
	 .display_get = "\\SSTE"},
L
Linus Torvalds 已提交
316 317

	{
L
Len Brown 已提交
318 319 320 321 322 323 324 325 326
	 .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 已提交
327 328

	{
L
Len Brown 已提交
329 330 331 332 333 334 335 336
	 .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 已提交
337 338

	{
L
Len Brown 已提交
339 340 341 342 343 344 345
	 .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 已提交
346 347

	{
L
Len Brown 已提交
348 349
	 .name = "xxN",
	 .mt_mled = "MLED",
L
Linus Torvalds 已提交
350
/* WLED present, but not controlled by ACPI */
L
Len Brown 已提交
351 352 353 354 355 356
	 .mt_lcd_switch = xxN_PREFIX "_Q10",
	 .lcd_status = "\\BKLT",
	 .brightness_set = "SPLV",
	 .brightness_get = "GPLV",
	 .display_set = "SDSP",
	 .display_get = "\\ADVG"}
L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
};

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

/*
 * 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;

/*
 * The hotkey driver declaration
 */
static int asus_hotk_add(struct acpi_device *device);
static int asus_hotk_remove(struct acpi_device *device, int type);
static struct acpi_driver asus_hotk_driver = {
L
Len Brown 已提交
378 379 380 381 382 383 384
	.name = ACPI_HOTK_NAME,
	.class = ACPI_HOTK_CLASS,
	.ids = ACPI_HOTK_HID,
	.ops = {
		.add = asus_hotk_add,
		.remove = asus_hotk_remove,
		},
L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
};

/* 
 * 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
 *
 * returns 1 if write is successful, 0 else. 
 */
static int write_acpi_int(acpi_handle handle, const char *method, int val,
			  struct acpi_buffer *output)
{
	struct acpi_object_list params;	//list of input parameters (an int here)
	union acpi_object in_obj;	//the only param we use
	acpi_status status;

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

L
Len Brown 已提交
406
	status = acpi_evaluate_object(handle, (char *)method, &params, output);
L
Linus Torvalds 已提交
407 408 409 410 411 412 413 414 415 416 417 418
	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 已提交
419
	status = acpi_evaluate_object(handle, (char *)method, NULL, &output);
L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427 428 429 430
	*val = out_obj.integer.value;
	return (status == AE_OK) && (out_obj.type == ACPI_TYPE_INTEGER);
}

/*
 * 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
 */
static int
proc_read_info(char *page, char **start, off_t off, int count, int *eof,
L
Len Brown 已提交
431
	       void *data)
L
Linus Torvalds 已提交
432 433 434 435 436 437 438 439 440 441
{
	int len = 0;
	int temp;
	char buf[16];		//enough for all info
	/*
	 * We use the easy way, we don't care of off and count, so we don't set eof
	 * to 1
	 */

	len += sprintf(page, ACPI_HOTK_NAME " " ASUS_ACPI_VERSION "\n");
L
Len Brown 已提交
442
	len += sprintf(page + len, "Model reference    : %s\n",
L
Linus Torvalds 已提交
443 444 445 446 447 448 449 450
		       hotk->methods->name);
	/* 
	 * The SFUN method probably allows the original driver to get the list 
	 * of features supported by a given model. For now, 0x0100 or 0x0800 
	 * 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))
L
Len Brown 已提交
451 452
		len +=
		    sprintf(page + len, "SFUN value         : 0x%04x\n", temp);
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460
	/*
	 * Another value for userspace: the ASYM method returns 0x02 for
	 * battery low and 0x04 for battery critical, its readings tend to be
	 * more accurate than those provided by _BST. 
	 * Note: since not all the laptops provide this method, errors are
	 * silently ignored.
	 */
	if (read_acpi_int(hotk->handle, "ASYM", &temp))
L
Len Brown 已提交
461 462
		len +=
		    sprintf(page + len, "ASYM value         : 0x%04x\n", temp);
L
Linus Torvalds 已提交
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 492
	if (asus_info) {
		snprintf(buf, 16, "%d", asus_info->length);
		len += sprintf(page + len, "DSDT length        : %s\n", buf);
		snprintf(buf, 16, "%d", asus_info->checksum);
		len += sprintf(page + len, "DSDT checksum      : %s\n", buf);
		snprintf(buf, 16, "%d", asus_info->revision);
		len += sprintf(page + len, "DSDT revision      : %s\n", buf);
		snprintf(buf, 7, "%s", asus_info->oem_id);
		len += sprintf(page + len, "OEM id             : %s\n", buf);
		snprintf(buf, 9, "%s", asus_info->oem_table_id);
		len += sprintf(page + len, "OEM table id       : %s\n", buf);
		snprintf(buf, 16, "%x", asus_info->oem_revision);
		len += sprintf(page + len, "OEM revision       : 0x%s\n", buf);
		snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
		len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
		snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
		len += sprintf(page + len, "ASL comp revision  : 0x%s\n", buf);
	}

	return len;
}

/*
 * /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 已提交
493
static int read_led(const char *ledname, int ledmask)
L
Linus Torvalds 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506
{
	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;
}

L
Len Brown 已提交
507
static int parse_arg(const char __user * buf, unsigned long count, int *val)
L
Linus Torvalds 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
{
	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
L
Len Brown 已提交
524 525
write_led(const char __user * buffer, unsigned long count,
	  char *ledname, int ledmask, int invert)
L
Linus Torvalds 已提交
526 527 528 529 530 531 532 533 534 535 536
{
	int value;
	int led_out = 0;

	count = parse_arg(buffer, count, &value);
	if (count > 0)
		led_out = value ? 1 : 0;

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

L
Len Brown 已提交
537
	if (invert)		/* invert target value */
L
Linus Torvalds 已提交
538 539 540
		led_out = !led_out & 0x1;

	if (!write_acpi_int(hotk->handle, ledname, led_out, NULL))
L
Len Brown 已提交
541 542
		printk(KERN_WARNING "Asus ACPI: LED (%s) write failed\n",
		       ledname);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553

	return count;
}

/*
 * Proc handlers for MLED
 */
static int
proc_read_mled(char *page, char **start, off_t off, int count, int *eof,
	       void *data)
{
L
Len Brown 已提交
554 555
	return sprintf(page, "%d\n",
		       read_led(hotk->methods->mled_status, MLED_ON));
L
Linus Torvalds 已提交
556 557 558
}

static int
L
Len Brown 已提交
559
proc_write_mled(struct file *file, const char __user * buffer,
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570 571
		unsigned long count, void *data)
{
	return write_led(buffer, count, hotk->methods->mt_mled, MLED_ON, 1);
}

/*
 * Proc handlers for WLED
 */
static int
proc_read_wled(char *page, char **start, off_t off, int count, int *eof,
	       void *data)
{
L
Len Brown 已提交
572 573
	return sprintf(page, "%d\n",
		       read_led(hotk->methods->wled_status, WLED_ON));
L
Linus Torvalds 已提交
574 575 576
}

static int
L
Len Brown 已提交
577
proc_write_wled(struct file *file, const char __user * buffer,
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586 587 588 589
		unsigned long count, void *data)
{
	return write_led(buffer, count, hotk->methods->mt_wled, WLED_ON, 0);
}

/*
 * Proc handlers for TLED
 */
static int
proc_read_tled(char *page, char **start, off_t off, int count, int *eof,
	       void *data)
{
L
Len Brown 已提交
590 591
	return sprintf(page, "%d\n",
		       read_led(hotk->methods->tled_status, TLED_ON));
L
Linus Torvalds 已提交
592 593 594
}

static int
L
Len Brown 已提交
595
proc_write_tled(struct file *file, const char __user * buffer,
L
Linus Torvalds 已提交
596 597 598 599 600 601 602 603 604 605
		unsigned long count, void *data)
{
	return write_led(buffer, count, hotk->methods->mt_tled, TLED_ON, 0);
}

static int get_lcd_state(void)
{
	int lcd = 0;

	if (hotk->model != L3H) {
L
Len Brown 已提交
606
		/* We don't have to check anything if we are here */
L
Linus Torvalds 已提交
607
		if (!read_acpi_int(NULL, hotk->methods->lcd_status, &lcd))
L
Len Brown 已提交
608 609 610
			printk(KERN_WARNING
			       "Asus ACPI: Error reading LCD status\n");

L
Linus Torvalds 已提交
611 612
		if (hotk->model == L2D)
			lcd = ~lcd;
L
Len Brown 已提交
613
	} else {		/* L3H and the like have to be handled differently */
L
Linus Torvalds 已提交
614 615 616 617 618
		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 已提交
619

L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630
		input.count = 2;
		input.pointer = mt_params;
		/* Note: the following values are partly guessed up, but 
		   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 已提交
631 632 633 634

		status =
		    acpi_evaluate_object(NULL, hotk->methods->lcd_status,
					 &input, &output);
L
Linus Torvalds 已提交
635 636 637 638 639 640
		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;
	}
L
Len Brown 已提交
641

L
Linus Torvalds 已提交
642 643 644 645 646 647 648 649 650 651 652 653 654
	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 已提交
655 656
			    acpi_evaluate_object(NULL,
						 hotk->methods->mt_lcd_switch,
L
Linus Torvalds 已提交
657
						 NULL, NULL);
L
Len Brown 已提交
658 659 660 661
		} else {	/* L3H and the like have to be handled differently */
			if (!write_acpi_int
			    (hotk->handle, hotk->methods->mt_lcd_switch, 0x07,
			     NULL))
L
Linus Torvalds 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
				status = AE_ERROR;
			/* L3H's AML executes EHK (0x07) upon Fn+F7 keypress, 
			   the exact behaviour is simulated here */
		}
		if (ACPI_FAILURE(status))
			printk(KERN_WARNING "Asus ACPI: Error switching LCD\n");
	}
	return 0;

}

static int
proc_read_lcd(char *page, char **start, off_t off, int count, int *eof,
	      void *data)
{
	return sprintf(page, "%d\n", get_lcd_state());
}

static int
L
Len Brown 已提交
681
proc_write_lcd(struct file *file, const char __user * buffer,
L
Linus Torvalds 已提交
682 683 684
	       unsigned long count, void *data)
{
	int value;
L
Len Brown 已提交
685

L
Linus Torvalds 已提交
686 687 688 689 690 691 692 693 694
	count = parse_arg(buffer, count, &value);
	if (count > 0)
		set_lcd_state(value);
	return count;
}

static int read_brightness(void)
{
	int value;
L
Len Brown 已提交
695 696 697

	if (hotk->methods->brightness_get) {	/* SPLV/GPLV laptop */
		if (!read_acpi_int(hotk->handle, hotk->methods->brightness_get,
L
Linus Torvalds 已提交
698
				   &value))
L
Len Brown 已提交
699 700 701 702
			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 已提交
703
				   &value))
L
Len Brown 已提交
704 705 706
			printk(KERN_WARNING
			       "Asus ACPI: Error reading brightness\n");
	} else			/* No GPLV method */
L
Linus Torvalds 已提交
707 708 709 710 711 712 713 714 715 716 717 718
		value = hotk->brightness;
	return value;
}

/*
 * Change the brightness level
 */
static void set_brightness(int value)
{
	acpi_status status = 0;

	/* SPLV laptop */
L
Len Brown 已提交
719 720
	if (hotk->methods->brightness_set) {
		if (!write_acpi_int(hotk->handle, hotk->methods->brightness_set,
L
Linus Torvalds 已提交
721
				    value, NULL))
L
Len Brown 已提交
722 723
			printk(KERN_WARNING
			       "Asus ACPI: Error changing brightness\n");
L
Linus Torvalds 已提交
724 725 726 727 728 729
		return;
	}

	/* No SPLV method if we are here, act as appropriate */
	value -= read_brightness();
	while (value != 0) {
L
Len Brown 已提交
730 731
		status = acpi_evaluate_object(NULL, (value > 0) ?
					      hotk->methods->brightness_up :
L
Linus Torvalds 已提交
732 733 734 735
					      hotk->methods->brightness_down,
					      NULL, NULL);
		(value > 0) ? value-- : value++;
		if (ACPI_FAILURE(status))
L
Len Brown 已提交
736 737
			printk(KERN_WARNING
			       "Asus ACPI: Error changing brightness\n");
L
Linus Torvalds 已提交
738 739 740 741 742 743 744 745 746 747 748 749
	}
	return;
}

static int
proc_read_brn(char *page, char **start, off_t off, int count, int *eof,
	      void *data)
{
	return sprintf(page, "%d\n", read_brightness());
}

static int
L
Len Brown 已提交
750
proc_write_brn(struct file *file, const char __user * buffer,
L
Linus Torvalds 已提交
751 752 753 754 755 756 757
	       unsigned long count, void *data)
{
	int value;

	count = parse_arg(buffer, count, &value);
	if (count > 0) {
		value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
L
Len Brown 已提交
758
		/* 0 <= value <= 15 */
L
Linus Torvalds 已提交
759 760 761 762 763 764 765 766 767 768 769
		set_brightness(value);
	} else if (count < 0) {
		printk(KERN_WARNING "Asus ACPI: Error reading user input\n");
	}

	return count;
}

static void set_display(int value)
{
	/* no sanity check needed for now */
L
Len Brown 已提交
770
	if (!write_acpi_int(hotk->handle, hotk->methods->display_set,
L
Linus Torvalds 已提交
771 772 773 774 775 776 777 778 779 780 781
			    value, NULL))
		printk(KERN_WARNING "Asus ACPI: Error setting display\n");
	return;
}

/*
 * Now, *this* one could be more user-friendly, but so far, no-one has 
 * complained. The significance of bits is the same as in proc_write_disp()
 */
static int
proc_read_disp(char *page, char **start, off_t off, int count, int *eof,
L
Len Brown 已提交
782
	       void *data)
L
Linus Torvalds 已提交
783 784
{
	int value = 0;
L
Len Brown 已提交
785

L
Linus Torvalds 已提交
786
	if (!read_acpi_int(hotk->handle, hotk->methods->display_get, &value))
L
Len Brown 已提交
787 788 789
		printk(KERN_WARNING
		       "Asus ACPI: Error reading display status\n");
	value &= 0x07;		/* needed for some models, shouldn't hurt others */
L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798 799
	return sprintf(page, "%d\n", value);
}

/*
 * 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.
 */
static int
L
Len Brown 已提交
800 801
proc_write_disp(struct file *file, const char __user * buffer,
		unsigned long count, void *data)
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813
{
	int value;

	count = parse_arg(buffer, count, &value);
	if (count > 0)
		set_display(value);
	else if (count < 0)
		printk(KERN_WARNING "Asus ACPI: Error reading user input\n");

	return count;
}

L
Len Brown 已提交
814 815 816 817
typedef int (proc_readfunc) (char *page, char **start, off_t off, int count,
			     int *eof, void *data);
typedef int (proc_writefunc) (struct file * file, const char __user * buffer,
			      unsigned long count, void *data);
L
Linus Torvalds 已提交
818 819

static int
820
asus_proc_add(char *name, proc_writefunc * writefunc,
L
Len Brown 已提交
821 822
		     proc_readfunc * readfunc, mode_t mode,
		     struct acpi_device *device)
L
Linus Torvalds 已提交
823
{
L
Len Brown 已提交
824 825 826
	struct proc_dir_entry *proc =
	    create_proc_entry(name, mode, acpi_device_dir(device));
	if (!proc) {
L
Linus Torvalds 已提交
827 828 829 830 831 832 833 834 835 836 837 838
		printk(KERN_WARNING "  Unable to create %s fs entry\n", name);
		return -1;
	}
	proc->write_proc = writefunc;
	proc->read_proc = readfunc;
	proc->data = acpi_driver_data(device);
	proc->owner = THIS_MODULE;
	proc->uid = asus_uid;
	proc->gid = asus_gid;
	return 0;
}

839
static int asus_hotk_add_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
840 841 842
{
	struct proc_dir_entry *proc;
	mode_t mode;
L
Len Brown 已提交
843

L
Linus Torvalds 已提交
844 845 846 847 848 849
	/*
	 * 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 已提交
850
	if ((asus_uid == 0) && (asus_gid == 0)) {
L
Linus Torvalds 已提交
851 852 853
		mode = S_IFREG | S_IRUGO | S_IWUGO;
	} else {
		mode = S_IFREG | S_IRUSR | S_IRGRP | S_IWUSR | S_IWGRP;
854 855
		printk(KERN_WARNING "  asus_uid and asus_gid parameters are "
		       "deprecated, use chown and chmod instead!\n");
L
Linus Torvalds 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
	}

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

	proc = create_proc_entry(PROC_INFO, mode, acpi_device_dir(device));
	if (proc) {
		proc->read_proc = proc_read_info;
		proc->data = acpi_driver_data(device);
		proc->owner = THIS_MODULE;
		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) {
L
Len Brown 已提交
875 876
		asus_proc_add(PROC_WLED, &proc_write_wled, &proc_read_wled,
			      mode, device);
L
Linus Torvalds 已提交
877 878 879
	}

	if (hotk->methods->mt_mled) {
L
Len Brown 已提交
880 881
		asus_proc_add(PROC_MLED, &proc_write_mled, &proc_read_mled,
			      mode, device);
L
Linus Torvalds 已提交
882 883 884
	}

	if (hotk->methods->mt_tled) {
L
Len Brown 已提交
885 886
		asus_proc_add(PROC_TLED, &proc_write_tled, &proc_read_tled,
			      mode, device);
L
Linus Torvalds 已提交
887 888 889 890 891 892 893
	}

	/* 
	 * We need both read node and write method as LCD switch is also accessible
	 * from keyboard 
	 */
	if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status) {
L
Len Brown 已提交
894 895
		asus_proc_add(PROC_LCD, &proc_write_lcd, &proc_read_lcd, mode,
			      device);
L
Linus Torvalds 已提交
896
	}
L
Len Brown 已提交
897

L
Linus Torvalds 已提交
898 899
	if ((hotk->methods->brightness_up && hotk->methods->brightness_down) ||
	    (hotk->methods->brightness_get && hotk->methods->brightness_set)) {
L
Len Brown 已提交
900 901
		asus_proc_add(PROC_BRN, &proc_write_brn, &proc_read_brn, mode,
			      device);
L
Linus Torvalds 已提交
902 903 904
	}

	if (hotk->methods->display_set) {
L
Len Brown 已提交
905 906
		asus_proc_add(PROC_DISP, &proc_write_disp, &proc_read_disp,
			      mode, device);
L
Linus Torvalds 已提交
907 908 909 910 911
	}

	return 0;
}

L
Len Brown 已提交
912
static int asus_hotk_remove_fs(struct acpi_device *device)
L
Linus Torvalds 已提交
913
{
L
Len Brown 已提交
914 915
	if (acpi_device_dir(device)) {
		remove_proc_entry(PROC_INFO, acpi_device_dir(device));
L
Linus Torvalds 已提交
916
		if (hotk->methods->mt_wled)
L
Len Brown 已提交
917
			remove_proc_entry(PROC_WLED, acpi_device_dir(device));
L
Linus Torvalds 已提交
918
		if (hotk->methods->mt_mled)
L
Len Brown 已提交
919
			remove_proc_entry(PROC_MLED, acpi_device_dir(device));
L
Linus Torvalds 已提交
920
		if (hotk->methods->mt_tled)
L
Len Brown 已提交
921
			remove_proc_entry(PROC_TLED, acpi_device_dir(device));
L
Linus Torvalds 已提交
922 923
		if (hotk->methods->mt_lcd_switch && hotk->methods->lcd_status)
			remove_proc_entry(PROC_LCD, acpi_device_dir(device));
L
Len Brown 已提交
924 925 926 927
		if ((hotk->methods->brightness_up
		     && hotk->methods->brightness_down)
		    || (hotk->methods->brightness_get
			&& hotk->methods->brightness_set))
L
Linus Torvalds 已提交
928 929 930 931 932 933 934 935 936
			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;
}

static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
{
L
Len Brown 已提交
937
	/* TODO Find a better way to handle events count. */
L
Linus Torvalds 已提交
938 939 940 941 942
	if (!hotk)
		return;

	if ((event & ~((u32) BR_UP)) < 16) {
		hotk->brightness = (event & ~((u32) BR_UP));
L
Len Brown 已提交
943
	} else if ((event & ~((u32) BR_DOWN)) < 16) {
L
Linus Torvalds 已提交
944 945 946 947 948 949 950 951 952 953 954 955 956
		hotk->brightness = (event & ~((u32) BR_DOWN));
	}

	acpi_bus_generate_event(hotk->device, event,
				hotk->event_count[event % 128]++);

	return;
}

/*
 * 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
 */
957
static int asus_hotk_get_info(void)
L
Linus Torvalds 已提交
958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
{
	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
	struct acpi_buffer dsdt = { ACPI_ALLOCATE_BUFFER, NULL };
	union acpi_object *model = NULL;
	int bsts_result;
	acpi_status status;

	/*
	 * Get DSDT headers early enough to allow for differentiating between 
	 * models, but late enough to allow acpi_bus_register_driver() to fail 
	 * 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.
	 */
B
Bob Moore 已提交
973
	status = acpi_get_table(ACPI_TABLE_ID_DSDT, 1, &dsdt);
L
Linus Torvalds 已提交
974 975 976
	if (ACPI_FAILURE(status))
		printk(KERN_WARNING "  Couldn't get the DSDT table header\n");
	else
L
Len Brown 已提交
977
		asus_info = (struct acpi_table_header *)dsdt.pointer;
L
Linus Torvalds 已提交
978 979 980 981 982 983 984 985 986 987 988

	/* 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 已提交
989 990
		printk(KERN_NOTICE "  BSTS called, 0x%02x returned\n",
		       bsts_result);
L
Linus Torvalds 已提交
991

992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	/* This is unlikely with implicit return */
	if (buffer.pointer == NULL)
		return -EINVAL;

	model = (union acpi_object *) buffer.pointer;
	/*
	 * Samsung P30 has a device with a valid _HID whose INIT does not 
	 * return anything. It used to be possible to catch this exception,
	 * but the implicit return code will now happily confuse the 
	 * driver. We assume that every ACPI_TYPE_STRING is a valid model
	 * identifier but it's still possible to get completely bogus data.
	 */
	if (model->type == ACPI_TYPE_STRING) {
		printk(KERN_NOTICE "  %s model detected, ", model->string.pointer);
	} else {
L
Len Brown 已提交
1007
		if (asus_info &&	/* Samsung P30 */
L
Linus Torvalds 已提交
1008 1009
		    strncmp(asus_info->oem_table_id, "ODEM", 4) == 0) {
			hotk->model = P30;
L
Len Brown 已提交
1010 1011
			printk(KERN_NOTICE
			       "  Samsung P30 detected, supported\n");
L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018
		} else {
			hotk->model = M2E;
			printk(KERN_WARNING "  no string returned by INIT\n");
			printk(KERN_WARNING "  trying default values, supply "
			       "the developers with your DSDT\n");
		}
		hotk->methods = &model_conf[hotk->model];
1019 1020
		
		acpi_os_free(model);
L
Len Brown 已提交
1021

1022
		return AE_OK;
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	}

	hotk->model = END_MODEL;
	if (strncmp(model->string.pointer, "L3D", 3) == 0)
		hotk->model = L3D;
	else if (strncmp(model->string.pointer, "L3H", 3) == 0 ||
		 strncmp(model->string.pointer, "L2E", 3) == 0)
		hotk->model = L3H;
	else if (strncmp(model->string.pointer, "L3", 2) == 0 ||
		 strncmp(model->string.pointer, "L2B", 3) == 0)
		hotk->model = L3C;
	else if (strncmp(model->string.pointer, "L8L", 3) == 0)
		hotk->model = L8L;
	else if (strncmp(model->string.pointer, "L4R", 3) == 0)
		hotk->model = L4R;
	else if (strncmp(model->string.pointer, "M6N", 3) == 0)
		hotk->model = M6N;
	else if (strncmp(model->string.pointer, "M6R", 3) == 0)
		hotk->model = M6R;
	else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
		 strncmp(model->string.pointer, "M3N", 3) == 0 ||
		 strncmp(model->string.pointer, "M5N", 3) == 0 ||
		 strncmp(model->string.pointer, "M6N", 3) == 0 ||
		 strncmp(model->string.pointer, "S1N", 3) == 0 ||
		 strncmp(model->string.pointer, "S5N", 3) == 0 ||
L
Len Brown 已提交
1048
		 strncmp(model->string.pointer, "W1N", 3) == 0)
L
Linus Torvalds 已提交
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
		hotk->model = xxN;
	else if (strncmp(model->string.pointer, "M1", 2) == 0)
		hotk->model = M1A;
	else if (strncmp(model->string.pointer, "M2", 2) == 0 ||
		 strncmp(model->string.pointer, "L4E", 3) == 0)
		hotk->model = M2E;
	else if (strncmp(model->string.pointer, "L2", 2) == 0)
		hotk->model = L2D;
	else if (strncmp(model->string.pointer, "L8", 2) == 0)
		hotk->model = S1x;
	else if (strncmp(model->string.pointer, "D1", 2) == 0)
		hotk->model = D1x;
	else if (strncmp(model->string.pointer, "A1", 2) == 0)
		hotk->model = A1x;
	else if (strncmp(model->string.pointer, "A2", 2) == 0)
		hotk->model = A2x;
	else if (strncmp(model->string.pointer, "J1", 2) == 0)
		hotk->model = S2x;
	else if (strncmp(model->string.pointer, "L5", 2) == 0)
		hotk->model = L5x;

	if (hotk->model == END_MODEL) {
		printk("unsupported, trying default values, supply the "
		       "developers with your DSDT\n");
		hotk->model = M2E;
	} else {
		printk("supported\n");
	}

	hotk->methods = &model_conf[hotk->model];

	/* Sort of per-model blacklist */
	if (strncmp(model->string.pointer, "L2B", 3) == 0)
L
Len Brown 已提交
1082
		hotk->methods->lcd_status = NULL;
L
Linus Torvalds 已提交
1083 1084 1085 1086
	/* L2B is similar enough to L3C to use its settings, with this only 
	   exception */
	else if (strncmp(model->string.pointer, "S5N", 3) == 0 ||
		 strncmp(model->string.pointer, "M5N", 3) == 0)
L
Len Brown 已提交
1087
		hotk->methods->mt_mled = NULL;
L
Linus Torvalds 已提交
1088 1089 1090
	/* S5N and M5N have no MLED */
	else if (strncmp(model->string.pointer, "M2N", 3) == 0 ||
		 strncmp(model->string.pointer, "W1N", 3) == 0)
L
Len Brown 已提交
1091
		hotk->methods->mt_wled = "WLED";
L
Linus Torvalds 已提交
1092 1093 1094 1095
	/* M2N and W1N have a usable WLED */
	else if (asus_info) {
		if (strncmp(asus_info->oem_table_id, "L1", 2) == 0)
			hotk->methods->mled_status = NULL;
L
Len Brown 已提交
1096
		/* S1300A reports L84F, but L1400B too, account for that */
L
Linus Torvalds 已提交
1097 1098 1099 1100 1101 1102 1103
	}

	acpi_os_free(model);

	return AE_OK;
}

1104
static int asus_hotk_check(void)
L
Linus Torvalds 已提交
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
{
	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;
}

1122 1123
static int asus_hotk_found;

1124
static int asus_hotk_add(struct acpi_device *device)
L
Linus Torvalds 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
{
	acpi_status status = AE_OK;
	int result;

	if (!device)
		return -EINVAL;

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

	hotk =
L
Len Brown 已提交
1136
	    (struct asus_hotk *)kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
L
Linus Torvalds 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	if (!hotk)
		return -ENOMEM;
	memset(hotk, 0, sizeof(struct asus_hotk));

	hotk->handle = device->handle;
	strcpy(acpi_device_name(device), ACPI_HOTK_DEVICE_NAME);
	strcpy(acpi_device_class(device), ACPI_HOTK_CLASS);
	acpi_driver_data(device) = hotk;
	hotk->device = device;

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

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

	/*
	 * We install the handler, it will receive the hotk in parameter, so, we
	 * could add other data to the hotk struct
	 */
	status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
					     asus_hotk_notify, hotk);
	if (ACPI_FAILURE(status))
		printk(KERN_ERR "  Error installing notify handler\n");

	/* For laptops without GPLV: init the hotk->brightness value */
L
Len Brown 已提交
1165 1166 1167 1168 1169 1170 1171
	if ((!hotk->methods->brightness_get)
	    && (!hotk->methods->brightness_status)
	    && (hotk->methods->brightness_up
		&& hotk->methods->brightness_down)) {
		status =
		    acpi_evaluate_object(NULL, hotk->methods->brightness_down,
					 NULL, NULL);
L
Linus Torvalds 已提交
1172 1173 1174
		if (ACPI_FAILURE(status))
			printk(KERN_WARNING "  Error changing brightness\n");
		else {
L
Len Brown 已提交
1175 1176 1177 1178
			status =
			    acpi_evaluate_object(NULL,
						 hotk->methods->brightness_up,
						 NULL, NULL);
L
Linus Torvalds 已提交
1179
			if (ACPI_FAILURE(status))
L
Len Brown 已提交
1180
				printk(KERN_WARNING "  Strange, error changing"
L
Linus Torvalds 已提交
1181 1182 1183 1184
				       " brightness\n");
		}
	}

1185 1186
	asus_hotk_found = 1;

L
Linus Torvalds 已提交
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
      end:
	if (result) {
		kfree(hotk);
	}

	return result;
}

static int asus_hotk_remove(struct acpi_device *device, int type)
{
	acpi_status status = 0;

	if (!device || !acpi_driver_data(device))
		return -EINVAL;

	status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
					    asus_hotk_notify);
	if (ACPI_FAILURE(status))
		printk(KERN_ERR "Asus ACPI: Error removing notify handler\n");

	asus_hotk_remove_fs(device);

	kfree(hotk);

	return 0;
}

static int __init asus_acpi_init(void)
{
	int result;

	if (acpi_disabled)
		return -ENODEV;

L
Len Brown 已提交
1221
	if (!acpi_specific_hotkey_enabled) {
L
Luming Yu 已提交
1222
		printk(KERN_ERR "Using generic hotkey driver\n");
L
Len Brown 已提交
1223
		return -ENODEV;
L
Luming Yu 已提交
1224
	}
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231 1232
	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");
		return -ENODEV;
	}
	asus_proc_dir->owner = THIS_MODULE;

	result = acpi_bus_register_driver(&asus_hotk_driver);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	if (result < 0) {
		remove_proc_entry(PROC_ASUS, acpi_root_dir);
		return -ENODEV;
	}

	/*
	 * 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 已提交
1246 1247
		acpi_bus_unregister_driver(&asus_hotk_driver);
		remove_proc_entry(PROC_ASUS, acpi_root_dir);
1248
		return result;
L
Linus Torvalds 已提交
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
	}

	return 0;
}

static void __exit asus_acpi_exit(void)
{
	acpi_bus_unregister_driver(&asus_hotk_driver);
	remove_proc_entry(PROC_ASUS, acpi_root_dir);

	acpi_os_free(asus_info);

	return;
}

module_init(asus_acpi_init);
module_exit(asus_acpi_exit);