treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / platform / x86 / asus-laptop.c
bloba666fbc2e73b5008ae6bbe02d24ac0f660fe5e61
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * asus-laptop.c - Asus Laptop Support
5 * Copyright (C) 2002-2005 Julien Lerouge, 2003-2006 Karol Kozimor
6 * Copyright (C) 2006-2007 Corentin Chary
7 * Copyright (C) 2011 Wind River Systems
9 * The development page for this driver is located at
10 * http://sourceforge.net/projects/acpi4asus/
12 * Credits:
13 * Pontus Fuchs - Helper functions, cleanup
14 * Johann Wiesner - Small compile fixes
15 * John Belmonte - ACPI code for Toshiba laptop was a good starting point.
16 * Eric Burghard - LED display support for W1N
17 * Josh Green - Light Sens support
18 * Thomas Tuttle - His first patch for led support was very helpful
19 * Sam Lin - GPS support
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/err.h>
29 #include <linux/proc_fs.h>
30 #include <linux/backlight.h>
31 #include <linux/fb.h>
32 #include <linux/leds.h>
33 #include <linux/platform_device.h>
34 #include <linux/uaccess.h>
35 #include <linux/input.h>
36 #include <linux/input/sparse-keymap.h>
37 #include <linux/rfkill.h>
38 #include <linux/slab.h>
39 #include <linux/dmi.h>
40 #include <linux/acpi.h>
41 #include <acpi/video.h>
43 #define ASUS_LAPTOP_VERSION "0.42"
45 #define ASUS_LAPTOP_NAME "Asus Laptop Support"
46 #define ASUS_LAPTOP_CLASS "hotkey"
47 #define ASUS_LAPTOP_DEVICE_NAME "Hotkey"
48 #define ASUS_LAPTOP_FILE KBUILD_MODNAME
49 #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
51 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
52 MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
53 MODULE_LICENSE("GPL");
56 * WAPF defines the behavior of the Fn+Fx wlan key
57 * The significance of values is yet to be found, but
58 * most of the time:
59 * Bit | Bluetooth | WLAN
60 * 0 | Hardware | Hardware
61 * 1 | Hardware | Software
62 * 4 | Software | Software
64 static uint wapf = 1;
65 module_param(wapf, uint, 0444);
66 MODULE_PARM_DESC(wapf, "WAPF value");
68 static char *wled_type = "unknown";
69 static char *bled_type = "unknown";
71 module_param(wled_type, charp, 0444);
72 MODULE_PARM_DESC(wled_type, "Set the wled type on boot "
73 "(unknown, led or rfkill). "
74 "default is unknown");
76 module_param(bled_type, charp, 0444);
77 MODULE_PARM_DESC(bled_type, "Set the bled type on boot "
78 "(unknown, led or rfkill). "
79 "default is unknown");
81 static int wlan_status = 1;
82 static int bluetooth_status = 1;
83 static int wimax_status = -1;
84 static int wwan_status = -1;
85 static int als_status;
87 module_param(wlan_status, int, 0444);
88 MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
89 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
90 "default is -1");
92 module_param(bluetooth_status, int, 0444);
93 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
94 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
95 "default is -1");
97 module_param(wimax_status, int, 0444);
98 MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
99 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
100 "default is -1");
102 module_param(wwan_status, int, 0444);
103 MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
104 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
105 "default is -1");
107 module_param(als_status, int, 0444);
108 MODULE_PARM_DESC(als_status, "Set the ALS status on boot "
109 "(0 = disabled, 1 = enabled). "
110 "default is 0");
113 * Some events we use, same for all Asus
115 #define ATKD_BRNUP_MIN 0x10
116 #define ATKD_BRNUP_MAX 0x1f
117 #define ATKD_BRNDOWN_MIN 0x20
118 #define ATKD_BRNDOWN_MAX 0x2f
119 #define ATKD_BRNDOWN 0x20
120 #define ATKD_BRNUP 0x2f
121 #define ATKD_LCD_ON 0x33
122 #define ATKD_LCD_OFF 0x34
125 * Known bits returned by \_SB.ATKD.HWRS
127 #define WL_HWRS 0x80
128 #define BT_HWRS 0x100
131 * Flags for hotk status
132 * WL_ON and BT_ON are also used for wireless_status()
134 #define WL_RSTS 0x01 /* internal Wifi */
135 #define BT_RSTS 0x02 /* internal Bluetooth */
136 #define WM_RSTS 0x08 /* internal wimax */
137 #define WW_RSTS 0x20 /* internal wwan */
139 /* WLED and BLED type */
140 #define TYPE_UNKNOWN 0
141 #define TYPE_LED 1
142 #define TYPE_RFKILL 2
144 /* LED */
145 #define METHOD_MLED "MLED"
146 #define METHOD_TLED "TLED"
147 #define METHOD_RLED "RLED" /* W1JC */
148 #define METHOD_PLED "PLED" /* A7J */
149 #define METHOD_GLED "GLED" /* G1, G2 (probably) */
151 /* LEDD */
152 #define METHOD_LEDD "SLCM"
155 * Bluetooth and WLAN
156 * WLED and BLED are not handled like other XLED, because in some dsdt
157 * they also control the WLAN/Bluetooth device.
159 #define METHOD_WLAN "WLED"
160 #define METHOD_BLUETOOTH "BLED"
162 /* WWAN and WIMAX */
163 #define METHOD_WWAN "GSMC"
164 #define METHOD_WIMAX "WMXC"
166 #define METHOD_WL_STATUS "RSTS"
168 /* Brightness */
169 #define METHOD_BRIGHTNESS_SET "SPLV"
170 #define METHOD_BRIGHTNESS_GET "GPLV"
172 /* Display */
173 #define METHOD_SWITCH_DISPLAY "SDSP"
175 #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
176 #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
178 /* GPS */
179 /* R2H use different handle for GPS on/off */
180 #define METHOD_GPS_ON "SDON"
181 #define METHOD_GPS_OFF "SDOF"
182 #define METHOD_GPS_STATUS "GPST"
184 /* Keyboard light */
185 #define METHOD_KBD_LIGHT_SET "SLKB"
186 #define METHOD_KBD_LIGHT_GET "GLKB"
188 /* For Pegatron Lucid tablet */
189 #define DEVICE_NAME_PEGA "Lucid"
191 #define METHOD_PEGA_ENABLE "ENPR"
192 #define METHOD_PEGA_DISABLE "DAPR"
193 #define PEGA_WLAN 0x00
194 #define PEGA_BLUETOOTH 0x01
195 #define PEGA_WWAN 0x02
196 #define PEGA_ALS 0x04
197 #define PEGA_ALS_POWER 0x05
199 #define METHOD_PEGA_READ "RDLN"
200 #define PEGA_READ_ALS_H 0x02
201 #define PEGA_READ_ALS_L 0x03
203 #define PEGA_ACCEL_NAME "pega_accel"
204 #define PEGA_ACCEL_DESC "Pegatron Lucid Tablet Accelerometer"
205 #define METHOD_XLRX "XLRX"
206 #define METHOD_XLRY "XLRY"
207 #define METHOD_XLRZ "XLRZ"
208 #define PEGA_ACC_CLAMP 512 /* 1G accel is reported as ~256, so clamp to 2G */
209 #define PEGA_ACC_RETRIES 3
212 * Define a specific led structure to keep the main structure clean
214 struct asus_led {
215 int wk;
216 struct work_struct work;
217 struct led_classdev led;
218 struct asus_laptop *asus;
219 const char *method;
223 * Same thing for rfkill
225 struct asus_rfkill {
226 /* type of control. Maps to PEGA_* values or *_RSTS */
227 int control_id;
228 struct rfkill *rfkill;
229 struct asus_laptop *asus;
233 * This is the main structure, we can use it to store anything interesting
234 * about the hotk device
236 struct asus_laptop {
237 char *name; /* laptop name */
239 struct acpi_table_header *dsdt_info;
240 struct platform_device *platform_device;
241 struct acpi_device *device; /* the device we are in */
242 struct backlight_device *backlight_device;
244 struct input_dev *inputdev;
245 struct key_entry *keymap;
246 struct input_dev *pega_accel_poll;
248 struct asus_led wled;
249 struct asus_led bled;
250 struct asus_led mled;
251 struct asus_led tled;
252 struct asus_led rled;
253 struct asus_led pled;
254 struct asus_led gled;
255 struct asus_led kled;
256 struct workqueue_struct *led_workqueue;
258 int wled_type;
259 int bled_type;
260 int wireless_status;
261 bool have_rsts;
262 bool is_pega_lucid;
263 bool pega_acc_live;
264 int pega_acc_x;
265 int pega_acc_y;
266 int pega_acc_z;
268 struct asus_rfkill wlan;
269 struct asus_rfkill bluetooth;
270 struct asus_rfkill wwan;
271 struct asus_rfkill wimax;
272 struct asus_rfkill gps;
274 acpi_handle handle; /* the handle of the hotk device */
275 u32 ledd_status; /* status of the LED display */
276 u8 light_level; /* light sensor level */
277 u8 light_switch; /* light sensor switch value */
278 u16 event_count[128]; /* count for each event TODO make this better */
281 static const struct key_entry asus_keymap[] = {
282 /* Lenovo SL Specific keycodes */
283 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
284 {KE_KEY, 0x05, { KEY_WLAN } },
285 {KE_KEY, 0x08, { KEY_F13 } },
286 {KE_KEY, 0x09, { KEY_PROG2 } }, /* Dock */
287 {KE_KEY, 0x17, { KEY_ZOOM } },
288 {KE_KEY, 0x1f, { KEY_BATTERY } },
289 /* End of Lenovo SL Specific keycodes */
290 {KE_KEY, ATKD_BRNDOWN, { KEY_BRIGHTNESSDOWN } },
291 {KE_KEY, ATKD_BRNUP, { KEY_BRIGHTNESSUP } },
292 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
293 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
294 {KE_KEY, 0x32, { KEY_MUTE } },
295 {KE_KEY, 0x33, { KEY_DISPLAYTOGGLE } }, /* LCD on */
296 {KE_KEY, 0x34, { KEY_DISPLAY_OFF } }, /* LCD off */
297 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
298 {KE_KEY, 0x41, { KEY_NEXTSONG } },
299 {KE_KEY, 0x43, { KEY_STOPCD } }, /* Stop/Eject */
300 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
301 {KE_KEY, 0x4c, { KEY_MEDIA } }, /* WMP Key */
302 {KE_KEY, 0x50, { KEY_EMAIL } },
303 {KE_KEY, 0x51, { KEY_WWW } },
304 {KE_KEY, 0x55, { KEY_CALC } },
305 {KE_IGNORE, 0x57, }, /* Battery mode */
306 {KE_IGNORE, 0x58, }, /* AC mode */
307 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
308 {KE_KEY, 0x5D, { KEY_WLAN } }, /* WLAN Toggle */
309 {KE_KEY, 0x5E, { KEY_WLAN } }, /* WLAN Enable */
310 {KE_KEY, 0x5F, { KEY_WLAN } }, /* WLAN Disable */
311 {KE_KEY, 0x60, { KEY_TOUCHPAD_ON } },
312 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD only */
313 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT only */
314 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT */
315 {KE_KEY, 0x64, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV */
316 {KE_KEY, 0x65, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV */
317 {KE_KEY, 0x66, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV */
318 {KE_KEY, 0x67, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV */
319 {KE_KEY, 0x6A, { KEY_TOUCHPAD_TOGGLE } }, /* Lock Touchpad Fn + F9 */
320 {KE_KEY, 0x6B, { KEY_TOUCHPAD_TOGGLE } }, /* Lock Touchpad */
321 {KE_KEY, 0x6C, { KEY_SLEEP } }, /* Suspend */
322 {KE_KEY, 0x6D, { KEY_SLEEP } }, /* Hibernate */
323 {KE_IGNORE, 0x6E, }, /* Low Battery notification */
324 {KE_KEY, 0x7D, { KEY_BLUETOOTH } }, /* Bluetooth Enable */
325 {KE_KEY, 0x7E, { KEY_BLUETOOTH } }, /* Bluetooth Disable */
326 {KE_KEY, 0x82, { KEY_CAMERA } },
327 {KE_KEY, 0x88, { KEY_RFKILL } }, /* Radio Toggle Key */
328 {KE_KEY, 0x8A, { KEY_PROG1 } }, /* Color enhancement mode */
329 {KE_KEY, 0x8C, { KEY_SWITCHVIDEOMODE } }, /* SDSP DVI only */
330 {KE_KEY, 0x8D, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + DVI */
331 {KE_KEY, 0x8E, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + DVI */
332 {KE_KEY, 0x8F, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV + DVI */
333 {KE_KEY, 0x90, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + DVI */
334 {KE_KEY, 0x91, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV + DVI */
335 {KE_KEY, 0x92, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV + DVI */
336 {KE_KEY, 0x93, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV + DVI */
337 {KE_KEY, 0x95, { KEY_MEDIA } },
338 {KE_KEY, 0x99, { KEY_PHONE } },
339 {KE_KEY, 0xA0, { KEY_SWITCHVIDEOMODE } }, /* SDSP HDMI only */
340 {KE_KEY, 0xA1, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + HDMI */
341 {KE_KEY, 0xA2, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + HDMI */
342 {KE_KEY, 0xA3, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV + HDMI */
343 {KE_KEY, 0xA4, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + HDMI */
344 {KE_KEY, 0xA5, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV + HDMI */
345 {KE_KEY, 0xA6, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV + HDMI */
346 {KE_KEY, 0xA7, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV + HDMI */
347 {KE_KEY, 0xB5, { KEY_CALC } },
348 {KE_KEY, 0xC4, { KEY_KBDILLUMUP } },
349 {KE_KEY, 0xC5, { KEY_KBDILLUMDOWN } },
350 {KE_END, 0},
355 * This function evaluates an ACPI method, given an int as parameter, the
356 * method is searched within the scope of the handle, can be NULL. The output
357 * of the method is written is output, which can also be NULL
359 * returns 0 if write is successful, -1 else.
361 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
362 struct acpi_buffer *output)
364 struct acpi_object_list params; /* list of input parameters (an int) */
365 union acpi_object in_obj; /* the only param we use */
366 acpi_status status;
368 if (!handle)
369 return -1;
371 params.count = 1;
372 params.pointer = &in_obj;
373 in_obj.type = ACPI_TYPE_INTEGER;
374 in_obj.integer.value = val;
376 status = acpi_evaluate_object(handle, (char *)method, &params, output);
377 if (status == AE_OK)
378 return 0;
379 else
380 return -1;
383 static int write_acpi_int(acpi_handle handle, const char *method, int val)
385 return write_acpi_int_ret(handle, method, val, NULL);
388 static int acpi_check_handle(acpi_handle handle, const char *method,
389 acpi_handle *ret)
391 acpi_status status;
393 if (method == NULL)
394 return -ENODEV;
396 if (ret)
397 status = acpi_get_handle(handle, (char *)method,
398 ret);
399 else {
400 acpi_handle dummy;
402 status = acpi_get_handle(handle, (char *)method,
403 &dummy);
406 if (status != AE_OK) {
407 if (ret)
408 pr_warn("Error finding %s\n", method);
409 return -ENODEV;
411 return 0;
414 static bool asus_check_pega_lucid(struct asus_laptop *asus)
416 return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
417 !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
418 !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
419 !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
422 static int asus_pega_lucid_set(struct asus_laptop *asus, int unit, bool enable)
424 char *method = enable ? METHOD_PEGA_ENABLE : METHOD_PEGA_DISABLE;
425 return write_acpi_int(asus->handle, method, unit);
428 static int pega_acc_axis(struct asus_laptop *asus, int curr, char *method)
430 int i, delta;
431 unsigned long long val;
432 for (i = 0; i < PEGA_ACC_RETRIES; i++) {
433 acpi_evaluate_integer(asus->handle, method, NULL, &val);
435 /* The output is noisy. From reading the ASL
436 * dissassembly, timeout errors are returned with 1's
437 * in the high word, and the lack of locking around
438 * thei hi/lo byte reads means that a transition
439 * between (for example) -1 and 0 could be read as
440 * 0xff00 or 0x00ff. */
441 delta = abs(curr - (short)val);
442 if (delta < 128 && !(val & ~0xffff))
443 break;
445 return clamp_val((short)val, -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP);
448 static void pega_accel_poll(struct input_dev *input)
450 struct device *parent = input->dev.parent;
451 struct asus_laptop *asus = dev_get_drvdata(parent);
453 /* In some cases, the very first call to poll causes a
454 * recursive fault under the polldev worker. This is
455 * apparently related to very early userspace access to the
456 * device, and perhaps a firmware bug. Fake the first report. */
457 if (!asus->pega_acc_live) {
458 asus->pega_acc_live = true;
459 input_report_abs(input, ABS_X, 0);
460 input_report_abs(input, ABS_Y, 0);
461 input_report_abs(input, ABS_Z, 0);
462 input_sync(input);
463 return;
466 asus->pega_acc_x = pega_acc_axis(asus, asus->pega_acc_x, METHOD_XLRX);
467 asus->pega_acc_y = pega_acc_axis(asus, asus->pega_acc_y, METHOD_XLRY);
468 asus->pega_acc_z = pega_acc_axis(asus, asus->pega_acc_z, METHOD_XLRZ);
470 /* Note transform, convert to "right/up/out" in the native
471 * landscape orientation (i.e. the vector is the direction of
472 * "real up" in the device's cartiesian coordinates). */
473 input_report_abs(input, ABS_X, -asus->pega_acc_x);
474 input_report_abs(input, ABS_Y, -asus->pega_acc_y);
475 input_report_abs(input, ABS_Z, asus->pega_acc_z);
476 input_sync(input);
479 static void pega_accel_exit(struct asus_laptop *asus)
481 if (asus->pega_accel_poll) {
482 input_unregister_device(asus->pega_accel_poll);
483 asus->pega_accel_poll = NULL;
487 static int pega_accel_init(struct asus_laptop *asus)
489 int err;
490 struct input_dev *input;
492 if (!asus->is_pega_lucid)
493 return -ENODEV;
495 if (acpi_check_handle(asus->handle, METHOD_XLRX, NULL) ||
496 acpi_check_handle(asus->handle, METHOD_XLRY, NULL) ||
497 acpi_check_handle(asus->handle, METHOD_XLRZ, NULL))
498 return -ENODEV;
500 input = input_allocate_device();
501 if (!input)
502 return -ENOMEM;
504 input->name = PEGA_ACCEL_DESC;
505 input->phys = PEGA_ACCEL_NAME "/input0";
506 input->dev.parent = &asus->platform_device->dev;
507 input->id.bustype = BUS_HOST;
509 input_set_abs_params(input, ABS_X,
510 -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
511 input_set_abs_params(input, ABS_Y,
512 -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
513 input_set_abs_params(input, ABS_Z,
514 -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP, 0, 0);
516 err = input_setup_polling(input, pega_accel_poll);
517 if (err)
518 goto exit;
520 input_set_poll_interval(input, 125);
521 input_set_min_poll_interval(input, 50);
522 input_set_max_poll_interval(input, 2000);
524 err = input_register_device(input);
525 if (err)
526 goto exit;
528 asus->pega_accel_poll = input;
529 return 0;
531 exit:
532 input_free_device(input);
533 return err;
536 /* Generic LED function */
537 static int asus_led_set(struct asus_laptop *asus, const char *method,
538 int value)
540 if (!strcmp(method, METHOD_MLED))
541 value = !value;
542 else if (!strcmp(method, METHOD_GLED))
543 value = !value + 1;
544 else
545 value = !!value;
547 return write_acpi_int(asus->handle, method, value);
551 * LEDs
553 /* /sys/class/led handlers */
554 static void asus_led_cdev_set(struct led_classdev *led_cdev,
555 enum led_brightness value)
557 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
558 struct asus_laptop *asus = led->asus;
560 led->wk = !!value;
561 queue_work(asus->led_workqueue, &led->work);
564 static void asus_led_cdev_update(struct work_struct *work)
566 struct asus_led *led = container_of(work, struct asus_led, work);
567 struct asus_laptop *asus = led->asus;
569 asus_led_set(asus, led->method, led->wk);
572 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
574 return led_cdev->brightness;
578 * Keyboard backlight (also a LED)
580 static int asus_kled_lvl(struct asus_laptop *asus)
582 unsigned long long kblv;
583 struct acpi_object_list params;
584 union acpi_object in_obj;
585 acpi_status rv;
587 params.count = 1;
588 params.pointer = &in_obj;
589 in_obj.type = ACPI_TYPE_INTEGER;
590 in_obj.integer.value = 2;
592 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
593 &params, &kblv);
594 if (ACPI_FAILURE(rv)) {
595 pr_warn("Error reading kled level\n");
596 return -ENODEV;
598 return kblv;
601 static int asus_kled_set(struct asus_laptop *asus, int kblv)
603 if (kblv > 0)
604 kblv = (1 << 7) | (kblv & 0x7F);
605 else
606 kblv = 0;
608 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
609 pr_warn("Keyboard LED display write failed\n");
610 return -EINVAL;
612 return 0;
615 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
616 enum led_brightness value)
618 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
619 struct asus_laptop *asus = led->asus;
621 led->wk = value;
622 queue_work(asus->led_workqueue, &led->work);
625 static void asus_kled_cdev_update(struct work_struct *work)
627 struct asus_led *led = container_of(work, struct asus_led, work);
628 struct asus_laptop *asus = led->asus;
630 asus_kled_set(asus, led->wk);
633 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
635 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
636 struct asus_laptop *asus = led->asus;
638 return asus_kled_lvl(asus);
641 static void asus_led_exit(struct asus_laptop *asus)
643 if (!IS_ERR_OR_NULL(asus->wled.led.dev))
644 led_classdev_unregister(&asus->wled.led);
645 if (!IS_ERR_OR_NULL(asus->bled.led.dev))
646 led_classdev_unregister(&asus->bled.led);
647 if (!IS_ERR_OR_NULL(asus->mled.led.dev))
648 led_classdev_unregister(&asus->mled.led);
649 if (!IS_ERR_OR_NULL(asus->tled.led.dev))
650 led_classdev_unregister(&asus->tled.led);
651 if (!IS_ERR_OR_NULL(asus->pled.led.dev))
652 led_classdev_unregister(&asus->pled.led);
653 if (!IS_ERR_OR_NULL(asus->rled.led.dev))
654 led_classdev_unregister(&asus->rled.led);
655 if (!IS_ERR_OR_NULL(asus->gled.led.dev))
656 led_classdev_unregister(&asus->gled.led);
657 if (!IS_ERR_OR_NULL(asus->kled.led.dev))
658 led_classdev_unregister(&asus->kled.led);
659 if (asus->led_workqueue) {
660 destroy_workqueue(asus->led_workqueue);
661 asus->led_workqueue = NULL;
665 /* Ugly macro, need to fix that later */
666 static int asus_led_register(struct asus_laptop *asus,
667 struct asus_led *led,
668 const char *name, const char *method)
670 struct led_classdev *led_cdev = &led->led;
672 if (!method || acpi_check_handle(asus->handle, method, NULL))
673 return 0; /* Led not present */
675 led->asus = asus;
676 led->method = method;
678 INIT_WORK(&led->work, asus_led_cdev_update);
679 led_cdev->name = name;
680 led_cdev->brightness_set = asus_led_cdev_set;
681 led_cdev->brightness_get = asus_led_cdev_get;
682 led_cdev->max_brightness = 1;
683 return led_classdev_register(&asus->platform_device->dev, led_cdev);
686 static int asus_led_init(struct asus_laptop *asus)
688 int r = 0;
691 * The Pegatron Lucid has no physical leds, but all methods are
692 * available in the DSDT...
694 if (asus->is_pega_lucid)
695 return 0;
698 * Functions that actually update the LED's are called from a
699 * workqueue. By doing this as separate work rather than when the LED
700 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
701 * potentially bad time, such as a timer interrupt.
703 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
704 if (!asus->led_workqueue)
705 return -ENOMEM;
707 if (asus->wled_type == TYPE_LED)
708 r = asus_led_register(asus, &asus->wled, "asus::wlan",
709 METHOD_WLAN);
710 if (r)
711 goto error;
712 if (asus->bled_type == TYPE_LED)
713 r = asus_led_register(asus, &asus->bled, "asus::bluetooth",
714 METHOD_BLUETOOTH);
715 if (r)
716 goto error;
717 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
718 if (r)
719 goto error;
720 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
721 if (r)
722 goto error;
723 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
724 if (r)
725 goto error;
726 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
727 if (r)
728 goto error;
729 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
730 if (r)
731 goto error;
732 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
733 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
734 struct asus_led *led = &asus->kled;
735 struct led_classdev *cdev = &led->led;
737 led->asus = asus;
739 INIT_WORK(&led->work, asus_kled_cdev_update);
740 cdev->name = "asus::kbd_backlight";
741 cdev->brightness_set = asus_kled_cdev_set;
742 cdev->brightness_get = asus_kled_cdev_get;
743 cdev->max_brightness = 3;
744 r = led_classdev_register(&asus->platform_device->dev, cdev);
746 error:
747 if (r)
748 asus_led_exit(asus);
749 return r;
753 * Backlight device
755 static int asus_read_brightness(struct backlight_device *bd)
757 struct asus_laptop *asus = bl_get_data(bd);
758 unsigned long long value;
759 acpi_status rv;
761 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
762 NULL, &value);
763 if (ACPI_FAILURE(rv)) {
764 pr_warn("Error reading brightness\n");
765 return 0;
768 return value;
771 static int asus_set_brightness(struct backlight_device *bd, int value)
773 struct asus_laptop *asus = bl_get_data(bd);
775 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
776 pr_warn("Error changing brightness\n");
777 return -EIO;
779 return 0;
782 static int update_bl_status(struct backlight_device *bd)
784 int value = bd->props.brightness;
786 return asus_set_brightness(bd, value);
789 static const struct backlight_ops asusbl_ops = {
790 .get_brightness = asus_read_brightness,
791 .update_status = update_bl_status,
794 static int asus_backlight_notify(struct asus_laptop *asus)
796 struct backlight_device *bd = asus->backlight_device;
797 int old = bd->props.brightness;
799 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
801 return old;
804 static int asus_backlight_init(struct asus_laptop *asus)
806 struct backlight_device *bd;
807 struct backlight_properties props;
809 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
810 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
811 return 0;
813 memset(&props, 0, sizeof(struct backlight_properties));
814 props.max_brightness = 15;
815 props.type = BACKLIGHT_PLATFORM;
817 bd = backlight_device_register(ASUS_LAPTOP_FILE,
818 &asus->platform_device->dev, asus,
819 &asusbl_ops, &props);
820 if (IS_ERR(bd)) {
821 pr_err("Could not register asus backlight device\n");
822 asus->backlight_device = NULL;
823 return PTR_ERR(bd);
826 asus->backlight_device = bd;
827 bd->props.brightness = asus_read_brightness(bd);
828 bd->props.power = FB_BLANK_UNBLANK;
829 backlight_update_status(bd);
830 return 0;
833 static void asus_backlight_exit(struct asus_laptop *asus)
835 backlight_device_unregister(asus->backlight_device);
836 asus->backlight_device = NULL;
840 * Platform device handlers
844 * We write our info in page, we begin at offset off and cannot write more
845 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
846 * number of bytes written in page
848 static ssize_t infos_show(struct device *dev, struct device_attribute *attr,
849 char *page)
851 struct asus_laptop *asus = dev_get_drvdata(dev);
852 int len = 0;
853 unsigned long long temp;
854 char buf[16]; /* enough for all info */
855 acpi_status rv;
858 * We use the easy way, we don't care of off and count,
859 * so we don't set eof to 1
862 len += sprintf(page, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
863 len += sprintf(page + len, "Model reference : %s\n", asus->name);
865 * The SFUN method probably allows the original driver to get the list
866 * of features supported by a given model. For now, 0x0100 or 0x0800
867 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
868 * The significance of others is yet to be found.
870 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
871 if (!ACPI_FAILURE(rv))
872 len += sprintf(page + len, "SFUN value : %#x\n",
873 (uint) temp);
875 * The HWRS method return informations about the hardware.
876 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
877 * 0x40 for WWAN, 0x10 for WIMAX.
878 * The significance of others is yet to be found.
879 * We don't currently use this for device detection, and it
880 * takes several seconds to run on some systems.
882 rv = acpi_evaluate_integer(asus->handle, "HWRS", NULL, &temp);
883 if (!ACPI_FAILURE(rv))
884 len += sprintf(page + len, "HWRS value : %#x\n",
885 (uint) temp);
887 * Another value for userspace: the ASYM method returns 0x02 for
888 * battery low and 0x04 for battery critical, its readings tend to be
889 * more accurate than those provided by _BST.
890 * Note: since not all the laptops provide this method, errors are
891 * silently ignored.
893 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
894 if (!ACPI_FAILURE(rv))
895 len += sprintf(page + len, "ASYM value : %#x\n",
896 (uint) temp);
897 if (asus->dsdt_info) {
898 snprintf(buf, 16, "%d", asus->dsdt_info->length);
899 len += sprintf(page + len, "DSDT length : %s\n", buf);
900 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
901 len += sprintf(page + len, "DSDT checksum : %s\n", buf);
902 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
903 len += sprintf(page + len, "DSDT revision : %s\n", buf);
904 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
905 len += sprintf(page + len, "OEM id : %s\n", buf);
906 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
907 len += sprintf(page + len, "OEM table id : %s\n", buf);
908 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
909 len += sprintf(page + len, "OEM revision : 0x%s\n", buf);
910 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
911 len += sprintf(page + len, "ASL comp vendor id : %s\n", buf);
912 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
913 len += sprintf(page + len, "ASL comp revision : 0x%s\n", buf);
916 return len;
918 static DEVICE_ATTR_RO(infos);
920 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
921 const char *buf, size_t count,
922 const char *method)
924 int rv, value;
926 rv = kstrtoint(buf, 0, &value);
927 if (rv < 0)
928 return rv;
930 if (write_acpi_int(asus->handle, method, value))
931 return -ENODEV;
932 return count;
936 * LEDD display
938 static ssize_t ledd_show(struct device *dev, struct device_attribute *attr,
939 char *buf)
941 struct asus_laptop *asus = dev_get_drvdata(dev);
943 return sprintf(buf, "0x%08x\n", asus->ledd_status);
946 static ssize_t ledd_store(struct device *dev, struct device_attribute *attr,
947 const char *buf, size_t count)
949 struct asus_laptop *asus = dev_get_drvdata(dev);
950 int rv, value;
952 rv = kstrtoint(buf, 0, &value);
953 if (rv < 0)
954 return rv;
956 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
957 pr_warn("LED display write failed\n");
958 return -ENODEV;
961 asus->ledd_status = (u32) value;
962 return count;
964 static DEVICE_ATTR_RW(ledd);
967 * Wireless
969 static int asus_wireless_status(struct asus_laptop *asus, int mask)
971 unsigned long long status;
972 acpi_status rv = AE_OK;
974 if (!asus->have_rsts)
975 return (asus->wireless_status & mask) ? 1 : 0;
977 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
978 NULL, &status);
979 if (ACPI_FAILURE(rv)) {
980 pr_warn("Error reading Wireless status\n");
981 return -EINVAL;
983 return !!(status & mask);
987 * WLAN
989 static int asus_wlan_set(struct asus_laptop *asus, int status)
991 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
992 pr_warn("Error setting wlan status to %d\n", status);
993 return -EIO;
995 return 0;
998 static ssize_t wlan_show(struct device *dev, struct device_attribute *attr,
999 char *buf)
1001 struct asus_laptop *asus = dev_get_drvdata(dev);
1003 return sprintf(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
1006 static ssize_t wlan_store(struct device *dev, struct device_attribute *attr,
1007 const char *buf, size_t count)
1009 struct asus_laptop *asus = dev_get_drvdata(dev);
1011 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
1013 static DEVICE_ATTR_RW(wlan);
1016 * Bluetooth
1018 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
1020 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
1021 pr_warn("Error setting bluetooth status to %d\n", status);
1022 return -EIO;
1024 return 0;
1027 static ssize_t bluetooth_show(struct device *dev, struct device_attribute *attr,
1028 char *buf)
1030 struct asus_laptop *asus = dev_get_drvdata(dev);
1032 return sprintf(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
1035 static ssize_t bluetooth_store(struct device *dev,
1036 struct device_attribute *attr, const char *buf,
1037 size_t count)
1039 struct asus_laptop *asus = dev_get_drvdata(dev);
1041 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
1043 static DEVICE_ATTR_RW(bluetooth);
1046 * Wimax
1048 static int asus_wimax_set(struct asus_laptop *asus, int status)
1050 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
1051 pr_warn("Error setting wimax status to %d\n", status);
1052 return -EIO;
1054 return 0;
1057 static ssize_t wimax_show(struct device *dev, struct device_attribute *attr,
1058 char *buf)
1060 struct asus_laptop *asus = dev_get_drvdata(dev);
1062 return sprintf(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
1065 static ssize_t wimax_store(struct device *dev, struct device_attribute *attr,
1066 const char *buf, size_t count)
1068 struct asus_laptop *asus = dev_get_drvdata(dev);
1070 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
1072 static DEVICE_ATTR_RW(wimax);
1075 * Wwan
1077 static int asus_wwan_set(struct asus_laptop *asus, int status)
1079 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
1080 pr_warn("Error setting wwan status to %d\n", status);
1081 return -EIO;
1083 return 0;
1086 static ssize_t wwan_show(struct device *dev, struct device_attribute *attr,
1087 char *buf)
1089 struct asus_laptop *asus = dev_get_drvdata(dev);
1091 return sprintf(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
1094 static ssize_t wwan_store(struct device *dev, struct device_attribute *attr,
1095 const char *buf, size_t count)
1097 struct asus_laptop *asus = dev_get_drvdata(dev);
1099 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
1101 static DEVICE_ATTR_RW(wwan);
1104 * Display
1106 static void asus_set_display(struct asus_laptop *asus, int value)
1108 /* no sanity check needed for now */
1109 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
1110 pr_warn("Error setting display\n");
1111 return;
1115 * Experimental support for display switching. As of now: 1 should activate
1116 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
1117 * Any combination (bitwise) of these will suffice. I never actually tested 4
1118 * displays hooked up simultaneously, so be warned. See the acpi4asus README
1119 * for more info.
1121 static ssize_t display_store(struct device *dev, struct device_attribute *attr,
1122 const char *buf, size_t count)
1124 struct asus_laptop *asus = dev_get_drvdata(dev);
1125 int rv, value;
1127 rv = kstrtoint(buf, 0, &value);
1128 if (rv < 0)
1129 return rv;
1131 asus_set_display(asus, value);
1132 return count;
1134 static DEVICE_ATTR_WO(display);
1137 * Light Sens
1139 static void asus_als_switch(struct asus_laptop *asus, int value)
1141 int ret;
1143 if (asus->is_pega_lucid) {
1144 ret = asus_pega_lucid_set(asus, PEGA_ALS, value);
1145 if (!ret)
1146 ret = asus_pega_lucid_set(asus, PEGA_ALS_POWER, value);
1147 } else {
1148 ret = write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value);
1150 if (ret)
1151 pr_warn("Error setting light sensor switch\n");
1153 asus->light_switch = value;
1156 static ssize_t ls_switch_show(struct device *dev, struct device_attribute *attr,
1157 char *buf)
1159 struct asus_laptop *asus = dev_get_drvdata(dev);
1161 return sprintf(buf, "%d\n", asus->light_switch);
1164 static ssize_t ls_switch_store(struct device *dev,
1165 struct device_attribute *attr, const char *buf,
1166 size_t count)
1168 struct asus_laptop *asus = dev_get_drvdata(dev);
1169 int rv, value;
1171 rv = kstrtoint(buf, 0, &value);
1172 if (rv < 0)
1173 return rv;
1175 asus_als_switch(asus, value ? 1 : 0);
1176 return count;
1178 static DEVICE_ATTR_RW(ls_switch);
1180 static void asus_als_level(struct asus_laptop *asus, int value)
1182 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
1183 pr_warn("Error setting light sensor level\n");
1184 asus->light_level = value;
1187 static ssize_t ls_level_show(struct device *dev, struct device_attribute *attr,
1188 char *buf)
1190 struct asus_laptop *asus = dev_get_drvdata(dev);
1192 return sprintf(buf, "%d\n", asus->light_level);
1195 static ssize_t ls_level_store(struct device *dev, struct device_attribute *attr,
1196 const char *buf, size_t count)
1198 struct asus_laptop *asus = dev_get_drvdata(dev);
1199 int rv, value;
1201 rv = kstrtoint(buf, 0, &value);
1202 if (rv < 0)
1203 return rv;
1205 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1206 /* 0 <= value <= 15 */
1207 asus_als_level(asus, value);
1209 return count;
1211 static DEVICE_ATTR_RW(ls_level);
1213 static int pega_int_read(struct asus_laptop *asus, int arg, int *result)
1215 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1216 int err = write_acpi_int_ret(asus->handle, METHOD_PEGA_READ, arg,
1217 &buffer);
1218 if (!err) {
1219 union acpi_object *obj = buffer.pointer;
1220 if (obj && obj->type == ACPI_TYPE_INTEGER)
1221 *result = obj->integer.value;
1222 else
1223 err = -EIO;
1225 return err;
1228 static ssize_t ls_value_show(struct device *dev, struct device_attribute *attr,
1229 char *buf)
1231 struct asus_laptop *asus = dev_get_drvdata(dev);
1232 int err, hi, lo;
1234 err = pega_int_read(asus, PEGA_READ_ALS_H, &hi);
1235 if (!err)
1236 err = pega_int_read(asus, PEGA_READ_ALS_L, &lo);
1237 if (!err)
1238 return sprintf(buf, "%d\n", 10 * hi + lo);
1239 return err;
1241 static DEVICE_ATTR_RO(ls_value);
1244 * GPS
1246 static int asus_gps_status(struct asus_laptop *asus)
1248 unsigned long long status;
1249 acpi_status rv;
1251 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1252 NULL, &status);
1253 if (ACPI_FAILURE(rv)) {
1254 pr_warn("Error reading GPS status\n");
1255 return -ENODEV;
1257 return !!status;
1260 static int asus_gps_switch(struct asus_laptop *asus, int status)
1262 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1264 if (write_acpi_int(asus->handle, meth, 0x02))
1265 return -ENODEV;
1266 return 0;
1269 static ssize_t gps_show(struct device *dev, struct device_attribute *attr,
1270 char *buf)
1272 struct asus_laptop *asus = dev_get_drvdata(dev);
1274 return sprintf(buf, "%d\n", asus_gps_status(asus));
1277 static ssize_t gps_store(struct device *dev, struct device_attribute *attr,
1278 const char *buf, size_t count)
1280 struct asus_laptop *asus = dev_get_drvdata(dev);
1281 int rv, value;
1282 int ret;
1284 rv = kstrtoint(buf, 0, &value);
1285 if (rv < 0)
1286 return rv;
1287 ret = asus_gps_switch(asus, !!value);
1288 if (ret)
1289 return ret;
1290 rfkill_set_sw_state(asus->gps.rfkill, !value);
1291 return count;
1293 static DEVICE_ATTR_RW(gps);
1296 * rfkill
1298 static int asus_gps_rfkill_set(void *data, bool blocked)
1300 struct asus_laptop *asus = data;
1302 return asus_gps_switch(asus, !blocked);
1305 static const struct rfkill_ops asus_gps_rfkill_ops = {
1306 .set_block = asus_gps_rfkill_set,
1309 static int asus_rfkill_set(void *data, bool blocked)
1311 struct asus_rfkill *rfk = data;
1312 struct asus_laptop *asus = rfk->asus;
1314 if (rfk->control_id == WL_RSTS)
1315 return asus_wlan_set(asus, !blocked);
1316 else if (rfk->control_id == BT_RSTS)
1317 return asus_bluetooth_set(asus, !blocked);
1318 else if (rfk->control_id == WM_RSTS)
1319 return asus_wimax_set(asus, !blocked);
1320 else if (rfk->control_id == WW_RSTS)
1321 return asus_wwan_set(asus, !blocked);
1323 return -EINVAL;
1326 static const struct rfkill_ops asus_rfkill_ops = {
1327 .set_block = asus_rfkill_set,
1330 static void asus_rfkill_terminate(struct asus_rfkill *rfk)
1332 if (!rfk->rfkill)
1333 return ;
1335 rfkill_unregister(rfk->rfkill);
1336 rfkill_destroy(rfk->rfkill);
1337 rfk->rfkill = NULL;
1340 static void asus_rfkill_exit(struct asus_laptop *asus)
1342 asus_rfkill_terminate(&asus->wwan);
1343 asus_rfkill_terminate(&asus->bluetooth);
1344 asus_rfkill_terminate(&asus->wlan);
1345 asus_rfkill_terminate(&asus->gps);
1348 static int asus_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
1349 const char *name, int control_id, int type,
1350 const struct rfkill_ops *ops)
1352 int result;
1354 rfk->control_id = control_id;
1355 rfk->asus = asus;
1356 rfk->rfkill = rfkill_alloc(name, &asus->platform_device->dev,
1357 type, ops, rfk);
1358 if (!rfk->rfkill)
1359 return -EINVAL;
1361 result = rfkill_register(rfk->rfkill);
1362 if (result) {
1363 rfkill_destroy(rfk->rfkill);
1364 rfk->rfkill = NULL;
1367 return result;
1370 static int asus_rfkill_init(struct asus_laptop *asus)
1372 int result = 0;
1374 if (asus->is_pega_lucid)
1375 return -ENODEV;
1377 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1378 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1379 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1380 result = asus_rfkill_setup(asus, &asus->gps, "asus-gps",
1381 -1, RFKILL_TYPE_GPS,
1382 &asus_gps_rfkill_ops);
1383 if (result)
1384 goto exit;
1387 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL) &&
1388 asus->wled_type == TYPE_RFKILL)
1389 result = asus_rfkill_setup(asus, &asus->wlan, "asus-wlan",
1390 WL_RSTS, RFKILL_TYPE_WLAN,
1391 &asus_rfkill_ops);
1392 if (result)
1393 goto exit;
1395 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL) &&
1396 asus->bled_type == TYPE_RFKILL)
1397 result = asus_rfkill_setup(asus, &asus->bluetooth,
1398 "asus-bluetooth", BT_RSTS,
1399 RFKILL_TYPE_BLUETOOTH,
1400 &asus_rfkill_ops);
1401 if (result)
1402 goto exit;
1404 if (!acpi_check_handle(asus->handle, METHOD_WWAN, NULL))
1405 result = asus_rfkill_setup(asus, &asus->wwan, "asus-wwan",
1406 WW_RSTS, RFKILL_TYPE_WWAN,
1407 &asus_rfkill_ops);
1408 if (result)
1409 goto exit;
1411 if (!acpi_check_handle(asus->handle, METHOD_WIMAX, NULL))
1412 result = asus_rfkill_setup(asus, &asus->wimax, "asus-wimax",
1413 WM_RSTS, RFKILL_TYPE_WIMAX,
1414 &asus_rfkill_ops);
1415 if (result)
1416 goto exit;
1418 exit:
1419 if (result)
1420 asus_rfkill_exit(asus);
1422 return result;
1425 static int pega_rfkill_set(void *data, bool blocked)
1427 struct asus_rfkill *rfk = data;
1429 int ret = asus_pega_lucid_set(rfk->asus, rfk->control_id, !blocked);
1430 return ret;
1433 static const struct rfkill_ops pega_rfkill_ops = {
1434 .set_block = pega_rfkill_set,
1437 static int pega_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
1438 const char *name, int controlid, int rfkill_type)
1440 return asus_rfkill_setup(asus, rfk, name, controlid, rfkill_type,
1441 &pega_rfkill_ops);
1444 static int pega_rfkill_init(struct asus_laptop *asus)
1446 int ret = 0;
1448 if(!asus->is_pega_lucid)
1449 return -ENODEV;
1451 ret = pega_rfkill_setup(asus, &asus->wlan, "pega-wlan",
1452 PEGA_WLAN, RFKILL_TYPE_WLAN);
1453 if(ret)
1454 goto exit;
1456 ret = pega_rfkill_setup(asus, &asus->bluetooth, "pega-bt",
1457 PEGA_BLUETOOTH, RFKILL_TYPE_BLUETOOTH);
1458 if(ret)
1459 goto exit;
1461 ret = pega_rfkill_setup(asus, &asus->wwan, "pega-wwan",
1462 PEGA_WWAN, RFKILL_TYPE_WWAN);
1464 exit:
1465 if (ret)
1466 asus_rfkill_exit(asus);
1468 return ret;
1472 * Input device (i.e. hotkeys)
1474 static void asus_input_notify(struct asus_laptop *asus, int event)
1476 if (!asus->inputdev)
1477 return ;
1478 if (!sparse_keymap_report_event(asus->inputdev, event, 1, true))
1479 pr_info("Unknown key %x pressed\n", event);
1482 static int asus_input_init(struct asus_laptop *asus)
1484 struct input_dev *input;
1485 int error;
1487 input = input_allocate_device();
1488 if (!input)
1489 return -ENOMEM;
1491 input->name = "Asus Laptop extra buttons";
1492 input->phys = ASUS_LAPTOP_FILE "/input0";
1493 input->id.bustype = BUS_HOST;
1494 input->dev.parent = &asus->platform_device->dev;
1496 error = sparse_keymap_setup(input, asus_keymap, NULL);
1497 if (error) {
1498 pr_err("Unable to setup input device keymap\n");
1499 goto err_free_dev;
1501 error = input_register_device(input);
1502 if (error) {
1503 pr_warn("Unable to register input device\n");
1504 goto err_free_dev;
1507 asus->inputdev = input;
1508 return 0;
1510 err_free_dev:
1511 input_free_device(input);
1512 return error;
1515 static void asus_input_exit(struct asus_laptop *asus)
1517 if (asus->inputdev)
1518 input_unregister_device(asus->inputdev);
1519 asus->inputdev = NULL;
1523 * ACPI driver
1525 static void asus_acpi_notify(struct acpi_device *device, u32 event)
1527 struct asus_laptop *asus = acpi_driver_data(device);
1528 u16 count;
1530 /* TODO Find a better way to handle events count. */
1531 count = asus->event_count[event % 128]++;
1532 acpi_bus_generate_netlink_event(asus->device->pnp.device_class,
1533 dev_name(&asus->device->dev), event,
1534 count);
1536 if (event >= ATKD_BRNUP_MIN && event <= ATKD_BRNUP_MAX)
1537 event = ATKD_BRNUP;
1538 else if (event >= ATKD_BRNDOWN_MIN &&
1539 event <= ATKD_BRNDOWN_MAX)
1540 event = ATKD_BRNDOWN;
1542 /* Brightness events are special */
1543 if (event == ATKD_BRNDOWN || event == ATKD_BRNUP) {
1544 if (asus->backlight_device != NULL) {
1545 /* Update the backlight device. */
1546 asus_backlight_notify(asus);
1547 return ;
1551 /* Accelerometer "coarse orientation change" event */
1552 if (asus->pega_accel_poll && event == 0xEA) {
1553 kobject_uevent(&asus->pega_accel_poll->dev.kobj, KOBJ_CHANGE);
1554 return ;
1557 asus_input_notify(asus, event);
1560 static struct attribute *asus_attributes[] = {
1561 &dev_attr_infos.attr,
1562 &dev_attr_wlan.attr,
1563 &dev_attr_bluetooth.attr,
1564 &dev_attr_wimax.attr,
1565 &dev_attr_wwan.attr,
1566 &dev_attr_display.attr,
1567 &dev_attr_ledd.attr,
1568 &dev_attr_ls_value.attr,
1569 &dev_attr_ls_level.attr,
1570 &dev_attr_ls_switch.attr,
1571 &dev_attr_gps.attr,
1572 NULL
1575 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1576 struct attribute *attr,
1577 int idx)
1579 struct device *dev = container_of(kobj, struct device, kobj);
1580 struct asus_laptop *asus = dev_get_drvdata(dev);
1581 acpi_handle handle = asus->handle;
1582 bool supported;
1584 if (asus->is_pega_lucid) {
1585 /* no ls_level interface on the Lucid */
1586 if (attr == &dev_attr_ls_switch.attr)
1587 supported = true;
1588 else if (attr == &dev_attr_ls_level.attr)
1589 supported = false;
1590 else
1591 goto normal;
1593 return supported ? attr->mode : 0;
1596 normal:
1597 if (attr == &dev_attr_wlan.attr) {
1598 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1600 } else if (attr == &dev_attr_bluetooth.attr) {
1601 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1603 } else if (attr == &dev_attr_display.attr) {
1604 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1606 } else if (attr == &dev_attr_wimax.attr) {
1607 supported =
1608 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1610 } else if (attr == &dev_attr_wwan.attr) {
1611 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1613 } else if (attr == &dev_attr_ledd.attr) {
1614 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1616 } else if (attr == &dev_attr_ls_switch.attr ||
1617 attr == &dev_attr_ls_level.attr) {
1618 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1619 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1620 } else if (attr == &dev_attr_ls_value.attr) {
1621 supported = asus->is_pega_lucid;
1622 } else if (attr == &dev_attr_gps.attr) {
1623 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1624 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1625 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1626 } else {
1627 supported = true;
1630 return supported ? attr->mode : 0;
1634 static const struct attribute_group asus_attr_group = {
1635 .is_visible = asus_sysfs_is_visible,
1636 .attrs = asus_attributes,
1639 static int asus_platform_init(struct asus_laptop *asus)
1641 int result;
1643 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, -1);
1644 if (!asus->platform_device)
1645 return -ENOMEM;
1646 platform_set_drvdata(asus->platform_device, asus);
1648 result = platform_device_add(asus->platform_device);
1649 if (result)
1650 goto fail_platform_device;
1652 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1653 &asus_attr_group);
1654 if (result)
1655 goto fail_sysfs;
1657 return 0;
1659 fail_sysfs:
1660 platform_device_del(asus->platform_device);
1661 fail_platform_device:
1662 platform_device_put(asus->platform_device);
1663 return result;
1666 static void asus_platform_exit(struct asus_laptop *asus)
1668 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
1669 platform_device_unregister(asus->platform_device);
1672 static struct platform_driver platform_driver = {
1673 .driver = {
1674 .name = ASUS_LAPTOP_FILE,
1679 * This function is used to initialize the context with right values. In this
1680 * method, we can make all the detection we want, and modify the asus_laptop
1681 * struct
1683 static int asus_laptop_get_info(struct asus_laptop *asus)
1685 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1686 union acpi_object *model = NULL;
1687 unsigned long long bsts_result;
1688 char *string = NULL;
1689 acpi_status status;
1692 * Get DSDT headers early enough to allow for differentiating between
1693 * models, but late enough to allow acpi_bus_register_driver() to fail
1694 * before doing anything ACPI-specific. Should we encounter a machine,
1695 * which needs special handling (i.e. its hotkey device has a different
1696 * HID), this bit will be moved.
1698 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1699 if (ACPI_FAILURE(status))
1700 pr_warn("Couldn't get the DSDT table header\n");
1702 /* We have to write 0 on init this far for all ASUS models */
1703 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1704 pr_err("Hotkey initialization failed\n");
1705 return -ENODEV;
1708 /* This needs to be called for some laptops to init properly */
1709 status =
1710 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1711 if (ACPI_FAILURE(status))
1712 pr_warn("Error calling BSTS\n");
1713 else if (bsts_result)
1714 pr_notice("BSTS called, 0x%02x returned\n",
1715 (uint) bsts_result);
1717 /* This too ... */
1718 if (write_acpi_int(asus->handle, "CWAP", wapf))
1719 pr_err("Error calling CWAP(%d)\n", wapf);
1721 * Try to match the object returned by INIT to the specific model.
1722 * Handle every possible object (or the lack of thereof) the DSDT
1723 * writers might throw at us. When in trouble, we pass NULL to
1724 * asus_model_match() and try something completely different.
1726 if (buffer.pointer) {
1727 model = buffer.pointer;
1728 switch (model->type) {
1729 case ACPI_TYPE_STRING:
1730 string = model->string.pointer;
1731 break;
1732 case ACPI_TYPE_BUFFER:
1733 string = model->buffer.pointer;
1734 break;
1735 default:
1736 string = "";
1737 break;
1740 asus->name = kstrdup(string, GFP_KERNEL);
1741 if (!asus->name) {
1742 kfree(buffer.pointer);
1743 return -ENOMEM;
1746 if (string)
1747 pr_notice(" %s model detected\n", string);
1749 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1750 asus->have_rsts = true;
1752 kfree(model);
1754 return AE_OK;
1757 static int asus_acpi_init(struct asus_laptop *asus)
1759 int result = 0;
1761 result = acpi_bus_get_status(asus->device);
1762 if (result)
1763 return result;
1764 if (!asus->device->status.present) {
1765 pr_err("Hotkey device not present, aborting\n");
1766 return -ENODEV;
1769 result = asus_laptop_get_info(asus);
1770 if (result)
1771 return result;
1773 if (!strcmp(bled_type, "led"))
1774 asus->bled_type = TYPE_LED;
1775 else if (!strcmp(bled_type, "rfkill"))
1776 asus->bled_type = TYPE_RFKILL;
1778 if (!strcmp(wled_type, "led"))
1779 asus->wled_type = TYPE_LED;
1780 else if (!strcmp(wled_type, "rfkill"))
1781 asus->wled_type = TYPE_RFKILL;
1783 if (bluetooth_status >= 0)
1784 asus_bluetooth_set(asus, !!bluetooth_status);
1786 if (wlan_status >= 0)
1787 asus_wlan_set(asus, !!wlan_status);
1789 if (wimax_status >= 0)
1790 asus_wimax_set(asus, !!wimax_status);
1792 if (wwan_status >= 0)
1793 asus_wwan_set(asus, !!wwan_status);
1795 /* Keyboard Backlight is on by default */
1796 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1797 asus_kled_set(asus, 1);
1799 /* LED display is off by default */
1800 asus->ledd_status = 0xFFF;
1802 /* Set initial values of light sensor and level */
1803 asus->light_switch = !!als_status;
1804 asus->light_level = 5; /* level 5 for sensor sensitivity */
1806 if (asus->is_pega_lucid) {
1807 asus_als_switch(asus, asus->light_switch);
1808 } else if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1809 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1810 asus_als_switch(asus, asus->light_switch);
1811 asus_als_level(asus, asus->light_level);
1814 return result;
1817 static void asus_dmi_check(void)
1819 const char *model;
1821 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1822 if (!model)
1823 return;
1825 /* On L1400B WLED control the sound card, don't mess with it ... */
1826 if (strncmp(model, "L1400B", 6) == 0) {
1827 wlan_status = -1;
1831 static bool asus_device_present;
1833 static int asus_acpi_add(struct acpi_device *device)
1835 struct asus_laptop *asus;
1836 int result;
1838 pr_notice("Asus Laptop Support version %s\n",
1839 ASUS_LAPTOP_VERSION);
1840 asus = kzalloc(sizeof(struct asus_laptop), GFP_KERNEL);
1841 if (!asus)
1842 return -ENOMEM;
1843 asus->handle = device->handle;
1844 strcpy(acpi_device_name(device), ASUS_LAPTOP_DEVICE_NAME);
1845 strcpy(acpi_device_class(device), ASUS_LAPTOP_CLASS);
1846 device->driver_data = asus;
1847 asus->device = device;
1849 asus_dmi_check();
1851 result = asus_acpi_init(asus);
1852 if (result)
1853 goto fail_platform;
1856 * Need platform type detection first, then the platform
1857 * device. It is used as a parent for the sub-devices below.
1859 asus->is_pega_lucid = asus_check_pega_lucid(asus);
1860 result = asus_platform_init(asus);
1861 if (result)
1862 goto fail_platform;
1864 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1865 result = asus_backlight_init(asus);
1866 if (result)
1867 goto fail_backlight;
1870 result = asus_input_init(asus);
1871 if (result)
1872 goto fail_input;
1874 result = asus_led_init(asus);
1875 if (result)
1876 goto fail_led;
1878 result = asus_rfkill_init(asus);
1879 if (result && result != -ENODEV)
1880 goto fail_rfkill;
1882 result = pega_accel_init(asus);
1883 if (result && result != -ENODEV)
1884 goto fail_pega_accel;
1886 result = pega_rfkill_init(asus);
1887 if (result && result != -ENODEV)
1888 goto fail_pega_rfkill;
1890 asus_device_present = true;
1891 return 0;
1893 fail_pega_rfkill:
1894 pega_accel_exit(asus);
1895 fail_pega_accel:
1896 asus_rfkill_exit(asus);
1897 fail_rfkill:
1898 asus_led_exit(asus);
1899 fail_led:
1900 asus_input_exit(asus);
1901 fail_input:
1902 asus_backlight_exit(asus);
1903 fail_backlight:
1904 asus_platform_exit(asus);
1905 fail_platform:
1906 kfree(asus);
1908 return result;
1911 static int asus_acpi_remove(struct acpi_device *device)
1913 struct asus_laptop *asus = acpi_driver_data(device);
1915 asus_backlight_exit(asus);
1916 asus_rfkill_exit(asus);
1917 asus_led_exit(asus);
1918 asus_input_exit(asus);
1919 pega_accel_exit(asus);
1920 asus_platform_exit(asus);
1922 kfree(asus->name);
1923 kfree(asus);
1924 return 0;
1927 static const struct acpi_device_id asus_device_ids[] = {
1928 {"ATK0100", 0},
1929 {"ATK0101", 0},
1930 {"", 0},
1932 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1934 static struct acpi_driver asus_acpi_driver = {
1935 .name = ASUS_LAPTOP_NAME,
1936 .class = ASUS_LAPTOP_CLASS,
1937 .owner = THIS_MODULE,
1938 .ids = asus_device_ids,
1939 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1940 .ops = {
1941 .add = asus_acpi_add,
1942 .remove = asus_acpi_remove,
1943 .notify = asus_acpi_notify,
1947 static int __init asus_laptop_init(void)
1949 int result;
1951 result = platform_driver_register(&platform_driver);
1952 if (result < 0)
1953 return result;
1955 result = acpi_bus_register_driver(&asus_acpi_driver);
1956 if (result < 0)
1957 goto fail_acpi_driver;
1958 if (!asus_device_present) {
1959 result = -ENODEV;
1960 goto fail_no_device;
1962 return 0;
1964 fail_no_device:
1965 acpi_bus_unregister_driver(&asus_acpi_driver);
1966 fail_acpi_driver:
1967 platform_driver_unregister(&platform_driver);
1968 return result;
1971 static void __exit asus_laptop_exit(void)
1973 acpi_bus_unregister_driver(&asus_acpi_driver);
1974 platform_driver_unregister(&platform_driver);
1977 module_init(asus_laptop_init);
1978 module_exit(asus_laptop_exit);