asus-laptop: add bluetooth keys found on M9V
[deliverable/linux.git] / drivers / platform / x86 / 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
8ec555c2 6 * Copyright (C) 2006-2007 Corentin Chary
85091b71
CC
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
e539c2f6 33 * Sam Lin - GPS support
85091b71
CC
34 */
35
2fcc23da
CC
36#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
85091b71
CC
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/err.h>
43#include <linux/proc_fs.h>
6b7091e7
CC
44#include <linux/backlight.h>
45#include <linux/fb.h>
be18cdab 46#include <linux/leds.h>
85091b71
CC
47#include <linux/platform_device.h>
48#include <acpi/acpi_drivers.h>
49#include <acpi/acpi_bus.h>
50#include <asm/uaccess.h>
034ce90a 51#include <linux/input.h>
66a71dd1 52#include <linux/input/sparse-keymap.h>
85091b71 53
91687cc8 54#define ASUS_LAPTOP_VERSION "0.42"
85091b71 55
50a90c4d
CC
56#define ASUS_LAPTOP_NAME "Asus Laptop Support"
57#define ASUS_LAPTOP_CLASS "hotkey"
58#define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
59#define ASUS_LAPTOP_FILE KBUILD_MODNAME
60#define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
85091b71 61
85091b71 62MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
50a90c4d 63MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
85091b71
CC
64MODULE_LICENSE("GPL");
65
be966660
CC
66/*
67 * WAPF defines the behavior of the Fn+Fx wlan key
185e5af9
CC
68 * The significance of values is yet to be found, but
69 * most of the time:
70 * 0x0 will do nothing
71 * 0x1 will allow to control the device with Fn+Fx key.
72 * 0x4 will send an ACPI event (0x88) while pressing the Fn+Fx key
73 * 0x5 like 0x1 or 0x4
74 * So, if something doesn't work as you want, just try other values =)
75 */
76static uint wapf = 1;
77module_param(wapf, uint, 0644);
78MODULE_PARM_DESC(wapf, "WAPF value");
79
d0930a2d 80static uint wlan_status = 1;
0e875f49
CC
81static uint bluetooth_status = 1;
82
d0930a2d
CC
83module_param(wlan_status, uint, 0644);
84MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
0e875f49
CC
85 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
86 "default is 1");
87
88module_param(bluetooth_status, uint, 0644);
89MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
90 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
91 "default is 1");
92
be4ee82d
CC
93/*
94 * Some events we use, same for all Asus
95 */
96#define ATKD_BR_UP 0x10
97#define ATKD_BR_DOWN 0x20
98#define ATKD_LCD_ON 0x33
99#define ATKD_LCD_OFF 0x34
100
101/*
102 * Known bits returned by \_SB.ATKD.HWRS
103 */
104#define WL_HWRS 0x80
105#define BT_HWRS 0x100
106
107/*
108 * Flags for hotk status
109 * WL_ON and BT_ON are also used for wireless_status()
110 */
17e78f62
CC
111#define WL_RSTS 0x01 /* internal Wifi */
112#define BT_RSTS 0x02 /* internal Bluetooth */
be4ee82d 113
85091b71
CC
114#define ASUS_HANDLE(object, paths...) \
115 static acpi_handle object##_handle = NULL; \
116 static char *object##_paths[] = { paths }
117
be18cdab 118/* LED */
d99b577c
CC
119#define METHOD_MLED "MLED"
120#define METHOD_TLED "TLED"
121#define METHOD_RLED "RLED" /* W1JC */
122#define METHOD_PLED "PLED" /* A7J */
123#define METHOD_GLED "GLED" /* G1, G2 (probably) */
be18cdab 124
722ad971 125/* LEDD */
d99b577c 126#define METHOD_LEDD "SLCM"
722ad971 127
be966660
CC
128/*
129 * Bluetooth and WLAN
4564de17
CC
130 * WLED and BLED are not handled like other XLED, because in some dsdt
131 * they also control the WLAN/Bluetooth device.
132 */
d99b577c
CC
133#define METHOD_WLAN "WLED"
134#define METHOD_BLUETOOTH "BLED"
135#define METHOD_WL_STATUS "RSTS"
4564de17 136
6b7091e7 137/* Brightness */
d99b577c
CC
138#define METHOD_BRIGHTNESS_SET "SPLV"
139#define METHOD_BRIGHTNESS_GET "GPLV"
6b7091e7
CC
140
141/* Backlight */
8def05fa
LB
142ASUS_HANDLE(lcd_switch, "\\_SB.PCI0.SBRG.EC0._Q10", /* All new models */
143 "\\_SB.PCI0.ISA.EC0._Q10", /* A1x */
144 "\\_SB.PCI0.PX40.ECD0._Q10", /* L3C */
145 "\\_SB.PCI0.PX40.EC0.Q10", /* M1A */
146 "\\_SB.PCI0.LPCB.EC0._Q10", /* P30 */
4d0b856e 147 "\\_SB.PCI0.LPCB.EC0._Q0E", /* P30/P35 */
8def05fa
LB
148 "\\_SB.PCI0.PX40.Q10", /* S1x */
149 "\\Q10"); /* A2x, L2D, L3D, M2E */
6b7091e7 150
78127b4a 151/* Display */
d99b577c 152#define METHOD_SWITCH_DISPLAY "SDSP"
be966660
CC
153ASUS_HANDLE(display_get,
154 /* A6B, A6K A6R A7D F3JM L4R M6R A3G M6A M6V VX-1 V6J V6V W3Z */
155 "\\_SB.PCI0.P0P1.VGA.GETD",
156 /* A3E A4K, A4D A4L A6J A7J A8J Z71V M9V S5A M5A z33A W1Jc W2V G1 */
157 "\\_SB.PCI0.P0P2.VGA.GETD",
158 /* A6V A6Q */
159 "\\_SB.PCI0.P0P3.VGA.GETD",
160 /* A6T, A6M */
161 "\\_SB.PCI0.P0PA.VGA.GETD",
162 /* L3C */
163 "\\_SB.PCI0.PCI1.VGAC.NMAP",
164 /* Z96F */
165 "\\_SB.PCI0.VGA.GETD",
166 /* A2D */
167 "\\ACTD",
168 /* A4G Z71A W1N W5A W5F M2N M3N M5N M6N S1N S5N */
169 "\\ADVG",
170 /* P30 */
171 "\\DNXT",
172 /* A2H D1 L2D L3D L3H L2E L5D L5C M1A M2E L4L W3V */
173 "\\INFB",
174 /* A3F A6F A3N A3L M6N W3N W6A */
175 "\\SSTE");
176
d99b577c
CC
177#define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
178#define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
8b857353 179
e539c2f6
CC
180/* GPS */
181/* R2H use different handle for GPS on/off */
d99b577c
CC
182#define METHOD_GPS_ON "SDON"
183#define METHOD_GPS_OFF "SDOF"
184#define METHOD_GPS_STATUS "GPST"
e539c2f6 185
b7d3fbc2 186/* Keyboard light */
d99b577c
CC
187#define METHOD_KBD_LIGHT_SET "SLKB"
188#define METHOD_KBD_LIGHT_GET "GLKB"
b7d3fbc2 189
9129d14d
CC
190/*
191 * Define a specific led structure to keep the main structure clean
192 */
193#define ASUS_DEFINE_LED(object) \
194 int object##_wk; \
195 struct work_struct object##_work; \
196 struct led_classdev object;
197
198
199#define led_to_asus(led_cdev, led) \
200 container_of(container_of(led_cdev, struct asus_laptop_leds, \
201 led), \
202 struct asus_laptop, leds)
203#define work_to_asus(work, led) \
204 container_of(container_of(work, struct asus_laptop_leds, \
205 led##_work), \
206 struct asus_laptop, leds)
207
208struct asus_laptop_leds {
209 ASUS_DEFINE_LED(mled)
210 ASUS_DEFINE_LED(tled)
211 ASUS_DEFINE_LED(rled)
212 ASUS_DEFINE_LED(pled)
213 ASUS_DEFINE_LED(gled)
214 ASUS_DEFINE_LED(kled)
215 struct workqueue_struct *workqueue;
216};
217
85091b71
CC
218/*
219 * This is the main structure, we can use it to store anything interesting
220 * about the hotk device
221 */
50a90c4d 222struct asus_laptop {
be966660 223 char *name; /* laptop name */
600ad520 224
7c247645 225 struct acpi_table_header *dsdt_info;
600ad520 226 struct platform_device *platform_device;
7c247645
CC
227 struct acpi_device *device; /* the device we are in */
228 struct backlight_device *backlight_device;
9129d14d 229
7c247645 230 struct input_dev *inputdev;
9129d14d
CC
231 struct key_entry *keymap;
232
233 struct asus_laptop_leds leds;
7c247645 234
aa9df930
CC
235 int wireless_status;
236 bool have_rsts;
3e68ae7c 237 int lcd_state;
aa9df930 238
be966660 239 acpi_handle handle; /* the handle of the hotk device */
be966660
CC
240 u32 ledd_status; /* status of the LED display */
241 u8 light_level; /* light sensor level */
242 u8 light_switch; /* light sensor switch value */
243 u16 event_count[128]; /* count for each event TODO make this better */
034ce90a 244 u16 *keycode_map;
85091b71
CC
245};
246
9129d14d 247static const struct key_entry asus_keymap[] = {
66a71dd1
CC
248 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
249 {KE_KEY, 0x05, { KEY_WLAN } },
250 {KE_KEY, 0x08, { KEY_F13 } },
251 {KE_KEY, 0x17, { KEY_ZOOM } },
252 {KE_KEY, 0x1f, { KEY_BATTERY } },
253 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
254 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
255 {KE_KEY, 0x32, { KEY_MUTE } },
256 {KE_KEY, 0x33, { KEY_SWITCHVIDEOMODE } },
257 {KE_KEY, 0x34, { KEY_SWITCHVIDEOMODE } },
258 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
259 {KE_KEY, 0x41, { KEY_NEXTSONG } },
260 {KE_KEY, 0x43, { KEY_STOPCD } },
261 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
262 {KE_KEY, 0x4c, { KEY_MEDIA } },
263 {KE_KEY, 0x50, { KEY_EMAIL } },
264 {KE_KEY, 0x51, { KEY_WWW } },
265 {KE_KEY, 0x55, { KEY_CALC } },
266 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
267 {KE_KEY, 0x5D, { KEY_WLAN } },
268 {KE_KEY, 0x5E, { KEY_WLAN } },
269 {KE_KEY, 0x5F, { KEY_WLAN } },
270 {KE_KEY, 0x60, { KEY_SWITCHVIDEOMODE } },
271 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } },
272 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } },
273 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } },
274 {KE_KEY, 0x6B, { KEY_F13 } }, /* Lock Touchpad */
7f607d71
CC
275 {KE_KEY, 0x7E, { KEY_BLUETOOTH } },
276 {KE_KEY, 0x7D, { KEY_BLUETOOTH } },
66a71dd1
CC
277 {KE_KEY, 0x82, { KEY_CAMERA } },
278 {KE_KEY, 0x88, { KEY_WLAN } },
279 {KE_KEY, 0x8A, { KEY_PROG1 } },
280 {KE_KEY, 0x95, { KEY_MEDIA } },
281 {KE_KEY, 0x99, { KEY_PHONE } },
282 {KE_KEY, 0xc4, { KEY_KBDILLUMUP } },
283 {KE_KEY, 0xc5, { KEY_KBDILLUMDOWN } },
034ce90a
CC
284 {KE_END, 0},
285};
286
66a71dd1 287
85091b71
CC
288/*
289 * This function evaluates an ACPI method, given an int as parameter, the
290 * method is searched within the scope of the handle, can be NULL. The output
291 * of the method is written is output, which can also be NULL
292 *
f8d1c94b 293 * returns 0 if write is successful, -1 else.
85091b71 294 */
d8c67323
CC
295static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
296 struct acpi_buffer *output)
85091b71 297{
be966660
CC
298 struct acpi_object_list params; /* list of input parameters (an int) */
299 union acpi_object in_obj; /* the only param we use */
85091b71
CC
300 acpi_status status;
301
f8d1c94b
CC
302 if (!handle)
303 return 0;
304
85091b71
CC
305 params.count = 1;
306 params.pointer = &in_obj;
307 in_obj.type = ACPI_TYPE_INTEGER;
308 in_obj.integer.value = val;
309
310 status = acpi_evaluate_object(handle, (char *)method, &params, output);
f8d1c94b
CC
311 if (status == AE_OK)
312 return 0;
313 else
314 return -1;
85091b71
CC
315}
316
d8c67323
CC
317static int write_acpi_int(acpi_handle handle, const char *method, int val)
318{
319 return write_acpi_int_ret(handle, method, val, NULL);
320}
321
d99b577c
CC
322static int acpi_check_handle(acpi_handle handle, const char *method,
323 acpi_handle *ret)
324{
325 acpi_status status;
326
327 if (method == NULL)
328 return -ENODEV;
329
330 if (ret)
331 status = acpi_get_handle(handle, (char *)method,
332 ret);
333 else {
334 acpi_handle dummy;
335
336 status = acpi_get_handle(handle, (char *)method,
337 &dummy);
338 }
339
340 if (status != AE_OK) {
341 if (ret)
342 pr_warning("Error finding %s\n", method);
343 return -ENODEV;
344 }
345 return 0;
346}
347
17e78f62 348/* Generic LED function */
e5593bf1 349static int asus_led_set(struct asus_laptop *asus, char *method,
17e78f62 350 int value)
be18cdab 351{
d99b577c 352 if (!strcmp(method, METHOD_MLED))
17e78f62 353 value = !value;
d99b577c 354 else if (!strcmp(method, METHOD_GLED))
17e78f62
CC
355 value = !value + 1;
356 else
357 value = !!value;
be18cdab 358
e5593bf1 359 return write_acpi_int(asus->handle, method, value);
be18cdab
CC
360}
361
be4ee82d
CC
362/*
363 * LEDs
364 */
365#define ASUS_LED(object, ledname, max) \
366 static void object##_led_set(struct led_classdev *led_cdev, \
367 enum led_brightness value); \
368 static enum led_brightness object##_led_get( \
369 struct led_classdev *led_cdev); \
370 static void object##_led_update(struct work_struct *ignored); \
371 static struct led_classdev object##_led = { \
372 .name = "asus::" ledname, \
373 .brightness_set = object##_led_set, \
374 .brightness_get = object##_led_get, \
375 .max_brightness = max \
376 }
377
378ASUS_LED(mled, "mail", 1);
379ASUS_LED(tled, "touchpad", 1);
380ASUS_LED(rled, "record", 1);
381ASUS_LED(pled, "phone", 1);
382ASUS_LED(gled, "gaming", 1);
383ASUS_LED(kled, "kbd_backlight", 3);
384
be18cdab 385/* /sys/class/led handlers */
d99b577c 386#define ASUS_LED_HANDLER(object, method) \
be18cdab
CC
387 static void object##_led_set(struct led_classdev *led_cdev, \
388 enum led_brightness value) \
389 { \
9129d14d
CC
390 struct asus_laptop *asus = \
391 led_to_asus(led_cdev, object); \
392 \
393 asus->leds.object##_wk = (value > 0) ? 1 : 0; \
394 queue_work(asus->leds.workqueue, \
395 &asus->leds.object##_work); \
be18cdab 396 } \
9129d14d 397 static void object##_led_update(struct work_struct *work) \
be18cdab 398 { \
9129d14d
CC
399 struct asus_laptop *asus = work_to_asus(work, object); \
400 \
401 int value = asus->leds.object##_wk; \
d99b577c 402 asus_led_set(asus, method, value); \
abfa57e1
CC
403 } \
404 static enum led_brightness object##_led_get( \
405 struct led_classdev *led_cdev) \
406 { \
407 return led_cdev->brightness; \
be18cdab
CC
408 }
409
d99b577c
CC
410ASUS_LED_HANDLER(mled, METHOD_MLED);
411ASUS_LED_HANDLER(pled, METHOD_PLED);
412ASUS_LED_HANDLER(rled, METHOD_RLED);
413ASUS_LED_HANDLER(tled, METHOD_TLED);
414ASUS_LED_HANDLER(gled, METHOD_GLED);
be18cdab 415
b7d3fbc2 416/*
be4ee82d 417 * Keyboard backlight (also a LED)
b7d3fbc2 418 */
d99b577c 419static int asus_kled_lvl(struct asus_laptop *asus)
b7d3fbc2
CC
420{
421 unsigned long long kblv;
422 struct acpi_object_list params;
423 union acpi_object in_obj;
424 acpi_status rv;
425
426 params.count = 1;
427 params.pointer = &in_obj;
428 in_obj.type = ACPI_TYPE_INTEGER;
429 in_obj.integer.value = 2;
430
d99b577c
CC
431 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
432 &params, &kblv);
b7d3fbc2
CC
433 if (ACPI_FAILURE(rv)) {
434 pr_warning("Error reading kled level\n");
4d441513 435 return -ENODEV;
b7d3fbc2
CC
436 }
437 return kblv;
438}
439
4d441513 440static int asus_kled_set(struct asus_laptop *asus, int kblv)
b7d3fbc2
CC
441{
442 if (kblv > 0)
443 kblv = (1 << 7) | (kblv & 0x7F);
444 else
445 kblv = 0;
446
d99b577c 447 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
b7d3fbc2
CC
448 pr_warning("Keyboard LED display write failed\n");
449 return -EINVAL;
450 }
451 return 0;
452}
453
454static void kled_led_set(struct led_classdev *led_cdev,
455 enum led_brightness value)
456{
9129d14d
CC
457 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
458
459 asus->leds.kled_wk = value;
460 queue_work(asus->leds.workqueue, &asus->leds.kled_work);
b7d3fbc2
CC
461}
462
9129d14d 463static void kled_led_update(struct work_struct *work)
b7d3fbc2 464{
9129d14d
CC
465 struct asus_laptop *asus = work_to_asus(work, kled);
466
4d441513 467 asus_kled_set(asus, asus->leds.kled_wk);
b7d3fbc2
CC
468}
469
470static enum led_brightness kled_led_get(struct led_classdev *led_cdev)
471{
d99b577c
CC
472 struct asus_laptop *asus = led_to_asus(led_cdev, kled);
473
474 return asus_kled_lvl(asus);
b7d3fbc2
CC
475}
476
be4ee82d
CC
477#define ASUS_LED_UNREGISTER(object) \
478 if (object##_led.dev) \
479 led_classdev_unregister(&object##_led)
480
481static void asus_led_exit(struct asus_laptop *asus)
482{
483 ASUS_LED_UNREGISTER(mled);
484 ASUS_LED_UNREGISTER(tled);
485 ASUS_LED_UNREGISTER(pled);
486 ASUS_LED_UNREGISTER(rled);
487 ASUS_LED_UNREGISTER(gled);
488 ASUS_LED_UNREGISTER(kled);
489 if (asus->leds.workqueue) {
490 destroy_workqueue(asus->leds.workqueue);
491 asus->leds.workqueue = NULL;
492 }
493}
494
495/* Ugly macro, need to fix that later */
d99b577c 496#define ASUS_LED_REGISTER(asus, object, _name, max, method) \
be4ee82d
CC
497 do { \
498 struct led_classdev *ldev = &asus->leds.object; \
d99b577c
CC
499 \
500 if (method && acpi_check_handle(asus->handle, method, NULL)) \
be4ee82d
CC
501 break ; \
502 \
503 INIT_WORK(&asus->leds.object##_work, object##_led_update); \
504 ldev->name = "asus::" _name; \
505 ldev->brightness_set = object##_led_set; \
506 ldev->max_brightness = max; \
507 rv = led_classdev_register(&asus->platform_device->dev, ldev); \
508 if (rv) \
509 goto error; \
510 } while (0)
511
512static int asus_led_init(struct asus_laptop *asus)
513{
514 int rv;
515
516 /*
517 * Functions that actually update the LED's are called from a
518 * workqueue. By doing this as separate work rather than when the LED
519 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
520 * potentially bad time, such as a timer interrupt.
521 */
522 asus->leds.workqueue = create_singlethread_workqueue("led_workqueue");
523 if (!asus->leds.workqueue)
524 return -ENOMEM;
525
d99b577c
CC
526 ASUS_LED_REGISTER(asus, mled, "mail", 1, METHOD_MLED);
527 ASUS_LED_REGISTER(asus, tled, "touchpad", 1, METHOD_TLED);
528 ASUS_LED_REGISTER(asus, rled, "record", 1, METHOD_RLED);
529 ASUS_LED_REGISTER(asus, pled, "phone", 1, METHOD_PLED);
530 ASUS_LED_REGISTER(asus, gled, "gaming", 1, METHOD_GLED);
531 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
532 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL))
533 ASUS_LED_REGISTER(asus, kled, "kbd_backlight", 3, NULL);
be4ee82d
CC
534error:
535 if (rv)
536 asus_led_exit(asus);
537 return rv;
538}
539
540/*
541 * Backlight device
542 */
3e68ae7c 543static int asus_lcd_status(struct asus_laptop *asus)
6b7091e7 544{
3e68ae7c 545 return asus->lcd_state;
6b7091e7
CC
546}
547
3e68ae7c 548static int asus_lcd_set(struct asus_laptop *asus, int value)
6b7091e7
CC
549{
550 int lcd = 0;
551 acpi_status status = 0;
552
3e68ae7c 553 lcd = !!value;
6b7091e7 554
3e68ae7c 555 if (lcd == asus_lcd_status(asus))
6b7091e7
CC
556 return 0;
557
3e68ae7c
CC
558 if (!lcd_switch_handle)
559 return -ENODEV;
560
561 status = acpi_evaluate_object(lcd_switch_handle,
562 NULL, NULL, NULL);
6b7091e7 563
3e68ae7c
CC
564 if (ACPI_FAILURE(status)) {
565 pr_warning("Error switching LCD\n");
566 return -ENODEV;
6b7091e7
CC
567 }
568
3e68ae7c 569 asus->lcd_state = lcd;
6b7091e7
CC
570 return 0;
571}
572
9129d14d 573static void lcd_blank(struct asus_laptop *asus, int blank)
6b7091e7 574{
7c247645 575 struct backlight_device *bd = asus->backlight_device;
6b7091e7 576
3e68ae7c
CC
577 asus->lcd_state = (blank == FB_BLANK_UNBLANK);
578
8def05fa 579 if (bd) {
599a52d1 580 bd->props.power = blank;
28ee086d 581 backlight_update_status(bd);
6b7091e7
CC
582 }
583}
584
4d441513 585static int asus_read_brightness(struct backlight_device *bd)
6b7091e7 586{
d99b577c 587 struct asus_laptop *asus = bl_get_data(bd);
27663c58 588 unsigned long long value;
9a816850 589 acpi_status rv = AE_OK;
6b7091e7 590
d99b577c
CC
591 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
592 NULL, &value);
9a816850 593 if (ACPI_FAILURE(rv))
2fcc23da 594 pr_warning("Error reading brightness\n");
6b7091e7
CC
595
596 return value;
597}
598
4d441513 599static int asus_set_brightness(struct backlight_device *bd, int value)
6b7091e7 600{
d99b577c
CC
601 struct asus_laptop *asus = bl_get_data(bd);
602
603 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
2fcc23da 604 pr_warning("Error changing brightness\n");
e5b50f6a 605 return -EIO;
6b7091e7 606 }
e5b50f6a 607 return 0;
6b7091e7
CC
608}
609
610static int update_bl_status(struct backlight_device *bd)
611{
9129d14d 612 struct asus_laptop *asus = bl_get_data(bd);
6b7091e7 613 int rv;
599a52d1 614 int value = bd->props.brightness;
6b7091e7 615
4d441513 616 rv = asus_set_brightness(bd, value);
8def05fa 617 if (rv)
6b7091e7
CC
618 return rv;
619
599a52d1 620 value = (bd->props.power == FB_BLANK_UNBLANK) ? 1 : 0;
3e68ae7c 621 return asus_lcd_set(asus, value);
6b7091e7
CC
622}
623
be4ee82d 624static struct backlight_ops asusbl_ops = {
4d441513 625 .get_brightness = asus_read_brightness,
be4ee82d
CC
626 .update_status = update_bl_status,
627};
628
629static int asus_backlight_init(struct asus_laptop *asus)
630{
631 struct backlight_device *bd;
632 struct device *dev = &asus->platform_device->dev;
633
d99b577c
CC
634 if (!acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) &&
635 !acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL) &&
636 lcd_switch_handle) {
be4ee82d
CC
637 bd = backlight_device_register(ASUS_LAPTOP_FILE, dev,
638 asus, &asusbl_ops);
639 if (IS_ERR(bd)) {
640 pr_err("Could not register asus backlight device\n");
641 asus->backlight_device = NULL;
642 return PTR_ERR(bd);
643 }
644
645 asus->backlight_device = bd;
646
647 bd->props.max_brightness = 15;
be4ee82d 648 bd->props.power = FB_BLANK_UNBLANK;
d99b577c 649 bd->props.brightness = asus_read_brightness(bd);
be4ee82d
CC
650 backlight_update_status(bd);
651 }
652 return 0;
653}
654
655static void asus_backlight_exit(struct asus_laptop *asus)
656{
657 if (asus->backlight_device)
658 backlight_device_unregister(asus->backlight_device);
659}
660
85091b71
CC
661/*
662 * Platform device handlers
663 */
664
665/*
666 * We write our info in page, we begin at offset off and cannot write more
667 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
668 * number of bytes written in page
669 */
670static ssize_t show_infos(struct device *dev,
8def05fa 671 struct device_attribute *attr, char *page)
85091b71 672{
9129d14d 673 struct asus_laptop *asus = dev_get_drvdata(dev);
85091b71 674 int len = 0;
27663c58 675 unsigned long long temp;
be966660 676 char buf[16]; /* enough for all info */
9a816850
CC
677 acpi_status rv = AE_OK;
678
85091b71
CC
679 /*
680 * We use the easy way, we don't care of off and count, so we don't set eof
681 * to 1
682 */
683
50a90c4d
CC
684 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
685 len += sprintf(page + len, "Model reference : %s\n", asus->name);
85091b71
CC
686 /*
687 * The SFUN method probably allows the original driver to get the list
688 * of features supported by a given model. For now, 0x0100 or 0x0800
689 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
690 * The significance of others is yet to be found.
691 */
50a90c4d 692 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
9a816850 693 if (!ACPI_FAILURE(rv))
1d4a3800
CC
694 len += sprintf(page + len, "SFUN value : %#x\n",
695 (uint) temp);
696 /*
697 * The HWRS method return informations about the hardware.
698 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
699 * The significance of others is yet to be found.
700 * If we don't find the method, we assume the device are present.
701 */
50a90c4d 702 rv = acpi_evaluate_integer(asus->handle, "HRWS", NULL, &temp);
1d4a3800
CC
703 if (!ACPI_FAILURE(rv))
704 len += sprintf(page + len, "HRWS value : %#x\n",
9a816850 705 (uint) temp);
85091b71
CC
706 /*
707 * Another value for userspace: the ASYM method returns 0x02 for
708 * battery low and 0x04 for battery critical, its readings tend to be
709 * more accurate than those provided by _BST.
710 * Note: since not all the laptops provide this method, errors are
711 * silently ignored.
712 */
50a90c4d 713 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
9a816850 714 if (!ACPI_FAILURE(rv))
1d4a3800 715 len += sprintf(page + len, "ASYM value : %#x\n",
9a816850 716 (uint) temp);
7c247645
CC
717 if (asus->dsdt_info) {
718 snprintf(buf, 16, "%d", asus->dsdt_info->length);
85091b71 719 len += sprintf(page + len, "DSDT length : %s\n", buf);
7c247645 720 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
85091b71 721 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
7c247645 722 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
85091b71 723 len += sprintf(page + len, "DSDT revision : %s\n", buf);
7c247645 724 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
85091b71 725 len += sprintf(page + len, "OEM id : %s\n", buf);
7c247645 726 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
85091b71 727 len += sprintf(page + len, "OEM table id : %s\n", buf);
7c247645 728 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
85091b71 729 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
7c247645 730 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
85091b71 731 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
7c247645 732 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
85091b71
CC
733 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
734 }
735
736 return len;
737}
738
739static int parse_arg(const char *buf, unsigned long count, int *val)
740{
741 if (!count)
742 return 0;
743 if (count > 31)
744 return -EINVAL;
745 if (sscanf(buf, "%i", val) != 1)
746 return -EINVAL;
747 return count;
748}
749
17e78f62
CC
750static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
751 const char *buf, size_t count,
d99b577c 752 const char *method)
4564de17
CC
753{
754 int rv, value;
755 int out = 0;
756
757 rv = parse_arg(buf, count, &value);
758 if (rv > 0)
759 out = value ? 1 : 0;
760
d99b577c 761 if (write_acpi_int(asus->handle, method, value))
17e78f62 762 return -ENODEV;
4564de17
CC
763 return rv;
764}
765
722ad971
CC
766/*
767 * LEDD display
768 */
769static ssize_t show_ledd(struct device *dev,
770 struct device_attribute *attr, char *buf)
771{
9129d14d
CC
772 struct asus_laptop *asus = dev_get_drvdata(dev);
773
50a90c4d 774 return sprintf(buf, "0x%08x\n", asus->ledd_status);
722ad971
CC
775}
776
777static ssize_t store_ledd(struct device *dev, struct device_attribute *attr,
778 const char *buf, size_t count)
779{
9129d14d 780 struct asus_laptop *asus = dev_get_drvdata(dev);
722ad971
CC
781 int rv, value;
782
783 rv = parse_arg(buf, count, &value);
784 if (rv > 0) {
d99b577c 785 if (write_acpi_int(asus->handle, METHOD_LEDD, value))
2fcc23da 786 pr_warning("LED display write failed\n");
722ad971 787 else
50a90c4d 788 asus->ledd_status = (u32) value;
722ad971
CC
789 }
790 return rv;
791}
792
aa9df930
CC
793/*
794 * Wireless
795 */
796static int asus_wireless_status(struct asus_laptop *asus, int mask)
797{
798 unsigned long long status;
799 acpi_status rv = AE_OK;
800
801 if (!asus->have_rsts)
802 return (asus->wireless_status & mask) ? 1 : 0;
803
d99b577c
CC
804 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
805 NULL, &status);
aa9df930
CC
806 if (ACPI_FAILURE(rv)) {
807 pr_warning("Error reading Wireless status\n");
808 return -EINVAL;
809 }
810 return !!(status & mask);
811}
812
4564de17
CC
813/*
814 * WLAN
815 */
e5593bf1
CC
816static int asus_wlan_set(struct asus_laptop *asus, int status)
817{
818 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
819 pr_warning("Error setting wlan status to %d", status);
820 return -EIO;
821 }
822 return 0;
823}
824
4564de17
CC
825static ssize_t show_wlan(struct device *dev,
826 struct device_attribute *attr, char *buf)
827{
9129d14d
CC
828 struct asus_laptop *asus = dev_get_drvdata(dev);
829
17e78f62 830 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
4564de17
CC
831}
832
833static ssize_t store_wlan(struct device *dev, struct device_attribute *attr,
834 const char *buf, size_t count)
835{
9129d14d
CC
836 struct asus_laptop *asus = dev_get_drvdata(dev);
837
d99b577c 838 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
4564de17
CC
839}
840
841/*
842 * Bluetooth
843 */
e5593bf1
CC
844static int asus_bluetooth_set(struct asus_laptop *asus, int status)
845{
846 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
847 pr_warning("Error setting bluetooth status to %d", status);
848 return -EIO;
849 }
850 return 0;
851}
852
4564de17
CC
853static ssize_t show_bluetooth(struct device *dev,
854 struct device_attribute *attr, char *buf)
855{
9129d14d
CC
856 struct asus_laptop *asus = dev_get_drvdata(dev);
857
17e78f62 858 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
4564de17
CC
859}
860
8def05fa
LB
861static ssize_t store_bluetooth(struct device *dev,
862 struct device_attribute *attr, const char *buf,
863 size_t count)
4564de17 864{
9129d14d
CC
865 struct asus_laptop *asus = dev_get_drvdata(dev);
866
d99b577c 867 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
4564de17
CC
868}
869
78127b4a
CC
870/*
871 * Display
872 */
4d441513 873static void asus_set_display(struct asus_laptop *asus, int value)
78127b4a
CC
874{
875 /* no sanity check needed for now */
d99b577c 876 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
2fcc23da 877 pr_warning("Error setting display\n");
78127b4a
CC
878 return;
879}
880
9129d14d 881static int read_display(struct asus_laptop *asus)
78127b4a 882{
27663c58 883 unsigned long long value = 0;
9a816850 884 acpi_status rv = AE_OK;
78127b4a 885
be966660
CC
886 /*
887 * In most of the case, we know how to set the display, but sometime
888 * we can't read it
889 */
8def05fa 890 if (display_get_handle) {
9a816850
CC
891 rv = acpi_evaluate_integer(display_get_handle, NULL,
892 NULL, &value);
893 if (ACPI_FAILURE(rv))
2fcc23da 894 pr_warning("Error reading display status\n");
78127b4a
CC
895 }
896
897 value &= 0x0F; /* needed for some models, shouldn't hurt others */
898
899 return value;
900}
8def05fa 901
78127b4a
CC
902/*
903 * Now, *this* one could be more user-friendly, but so far, no-one has
904 * complained. The significance of bits is the same as in store_disp()
905 */
906static ssize_t show_disp(struct device *dev,
907 struct device_attribute *attr, char *buf)
908{
9129d14d
CC
909 struct asus_laptop *asus = dev_get_drvdata(dev);
910
911 return sprintf(buf, "%d\n", read_display(asus));
78127b4a
CC
912}
913
914/*
915 * Experimental support for display switching. As of now: 1 should activate
916 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
917 * Any combination (bitwise) of these will suffice. I never actually tested 4
918 * displays hooked up simultaneously, so be warned. See the acpi4asus README
919 * for more info.
920 */
921static ssize_t store_disp(struct device *dev, struct device_attribute *attr,
922 const char *buf, size_t count)
923{
9129d14d 924 struct asus_laptop *asus = dev_get_drvdata(dev);
78127b4a
CC
925 int rv, value;
926
927 rv = parse_arg(buf, count, &value);
928 if (rv > 0)
4d441513 929 asus_set_display(asus, value);
78127b4a
CC
930 return rv;
931}
932
8b857353
CC
933/*
934 * Light Sens
935 */
4d441513 936static void asus_als_switch(struct asus_laptop *asus, int value)
8b857353 937{
d99b577c 938 if (write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value))
2fcc23da 939 pr_warning("Error setting light sensor switch\n");
50a90c4d 940 asus->light_switch = value;
8b857353
CC
941}
942
943static ssize_t show_lssw(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
9129d14d
CC
946 struct asus_laptop *asus = dev_get_drvdata(dev);
947
50a90c4d 948 return sprintf(buf, "%d\n", asus->light_switch);
8b857353
CC
949}
950
951static ssize_t store_lssw(struct device *dev, struct device_attribute *attr,
952 const char *buf, size_t count)
953{
9129d14d 954 struct asus_laptop *asus = dev_get_drvdata(dev);
8b857353
CC
955 int rv, value;
956
957 rv = parse_arg(buf, count, &value);
958 if (rv > 0)
4d441513 959 asus_als_switch(asus, value ? 1 : 0);
8b857353
CC
960
961 return rv;
962}
963
4d441513 964static void asus_als_level(struct asus_laptop *asus, int value)
8b857353 965{
d99b577c 966 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
2fcc23da 967 pr_warning("Error setting light sensor level\n");
50a90c4d 968 asus->light_level = value;
8b857353
CC
969}
970
971static ssize_t show_lslvl(struct device *dev,
972 struct device_attribute *attr, char *buf)
973{
9129d14d
CC
974 struct asus_laptop *asus = dev_get_drvdata(dev);
975
50a90c4d 976 return sprintf(buf, "%d\n", asus->light_level);
8b857353
CC
977}
978
979static ssize_t store_lslvl(struct device *dev, struct device_attribute *attr,
980 const char *buf, size_t count)
981{
9129d14d 982 struct asus_laptop *asus = dev_get_drvdata(dev);
8b857353
CC
983 int rv, value;
984
985 rv = parse_arg(buf, count, &value);
986 if (rv > 0) {
987 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
988 /* 0 <= value <= 15 */
4d441513 989 asus_als_level(asus, value);
8b857353
CC
990 }
991
992 return rv;
993}
994
e539c2f6
CC
995/*
996 * GPS
6358bf2c 997 * TODO: use rfkill
e539c2f6 998 */
6358bf2c
CC
999static int asus_gps_status(struct asus_laptop *asus)
1000{
1001 unsigned long long status;
1002 acpi_status rv = AE_OK;
1003
d99b577c
CC
1004 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1005 NULL, &status);
6358bf2c
CC
1006 if (ACPI_FAILURE(rv)) {
1007 pr_warning("Error reading GPS status\n");
1008 return -ENODEV;
1009 }
1010 return !!status;
1011}
1012
1013static int asus_gps_switch(struct asus_laptop *asus, int status)
1014{
d99b577c 1015 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
6358bf2c 1016
d99b577c 1017 if (write_acpi_int(asus->handle, meth, 0x02))
6358bf2c
CC
1018 return -ENODEV;
1019 return 0;
1020}
1021
e539c2f6
CC
1022static ssize_t show_gps(struct device *dev,
1023 struct device_attribute *attr, char *buf)
1024{
9129d14d
CC
1025 struct asus_laptop *asus = dev_get_drvdata(dev);
1026
6358bf2c 1027 return sprintf(buf, "%d\n", asus_gps_status(asus));
e539c2f6
CC
1028}
1029
1030static ssize_t store_gps(struct device *dev, struct device_attribute *attr,
1031 const char *buf, size_t count)
1032{
6358bf2c
CC
1033 struct asus_laptop *asus = dev_get_drvdata(dev);
1034 int rv, value;
1035 int ret;
9129d14d 1036
6358bf2c
CC
1037 rv = parse_arg(buf, count, &value);
1038 if (rv <= 0)
1039 return -EINVAL;
1040 ret = asus_gps_switch(asus, !!value);
1041 if (ret)
1042 return ret;
1043 return rv;
e539c2f6
CC
1044}
1045
034ce90a 1046/*
be4ee82d 1047 * Input device (i.e. hotkeys)
034ce90a 1048 */
be4ee82d
CC
1049static void asus_input_notify(struct asus_laptop *asus, int event)
1050{
66a71dd1
CC
1051 if (asus->inputdev)
1052 sparse_keymap_report_event(asus->inputdev, event, 1, true);
be4ee82d
CC
1053}
1054
1055static int asus_input_init(struct asus_laptop *asus)
1056{
66a71dd1
CC
1057 struct input_dev *input;
1058 int error;
be4ee82d 1059
66a71dd1
CC
1060 input = input_allocate_device();
1061 if (!input) {
be4ee82d
CC
1062 pr_info("Unable to allocate input device\n");
1063 return 0;
1064 }
66a71dd1
CC
1065 input->name = "Asus Laptop extra buttons";
1066 input->phys = ASUS_LAPTOP_FILE "/input0";
1067 input->id.bustype = BUS_HOST;
1068 input->dev.parent = &asus->platform_device->dev;
1069 input_set_drvdata(input, asus);
1070
1071 error = sparse_keymap_setup(input, asus_keymap, NULL);
1072 if (error) {
1073 pr_err("Unable to setup input device keymap\n");
1074 goto err_keymap;
be4ee82d 1075 }
66a71dd1
CC
1076 error = input_register_device(input);
1077 if (error) {
be4ee82d 1078 pr_info("Unable to register input device\n");
66a71dd1 1079 goto err_device;
be4ee82d 1080 }
66a71dd1
CC
1081
1082 asus->inputdev = input;
1083 return 0;
1084
1085err_keymap:
1086 sparse_keymap_free(input);
1087err_device:
1088 input_free_device(input);
1089 return error;
be4ee82d
CC
1090}
1091
1092static void asus_input_exit(struct asus_laptop *asus)
1093{
66a71dd1
CC
1094 if (asus->inputdev) {
1095 sparse_keymap_free(asus->inputdev);
be4ee82d 1096 input_unregister_device(asus->inputdev);
66a71dd1 1097 }
be4ee82d
CC
1098}
1099
1100/*
1101 * ACPI driver
1102 */
600ad520 1103static void asus_acpi_notify(struct acpi_device *device, u32 event)
85091b71 1104{
9129d14d 1105 struct asus_laptop *asus = acpi_driver_data(device);
6050c8dd 1106 u16 count;
034ce90a 1107
6b7091e7
CC
1108 /*
1109 * We need to tell the backlight device when the backlight power is
1110 * switched
1111 */
3e68ae7c 1112 if (event == ATKD_LCD_ON)
9129d14d 1113 lcd_blank(asus, FB_BLANK_UNBLANK);
3e68ae7c 1114 else if (event == ATKD_LCD_OFF)
9129d14d 1115 lcd_blank(asus, FB_BLANK_POWERDOWN);
6b7091e7 1116
619d8b11 1117 /* TODO Find a better way to handle events count. */
50a90c4d
CC
1118 count = asus->event_count[event % 128]++;
1119 acpi_bus_generate_proc_event(asus->device, event, count);
1120 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1121 dev_name(&asus->device->dev), event,
6050c8dd 1122 count);
85091b71 1123
be4ee82d 1124 asus_input_notify(asus, event);
85091b71
CC
1125}
1126
1127#define ASUS_CREATE_DEVICE_ATTR(_name) \
1128 struct device_attribute dev_attr_##_name = { \
1129 .attr = { \
1130 .name = __stringify(_name), \
7b595756 1131 .mode = 0 }, \
85091b71
CC
1132 .show = NULL, \
1133 .store = NULL, \
1134 }
1135
1136#define ASUS_SET_DEVICE_ATTR(_name, _mode, _show, _store) \
1137 do { \
1138 dev_attr_##_name.attr.mode = _mode; \
1139 dev_attr_##_name.show = _show; \
1140 dev_attr_##_name.store = _store; \
1141 } while(0)
1142
1143static ASUS_CREATE_DEVICE_ATTR(infos);
4564de17
CC
1144static ASUS_CREATE_DEVICE_ATTR(wlan);
1145static ASUS_CREATE_DEVICE_ATTR(bluetooth);
78127b4a 1146static ASUS_CREATE_DEVICE_ATTR(display);
722ad971 1147static ASUS_CREATE_DEVICE_ATTR(ledd);
8b857353
CC
1148static ASUS_CREATE_DEVICE_ATTR(ls_switch);
1149static ASUS_CREATE_DEVICE_ATTR(ls_level);
e539c2f6 1150static ASUS_CREATE_DEVICE_ATTR(gps);
85091b71
CC
1151
1152static struct attribute *asuspf_attributes[] = {
8def05fa
LB
1153 &dev_attr_infos.attr,
1154 &dev_attr_wlan.attr,
1155 &dev_attr_bluetooth.attr,
1156 &dev_attr_display.attr,
1157 &dev_attr_ledd.attr,
1158 &dev_attr_ls_switch.attr,
1159 &dev_attr_ls_level.attr,
e539c2f6 1160 &dev_attr_gps.attr,
8def05fa 1161 NULL
85091b71
CC
1162};
1163
600ad520 1164static struct attribute_group platform_attribute_group = {
8def05fa 1165 .attrs = asuspf_attributes
85091b71
CC
1166};
1167
9129d14d 1168static int asus_platform_init(struct asus_laptop *asus)
600ad520
CC
1169{
1170 int result;
1171
50a90c4d
CC
1172 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1173 if (!asus->platform_device)
600ad520 1174 return -ENOMEM;
9129d14d 1175 platform_set_drvdata(asus->platform_device, asus);
600ad520 1176
50a90c4d 1177 result = platform_device_add(asus->platform_device);
600ad520
CC
1178 if (result)
1179 goto fail_platform_device;
1180
50a90c4d 1181 result = sysfs_create_group(&asus->platform_device->dev.kobj,
600ad520
CC
1182 &platform_attribute_group);
1183 if (result)
1184 goto fail_sysfs;
1185 return 0;
1186
1187fail_sysfs:
50a90c4d 1188 platform_device_del(asus->platform_device);
600ad520 1189fail_platform_device:
50a90c4d 1190 platform_device_put(asus->platform_device);
600ad520
CC
1191 return result;
1192}
1193
9129d14d 1194static void asus_platform_exit(struct asus_laptop *asus)
600ad520 1195{
50a90c4d 1196 sysfs_remove_group(&asus->platform_device->dev.kobj,
600ad520 1197 &platform_attribute_group);
50a90c4d 1198 platform_device_unregister(asus->platform_device);
600ad520
CC
1199}
1200
1201static struct platform_driver platform_driver = {
8def05fa 1202 .driver = {
9129d14d
CC
1203 .name = ASUS_LAPTOP_FILE,
1204 .owner = THIS_MODULE,
1205 }
85091b71
CC
1206};
1207
9129d14d 1208static void asus_laptop_add_fs(struct asus_laptop *asus)
85091b71
CC
1209{
1210 ASUS_SET_DEVICE_ATTR(infos, 0444, show_infos, NULL);
4564de17 1211
d99b577c 1212 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL))
4564de17
CC
1213 ASUS_SET_DEVICE_ATTR(wlan, 0644, show_wlan, store_wlan);
1214
d99b577c 1215 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL))
4564de17
CC
1216 ASUS_SET_DEVICE_ATTR(bluetooth, 0644,
1217 show_bluetooth, store_bluetooth);
78127b4a 1218
d99b577c
CC
1219 if (!acpi_check_handle(asus->handle, METHOD_SWITCH_DISPLAY, NULL)) {
1220 if (display_get_handle)
1221 ASUS_SET_DEVICE_ATTR(display, 0644, show_disp,
1222 store_disp);
1223 else
1224 ASUS_SET_DEVICE_ATTR(display, 0200, NULL, store_disp);
1225 }
78127b4a 1226
d99b577c 1227 if (!acpi_check_handle(asus->handle, METHOD_LEDD, NULL))
722ad971
CC
1228 ASUS_SET_DEVICE_ATTR(ledd, 0644, show_ledd, store_ledd);
1229
d99b577c
CC
1230 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1231 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
8b857353
CC
1232 ASUS_SET_DEVICE_ATTR(ls_level, 0644, show_lslvl, store_lslvl);
1233 ASUS_SET_DEVICE_ATTR(ls_switch, 0644, show_lssw, store_lssw);
1234 }
e539c2f6 1235
d99b577c
CC
1236 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1237 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1238 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
e539c2f6 1239 ASUS_SET_DEVICE_ATTR(gps, 0644, show_gps, store_gps);
85091b71
CC
1240}
1241
8def05fa 1242static int asus_handle_init(char *name, acpi_handle * handle,
85091b71
CC
1243 char **paths, int num_paths)
1244{
1245 int i;
1246 acpi_status status;
1247
1248 for (i = 0; i < num_paths; i++) {
1249 status = acpi_get_handle(NULL, paths[i], handle);
1250 if (ACPI_SUCCESS(status))
1251 return 0;
1252 }
1253
1254 *handle = NULL;
1255 return -ENODEV;
1256}
1257
1258#define ASUS_HANDLE_INIT(object) \
1259 asus_handle_init(#object, &object##_handle, object##_paths, \
1260 ARRAY_SIZE(object##_paths))
1261
85091b71 1262/*
50a90c4d
CC
1263 * This function is used to initialize the context with right values. In this
1264 * method, we can make all the detection we want, and modify the asus_laptop
1265 * struct
85091b71 1266 */
7c247645 1267static int asus_laptop_get_info(struct asus_laptop *asus)
85091b71
CC
1268{
1269 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
85091b71 1270 union acpi_object *model = NULL;
27663c58 1271 unsigned long long bsts_result, hwrs_result;
85091b71
CC
1272 char *string = NULL;
1273 acpi_status status;
1274
1275 /*
1276 * Get DSDT headers early enough to allow for differentiating between
1277 * models, but late enough to allow acpi_bus_register_driver() to fail
1278 * before doing anything ACPI-specific. Should we encounter a machine,
1279 * which needs special handling (i.e. its hotkey device has a different
7c247645 1280 * HID), this bit will be moved.
85091b71 1281 */
7c247645 1282 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
85091b71 1283 if (ACPI_FAILURE(status))
2fcc23da 1284 pr_warning("Couldn't get the DSDT table header\n");
85091b71
CC
1285
1286 /* We have to write 0 on init this far for all ASUS models */
50a90c4d 1287 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
2fcc23da 1288 pr_err("Hotkey initialization failed\n");
85091b71
CC
1289 return -ENODEV;
1290 }
1291
1292 /* This needs to be called for some laptops to init properly */
9a816850 1293 status =
50a90c4d 1294 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
9a816850 1295 if (ACPI_FAILURE(status))
2fcc23da 1296 pr_warning("Error calling BSTS\n");
85091b71 1297 else if (bsts_result)
2fcc23da 1298 pr_notice("BSTS called, 0x%02x returned\n",
9a816850 1299 (uint) bsts_result);
85091b71 1300
185e5af9 1301 /* This too ... */
e5593bf1
CC
1302 if (write_acpi_int(asus->handle, "CWAP", wapf))
1303 pr_err("Error calling CWAP(%d)\n", wapf);
85091b71
CC
1304 /*
1305 * Try to match the object returned by INIT to the specific model.
1306 * Handle every possible object (or the lack of thereof) the DSDT
1307 * writers might throw at us. When in trouble, we pass NULL to
1308 * asus_model_match() and try something completely different.
1309 */
1310 if (buffer.pointer) {
1311 model = buffer.pointer;
1312 switch (model->type) {
1313 case ACPI_TYPE_STRING:
1314 string = model->string.pointer;
1315 break;
1316 case ACPI_TYPE_BUFFER:
1317 string = model->buffer.pointer;
1318 break;
1319 default:
1320 string = "";
1321 break;
1322 }
1323 }
50a90c4d
CC
1324 asus->name = kstrdup(string, GFP_KERNEL);
1325 if (!asus->name)
85091b71
CC
1326 return -ENOMEM;
1327
8def05fa 1328 if (*string)
2fcc23da 1329 pr_notice(" %s model detected\n", string);
85091b71 1330
4564de17
CC
1331 /*
1332 * The HWRS method return informations about the hardware.
1333 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
1334 * The significance of others is yet to be found.
4564de17 1335 */
9a816850 1336 status =
50a90c4d 1337 acpi_evaluate_integer(asus->handle, "HRWS", NULL, &hwrs_result);
d99b577c
CC
1338 if (!ACPI_FAILURE(status))
1339 pr_notice(" HRWS returned %x", (int)hwrs_result);
4564de17 1340
d99b577c 1341 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
aa9df930 1342 asus->have_rsts = true;
4564de17 1343
d99b577c 1344 /* Scheduled for removal */
6b7091e7 1345 ASUS_HANDLE_INIT(lcd_switch);
78127b4a
CC
1346 ASUS_HANDLE_INIT(display_get);
1347
85091b71
CC
1348 kfree(model);
1349
1350 return AE_OK;
1351}
1352
600ad520
CC
1353static bool asus_device_present;
1354
9129d14d 1355static int __devinit asus_acpi_init(struct asus_laptop *asus)
85091b71 1356{
600ad520 1357 int result = 0;
85091b71 1358
50a90c4d 1359 result = acpi_bus_get_status(asus->device);
600ad520 1360 if (result)
85091b71 1361 return result;
50a90c4d 1362 if (!asus->device->status.present) {
600ad520 1363 pr_err("Hotkey device not present, aborting\n");
85091b71
CC
1364 return -ENODEV;
1365 }
1366
7c247645 1367 result = asus_laptop_get_info(asus);
034ce90a 1368 if (result)
600ad520 1369 return result;
034ce90a 1370
9129d14d 1371 asus_laptop_add_fs(asus);
85091b71 1372
600ad520 1373 /* WLED and BLED are on by default */
be4ee82d 1374 if (bluetooth_status >= 0)
e5593bf1
CC
1375 asus_bluetooth_set(asus, !!bluetooth_status);
1376
d0930a2d
CC
1377 if (wlan_status >= 0)
1378 asus_wlan_set(asus, !!wlan_status);
85091b71 1379
600ad520 1380 /* Keyboard Backlight is on by default */
d99b577c 1381 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
4d441513 1382 asus_kled_set(asus, 1);
600ad520
CC
1383
1384 /* LED display is off by default */
50a90c4d 1385 asus->ledd_status = 0xFFF;
600ad520
CC
1386
1387 /* Set initial values of light sensor and level */
be4ee82d
CC
1388 asus->light_switch = 0; /* Default to light sensor disabled */
1389 asus->light_level = 5; /* level 5 for sensor sensitivity */
600ad520 1390
d99b577c
CC
1391 if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1392 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
4d441513 1393 asus_als_switch(asus, asus->light_switch);
4d441513 1394 asus_als_level(asus, asus->light_level);
d99b577c 1395 }
600ad520
CC
1396
1397 /* GPS is on by default */
d99b577c
CC
1398 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1399 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1400 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1401 asus_gps_switch(asus, 1);
600ad520
CC
1402 return result;
1403}
1404
1405static int __devinit asus_acpi_add(struct acpi_device *device)
1406{
9129d14d 1407 struct asus_laptop *asus;
600ad520
CC
1408 int result;
1409
1410 pr_notice("Asus Laptop Support version %s\n",
1411 ASUS_LAPTOP_VERSION);
50a90c4d
CC
1412 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1413 if (!asus)
600ad520 1414 return -ENOMEM;
50a90c4d
CC
1415 asus->handle = device->handle;
1416 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1417 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1418 device->driver_data = asus;
1419 asus->device = device;
600ad520 1420
9129d14d 1421 result = asus_acpi_init(asus);
8def05fa 1422 if (result)
600ad520 1423 goto fail_platform;
85091b71 1424
600ad520
CC
1425 /*
1426 * Register the platform device first. It is used as a parent for the
1427 * sub-devices below.
1428 */
9129d14d 1429 result = asus_platform_init(asus);
116bf2e0 1430 if (result)
600ad520 1431 goto fail_platform;
116bf2e0
CC
1432
1433 if (!acpi_video_backlight_support()) {
9129d14d 1434 result = asus_backlight_init(asus);
116bf2e0
CC
1435 if (result)
1436 goto fail_backlight;
1437 } else
600ad520 1438 pr_info("Backlight controlled by ACPI video driver\n");
116bf2e0 1439
9129d14d 1440 result = asus_input_init(asus);
600ad520
CC
1441 if (result)
1442 goto fail_input;
1443
9129d14d 1444 result = asus_led_init(asus);
600ad520
CC
1445 if (result)
1446 goto fail_led;
1447
1448 asus_device_present = true;
8def05fa 1449 return 0;
85091b71 1450
600ad520 1451fail_led:
9129d14d 1452 asus_input_exit(asus);
600ad520 1453fail_input:
9129d14d 1454 asus_backlight_exit(asus);
116bf2e0 1455fail_backlight:
9129d14d 1456 asus_platform_exit(asus);
600ad520 1457fail_platform:
50a90c4d
CC
1458 kfree(asus->name);
1459 kfree(asus);
116bf2e0 1460
600ad520
CC
1461 return result;
1462}
116bf2e0 1463
600ad520
CC
1464static int asus_acpi_remove(struct acpi_device *device, int type)
1465{
9129d14d
CC
1466 struct asus_laptop *asus = acpi_driver_data(device);
1467
1468 asus_backlight_exit(asus);
1469 asus_led_exit(asus);
1470 asus_input_exit(asus);
1471 asus_platform_exit(asus);
600ad520 1472
50a90c4d
CC
1473 kfree(asus->name);
1474 kfree(asus);
600ad520
CC
1475 return 0;
1476}
1477
1478static const struct acpi_device_id asus_device_ids[] = {
1479 {"ATK0100", 0},
1480 {"ATK0101", 0},
1481 {"", 0},
1482};
1483MODULE_DEVICE_TABLE(acpi, asus_device_ids);
85091b71 1484
600ad520 1485static struct acpi_driver asus_acpi_driver = {
50a90c4d
CC
1486 .name = ASUS_LAPTOP_NAME,
1487 .class = ASUS_LAPTOP_CLASS,
600ad520
CC
1488 .owner = THIS_MODULE,
1489 .ids = asus_device_ids,
1490 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1491 .ops = {
1492 .add = asus_acpi_add,
1493 .remove = asus_acpi_remove,
1494 .notify = asus_acpi_notify,
1495 },
1496};
85091b71 1497
600ad520
CC
1498static int __init asus_laptop_init(void)
1499{
1500 int result;
85091b71 1501
600ad520
CC
1502 result = platform_driver_register(&platform_driver);
1503 if (result < 0)
1504 return result;
034ce90a 1505
600ad520
CC
1506 result = acpi_bus_register_driver(&asus_acpi_driver);
1507 if (result < 0)
1508 goto fail_acpi_driver;
1509 if (!asus_device_present) {
1510 result = -ENODEV;
1511 goto fail_no_device;
1512 }
1513 return 0;
85091b71 1514
600ad520
CC
1515fail_no_device:
1516 acpi_bus_unregister_driver(&asus_acpi_driver);
1517fail_acpi_driver:
1518 platform_driver_unregister(&platform_driver);
85091b71
CC
1519 return result;
1520}
1521
600ad520
CC
1522static void __exit asus_laptop_exit(void)
1523{
1524 acpi_bus_unregister_driver(&asus_acpi_driver);
1525 platform_driver_unregister(&platform_driver);
1526}
1527
85091b71
CC
1528module_init(asus_laptop_init);
1529module_exit(asus_laptop_exit);
This page took 0.424264 seconds and 5 git commands to generate.