]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/misc/asus-laptop.c
asus-laptop: add GLED
[mirror_ubuntu-bionic-kernel.git] / drivers / misc / asus-laptop.c
CommitLineData
85091b71
CC
1/*
2 * asus-laptop.c - Asus Laptop Support
3 *
4 *
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006 Corentin Chary
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 *
23 * The development page for this driver is located at
24 * http://sourceforge.net/projects/acpi4asus/
25 *
26 * Credits:
27 * Pontus Fuchs - Helper functions, cleanup
28 * Johann Wiesner - Small compile fixes
29 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
30 * Eric Burghard - LED display support for W1N
31 * Josh Green - Light Sens support
32 * Thomas Tuttle - His first patch for led support was very helpfull
33 *
34 */
35
36#include <linux/autoconf.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/types.h>
41#include <linux/err.h>
42#include <linux/proc_fs.h>
6b7091e7
CC
43#include <linux/backlight.h>
44#include <linux/fb.h>
be18cdab 45#include <linux/leds.h>
85091b71
CC
46#include <linux/platform_device.h>
47#include <acpi/acpi_drivers.h>
48#include <acpi/acpi_bus.h>
49#include <asm/uaccess.h>
50
51#define ASUS_LAPTOP_VERSION "0.40"
52
53#define ASUS_HOTK_NAME "Asus Laptop Support"
54#define ASUS_HOTK_CLASS "hotkey"
55#define ASUS_HOTK_DEVICE_NAME "Hotkey"
56#define ASUS_HOTK_HID "ATK0100"
57#define ASUS_HOTK_FILE "asus-laptop"
58#define ASUS_HOTK_PREFIX "\\_SB.ATKD."
59
6b7091e7
CC
60/*
61 * Some events we use, same for all Asus
62 */
63#define ATKD_BR_UP 0x10
64#define ATKD_BR_DOWN 0x20
65#define ATKD_LCD_ON 0x33
66#define ATKD_LCD_OFF 0x34
67
4564de17
CC
68/*
69 * Known bits returned by \_SB.ATKD.HWRS
70 */
71#define WL_HWRS 0x80
72#define BT_HWRS 0x100
73
be18cdab
CC
74/*
75 * Flags for hotk status
4564de17 76 * WL_ON and BT_ON are also used for wireless_status()
be18cdab 77 */
4564de17
CC
78#define WL_ON 0x01 //internal Wifi
79#define BT_ON 0x02 //internal Bluetooth
be18cdab
CC
80#define MLED_ON 0x04 //mail LED
81#define TLED_ON 0x08 //touchpad LED
8def05fa
LB
82#define RLED_ON 0x10 //Record LED
83#define PLED_ON 0x20 //Phone LED
fdd8d080
CC
84#define GLED_ON 0x40 //Gaming LED
85#define LCD_ON 0x80 //LCD backlight
be18cdab 86
85091b71
CC
87#define ASUS_LOG ASUS_HOTK_FILE ": "
88#define ASUS_ERR KERN_ERR ASUS_LOG
89#define ASUS_WARNING KERN_WARNING ASUS_LOG
90#define ASUS_NOTICE KERN_NOTICE ASUS_LOG
91#define ASUS_INFO KERN_INFO ASUS_LOG
92#define ASUS_DEBUG KERN_DEBUG ASUS_LOG
93
94MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
95MODULE_DESCRIPTION(ASUS_HOTK_NAME);
96MODULE_LICENSE("GPL");
97
98#define ASUS_HANDLE(object, paths...) \
99 static acpi_handle object##_handle = NULL; \
100 static char *object##_paths[] = { paths }
101
be18cdab
CC
102/* LED */
103ASUS_HANDLE(mled_set, ASUS_HOTK_PREFIX "MLED");
104ASUS_HANDLE(tled_set, ASUS_HOTK_PREFIX "TLED");
8def05fa
LB
105ASUS_HANDLE(rled_set, ASUS_HOTK_PREFIX "RLED"); /* W1JC */
106ASUS_HANDLE(pled_set, ASUS_HOTK_PREFIX "PLED"); /* A7J */
fdd8d080 107ASUS_HANDLE(gled_set, ASUS_HOTK_PREFIX "GLED"); /* G1, G2 (probably) */
be18cdab 108
722ad971
CC
109/* LEDD */
110ASUS_HANDLE(ledd_set, ASUS_HOTK_PREFIX "SLCM");
111
4564de17
CC
112/* Bluetooth and WLAN
113 * WLED and BLED are not handled like other XLED, because in some dsdt
114 * they also control the WLAN/Bluetooth device.
115 */
116ASUS_HANDLE(wl_switch, ASUS_HOTK_PREFIX "WLED");
117ASUS_HANDLE(bt_switch, ASUS_HOTK_PREFIX "BLED");
8def05fa 118ASUS_HANDLE(wireless_status, ASUS_HOTK_PREFIX "RSTS"); /* All new models */
4564de17 119
6b7091e7
CC
120/* Brightness */
121ASUS_HANDLE(brightness_set, ASUS_HOTK_PREFIX "SPLV");
122ASUS_HANDLE(brightness_get, ASUS_HOTK_PREFIX "GPLV");
123
124/* Backlight */
8def05fa
LB
125ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
126 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
127 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
128 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
129 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
130 "\\_SB.PCI0.PX40.Q10", /* S1x */
131 "\\Q10"); /* A2x, L2D, L3D, M2E */
6b7091e7 132
78127b4a
CC
133/* Display */
134ASUS_HANDLE(display_set, ASUS_HOTK_PREFIX "SDSP");
8def05fa
LB
135ASUS_HANDLE(display_get, "\\_SB.PCI0.P0P1.VGA.GETD", /* A6B, A6K A6R A7D F3JM L4R M6R A3G
136 M6A M6V VX-1 V6J V6V W3Z */
137 "\\_SB.PCI0.P0P2.VGA.GETD", /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V
78127b4a 138 S5A M5A z33A W1Jc W2V */
8def05fa
LB
139 "\\_SB.PCI0.P0P3.VGA.GETD", /* A6V A6Q */
140 "\\_SB.PCI0.P0PA.VGA.GETD", /* A6T, A6M */
141 "\\_SB.PCI0.PCI1.VGAC.NMAP", /* L3C */
142 "\\_SB.PCI0.VGA.GETD", /* Z96F */
143 "\\ACTD", /* A2D */
144 "\\ADVG", /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
145 "\\DNXT", /* P30 */
146 "\\INFB", /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
147 "\\SSTE"); /* A3F A6F A3N A3L M6N W3N W6A */
148
149ASUS_HANDLE(ls_switch, ASUS_HOTK_PREFIX "ALSC"); /* Z71A Z71V */
150ASUS_HANDLE(ls_level, ASUS_HOTK_PREFIX "ALSL"); /* Z71A Z71V */
8b857353 151
85091b71
CC
152/*
153 * This is the main structure, we can use it to store anything interesting
154 * about the hotk device
155 */
156struct asus_hotk {
8def05fa 157 char *name; //laptop name
85091b71
CC
158 struct acpi_device *device; //the device we are in
159 acpi_handle handle; //the handle of the hotk device
160 char status; //status of the hotk, for LEDs, ...
722ad971 161 u32 ledd_status; //status of the LED display
8def05fa
LB
162 u8 light_level; //light sensor level
163 u8 light_switch; //light sensor switch value
85091b71
CC
164 u16 event_count[128]; //count for each event TODO make this better
165};
166
167/*
168 * This header is made available to allow proper configuration given model,
169 * revision number , ... this info cannot go in struct asus_hotk because it is
170 * available before the hotk
171 */
172static struct acpi_table_header *asus_info;
173
174/* The actual device the driver binds to */
175static struct asus_hotk *hotk;
176
177/*
178 * The hotkey driver declaration
179 */
180static int asus_hotk_add(struct acpi_device *device);
181static int asus_hotk_remove(struct acpi_device *device, int type);
182static struct acpi_driver asus_hotk_driver = {
183 .name = ASUS_HOTK_NAME,
184 .class = ASUS_HOTK_CLASS,
185 .ids = ASUS_HOTK_HID,
186 .ops = {
187 .add = asus_hotk_add,
188 .remove = asus_hotk_remove,
189 },
190};
191
6b7091e7
CC
192/* The backlight device /sys/class/backlight */
193static struct backlight_device *asus_backlight_device;
194
195/*
196 * The backlight class declaration
197 */
198static int read_brightness(struct backlight_device *bd);
199static int update_bl_status(struct backlight_device *bd);
599a52d1 200static struct backlight_ops asusbl_ops = {
8def05fa
LB
201 .get_brightness = read_brightness,
202 .update_status = update_bl_status,
6b7091e7
CC
203};
204
be18cdab
CC
205/* These functions actually update the LED's, and are called from a
206 * workqueue. By doing this as separate work rather than when the LED
207 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
208 * potentially bad time, such as a timer interrupt. */
209static struct workqueue_struct *led_workqueue;
210
211#define ASUS_LED(object, ledname) \
212 static void object##_led_set(struct led_classdev *led_cdev, \
213 enum led_brightness value); \
214 static void object##_led_update(struct work_struct *ignored); \
215 static int object##_led_wk; \
f110ef58 216 static DECLARE_WORK(object##_led_work, object##_led_update); \
be18cdab
CC
217 static struct led_classdev object##_led = { \
218 .name = "asus:" ledname, \
219 .brightness_set = object##_led_set, \
220 }
221
222ASUS_LED(mled, "mail");
223ASUS_LED(tled, "touchpad");
224ASUS_LED(rled, "record");
225ASUS_LED(pled, "phone");
fdd8d080 226ASUS_LED(gled, "gaming");
be18cdab 227
85091b71
CC
228/*
229 * This function evaluates an ACPI method, given an int as parameter, the
230 * method is searched within the scope of the handle, can be NULL. The output
231 * of the method is written is output, which can also be NULL
232 *
233 * returns 1 if write is successful, 0 else.
234 */
235static int write_acpi_int(acpi_handle handle, const char *method, int val,
236 struct acpi_buffer *output)
237{
238 struct acpi_object_list params; //list of input parameters (an int here)
239 union acpi_object in_obj; //the only param we use
240 acpi_status status;
241
242 params.count = 1;
243 params.pointer = &in_obj;
244 in_obj.type = ACPI_TYPE_INTEGER;
245 in_obj.integer.value = val;
246
247 status = acpi_evaluate_object(handle, (char *)method, &params, output);
248 return (status == AE_OK);
249}
250
8def05fa
LB
251static int read_wireless_status(int mask)
252{
9a816850
CC
253 ulong status;
254 acpi_status rv = AE_OK;
4564de17
CC
255
256 if (!wireless_status_handle)
257 return (hotk->status & mask) ? 1 : 0;
258
9a816850
CC
259 rv = acpi_evaluate_integer(wireless_status_handle, NULL, NULL, &status);
260 if (ACPI_FAILURE(rv))
4564de17 261 printk(ASUS_WARNING "Error reading Wireless status\n");
9a816850
CC
262 else
263 return (status & mask) ? 1 : 0;
4564de17
CC
264
265 return (hotk->status & mask) ? 1 : 0;
266}
267
be18cdab
CC
268/* Generic LED functions */
269static int read_status(int mask)
270{
4564de17
CC
271 /* There is a special method for both wireless devices */
272 if (mask == BT_ON || mask == WL_ON)
273 return read_wireless_status(mask);
274
be18cdab
CC
275 return (hotk->status & mask) ? 1 : 0;
276}
277
935ffeec 278static void write_status(acpi_handle handle, int out, int mask)
be18cdab
CC
279{
280 hotk->status = (out) ? (hotk->status | mask) : (hotk->status & ~mask);
281
935ffeec
CC
282 switch (mask) {
283 case MLED_ON:
be18cdab 284 out = !out & 0x1;
935ffeec 285 break;
fdd8d080
CC
286 case GLED_ON:
287 out = (out & 0x1) + 1;
288 break;
935ffeec
CC
289 default:
290 out &= 0x1;
291 break;
292 }
be18cdab
CC
293
294 if (handle && !write_acpi_int(handle, NULL, out, NULL))
935ffeec 295 printk(ASUS_WARNING " write failed %x\n", mask);
be18cdab
CC
296}
297
298/* /sys/class/led handlers */
935ffeec 299#define ASUS_LED_HANDLER(object, mask) \
be18cdab
CC
300 static void object##_led_set(struct led_classdev *led_cdev, \
301 enum led_brightness value) \
302 { \
303 object##_led_wk = value; \
304 queue_work(led_workqueue, &object##_led_work); \
305 } \
306 static void object##_led_update(struct work_struct *ignored) \
307 { \
308 int value = object##_led_wk; \
935ffeec 309 write_status(object##_set_handle, value, (mask)); \
be18cdab
CC
310 }
311
935ffeec
CC
312ASUS_LED_HANDLER(mled, MLED_ON);
313ASUS_LED_HANDLER(pled, PLED_ON);
314ASUS_LED_HANDLER(rled, RLED_ON);
315ASUS_LED_HANDLER(tled, TLED_ON);
fdd8d080 316ASUS_LED_HANDLER(gled, GLED_ON);
be18cdab 317
6b7091e7
CC
318static int get_lcd_state(void)
319{
320 return read_status(LCD_ON);
321}
322
323static int set_lcd_state(int value)
324{
325 int lcd = 0;
326 acpi_status status = 0;
327
328 lcd = value ? 1 : 0;
329
330 if (lcd == get_lcd_state())
331 return 0;
332
8def05fa 333 if (lcd_switch_handle) {
6b7091e7
CC
334 status = acpi_evaluate_object(lcd_switch_handle,
335 NULL, NULL, NULL);
336
337 if (ACPI_FAILURE(status))
338 printk(ASUS_WARNING "Error switching LCD\n");
339 }
340
935ffeec 341 write_status(NULL, lcd, LCD_ON);
6b7091e7
CC
342 return 0;
343}
344
345static void lcd_blank(int blank)
346{
347 struct backlight_device *bd = asus_backlight_device;
348
8def05fa 349 if (bd) {
599a52d1 350 bd->props.power = blank;
28ee086d 351 backlight_update_status(bd);
6b7091e7
CC
352 }
353}
354
355static int read_brightness(struct backlight_device *bd)
356{
9a816850
CC
357 ulong value;
358 acpi_status rv = AE_OK;
6b7091e7 359
9a816850
CC
360 rv = acpi_evaluate_integer(brightness_get_handle, NULL, NULL, &value);
361 if (ACPI_FAILURE(rv))
6b7091e7
CC
362 printk(ASUS_WARNING "Error reading brightness\n");
363
364 return value;
365}
366
367static int set_brightness(struct backlight_device *bd, int value)
368{
369 int ret = 0;
370
371 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
372 /* 0 <= value <= 15 */
373
374 if (!write_acpi_int(brightness_set_handle, NULL, value, NULL)) {
375 printk(ASUS_WARNING "Error changing brightness\n");
376 ret = -EIO;
377 }
378
379 return ret;
380}
381
382static int update_bl_status(struct backlight_device *bd)
383{
384 int rv;
599a52d1 385 int value = bd->props.brightness;
6b7091e7
CC
386
387 rv = set_brightness(bd, value);
8def05fa 388 if (rv)
6b7091e7
CC
389 return rv;
390
599a52d1 391 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
6b7091e7
CC
392 return set_lcd_state(value);
393}
394
85091b71
CC
395/*
396 * Platform device handlers
397 */
398
399/*
400 * We write our info in page, we begin at offset off and cannot write more
401 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
402 * number of bytes written in page
403 */
404static ssize_t show_infos(struct device *dev,
8def05fa 405 struct device_attribute *attr, char *page)
85091b71
CC
406{
407 int len = 0;
9a816850 408 ulong temp;
85091b71 409 char buf[16]; //enough for all info
9a816850
CC
410 acpi_status rv = AE_OK;
411
85091b71
CC
412 /*
413 * We use the easy way, we don't care of off and count, so we don't set eof
414 * to 1
415 */
416
417 len += sprintf(page, ASUS_HOTK_NAME " " ASUS_LAPTOP_VERSION "\n");
418 len += sprintf(page + len, "Model reference : %s\n", hotk->name);
419 /*
420 * The SFUN method probably allows the original driver to get the list
421 * of features supported by a given model. For now, 0x0100 or 0x0800
422 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
423 * The significance of others is yet to be found.
424 */
9a816850
CC
425 rv = acpi_evaluate_integer(hotk->handle, "SFUN", NULL, &temp);
426 if (!ACPI_FAILURE(rv))
427 len += sprintf(page + len, "SFUN value : 0x%04x\n",
428 (uint) temp);
85091b71
CC
429 /*
430 * Another value for userspace: the ASYM method returns 0x02 for
431 * battery low and 0x04 for battery critical, its readings tend to be
432 * more accurate than those provided by _BST.
433 * Note: since not all the laptops provide this method, errors are
434 * silently ignored.
435 */
9a816850
CC
436 rv = acpi_evaluate_integer(hotk->handle, "ASYM", NULL, &temp);
437 if (!ACPI_FAILURE(rv))
438 len += sprintf(page + len, "ASYM value : 0x%04x\n",
439 (uint) temp);
85091b71
CC
440 if (asus_info) {
441 snprintf(buf, 16, "%d", asus_info->length);
442 len += sprintf(page + len, "DSDT length : %s\n", buf);
443 snprintf(buf, 16, "%d", asus_info->checksum);
444 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
445 snprintf(buf, 16, "%d", asus_info->revision);
446 len += sprintf(page + len, "DSDT revision : %s\n", buf);
447 snprintf(buf, 7, "%s", asus_info->oem_id);
448 len += sprintf(page + len, "OEM id : %s\n", buf);
449 snprintf(buf, 9, "%s", asus_info->oem_table_id);
450 len += sprintf(page + len, "OEM table id : %s\n", buf);
451 snprintf(buf, 16, "%x", asus_info->oem_revision);
452 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
453 snprintf(buf, 5, "%s", asus_info->asl_compiler_id);
454 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
455 snprintf(buf, 16, "%x", asus_info->asl_compiler_revision);
456 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
457 }
458
459 return len;
460}
461
462static int parse_arg(const char *buf, unsigned long count, int *val)
463{
464 if (!count)
465 return 0;
466 if (count > 31)
467 return -EINVAL;
468 if (sscanf(buf, "%i", val) != 1)
469 return -EINVAL;
470 return count;
471}
472
4564de17 473static ssize_t store_status(const char *buf, size_t count,
935ffeec 474 acpi_handle handle, int mask)
4564de17
CC
475{
476 int rv, value;
477 int out = 0;
478
479 rv = parse_arg(buf, count, &value);
480 if (rv > 0)
481 out = value ? 1 : 0;
482
935ffeec 483 write_status(handle, out, mask);
4564de17
CC
484
485 return rv;
486}
487
722ad971
CC
488/*
489 * LEDD display
490 */
491static ssize_t show_ledd(struct device *dev,
492 struct device_attribute *attr, char *buf)
493{
494 return sprintf(buf, "0x%08x\n", hotk->ledd_status);
495}
496
497static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
498 const char *buf, size_t count)
499{
500 int rv, value;
501
502 rv = parse_arg(buf, count, &value);
503 if (rv > 0) {
504 if (!write_acpi_int(ledd_set_handle, NULL, value, NULL))
505 printk(ASUS_WARNING "LED display write failed\n");
506 else
507 hotk->ledd_status = (u32) value;
508 }
509 return rv;
510}
511
4564de17
CC
512/*
513 * WLAN
514 */
515static ssize_t show_wlan(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
518 return sprintf(buf, "%d\n", read_status(WL_ON));
519}
520
521static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
522 const char *buf, size_t count)
523{
935ffeec 524 return store_status(buf, count, wl_switch_handle, WL_ON);
4564de17
CC
525}
526
527/*
528 * Bluetooth
529 */
530static ssize_t show_bluetooth(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
533 return sprintf(buf, "%d\n", read_status(BT_ON));
534}
535
8def05fa
LB
536static ssize_t store_bluetooth(struct device *dev,
537 struct device_attribute *attr, const char *buf,
538 size_t count)
4564de17 539{
935ffeec 540 return store_status(buf, count, bt_switch_handle, BT_ON);
4564de17
CC
541}
542
78127b4a
CC
543/*
544 * Display
545 */
546static void set_display(int value)
547{
548 /* no sanity check needed for now */
549 if (!write_acpi_int(display_set_handle, NULL, value, NULL))
550 printk(ASUS_WARNING "Error setting display\n");
551 return;
552}
553
554static int read_display(void)
555{
9a816850
CC
556 ulong value = 0;
557 acpi_status rv = AE_OK;
78127b4a
CC
558
559 /* In most of the case, we know how to set the display, but sometime
560 we can't read it */
8def05fa 561 if (display_get_handle) {
9a816850
CC
562 rv = acpi_evaluate_integer(display_get_handle, NULL,
563 NULL, &value);
564 if (ACPI_FAILURE(rv))
78127b4a
CC
565 printk(ASUS_WARNING "Error reading display status\n");
566 }
567
568 value &= 0x0F; /* needed for some models, shouldn't hurt others */
569
570 return value;
571}
8def05fa 572
78127b4a
CC
573/*
574 * Now, *this* one could be more user-friendly, but so far, no-one has
575 * complained. The significance of bits is the same as in store_disp()
576 */
577static ssize_t show_disp(struct device *dev,
578 struct device_attribute *attr, char *buf)
579{
580 return sprintf(buf, "%d\n", read_display());
581}
582
583/*
584 * Experimental support for display switching. As of now: 1 should activate
585 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
586 * Any combination (bitwise) of these will suffice. I never actually tested 4
587 * displays hooked up simultaneously, so be warned. See the acpi4asus README
588 * for more info.
589 */
590static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
591 const char *buf, size_t count)
592{
593 int rv, value;
594
595 rv = parse_arg(buf, count, &value);
596 if (rv > 0)
597 set_display(value);
598 return rv;
599}
600
8b857353
CC
601/*
602 * Light Sens
603 */
604static void set_light_sens_switch(int value)
605{
606 if (!write_acpi_int(ls_switch_handle, NULL, value, NULL))
607 printk(ASUS_WARNING "Error setting light sensor switch\n");
608 hotk->light_switch = value;
609}
610
611static ssize_t show_lssw(struct device *dev,
612 struct device_attribute *attr, char *buf)
613{
614 return sprintf(buf, "%d\n", hotk->light_switch);
615}
616
617static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
618 const char *buf, size_t count)
619{
620 int rv, value;
621
622 rv = parse_arg(buf, count, &value);
623 if (rv > 0)
624 set_light_sens_switch(value ? 1 : 0);
625
626 return rv;
627}
628
629static void set_light_sens_level(int value)
630{
631 if (!write_acpi_int(ls_level_handle, NULL, value, NULL))
632 printk(ASUS_WARNING "Error setting light sensor level\n");
633 hotk->light_level = value;
634}
635
636static ssize_t show_lslvl(struct device *dev,
637 struct device_attribute *attr, char *buf)
638{
639 return sprintf(buf, "%d\n", hotk->light_level);
640}
641
642static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
643 const char *buf, size_t count)
644{
645 int rv, value;
646
647 rv = parse_arg(buf, count, &value);
648 if (rv > 0) {
649 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
650 /* 0 <= value <= 15 */
651 set_light_sens_level(value);
652 }
653
654 return rv;
655}
656
85091b71
CC
657static void asus_hotk_notify(acpi_handle handle, u32 event, void *data)
658{
659 /* TODO Find a better way to handle events count. */
660 if (!hotk)
661 return;
662
6b7091e7
CC
663 /*
664 * We need to tell the backlight device when the backlight power is
665 * switched
666 */
667 if (event == ATKD_LCD_ON) {
935ffeec 668 write_status(NULL, 1, LCD_ON);
6b7091e7 669 lcd_blank(FB_BLANK_UNBLANK);
8def05fa 670 } else if (event == ATKD_LCD_OFF) {
935ffeec 671 write_status(NULL, 0, LCD_ON);
6b7091e7
CC
672 lcd_blank(FB_BLANK_POWERDOWN);
673 }
674
85091b71
CC
675 acpi_bus_generate_event(hotk->device, event,
676 hotk->event_count[event % 128]++);
677
678 return;
679}
680
681#define ASUS_CREATE_DEVICE_ATTR(_name) \
682 struct device_attribute dev_attr_##_name = { \
683 .attr = { \
684 .name = __stringify(_name), \
685 .mode = 0, \
686 .owner = THIS_MODULE }, \
687 .show = NULL, \
688 .store = NULL, \
689 }
690
691#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
692 do { \
693 dev_attr_##_name.attr.mode = _mode; \
694 dev_attr_##_name.show = _show; \
695 dev_attr_##_name.store = _store; \
696 } while(0)
697
698static ASUS_CREATE_DEVICE_ATTR(infos);
4564de17
CC
699static ASUS_CREATE_DEVICE_ATTR(wlan);
700static ASUS_CREATE_DEVICE_ATTR(bluetooth);
78127b4a 701static ASUS_CREATE_DEVICE_ATTR(display);
722ad971 702static ASUS_CREATE_DEVICE_ATTR(ledd);
8b857353
CC
703static ASUS_CREATE_DEVICE_ATTR(ls_switch);
704static ASUS_CREATE_DEVICE_ATTR(ls_level);
85091b71
CC
705
706static struct attribute *asuspf_attributes[] = {
8def05fa
LB
707 &dev_attr_infos.attr,
708 &dev_attr_wlan.attr,
709 &dev_attr_bluetooth.attr,
710 &dev_attr_display.attr,
711 &dev_attr_ledd.attr,
712 &dev_attr_ls_switch.attr,
713 &dev_attr_ls_level.attr,
714 NULL
85091b71
CC
715};
716
717static struct attribute_group asuspf_attribute_group = {
8def05fa 718 .attrs = asuspf_attributes
85091b71
CC
719};
720
721static struct platform_driver asuspf_driver = {
8def05fa
LB
722 .driver = {
723 .name = ASUS_HOTK_FILE,
724 .owner = THIS_MODULE,
725 }
85091b71
CC
726};
727
728static struct platform_device *asuspf_device;
729
85091b71
CC
730static void asus_hotk_add_fs(void)
731{
732 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
4564de17
CC
733
734 if (wl_switch_handle)
735 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
736
737 if (bt_switch_handle)
738 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
739 show_bluetooth, store_bluetooth);
78127b4a
CC
740
741 if (display_set_handle && display_get_handle)
742 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp, store_disp);
8def05fa 743 else if (display_set_handle)
78127b4a
CC
744 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
745
722ad971
CC
746 if (ledd_set_handle)
747 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
748
8b857353
CC
749 if (ls_switch_handle && ls_level_handle) {
750 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
751 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
752 }
85091b71
CC
753}
754
8def05fa 755static int asus_handle_init(char *name, acpi_handle * handle,
85091b71
CC
756 char **paths, int num_paths)
757{
758 int i;
759 acpi_status status;
760
761 for (i = 0; i < num_paths; i++) {
762 status = acpi_get_handle(NULL, paths[i], handle);
763 if (ACPI_SUCCESS(status))
764 return 0;
765 }
766
767 *handle = NULL;
768 return -ENODEV;
769}
770
771#define ASUS_HANDLE_INIT(object) \
772 asus_handle_init(#object, &object##_handle, object##_paths, \
773 ARRAY_SIZE(object##_paths))
774
85091b71
CC
775/*
776 * This function is used to initialize the hotk with right values. In this
777 * method, we can make all the detection we want, and modify the hotk struct
778 */
779static int asus_hotk_get_info(void)
780{
781 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85091b71 782 union acpi_object *model = NULL;
9a816850 783 ulong bsts_result, hwrs_result;
85091b71
CC
784 char *string = NULL;
785 acpi_status status;
786
787 /*
788 * Get DSDT headers early enough to allow for differentiating between
789 * models, but late enough to allow acpi_bus_register_driver() to fail
790 * before doing anything ACPI-specific. Should we encounter a machine,
791 * which needs special handling (i.e. its hotkey device has a different
792 * HID), this bit will be moved. A global variable asus_info contains
793 * the DSDT header.
794 */
894d79be 795 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus_info);
85091b71
CC
796 if (ACPI_FAILURE(status))
797 printk(ASUS_WARNING "Couldn't get the DSDT table header\n");
85091b71
CC
798
799 /* We have to write 0 on init this far for all ASUS models */
800 if (!write_acpi_int(hotk->handle, "INIT", 0, &buffer)) {
801 printk(ASUS_ERR "Hotkey initialization failed\n");
802 return -ENODEV;
803 }
804
805 /* This needs to be called for some laptops to init properly */
9a816850
CC
806 status =
807 acpi_evaluate_integer(hotk->handle, "BSTS", NULL, &bsts_result);
808 if (ACPI_FAILURE(status))
85091b71
CC
809 printk(ASUS_WARNING "Error calling BSTS\n");
810 else if (bsts_result)
811 printk(ASUS_NOTICE "BSTS called, 0x%02x returned\n",
9a816850 812 (uint) bsts_result);
85091b71
CC
813
814 /*
815 * Try to match the object returned by INIT to the specific model.
816 * Handle every possible object (or the lack of thereof) the DSDT
817 * writers might throw at us. When in trouble, we pass NULL to
818 * asus_model_match() and try something completely different.
819 */
820 if (buffer.pointer) {
821 model = buffer.pointer;
822 switch (model->type) {
823 case ACPI_TYPE_STRING:
824 string = model->string.pointer;
825 break;
826 case ACPI_TYPE_BUFFER:
827 string = model->buffer.pointer;
828 break;
829 default:
830 string = "";
831 break;
832 }
833 }
834 hotk->name = kstrdup(string, GFP_KERNEL);
835 if (!hotk->name)
836 return -ENOMEM;
837
8def05fa 838 if (*string)
85091b71
CC
839 printk(ASUS_NOTICE " %s model detected\n", string);
840
be18cdab
CC
841 ASUS_HANDLE_INIT(mled_set);
842 ASUS_HANDLE_INIT(tled_set);
843 ASUS_HANDLE_INIT(rled_set);
844 ASUS_HANDLE_INIT(pled_set);
fdd8d080 845 ASUS_HANDLE_INIT(gled_set);
be18cdab 846
722ad971
CC
847 ASUS_HANDLE_INIT(ledd_set);
848
4564de17
CC
849 /*
850 * The HWRS method return informations about the hardware.
851 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
852 * The significance of others is yet to be found.
853 * If we don't find the method, we assume the device are present.
854 */
9a816850
CC
855 status =
856 acpi_evaluate_integer(hotk->handle, "HRWS", NULL, &hwrs_result);
857 if (ACPI_FAILURE(status))
4564de17
CC
858 hwrs_result = WL_HWRS | BT_HWRS;
859
8def05fa 860 if (hwrs_result & WL_HWRS)
4564de17 861 ASUS_HANDLE_INIT(wl_switch);
8def05fa 862 if (hwrs_result & BT_HWRS)
4564de17
CC
863 ASUS_HANDLE_INIT(bt_switch);
864
865 ASUS_HANDLE_INIT(wireless_status);
866
6b7091e7
CC
867 ASUS_HANDLE_INIT(brightness_set);
868 ASUS_HANDLE_INIT(brightness_get);
869
870 ASUS_HANDLE_INIT(lcd_switch);
871
78127b4a
CC
872 ASUS_HANDLE_INIT(display_set);
873 ASUS_HANDLE_INIT(display_get);
874
8b857353
CC
875 /* There is a lot of models with "ALSL", but a few get
876 a real light sens, so we need to check it. */
8def05fa 877 if (ASUS_HANDLE_INIT(ls_switch))
8b857353
CC
878 ASUS_HANDLE_INIT(ls_level);
879
85091b71
CC
880 kfree(model);
881
882 return AE_OK;
883}
884
885static int asus_hotk_check(void)
886{
887 int result = 0;
888
889 result = acpi_bus_get_status(hotk->device);
890 if (result)
891 return result;
892
893 if (hotk->device->status.present) {
894 result = asus_hotk_get_info();
895 } else {
896 printk(ASUS_ERR "Hotkey device not present, aborting\n");
897 return -EINVAL;
898 }
899
900 return result;
901}
902
903static int asus_hotk_found;
904
905static int asus_hotk_add(struct acpi_device *device)
906{
907 acpi_status status = AE_OK;
908 int result;
909
910 if (!device)
911 return -EINVAL;
912
913 printk(ASUS_NOTICE "Asus Laptop Support version %s\n",
914 ASUS_LAPTOP_VERSION);
915
916 hotk = kmalloc(sizeof(struct asus_hotk), GFP_KERNEL);
917 if (!hotk)
918 return -ENOMEM;
919 memset(hotk, 0, sizeof(struct asus_hotk));
920
921 hotk->handle = device->handle;
922 strcpy(acpi_device_name(device), ASUS_HOTK_DEVICE_NAME);
923 strcpy(acpi_device_class(device), ASUS_HOTK_CLASS);
924 acpi_driver_data(device) = hotk;
925 hotk->device = device;
926
927 result = asus_hotk_check();
928 if (result)
929 goto end;
930
931 asus_hotk_add_fs();
932
933 /*
934 * We install the handler, it will receive the hotk in parameter, so, we
935 * could add other data to the hotk struct
936 */
937 status = acpi_install_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
938 asus_hotk_notify, hotk);
939 if (ACPI_FAILURE(status))
940 printk(ASUS_ERR "Error installing notify handler\n");
941
942 asus_hotk_found = 1;
943
6b7091e7 944 /* WLED and BLED are on by default */
935ffeec
CC
945 write_status(bt_switch_handle, 1, BT_ON);
946 write_status(wl_switch_handle, 1, WL_ON);
947
948 /* If the h/w switch is off, we need to check the real status */
949 write_status(NULL, read_status(BT_ON), BT_ON);
950 write_status(NULL, read_status(WL_ON), WL_ON);
4564de17 951
6b7091e7 952 /* LCD Backlight is on by default */
935ffeec 953 write_status(NULL, 1, LCD_ON);
6b7091e7 954
722ad971
CC
955 /* LED display is off by default */
956 hotk->ledd_status = 0xFFF;
957
8def05fa
LB
958 /* Set initial values of light sensor and level */
959 hotk->light_switch = 1; /* Default to light sensor disabled */
960 hotk->light_level = 0; /* level 5 for sensor sensitivity */
8b857353 961
8def05fa
LB
962 if (ls_switch_handle)
963 set_light_sens_switch(hotk->light_switch);
8b857353 964
8def05fa
LB
965 if (ls_level_handle)
966 set_light_sens_level(hotk->light_level);
8b857353 967
85091b71
CC
968 end:
969 if (result) {
970 kfree(hotk->name);
971 kfree(hotk);
972 }
973
974 return result;
975}
976
977static int asus_hotk_remove(struct acpi_device *device, int type)
978{
979 acpi_status status = 0;
980
981 if (!device || !acpi_driver_data(device))
982 return -EINVAL;
983
984 status = acpi_remove_notify_handler(hotk->handle, ACPI_SYSTEM_NOTIFY,
985 asus_hotk_notify);
986 if (ACPI_FAILURE(status))
987 printk(ASUS_ERR "Error removing notify handler\n");
988
989 kfree(hotk->name);
990 kfree(hotk);
991
992 return 0;
993}
994
6b7091e7
CC
995static void asus_backlight_exit(void)
996{
8def05fa 997 if (asus_backlight_device)
6b7091e7
CC
998 backlight_device_unregister(asus_backlight_device);
999}
1000
be18cdab
CC
1001#define ASUS_LED_UNREGISTER(object) \
1002 if(object##_led.class_dev \
1003 && !IS_ERR(object##_led.class_dev)) \
1004 led_classdev_unregister(&object##_led)
1005
1006static void asus_led_exit(void)
1007{
1008 ASUS_LED_UNREGISTER(mled);
1009 ASUS_LED_UNREGISTER(tled);
1010 ASUS_LED_UNREGISTER(pled);
1011 ASUS_LED_UNREGISTER(rled);
fdd8d080 1012 ASUS_LED_UNREGISTER(gled);
be18cdab
CC
1013
1014 destroy_workqueue(led_workqueue);
1015}
1016
85091b71
CC
1017static void __exit asus_laptop_exit(void)
1018{
6b7091e7 1019 asus_backlight_exit();
be18cdab
CC
1020 asus_led_exit();
1021
85091b71 1022 acpi_bus_unregister_driver(&asus_hotk_driver);
8def05fa
LB
1023 sysfs_remove_group(&asuspf_device->dev.kobj, &asuspf_attribute_group);
1024 platform_device_unregister(asuspf_device);
1025 platform_driver_unregister(&asuspf_driver);
85091b71
CC
1026}
1027
8def05fa 1028static int asus_backlight_init(struct device *dev)
6b7091e7
CC
1029{
1030 struct backlight_device *bd;
1031
8def05fa
LB
1032 if (brightness_set_handle && lcd_switch_handle) {
1033 bd = backlight_device_register(ASUS_HOTK_FILE, dev,
599a52d1 1034 NULL, &asusbl_ops);
8def05fa 1035 if (IS_ERR(bd)) {
6b7091e7
CC
1036 printk(ASUS_ERR
1037 "Could not register asus backlight device\n");
1038 asus_backlight_device = NULL;
1039 return PTR_ERR(bd);
1040 }
1041
1042 asus_backlight_device = bd;
1043
599a52d1
RP
1044 bd->props.max_brightness = 15;
1045 bd->props.brightness = read_brightness(NULL);
1046 bd->props.power = FB_BLANK_UNBLANK;
28ee086d 1047 backlight_update_status(bd);
6b7091e7
CC
1048 }
1049 return 0;
1050}
1051
be18cdab 1052static int asus_led_register(acpi_handle handle,
8def05fa 1053 struct led_classdev *ldev, struct device *dev)
be18cdab 1054{
8def05fa 1055 if (!handle)
be18cdab
CC
1056 return 0;
1057
1058 return led_classdev_register(dev, ldev);
1059}
8def05fa 1060
be18cdab
CC
1061#define ASUS_LED_REGISTER(object, device) \
1062 asus_led_register(object##_set_handle, &object##_led, device)
1063
8def05fa 1064static int asus_led_init(struct device *dev)
be18cdab
CC
1065{
1066 int rv;
1067
1068 rv = ASUS_LED_REGISTER(mled, dev);
8def05fa 1069 if (rv)
be18cdab
CC
1070 return rv;
1071
1072 rv = ASUS_LED_REGISTER(tled, dev);
8def05fa 1073 if (rv)
be18cdab
CC
1074 return rv;
1075
1076 rv = ASUS_LED_REGISTER(rled, dev);
8def05fa 1077 if (rv)
be18cdab
CC
1078 return rv;
1079
1080 rv = ASUS_LED_REGISTER(pled, dev);
8def05fa 1081 if (rv)
be18cdab
CC
1082 return rv;
1083
fdd8d080
CC
1084 rv = ASUS_LED_REGISTER(gled, dev);
1085 if (rv)
1086 return rv;
1087
be18cdab 1088 led_workqueue = create_singlethread_workqueue("led_workqueue");
8def05fa 1089 if (!led_workqueue)
be18cdab
CC
1090 return -ENOMEM;
1091
1092 return 0;
1093}
1094
85091b71
CC
1095static int __init asus_laptop_init(void)
1096{
be18cdab 1097 struct device *dev;
85091b71
CC
1098 int result;
1099
1100 if (acpi_disabled)
1101 return -ENODEV;
1102
85091b71
CC
1103 result = acpi_bus_register_driver(&asus_hotk_driver);
1104 if (result < 0)
1105 return result;
1106
1107 /*
1108 * This is a bit of a kludge. We only want this module loaded
1109 * for ASUS systems, but there's currently no way to probe the
1110 * ACPI namespace for ASUS HIDs. So we just return failure if
1111 * we didn't find one, which will cause the module to be
1112 * unloaded.
1113 */
1114 if (!asus_hotk_found) {
1115 acpi_bus_unregister_driver(&asus_hotk_driver);
1116 return -ENODEV;
1117 }
1118
be18cdab
CC
1119 dev = acpi_get_physical_device(hotk->device->handle);
1120
6b7091e7 1121 result = asus_backlight_init(dev);
8def05fa 1122 if (result)
6b7091e7
CC
1123 goto fail_backlight;
1124
be18cdab 1125 result = asus_led_init(dev);
8def05fa 1126 if (result)
be18cdab
CC
1127 goto fail_led;
1128
8def05fa 1129 /* Register platform stuff */
85091b71 1130 result = platform_driver_register(&asuspf_driver);
8def05fa
LB
1131 if (result)
1132 goto fail_platform_driver;
85091b71 1133
8def05fa
LB
1134 asuspf_device = platform_device_alloc(ASUS_HOTK_FILE, -1);
1135 if (!asuspf_device) {
1136 result = -ENOMEM;
1137 goto fail_platform_device1;
1138 }
85091b71 1139
8def05fa
LB
1140 result = platform_device_add(asuspf_device);
1141 if (result)
1142 goto fail_platform_device2;
85091b71 1143
8def05fa 1144 result = sysfs_create_group(&asuspf_device->dev.kobj,
85091b71 1145 &asuspf_attribute_group);
8def05fa
LB
1146 if (result)
1147 goto fail_sysfs;
85091b71 1148
8def05fa 1149 return 0;
85091b71 1150
8def05fa
LB
1151 fail_sysfs:
1152 platform_device_del(asuspf_device);
85091b71 1153
8def05fa 1154 fail_platform_device2:
85091b71
CC
1155 platform_device_put(asuspf_device);
1156
8def05fa
LB
1157 fail_platform_device1:
1158 platform_driver_unregister(&asuspf_driver);
85091b71 1159
8def05fa 1160 fail_platform_driver:
be18cdab
CC
1161 asus_led_exit();
1162
8def05fa 1163 fail_led:
6b7091e7
CC
1164 asus_backlight_exit();
1165
8def05fa 1166 fail_backlight:
85091b71
CC
1167
1168 return result;
1169}
1170
1171module_init(asus_laptop_init);
1172module_exit(asus_laptop_exit);