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