1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * asus-laptop.c - Asus Laptop Support
4 *
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
8 *
9 * The development page for this driver is located at
10 * http://sourceforge.net/projects/acpi4asus/
11 *
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
20 */
21
22 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23
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/leds.h>
32 #include <linux/platform_device.h>
33 #include <linux/uaccess.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/rfkill.h>
37 #include <linux/slab.h>
38 #include <linux/dmi.h>
39 #include <linux/acpi.h>
40 #include <acpi/video.h>
41
42 #define ASUS_LAPTOP_VERSION "0.42"
43
44 #define ASUS_LAPTOP_NAME "Asus Laptop Support"
45 #define ASUS_LAPTOP_FILE KBUILD_MODNAME
46 #define ASUS_LAPTOP_PREFIX "\\_SB.ATKD."
47
48 MODULE_AUTHOR("Julien Lerouge, Karol Kozimor, Corentin Chary");
49 MODULE_DESCRIPTION(ASUS_LAPTOP_NAME);
50 MODULE_LICENSE("GPL");
51
52 /*
53 * WAPF defines the behavior of the Fn+Fx wlan key
54 * The significance of values is yet to be found, but
55 * most of the time:
56 * Bit | Bluetooth | WLAN
57 * 0 | Hardware | Hardware
58 * 1 | Hardware | Software
59 * 4 | Software | Software
60 */
61 static uint wapf = 1;
62 module_param(wapf, uint, 0444);
63 MODULE_PARM_DESC(wapf, "WAPF value");
64
65 static char *wled_type = "unknown";
66 static char *bled_type = "unknown";
67
68 module_param(wled_type, charp, 0444);
69 MODULE_PARM_DESC(wled_type, "Set the wled type on boot "
70 "(unknown, led or rfkill). "
71 "default is unknown");
72
73 module_param(bled_type, charp, 0444);
74 MODULE_PARM_DESC(bled_type, "Set the bled type on boot "
75 "(unknown, led or rfkill). "
76 "default is unknown");
77
78 static int wlan_status = 1;
79 static int bluetooth_status = 1;
80 static int wimax_status = -1;
81 static int wwan_status = -1;
82 static int als_status;
83
84 module_param(wlan_status, int, 0444);
85 MODULE_PARM_DESC(wlan_status, "Set the wireless status on boot "
86 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
87 "default is -1");
88
89 module_param(bluetooth_status, int, 0444);
90 MODULE_PARM_DESC(bluetooth_status, "Set the wireless status on boot "
91 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
92 "default is -1");
93
94 module_param(wimax_status, int, 0444);
95 MODULE_PARM_DESC(wimax_status, "Set the wireless status on boot "
96 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
97 "default is -1");
98
99 module_param(wwan_status, int, 0444);
100 MODULE_PARM_DESC(wwan_status, "Set the wireless status on boot "
101 "(0 = disabled, 1 = enabled, -1 = don't do anything). "
102 "default is -1");
103
104 module_param(als_status, int, 0444);
105 MODULE_PARM_DESC(als_status, "Set the ALS status on boot "
106 "(0 = disabled, 1 = enabled). "
107 "default is 0");
108
109 /*
110 * Some events we use, same for all Asus
111 */
112 #define ATKD_BRNUP_MIN 0x10
113 #define ATKD_BRNUP_MAX 0x1f
114 #define ATKD_BRNDOWN_MIN 0x20
115 #define ATKD_BRNDOWN_MAX 0x2f
116 #define ATKD_BRNDOWN 0x20
117 #define ATKD_BRNUP 0x2f
118 #define ATKD_LCD_ON 0x33
119 #define ATKD_LCD_OFF 0x34
120
121 /*
122 * Known bits returned by \_SB.ATKD.HWRS
123 */
124 #define WL_HWRS 0x80
125 #define BT_HWRS 0x100
126
127 /*
128 * Flags for hotk status
129 * WL_ON and BT_ON are also used for wireless_status()
130 */
131 #define WL_RSTS 0x01 /* internal Wifi */
132 #define BT_RSTS 0x02 /* internal Bluetooth */
133 #define WM_RSTS 0x08 /* internal wimax */
134 #define WW_RSTS 0x20 /* internal wwan */
135
136 /* WLED and BLED type */
137 #define TYPE_UNKNOWN 0
138 #define TYPE_LED 1
139 #define TYPE_RFKILL 2
140
141 /* LED */
142 #define METHOD_MLED "MLED"
143 #define METHOD_TLED "TLED"
144 #define METHOD_RLED "RLED" /* W1JC */
145 #define METHOD_PLED "PLED" /* A7J */
146 #define METHOD_GLED "GLED" /* G1, G2 (probably) */
147
148 /* LEDD */
149 #define METHOD_LEDD "SLCM"
150
151 /*
152 * Bluetooth and WLAN
153 * WLED and BLED are not handled like other XLED, because in some dsdt
154 * they also control the WLAN/Bluetooth device.
155 */
156 #define METHOD_WLAN "WLED"
157 #define METHOD_BLUETOOTH "BLED"
158
159 /* WWAN and WIMAX */
160 #define METHOD_WWAN "GSMC"
161 #define METHOD_WIMAX "WMXC"
162
163 #define METHOD_WL_STATUS "RSTS"
164
165 /* Brightness */
166 #define METHOD_BRIGHTNESS_SET "SPLV"
167 #define METHOD_BRIGHTNESS_GET "GPLV"
168
169 /* Display */
170 #define METHOD_SWITCH_DISPLAY "SDSP"
171
172 #define METHOD_ALS_CONTROL "ALSC" /* Z71A Z71V */
173 #define METHOD_ALS_LEVEL "ALSL" /* Z71A Z71V */
174
175 /* GPS */
176 /* R2H use different handle for GPS on/off */
177 #define METHOD_GPS_ON "SDON"
178 #define METHOD_GPS_OFF "SDOF"
179 #define METHOD_GPS_STATUS "GPST"
180
181 /* Keyboard light */
182 #define METHOD_KBD_LIGHT_SET "SLKB"
183 #define METHOD_KBD_LIGHT_GET "GLKB"
184
185 /* For Pegatron Lucid tablet */
186 #define DEVICE_NAME_PEGA "Lucid"
187
188 #define METHOD_PEGA_ENABLE "ENPR"
189 #define METHOD_PEGA_DISABLE "DAPR"
190 #define PEGA_WLAN 0x00
191 #define PEGA_BLUETOOTH 0x01
192 #define PEGA_WWAN 0x02
193 #define PEGA_ALS 0x04
194 #define PEGA_ALS_POWER 0x05
195
196 #define METHOD_PEGA_READ "RDLN"
197 #define PEGA_READ_ALS_H 0x02
198 #define PEGA_READ_ALS_L 0x03
199
200 #define PEGA_ACCEL_NAME "pega_accel"
201 #define PEGA_ACCEL_DESC "Pegatron Lucid Tablet Accelerometer"
202 #define METHOD_XLRX "XLRX"
203 #define METHOD_XLRY "XLRY"
204 #define METHOD_XLRZ "XLRZ"
205 #define PEGA_ACC_CLAMP 512 /* 1G accel is reported as ~256, so clamp to 2G */
206 #define PEGA_ACC_RETRIES 3
207
208 /*
209 * Define a specific led structure to keep the main structure clean
210 */
211 struct asus_led {
212 int wk;
213 struct work_struct work;
214 struct led_classdev led;
215 struct asus_laptop *asus;
216 const char *method;
217 };
218
219 /*
220 * Same thing for rfkill
221 */
222 struct asus_rfkill {
223 /* type of control. Maps to PEGA_* values or *_RSTS */
224 int control_id;
225 struct rfkill *rfkill;
226 struct asus_laptop *asus;
227 };
228
229 /*
230 * This is the main structure, we can use it to store anything interesting
231 * about the hotk device
232 */
233 struct asus_laptop {
234 char *name; /* laptop name */
235
236 struct acpi_table_header *dsdt_info;
237 struct platform_device *platform_device;
238 struct acpi_device *device; /* the device we are in */
239 struct backlight_device *backlight_device;
240
241 struct input_dev *inputdev;
242 struct key_entry *keymap;
243 struct input_dev *pega_accel_poll;
244
245 struct asus_led wled;
246 struct asus_led bled;
247 struct asus_led mled;
248 struct asus_led tled;
249 struct asus_led rled;
250 struct asus_led pled;
251 struct asus_led gled;
252 struct asus_led kled;
253 struct workqueue_struct *led_workqueue;
254
255 int wled_type;
256 int bled_type;
257 int wireless_status;
258 bool have_rsts;
259 bool is_pega_lucid;
260 bool pega_acc_live;
261 int pega_acc_x;
262 int pega_acc_y;
263 int pega_acc_z;
264
265 struct asus_rfkill wlan;
266 struct asus_rfkill bluetooth;
267 struct asus_rfkill wwan;
268 struct asus_rfkill wimax;
269 struct asus_rfkill gps;
270
271 acpi_handle handle; /* the handle of the hotk device */
272 u32 ledd_status; /* status of the LED display */
273 u8 light_level; /* light sensor level */
274 u8 light_switch; /* light sensor switch value */
275 u16 event_count[128]; /* count for each event TODO make this better */
276 };
277
278 static const struct key_entry asus_keymap[] = {
279 /* Lenovo SL Specific keycodes */
280 {KE_KEY, 0x02, { KEY_SCREENLOCK } },
281 {KE_KEY, 0x05, { KEY_WLAN } },
282 {KE_KEY, 0x08, { KEY_F13 } },
283 {KE_KEY, 0x09, { KEY_PROG2 } }, /* Dock */
284 {KE_KEY, 0x17, { KEY_ZOOM } },
285 {KE_KEY, 0x1f, { KEY_BATTERY } },
286 /* End of Lenovo SL Specific keycodes */
287 {KE_KEY, ATKD_BRNDOWN, { KEY_BRIGHTNESSDOWN } },
288 {KE_KEY, ATKD_BRNUP, { KEY_BRIGHTNESSUP } },
289 {KE_KEY, 0x30, { KEY_VOLUMEUP } },
290 {KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
291 {KE_KEY, 0x32, { KEY_MUTE } },
292 {KE_KEY, 0x33, { KEY_DISPLAYTOGGLE } }, /* LCD on */
293 {KE_KEY, 0x34, { KEY_DISPLAY_OFF } }, /* LCD off */
294 {KE_KEY, 0x40, { KEY_PREVIOUSSONG } },
295 {KE_KEY, 0x41, { KEY_NEXTSONG } },
296 {KE_KEY, 0x43, { KEY_STOPCD } }, /* Stop/Eject */
297 {KE_KEY, 0x45, { KEY_PLAYPAUSE } },
298 {KE_KEY, 0x4c, { KEY_MEDIA } }, /* WMP Key */
299 {KE_KEY, 0x50, { KEY_EMAIL } },
300 {KE_KEY, 0x51, { KEY_WWW } },
301 {KE_KEY, 0x55, { KEY_CALC } },
302 {KE_IGNORE, 0x57, }, /* Battery mode */
303 {KE_IGNORE, 0x58, }, /* AC mode */
304 {KE_KEY, 0x5C, { KEY_SCREENLOCK } }, /* Screenlock */
305 {KE_KEY, 0x5D, { KEY_WLAN } }, /* WLAN Toggle */
306 {KE_KEY, 0x5E, { KEY_WLAN } }, /* WLAN Enable */
307 {KE_KEY, 0x5F, { KEY_WLAN } }, /* WLAN Disable */
308 {KE_KEY, 0x60, { KEY_TOUCHPAD_ON } },
309 {KE_KEY, 0x61, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD only */
310 {KE_KEY, 0x62, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT only */
311 {KE_KEY, 0x63, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT */
312 {KE_KEY, 0x64, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV */
313 {KE_KEY, 0x65, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV */
314 {KE_KEY, 0x66, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV */
315 {KE_KEY, 0x67, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV */
316 {KE_KEY, 0x6A, { KEY_TOUCHPAD_TOGGLE } }, /* Lock Touchpad Fn + F9 */
317 {KE_KEY, 0x6B, { KEY_TOUCHPAD_TOGGLE } }, /* Lock Touchpad */
318 {KE_KEY, 0x6C, { KEY_SLEEP } }, /* Suspend */
319 {KE_KEY, 0x6D, { KEY_SLEEP } }, /* Hibernate */
320 {KE_IGNORE, 0x6E, }, /* Low Battery notification */
321 {KE_KEY, 0x7D, { KEY_BLUETOOTH } }, /* Bluetooth Enable */
322 {KE_KEY, 0x7E, { KEY_BLUETOOTH } }, /* Bluetooth Disable */
323 {KE_KEY, 0x82, { KEY_CAMERA } },
324 {KE_KEY, 0x88, { KEY_RFKILL } }, /* Radio Toggle Key */
325 {KE_KEY, 0x8A, { KEY_PROG1 } }, /* Color enhancement mode */
326 {KE_KEY, 0x8C, { KEY_SWITCHVIDEOMODE } }, /* SDSP DVI only */
327 {KE_KEY, 0x8D, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + DVI */
328 {KE_KEY, 0x8E, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + DVI */
329 {KE_KEY, 0x8F, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV + DVI */
330 {KE_KEY, 0x90, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + DVI */
331 {KE_KEY, 0x91, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV + DVI */
332 {KE_KEY, 0x92, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV + DVI */
333 {KE_KEY, 0x93, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV + DVI */
334 {KE_KEY, 0x95, { KEY_MEDIA } },
335 {KE_KEY, 0x99, { KEY_PHONE } },
336 {KE_KEY, 0xA0, { KEY_SWITCHVIDEOMODE } }, /* SDSP HDMI only */
337 {KE_KEY, 0xA1, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + HDMI */
338 {KE_KEY, 0xA2, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + HDMI */
339 {KE_KEY, 0xA3, { KEY_SWITCHVIDEOMODE } }, /* SDSP TV + HDMI */
340 {KE_KEY, 0xA4, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + HDMI */
341 {KE_KEY, 0xA5, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + TV + HDMI */
342 {KE_KEY, 0xA6, { KEY_SWITCHVIDEOMODE } }, /* SDSP CRT + TV + HDMI */
343 {KE_KEY, 0xA7, { KEY_SWITCHVIDEOMODE } }, /* SDSP LCD + CRT + TV + HDMI */
344 {KE_KEY, 0xB5, { KEY_CALC } },
345 {KE_KEY, 0xC4, { KEY_KBDILLUMUP } },
346 {KE_KEY, 0xC5, { KEY_KBDILLUMDOWN } },
347 {KE_END, 0},
348 };
349
350
351 /*
352 * This function evaluates an ACPI method, given an int as parameter, the
353 * method is searched within the scope of the handle, can be NULL. The output
354 * of the method is written is output, which can also be NULL
355 *
356 * returns 0 if write is successful, -1 else.
357 */
write_acpi_int_ret(acpi_handle handle,const char * method,int val,struct acpi_buffer * output)358 static int write_acpi_int_ret(acpi_handle handle, const char *method, int val,
359 struct acpi_buffer *output)
360 {
361 struct acpi_object_list params; /* list of input parameters (an int) */
362 union acpi_object in_obj; /* the only param we use */
363 acpi_status status;
364
365 if (!handle)
366 return -1;
367
368 params.count = 1;
369 params.pointer = &in_obj;
370 in_obj.type = ACPI_TYPE_INTEGER;
371 in_obj.integer.value = val;
372
373 status = acpi_evaluate_object(handle, (char *)method, ¶ms, output);
374 if (status == AE_OK)
375 return 0;
376 else
377 return -1;
378 }
379
write_acpi_int(acpi_handle handle,const char * method,int val)380 static int write_acpi_int(acpi_handle handle, const char *method, int val)
381 {
382 return write_acpi_int_ret(handle, method, val, NULL);
383 }
384
acpi_check_handle(acpi_handle handle,const char * method,acpi_handle * ret)385 static int acpi_check_handle(acpi_handle handle, const char *method,
386 acpi_handle *ret)
387 {
388 acpi_status status;
389
390 if (method == NULL)
391 return -ENODEV;
392
393 if (ret)
394 status = acpi_get_handle(handle, (char *)method,
395 ret);
396 else {
397 acpi_handle dummy;
398
399 status = acpi_get_handle(handle, (char *)method,
400 &dummy);
401 }
402
403 if (status != AE_OK) {
404 if (ret)
405 pr_warn("Error finding %s\n", method);
406 return -ENODEV;
407 }
408 return 0;
409 }
410
asus_check_pega_lucid(struct asus_laptop * asus)411 static bool asus_check_pega_lucid(struct asus_laptop *asus)
412 {
413 return !strcmp(asus->name, DEVICE_NAME_PEGA) &&
414 !acpi_check_handle(asus->handle, METHOD_PEGA_ENABLE, NULL) &&
415 !acpi_check_handle(asus->handle, METHOD_PEGA_DISABLE, NULL) &&
416 !acpi_check_handle(asus->handle, METHOD_PEGA_READ, NULL);
417 }
418
asus_pega_lucid_set(struct asus_laptop * asus,int unit,bool enable)419 static int asus_pega_lucid_set(struct asus_laptop *asus, int unit, bool enable)
420 {
421 char *method = enable ? METHOD_PEGA_ENABLE : METHOD_PEGA_DISABLE;
422 return write_acpi_int(asus->handle, method, unit);
423 }
424
pega_acc_axis(struct asus_laptop * asus,int curr,char * method)425 static int pega_acc_axis(struct asus_laptop *asus, int curr, char *method)
426 {
427 unsigned long long val = (unsigned long long)curr;
428 acpi_status status;
429 int i, delta;
430
431 for (i = 0; i < PEGA_ACC_RETRIES; i++) {
432 status = acpi_evaluate_integer(asus->handle, method, NULL, &val);
433 if (ACPI_FAILURE(status))
434 continue;
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;
444 }
445 return clamp_val((short)val, -PEGA_ACC_CLAMP, PEGA_ACC_CLAMP);
446 }
447
pega_accel_poll(struct input_dev * input)448 static void pega_accel_poll(struct input_dev *input)
449 {
450 struct device *parent = input->dev.parent;
451 struct asus_laptop *asus = dev_get_drvdata(parent);
452
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;
464 }
465
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);
469
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);
477 }
478
pega_accel_exit(struct asus_laptop * asus)479 static void pega_accel_exit(struct asus_laptop *asus)
480 {
481 if (asus->pega_accel_poll) {
482 input_unregister_device(asus->pega_accel_poll);
483 asus->pega_accel_poll = NULL;
484 }
485 }
486
pega_accel_init(struct asus_laptop * asus)487 static int pega_accel_init(struct asus_laptop *asus)
488 {
489 int err;
490 struct input_dev *input;
491
492 if (!asus->is_pega_lucid)
493 return -ENODEV;
494
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;
499
500 input = input_allocate_device();
501 if (!input)
502 return -ENOMEM;
503
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;
508
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);
515
516 err = input_setup_polling(input, pega_accel_poll);
517 if (err)
518 goto exit;
519
520 input_set_poll_interval(input, 125);
521 input_set_min_poll_interval(input, 50);
522 input_set_max_poll_interval(input, 2000);
523
524 err = input_register_device(input);
525 if (err)
526 goto exit;
527
528 asus->pega_accel_poll = input;
529 return 0;
530
531 exit:
532 input_free_device(input);
533 return err;
534 }
535
536 /* Generic LED function */
asus_led_set(struct asus_laptop * asus,const char * method,int value)537 static int asus_led_set(struct asus_laptop *asus, const char *method,
538 int value)
539 {
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;
546
547 return write_acpi_int(asus->handle, method, value);
548 }
549
550 /*
551 * LEDs
552 */
553 /* /sys/class/led handlers */
asus_led_cdev_set(struct led_classdev * led_cdev,enum led_brightness value)554 static void asus_led_cdev_set(struct led_classdev *led_cdev,
555 enum led_brightness value)
556 {
557 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
558 struct asus_laptop *asus = led->asus;
559
560 led->wk = !!value;
561 queue_work(asus->led_workqueue, &led->work);
562 }
563
asus_led_cdev_update(struct work_struct * work)564 static void asus_led_cdev_update(struct work_struct *work)
565 {
566 struct asus_led *led = container_of(work, struct asus_led, work);
567 struct asus_laptop *asus = led->asus;
568
569 asus_led_set(asus, led->method, led->wk);
570 }
571
asus_led_cdev_get(struct led_classdev * led_cdev)572 static enum led_brightness asus_led_cdev_get(struct led_classdev *led_cdev)
573 {
574 return led_cdev->brightness;
575 }
576
577 /*
578 * Keyboard backlight (also a LED)
579 */
asus_kled_lvl(struct asus_laptop * asus)580 static int asus_kled_lvl(struct asus_laptop *asus)
581 {
582 unsigned long long kblv;
583 struct acpi_object_list params;
584 union acpi_object in_obj;
585 acpi_status rv;
586
587 params.count = 1;
588 params.pointer = &in_obj;
589 in_obj.type = ACPI_TYPE_INTEGER;
590 in_obj.integer.value = 2;
591
592 rv = acpi_evaluate_integer(asus->handle, METHOD_KBD_LIGHT_GET,
593 ¶ms, &kblv);
594 if (ACPI_FAILURE(rv)) {
595 pr_warn("Error reading kled level\n");
596 return -ENODEV;
597 }
598 return kblv;
599 }
600
asus_kled_set(struct asus_laptop * asus,int kblv)601 static int asus_kled_set(struct asus_laptop *asus, int kblv)
602 {
603 if (kblv > 0)
604 kblv = (1 << 7) | (kblv & 0x7F);
605 else
606 kblv = 0;
607
608 if (write_acpi_int(asus->handle, METHOD_KBD_LIGHT_SET, kblv)) {
609 pr_warn("Keyboard LED display write failed\n");
610 return -EINVAL;
611 }
612 return 0;
613 }
614
asus_kled_cdev_set(struct led_classdev * led_cdev,enum led_brightness value)615 static void asus_kled_cdev_set(struct led_classdev *led_cdev,
616 enum led_brightness value)
617 {
618 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
619 struct asus_laptop *asus = led->asus;
620
621 led->wk = value;
622 queue_work(asus->led_workqueue, &led->work);
623 }
624
asus_kled_cdev_update(struct work_struct * work)625 static void asus_kled_cdev_update(struct work_struct *work)
626 {
627 struct asus_led *led = container_of(work, struct asus_led, work);
628 struct asus_laptop *asus = led->asus;
629
630 asus_kled_set(asus, led->wk);
631 }
632
asus_kled_cdev_get(struct led_classdev * led_cdev)633 static enum led_brightness asus_kled_cdev_get(struct led_classdev *led_cdev)
634 {
635 struct asus_led *led = container_of(led_cdev, struct asus_led, led);
636 struct asus_laptop *asus = led->asus;
637
638 return asus_kled_lvl(asus);
639 }
640
asus_led_exit(struct asus_laptop * asus)641 static void asus_led_exit(struct asus_laptop *asus)
642 {
643 led_classdev_unregister(&asus->wled.led);
644 led_classdev_unregister(&asus->bled.led);
645 led_classdev_unregister(&asus->mled.led);
646 led_classdev_unregister(&asus->tled.led);
647 led_classdev_unregister(&asus->pled.led);
648 led_classdev_unregister(&asus->rled.led);
649 led_classdev_unregister(&asus->gled.led);
650 led_classdev_unregister(&asus->kled.led);
651
652 if (asus->led_workqueue) {
653 destroy_workqueue(asus->led_workqueue);
654 asus->led_workqueue = NULL;
655 }
656 }
657
658 /* Ugly macro, need to fix that later */
asus_led_register(struct asus_laptop * asus,struct asus_led * led,const char * name,const char * method)659 static int asus_led_register(struct asus_laptop *asus,
660 struct asus_led *led,
661 const char *name, const char *method)
662 {
663 struct led_classdev *led_cdev = &led->led;
664
665 if (!method || acpi_check_handle(asus->handle, method, NULL))
666 return 0; /* Led not present */
667
668 led->asus = asus;
669 led->method = method;
670
671 INIT_WORK(&led->work, asus_led_cdev_update);
672 led_cdev->name = name;
673 led_cdev->brightness_set = asus_led_cdev_set;
674 led_cdev->brightness_get = asus_led_cdev_get;
675 led_cdev->max_brightness = 1;
676 return led_classdev_register(&asus->platform_device->dev, led_cdev);
677 }
678
asus_led_init(struct asus_laptop * asus)679 static int asus_led_init(struct asus_laptop *asus)
680 {
681 int r = 0;
682
683 /*
684 * The Pegatron Lucid has no physical leds, but all methods are
685 * available in the DSDT...
686 */
687 if (asus->is_pega_lucid)
688 return 0;
689
690 /*
691 * Functions that actually update the LED's are called from a
692 * workqueue. By doing this as separate work rather than when the LED
693 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
694 * potentially bad time, such as a timer interrupt.
695 */
696 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
697 if (!asus->led_workqueue)
698 return -ENOMEM;
699
700 if (asus->wled_type == TYPE_LED)
701 r = asus_led_register(asus, &asus->wled, "asus::wlan",
702 METHOD_WLAN);
703 if (r)
704 goto error;
705 if (asus->bled_type == TYPE_LED)
706 r = asus_led_register(asus, &asus->bled, "asus::bluetooth",
707 METHOD_BLUETOOTH);
708 if (r)
709 goto error;
710 r = asus_led_register(asus, &asus->mled, "asus::mail", METHOD_MLED);
711 if (r)
712 goto error;
713 r = asus_led_register(asus, &asus->tled, "asus::touchpad", METHOD_TLED);
714 if (r)
715 goto error;
716 r = asus_led_register(asus, &asus->rled, "asus::record", METHOD_RLED);
717 if (r)
718 goto error;
719 r = asus_led_register(asus, &asus->pled, "asus::phone", METHOD_PLED);
720 if (r)
721 goto error;
722 r = asus_led_register(asus, &asus->gled, "asus::gaming", METHOD_GLED);
723 if (r)
724 goto error;
725 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL) &&
726 !acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_GET, NULL)) {
727 struct asus_led *led = &asus->kled;
728 struct led_classdev *cdev = &led->led;
729
730 led->asus = asus;
731
732 INIT_WORK(&led->work, asus_kled_cdev_update);
733 cdev->name = "asus::kbd_backlight";
734 cdev->brightness_set = asus_kled_cdev_set;
735 cdev->brightness_get = asus_kled_cdev_get;
736 cdev->max_brightness = 3;
737 r = led_classdev_register(&asus->platform_device->dev, cdev);
738 }
739 error:
740 if (r)
741 asus_led_exit(asus);
742 return r;
743 }
744
745 /*
746 * Backlight device
747 */
asus_read_brightness(struct backlight_device * bd)748 static int asus_read_brightness(struct backlight_device *bd)
749 {
750 struct asus_laptop *asus = bl_get_data(bd);
751 unsigned long long value;
752 acpi_status rv;
753
754 rv = acpi_evaluate_integer(asus->handle, METHOD_BRIGHTNESS_GET,
755 NULL, &value);
756 if (ACPI_FAILURE(rv)) {
757 pr_warn("Error reading brightness\n");
758 return 0;
759 }
760
761 return value;
762 }
763
asus_set_brightness(struct backlight_device * bd,int value)764 static int asus_set_brightness(struct backlight_device *bd, int value)
765 {
766 struct asus_laptop *asus = bl_get_data(bd);
767
768 if (write_acpi_int(asus->handle, METHOD_BRIGHTNESS_SET, value)) {
769 pr_warn("Error changing brightness\n");
770 return -EIO;
771 }
772 return 0;
773 }
774
update_bl_status(struct backlight_device * bd)775 static int update_bl_status(struct backlight_device *bd)
776 {
777 int value = bd->props.brightness;
778
779 return asus_set_brightness(bd, value);
780 }
781
782 static const struct backlight_ops asusbl_ops = {
783 .get_brightness = asus_read_brightness,
784 .update_status = update_bl_status,
785 };
786
asus_backlight_notify(struct asus_laptop * asus)787 static int asus_backlight_notify(struct asus_laptop *asus)
788 {
789 struct backlight_device *bd = asus->backlight_device;
790 int old = bd->props.brightness;
791
792 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
793
794 return old;
795 }
796
asus_backlight_init(struct asus_laptop * asus)797 static int asus_backlight_init(struct asus_laptop *asus)
798 {
799 struct backlight_device *bd;
800 struct backlight_properties props;
801
802 if (acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_GET, NULL) ||
803 acpi_check_handle(asus->handle, METHOD_BRIGHTNESS_SET, NULL))
804 return 0;
805
806 memset(&props, 0, sizeof(struct backlight_properties));
807 props.max_brightness = 15;
808 props.type = BACKLIGHT_PLATFORM;
809
810 bd = backlight_device_register(ASUS_LAPTOP_FILE,
811 &asus->platform_device->dev, asus,
812 &asusbl_ops, &props);
813 if (IS_ERR(bd)) {
814 pr_err("Could not register asus backlight device\n");
815 asus->backlight_device = NULL;
816 return PTR_ERR(bd);
817 }
818
819 asus->backlight_device = bd;
820 bd->props.brightness = asus_read_brightness(bd);
821 bd->props.power = BACKLIGHT_POWER_ON;
822 backlight_update_status(bd);
823 return 0;
824 }
825
asus_backlight_exit(struct asus_laptop * asus)826 static void asus_backlight_exit(struct asus_laptop *asus)
827 {
828 backlight_device_unregister(asus->backlight_device);
829 asus->backlight_device = NULL;
830 }
831
832 /*
833 * Platform device handlers
834 */
835
836 /*
837 * We write our info in page, we begin at offset off and cannot write more
838 * than count bytes. We set eof to 1 if we handle those 2 values. We return the
839 * number of bytes written in page
840 */
infos_show(struct device * dev,struct device_attribute * attr,char * page)841 static ssize_t infos_show(struct device *dev, struct device_attribute *attr,
842 char *page)
843 {
844 struct asus_laptop *asus = dev_get_drvdata(dev);
845 int len = 0;
846 unsigned long long temp;
847 char buf[16]; /* enough for all info */
848 acpi_status rv;
849
850 /*
851 * We use the easy way, we don't care of off and count,
852 * so we don't set eof to 1
853 */
854
855 len += sysfs_emit_at(page, len, ASUS_LAPTOP_NAME " " ASUS_LAPTOP_VERSION "\n");
856 len += sysfs_emit_at(page, len, "Model reference : %s\n", asus->name);
857 /*
858 * The SFUN method probably allows the original driver to get the list
859 * of features supported by a given model. For now, 0x0100 or 0x0800
860 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
861 * The significance of others is yet to be found.
862 */
863 rv = acpi_evaluate_integer(asus->handle, "SFUN", NULL, &temp);
864 if (ACPI_SUCCESS(rv))
865 len += sysfs_emit_at(page, len, "SFUN value : %#x\n",
866 (uint) temp);
867 /*
868 * The HWRS method return informations about the hardware.
869 * 0x80 bit is for WLAN, 0x100 for Bluetooth.
870 * 0x40 for WWAN, 0x10 for WIMAX.
871 * The significance of others is yet to be found.
872 * We don't currently use this for device detection, and it
873 * takes several seconds to run on some systems.
874 */
875 rv = acpi_evaluate_integer(asus->handle, "HWRS", NULL, &temp);
876 if (ACPI_SUCCESS(rv))
877 len += sysfs_emit_at(page, len, "HWRS value : %#x\n",
878 (uint) temp);
879 /*
880 * Another value for userspace: the ASYM method returns 0x02 for
881 * battery low and 0x04 for battery critical, its readings tend to be
882 * more accurate than those provided by _BST.
883 * Note: since not all the laptops provide this method, errors are
884 * silently ignored.
885 */
886 rv = acpi_evaluate_integer(asus->handle, "ASYM", NULL, &temp);
887 if (ACPI_SUCCESS(rv))
888 len += sysfs_emit_at(page, len, "ASYM value : %#x\n",
889 (uint) temp);
890 if (asus->dsdt_info) {
891 snprintf(buf, 16, "%d", asus->dsdt_info->length);
892 len += sysfs_emit_at(page, len, "DSDT length : %s\n", buf);
893 snprintf(buf, 16, "%d", asus->dsdt_info->checksum);
894 len += sysfs_emit_at(page, len, "DSDT checksum : %s\n", buf);
895 snprintf(buf, 16, "%d", asus->dsdt_info->revision);
896 len += sysfs_emit_at(page, len, "DSDT revision : %s\n", buf);
897 snprintf(buf, 7, "%s", asus->dsdt_info->oem_id);
898 len += sysfs_emit_at(page, len, "OEM id : %s\n", buf);
899 snprintf(buf, 9, "%s", asus->dsdt_info->oem_table_id);
900 len += sysfs_emit_at(page, len, "OEM table id : %s\n", buf);
901 snprintf(buf, 16, "%x", asus->dsdt_info->oem_revision);
902 len += sysfs_emit_at(page, len, "OEM revision : 0x%s\n", buf);
903 snprintf(buf, 5, "%s", asus->dsdt_info->asl_compiler_id);
904 len += sysfs_emit_at(page, len, "ASL comp vendor id : %s\n", buf);
905 snprintf(buf, 16, "%x", asus->dsdt_info->asl_compiler_revision);
906 len += sysfs_emit_at(page, len, "ASL comp revision : 0x%s\n", buf);
907 }
908
909 return len;
910 }
911 static DEVICE_ATTR_RO(infos);
912
sysfs_acpi_set(struct asus_laptop * asus,const char * buf,size_t count,const char * method)913 static ssize_t sysfs_acpi_set(struct asus_laptop *asus,
914 const char *buf, size_t count,
915 const char *method)
916 {
917 int rv, value;
918
919 rv = kstrtoint(buf, 0, &value);
920 if (rv < 0)
921 return rv;
922
923 if (write_acpi_int(asus->handle, method, value))
924 return -ENODEV;
925 return count;
926 }
927
928 /*
929 * LEDD display
930 */
ledd_show(struct device * dev,struct device_attribute * attr,char * buf)931 static ssize_t ledd_show(struct device *dev, struct device_attribute *attr,
932 char *buf)
933 {
934 struct asus_laptop *asus = dev_get_drvdata(dev);
935
936 return sysfs_emit(buf, "0x%08x\n", asus->ledd_status);
937 }
938
ledd_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)939 static ssize_t ledd_store(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 = kstrtoint(buf, 0, &value);
946 if (rv < 0)
947 return rv;
948
949 if (write_acpi_int(asus->handle, METHOD_LEDD, value)) {
950 pr_warn("LED display write failed\n");
951 return -ENODEV;
952 }
953
954 asus->ledd_status = (u32) value;
955 return count;
956 }
957 static DEVICE_ATTR_RW(ledd);
958
959 /*
960 * Wireless
961 */
asus_wireless_status(struct asus_laptop * asus,int mask)962 static int asus_wireless_status(struct asus_laptop *asus, int mask)
963 {
964 unsigned long long status;
965 acpi_status rv = AE_OK;
966
967 if (!asus->have_rsts)
968 return (asus->wireless_status & mask) ? 1 : 0;
969
970 rv = acpi_evaluate_integer(asus->handle, METHOD_WL_STATUS,
971 NULL, &status);
972 if (ACPI_FAILURE(rv)) {
973 pr_warn("Error reading Wireless status\n");
974 return -EINVAL;
975 }
976 return !!(status & mask);
977 }
978
979 /*
980 * WLAN
981 */
asus_wlan_set(struct asus_laptop * asus,int status)982 static int asus_wlan_set(struct asus_laptop *asus, int status)
983 {
984 if (write_acpi_int(asus->handle, METHOD_WLAN, !!status)) {
985 pr_warn("Error setting wlan status to %d\n", status);
986 return -EIO;
987 }
988 return 0;
989 }
990
wlan_show(struct device * dev,struct device_attribute * attr,char * buf)991 static ssize_t wlan_show(struct device *dev, struct device_attribute *attr,
992 char *buf)
993 {
994 struct asus_laptop *asus = dev_get_drvdata(dev);
995
996 return sysfs_emit(buf, "%d\n", asus_wireless_status(asus, WL_RSTS));
997 }
998
wlan_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)999 static ssize_t wlan_store(struct device *dev, struct device_attribute *attr,
1000 const char *buf, size_t count)
1001 {
1002 struct asus_laptop *asus = dev_get_drvdata(dev);
1003
1004 return sysfs_acpi_set(asus, buf, count, METHOD_WLAN);
1005 }
1006 static DEVICE_ATTR_RW(wlan);
1007
1008 /*e
1009 * Bluetooth
1010 */
asus_bluetooth_set(struct asus_laptop * asus,int status)1011 static int asus_bluetooth_set(struct asus_laptop *asus, int status)
1012 {
1013 if (write_acpi_int(asus->handle, METHOD_BLUETOOTH, !!status)) {
1014 pr_warn("Error setting bluetooth status to %d\n", status);
1015 return -EIO;
1016 }
1017 return 0;
1018 }
1019
bluetooth_show(struct device * dev,struct device_attribute * attr,char * buf)1020 static ssize_t bluetooth_show(struct device *dev, struct device_attribute *attr,
1021 char *buf)
1022 {
1023 struct asus_laptop *asus = dev_get_drvdata(dev);
1024
1025 return sysfs_emit(buf, "%d\n", asus_wireless_status(asus, BT_RSTS));
1026 }
1027
bluetooth_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1028 static ssize_t bluetooth_store(struct device *dev,
1029 struct device_attribute *attr, const char *buf,
1030 size_t count)
1031 {
1032 struct asus_laptop *asus = dev_get_drvdata(dev);
1033
1034 return sysfs_acpi_set(asus, buf, count, METHOD_BLUETOOTH);
1035 }
1036 static DEVICE_ATTR_RW(bluetooth);
1037
1038 /*
1039 * Wimax
1040 */
asus_wimax_set(struct asus_laptop * asus,int status)1041 static int asus_wimax_set(struct asus_laptop *asus, int status)
1042 {
1043 if (write_acpi_int(asus->handle, METHOD_WIMAX, !!status)) {
1044 pr_warn("Error setting wimax status to %d\n", status);
1045 return -EIO;
1046 }
1047 return 0;
1048 }
1049
wimax_show(struct device * dev,struct device_attribute * attr,char * buf)1050 static ssize_t wimax_show(struct device *dev, struct device_attribute *attr,
1051 char *buf)
1052 {
1053 struct asus_laptop *asus = dev_get_drvdata(dev);
1054
1055 return sysfs_emit(buf, "%d\n", asus_wireless_status(asus, WM_RSTS));
1056 }
1057
wimax_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1058 static ssize_t wimax_store(struct device *dev, struct device_attribute *attr,
1059 const char *buf, size_t count)
1060 {
1061 struct asus_laptop *asus = dev_get_drvdata(dev);
1062
1063 return sysfs_acpi_set(asus, buf, count, METHOD_WIMAX);
1064 }
1065 static DEVICE_ATTR_RW(wimax);
1066
1067 /*
1068 * Wwan
1069 */
asus_wwan_set(struct asus_laptop * asus,int status)1070 static int asus_wwan_set(struct asus_laptop *asus, int status)
1071 {
1072 if (write_acpi_int(asus->handle, METHOD_WWAN, !!status)) {
1073 pr_warn("Error setting wwan status to %d\n", status);
1074 return -EIO;
1075 }
1076 return 0;
1077 }
1078
wwan_show(struct device * dev,struct device_attribute * attr,char * buf)1079 static ssize_t wwan_show(struct device *dev, struct device_attribute *attr,
1080 char *buf)
1081 {
1082 struct asus_laptop *asus = dev_get_drvdata(dev);
1083
1084 return sysfs_emit(buf, "%d\n", asus_wireless_status(asus, WW_RSTS));
1085 }
1086
wwan_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1087 static ssize_t wwan_store(struct device *dev, struct device_attribute *attr,
1088 const char *buf, size_t count)
1089 {
1090 struct asus_laptop *asus = dev_get_drvdata(dev);
1091
1092 return sysfs_acpi_set(asus, buf, count, METHOD_WWAN);
1093 }
1094 static DEVICE_ATTR_RW(wwan);
1095
1096 /*
1097 * Display
1098 */
asus_set_display(struct asus_laptop * asus,int value)1099 static void asus_set_display(struct asus_laptop *asus, int value)
1100 {
1101 /* no sanity check needed for now */
1102 if (write_acpi_int(asus->handle, METHOD_SWITCH_DISPLAY, value))
1103 pr_warn("Error setting display\n");
1104 return;
1105 }
1106
1107 /*
1108 * Experimental support for display switching. As of now: 1 should activate
1109 * the LCD output, 2 should do for CRT, 4 for TV-Out and 8 for DVI.
1110 * Any combination (bitwise) of these will suffice. I never actually tested 4
1111 * displays hooked up simultaneously, so be warned. See the acpi4asus README
1112 * for more info.
1113 */
display_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1114 static ssize_t display_store(struct device *dev, struct device_attribute *attr,
1115 const char *buf, size_t count)
1116 {
1117 struct asus_laptop *asus = dev_get_drvdata(dev);
1118 int rv, value;
1119
1120 rv = kstrtoint(buf, 0, &value);
1121 if (rv < 0)
1122 return rv;
1123
1124 asus_set_display(asus, value);
1125 return count;
1126 }
1127 static DEVICE_ATTR_WO(display);
1128
1129 /*
1130 * Light Sens
1131 */
asus_als_switch(struct asus_laptop * asus,int value)1132 static void asus_als_switch(struct asus_laptop *asus, int value)
1133 {
1134 int ret;
1135
1136 if (asus->is_pega_lucid) {
1137 ret = asus_pega_lucid_set(asus, PEGA_ALS, value);
1138 if (!ret)
1139 ret = asus_pega_lucid_set(asus, PEGA_ALS_POWER, value);
1140 } else {
1141 ret = write_acpi_int(asus->handle, METHOD_ALS_CONTROL, value);
1142 }
1143 if (ret)
1144 pr_warn("Error setting light sensor switch\n");
1145
1146 asus->light_switch = value;
1147 }
1148
ls_switch_show(struct device * dev,struct device_attribute * attr,char * buf)1149 static ssize_t ls_switch_show(struct device *dev, struct device_attribute *attr,
1150 char *buf)
1151 {
1152 struct asus_laptop *asus = dev_get_drvdata(dev);
1153
1154 return sysfs_emit(buf, "%d\n", asus->light_switch);
1155 }
1156
ls_switch_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1157 static ssize_t ls_switch_store(struct device *dev,
1158 struct device_attribute *attr, const char *buf,
1159 size_t count)
1160 {
1161 struct asus_laptop *asus = dev_get_drvdata(dev);
1162 int rv, value;
1163
1164 rv = kstrtoint(buf, 0, &value);
1165 if (rv < 0)
1166 return rv;
1167
1168 asus_als_switch(asus, value ? 1 : 0);
1169 return count;
1170 }
1171 static DEVICE_ATTR_RW(ls_switch);
1172
asus_als_level(struct asus_laptop * asus,int value)1173 static void asus_als_level(struct asus_laptop *asus, int value)
1174 {
1175 if (write_acpi_int(asus->handle, METHOD_ALS_LEVEL, value))
1176 pr_warn("Error setting light sensor level\n");
1177 asus->light_level = value;
1178 }
1179
ls_level_show(struct device * dev,struct device_attribute * attr,char * buf)1180 static ssize_t ls_level_show(struct device *dev, struct device_attribute *attr,
1181 char *buf)
1182 {
1183 struct asus_laptop *asus = dev_get_drvdata(dev);
1184
1185 return sysfs_emit(buf, "%d\n", asus->light_level);
1186 }
1187
ls_level_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1188 static ssize_t ls_level_store(struct device *dev, struct device_attribute *attr,
1189 const char *buf, size_t count)
1190 {
1191 struct asus_laptop *asus = dev_get_drvdata(dev);
1192 int rv, value;
1193
1194 rv = kstrtoint(buf, 0, &value);
1195 if (rv < 0)
1196 return rv;
1197
1198 value = (0 < value) ? ((15 < value) ? 15 : value) : 0;
1199 /* 0 <= value <= 15 */
1200 asus_als_level(asus, value);
1201
1202 return count;
1203 }
1204 static DEVICE_ATTR_RW(ls_level);
1205
pega_int_read(struct asus_laptop * asus,int arg,int * result)1206 static int pega_int_read(struct asus_laptop *asus, int arg, int *result)
1207 {
1208 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1209 int err = write_acpi_int_ret(asus->handle, METHOD_PEGA_READ, arg,
1210 &buffer);
1211 if (!err) {
1212 union acpi_object *obj = buffer.pointer;
1213 if (obj && obj->type == ACPI_TYPE_INTEGER)
1214 *result = obj->integer.value;
1215 else
1216 err = -EIO;
1217 }
1218 return err;
1219 }
1220
ls_value_show(struct device * dev,struct device_attribute * attr,char * buf)1221 static ssize_t ls_value_show(struct device *dev, struct device_attribute *attr,
1222 char *buf)
1223 {
1224 struct asus_laptop *asus = dev_get_drvdata(dev);
1225 int err, hi, lo;
1226
1227 err = pega_int_read(asus, PEGA_READ_ALS_H, &hi);
1228 if (!err)
1229 err = pega_int_read(asus, PEGA_READ_ALS_L, &lo);
1230 if (!err)
1231 return sysfs_emit(buf, "%d\n", 10 * hi + lo);
1232 return err;
1233 }
1234 static DEVICE_ATTR_RO(ls_value);
1235
1236 /*
1237 * GPS
1238 */
asus_gps_status(struct asus_laptop * asus)1239 static int asus_gps_status(struct asus_laptop *asus)
1240 {
1241 unsigned long long status;
1242 acpi_status rv;
1243
1244 rv = acpi_evaluate_integer(asus->handle, METHOD_GPS_STATUS,
1245 NULL, &status);
1246 if (ACPI_FAILURE(rv)) {
1247 pr_warn("Error reading GPS status\n");
1248 return -ENODEV;
1249 }
1250 return !!status;
1251 }
1252
asus_gps_switch(struct asus_laptop * asus,int status)1253 static int asus_gps_switch(struct asus_laptop *asus, int status)
1254 {
1255 const char *meth = status ? METHOD_GPS_ON : METHOD_GPS_OFF;
1256
1257 if (write_acpi_int(asus->handle, meth, 0x02))
1258 return -ENODEV;
1259 return 0;
1260 }
1261
gps_show(struct device * dev,struct device_attribute * attr,char * buf)1262 static ssize_t gps_show(struct device *dev, struct device_attribute *attr,
1263 char *buf)
1264 {
1265 struct asus_laptop *asus = dev_get_drvdata(dev);
1266
1267 return sysfs_emit(buf, "%d\n", asus_gps_status(asus));
1268 }
1269
gps_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1270 static ssize_t gps_store(struct device *dev, struct device_attribute *attr,
1271 const char *buf, size_t count)
1272 {
1273 struct asus_laptop *asus = dev_get_drvdata(dev);
1274 int rv, value;
1275 int ret;
1276
1277 rv = kstrtoint(buf, 0, &value);
1278 if (rv < 0)
1279 return rv;
1280 ret = asus_gps_switch(asus, !!value);
1281 if (ret)
1282 return ret;
1283 rfkill_set_sw_state(asus->gps.rfkill, !value);
1284 return count;
1285 }
1286 static DEVICE_ATTR_RW(gps);
1287
1288 /*
1289 * rfkill
1290 */
asus_gps_rfkill_set(void * data,bool blocked)1291 static int asus_gps_rfkill_set(void *data, bool blocked)
1292 {
1293 struct asus_laptop *asus = data;
1294
1295 return asus_gps_switch(asus, !blocked);
1296 }
1297
1298 static const struct rfkill_ops asus_gps_rfkill_ops = {
1299 .set_block = asus_gps_rfkill_set,
1300 };
1301
asus_rfkill_set(void * data,bool blocked)1302 static int asus_rfkill_set(void *data, bool blocked)
1303 {
1304 struct asus_rfkill *rfk = data;
1305 struct asus_laptop *asus = rfk->asus;
1306
1307 if (rfk->control_id == WL_RSTS)
1308 return asus_wlan_set(asus, !blocked);
1309 else if (rfk->control_id == BT_RSTS)
1310 return asus_bluetooth_set(asus, !blocked);
1311 else if (rfk->control_id == WM_RSTS)
1312 return asus_wimax_set(asus, !blocked);
1313 else if (rfk->control_id == WW_RSTS)
1314 return asus_wwan_set(asus, !blocked);
1315
1316 return -EINVAL;
1317 }
1318
1319 static const struct rfkill_ops asus_rfkill_ops = {
1320 .set_block = asus_rfkill_set,
1321 };
1322
asus_rfkill_terminate(struct asus_rfkill * rfk)1323 static void asus_rfkill_terminate(struct asus_rfkill *rfk)
1324 {
1325 if (!rfk->rfkill)
1326 return ;
1327
1328 rfkill_unregister(rfk->rfkill);
1329 rfkill_destroy(rfk->rfkill);
1330 rfk->rfkill = NULL;
1331 }
1332
asus_rfkill_exit(struct asus_laptop * asus)1333 static void asus_rfkill_exit(struct asus_laptop *asus)
1334 {
1335 asus_rfkill_terminate(&asus->wwan);
1336 asus_rfkill_terminate(&asus->bluetooth);
1337 asus_rfkill_terminate(&asus->wlan);
1338 asus_rfkill_terminate(&asus->gps);
1339 }
1340
asus_rfkill_setup(struct asus_laptop * asus,struct asus_rfkill * rfk,const char * name,int control_id,int type,const struct rfkill_ops * ops)1341 static int asus_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
1342 const char *name, int control_id, int type,
1343 const struct rfkill_ops *ops)
1344 {
1345 int result;
1346
1347 rfk->control_id = control_id;
1348 rfk->asus = asus;
1349 rfk->rfkill = rfkill_alloc(name, &asus->platform_device->dev,
1350 type, ops, rfk);
1351 if (!rfk->rfkill)
1352 return -EINVAL;
1353
1354 result = rfkill_register(rfk->rfkill);
1355 if (result) {
1356 rfkill_destroy(rfk->rfkill);
1357 rfk->rfkill = NULL;
1358 }
1359
1360 return result;
1361 }
1362
asus_rfkill_init(struct asus_laptop * asus)1363 static int asus_rfkill_init(struct asus_laptop *asus)
1364 {
1365 int result = 0;
1366
1367 if (asus->is_pega_lucid)
1368 return -ENODEV;
1369
1370 if (!acpi_check_handle(asus->handle, METHOD_GPS_ON, NULL) &&
1371 !acpi_check_handle(asus->handle, METHOD_GPS_OFF, NULL) &&
1372 !acpi_check_handle(asus->handle, METHOD_GPS_STATUS, NULL))
1373 result = asus_rfkill_setup(asus, &asus->gps, "asus-gps",
1374 -1, RFKILL_TYPE_GPS,
1375 &asus_gps_rfkill_ops);
1376 if (result)
1377 goto exit;
1378
1379
1380 if (!acpi_check_handle(asus->handle, METHOD_WLAN, NULL) &&
1381 asus->wled_type == TYPE_RFKILL)
1382 result = asus_rfkill_setup(asus, &asus->wlan, "asus-wlan",
1383 WL_RSTS, RFKILL_TYPE_WLAN,
1384 &asus_rfkill_ops);
1385 if (result)
1386 goto exit;
1387
1388 if (!acpi_check_handle(asus->handle, METHOD_BLUETOOTH, NULL) &&
1389 asus->bled_type == TYPE_RFKILL)
1390 result = asus_rfkill_setup(asus, &asus->bluetooth,
1391 "asus-bluetooth", BT_RSTS,
1392 RFKILL_TYPE_BLUETOOTH,
1393 &asus_rfkill_ops);
1394 if (result)
1395 goto exit;
1396
1397 if (!acpi_check_handle(asus->handle, METHOD_WWAN, NULL))
1398 result = asus_rfkill_setup(asus, &asus->wwan, "asus-wwan",
1399 WW_RSTS, RFKILL_TYPE_WWAN,
1400 &asus_rfkill_ops);
1401 if (result)
1402 goto exit;
1403
1404 if (!acpi_check_handle(asus->handle, METHOD_WIMAX, NULL))
1405 result = asus_rfkill_setup(asus, &asus->wimax, "asus-wimax",
1406 WM_RSTS, RFKILL_TYPE_WIMAX,
1407 &asus_rfkill_ops);
1408 if (result)
1409 goto exit;
1410
1411 exit:
1412 if (result)
1413 asus_rfkill_exit(asus);
1414
1415 return result;
1416 }
1417
pega_rfkill_set(void * data,bool blocked)1418 static int pega_rfkill_set(void *data, bool blocked)
1419 {
1420 struct asus_rfkill *rfk = data;
1421
1422 int ret = asus_pega_lucid_set(rfk->asus, rfk->control_id, !blocked);
1423 return ret;
1424 }
1425
1426 static const struct rfkill_ops pega_rfkill_ops = {
1427 .set_block = pega_rfkill_set,
1428 };
1429
pega_rfkill_setup(struct asus_laptop * asus,struct asus_rfkill * rfk,const char * name,int controlid,int rfkill_type)1430 static int pega_rfkill_setup(struct asus_laptop *asus, struct asus_rfkill *rfk,
1431 const char *name, int controlid, int rfkill_type)
1432 {
1433 return asus_rfkill_setup(asus, rfk, name, controlid, rfkill_type,
1434 &pega_rfkill_ops);
1435 }
1436
pega_rfkill_init(struct asus_laptop * asus)1437 static int pega_rfkill_init(struct asus_laptop *asus)
1438 {
1439 int ret = 0;
1440
1441 if(!asus->is_pega_lucid)
1442 return -ENODEV;
1443
1444 ret = pega_rfkill_setup(asus, &asus->wlan, "pega-wlan",
1445 PEGA_WLAN, RFKILL_TYPE_WLAN);
1446 if(ret)
1447 goto exit;
1448
1449 ret = pega_rfkill_setup(asus, &asus->bluetooth, "pega-bt",
1450 PEGA_BLUETOOTH, RFKILL_TYPE_BLUETOOTH);
1451 if(ret)
1452 goto exit;
1453
1454 ret = pega_rfkill_setup(asus, &asus->wwan, "pega-wwan",
1455 PEGA_WWAN, RFKILL_TYPE_WWAN);
1456
1457 exit:
1458 if (ret)
1459 asus_rfkill_exit(asus);
1460
1461 return ret;
1462 }
1463
1464 /*
1465 * Input device (i.e. hotkeys)
1466 */
asus_input_notify(struct asus_laptop * asus,int event)1467 static void asus_input_notify(struct asus_laptop *asus, int event)
1468 {
1469 if (!asus->inputdev)
1470 return ;
1471 if (!sparse_keymap_report_event(asus->inputdev, event, 1, true))
1472 pr_info("Unknown key %x pressed\n", event);
1473 }
1474
asus_input_init(struct asus_laptop * asus)1475 static int asus_input_init(struct asus_laptop *asus)
1476 {
1477 struct input_dev *input;
1478 int error;
1479
1480 input = input_allocate_device();
1481 if (!input)
1482 return -ENOMEM;
1483
1484 input->name = "Asus Laptop extra buttons";
1485 input->phys = ASUS_LAPTOP_FILE "/input0";
1486 input->id.bustype = BUS_HOST;
1487 input->dev.parent = &asus->platform_device->dev;
1488
1489 error = sparse_keymap_setup(input, asus_keymap, NULL);
1490 if (error) {
1491 pr_err("Unable to setup input device keymap\n");
1492 goto err_free_dev;
1493 }
1494 error = input_register_device(input);
1495 if (error) {
1496 pr_warn("Unable to register input device\n");
1497 goto err_free_dev;
1498 }
1499
1500 asus->inputdev = input;
1501 return 0;
1502
1503 err_free_dev:
1504 input_free_device(input);
1505 return error;
1506 }
1507
asus_input_exit(struct asus_laptop * asus)1508 static void asus_input_exit(struct asus_laptop *asus)
1509 {
1510 if (asus->inputdev)
1511 input_unregister_device(asus->inputdev);
1512 asus->inputdev = NULL;
1513 }
1514
1515 /*
1516 * ACPI driver
1517 */
asus_acpi_notify(acpi_handle handle,u32 event,void * data)1518 static void asus_acpi_notify(acpi_handle handle, u32 event, void *data)
1519 {
1520 struct asus_laptop *asus = data;
1521 u16 count;
1522
1523 /* TODO Find a better way to handle events count. */
1524 count = asus->event_count[event % 128]++;
1525 acpi_bus_generate_netlink_event("hotkey", dev_name(&asus->device->dev),
1526 event, count);
1527
1528 if (event >= ATKD_BRNUP_MIN && event <= ATKD_BRNUP_MAX)
1529 event = ATKD_BRNUP;
1530 else if (event >= ATKD_BRNDOWN_MIN &&
1531 event <= ATKD_BRNDOWN_MAX)
1532 event = ATKD_BRNDOWN;
1533
1534 /* Brightness events are special */
1535 if (event == ATKD_BRNDOWN || event == ATKD_BRNUP) {
1536 if (asus->backlight_device != NULL) {
1537 /* Update the backlight device. */
1538 asus_backlight_notify(asus);
1539 return ;
1540 }
1541 }
1542
1543 /* Accelerometer "coarse orientation change" event */
1544 if (asus->pega_accel_poll && event == 0xEA) {
1545 kobject_uevent(&asus->pega_accel_poll->dev.kobj, KOBJ_CHANGE);
1546 return ;
1547 }
1548
1549 asus_input_notify(asus, event);
1550 }
1551
1552 static struct attribute *asus_attributes[] = {
1553 &dev_attr_infos.attr,
1554 &dev_attr_wlan.attr,
1555 &dev_attr_bluetooth.attr,
1556 &dev_attr_wimax.attr,
1557 &dev_attr_wwan.attr,
1558 &dev_attr_display.attr,
1559 &dev_attr_ledd.attr,
1560 &dev_attr_ls_value.attr,
1561 &dev_attr_ls_level.attr,
1562 &dev_attr_ls_switch.attr,
1563 &dev_attr_gps.attr,
1564 NULL
1565 };
1566
asus_sysfs_is_visible(struct kobject * kobj,struct attribute * attr,int idx)1567 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1568 struct attribute *attr,
1569 int idx)
1570 {
1571 struct device *dev = kobj_to_dev(kobj);
1572 struct asus_laptop *asus = dev_get_drvdata(dev);
1573 acpi_handle handle = asus->handle;
1574 bool supported;
1575
1576 if (asus->is_pega_lucid) {
1577 /* no ls_level interface on the Lucid */
1578 if (attr == &dev_attr_ls_switch.attr)
1579 supported = true;
1580 else if (attr == &dev_attr_ls_level.attr)
1581 supported = false;
1582 else
1583 goto normal;
1584
1585 return supported ? attr->mode : 0;
1586 }
1587
1588 normal:
1589 if (attr == &dev_attr_wlan.attr) {
1590 supported = !acpi_check_handle(handle, METHOD_WLAN, NULL);
1591
1592 } else if (attr == &dev_attr_bluetooth.attr) {
1593 supported = !acpi_check_handle(handle, METHOD_BLUETOOTH, NULL);
1594
1595 } else if (attr == &dev_attr_display.attr) {
1596 supported = !acpi_check_handle(handle, METHOD_SWITCH_DISPLAY, NULL);
1597
1598 } else if (attr == &dev_attr_wimax.attr) {
1599 supported =
1600 !acpi_check_handle(asus->handle, METHOD_WIMAX, NULL);
1601
1602 } else if (attr == &dev_attr_wwan.attr) {
1603 supported = !acpi_check_handle(asus->handle, METHOD_WWAN, NULL);
1604
1605 } else if (attr == &dev_attr_ledd.attr) {
1606 supported = !acpi_check_handle(handle, METHOD_LEDD, NULL);
1607
1608 } else if (attr == &dev_attr_ls_switch.attr ||
1609 attr == &dev_attr_ls_level.attr) {
1610 supported = !acpi_check_handle(handle, METHOD_ALS_CONTROL, NULL) &&
1611 !acpi_check_handle(handle, METHOD_ALS_LEVEL, NULL);
1612 } else if (attr == &dev_attr_ls_value.attr) {
1613 supported = asus->is_pega_lucid;
1614 } else if (attr == &dev_attr_gps.attr) {
1615 supported = !acpi_check_handle(handle, METHOD_GPS_ON, NULL) &&
1616 !acpi_check_handle(handle, METHOD_GPS_OFF, NULL) &&
1617 !acpi_check_handle(handle, METHOD_GPS_STATUS, NULL);
1618 } else {
1619 supported = true;
1620 }
1621
1622 return supported ? attr->mode : 0;
1623 }
1624
1625
1626 static const struct attribute_group asus_attr_group = {
1627 .is_visible = asus_sysfs_is_visible,
1628 .attrs = asus_attributes,
1629 };
1630
asus_platform_init(struct asus_laptop * asus)1631 static int asus_platform_init(struct asus_laptop *asus)
1632 {
1633 int result;
1634
1635 asus->platform_device = platform_device_alloc(ASUS_LAPTOP_FILE, PLATFORM_DEVID_NONE);
1636 if (!asus->platform_device)
1637 return -ENOMEM;
1638 platform_set_drvdata(asus->platform_device, asus);
1639
1640 result = platform_device_add(asus->platform_device);
1641 if (result)
1642 goto fail_platform_device;
1643
1644 result = sysfs_create_group(&asus->platform_device->dev.kobj,
1645 &asus_attr_group);
1646 if (result)
1647 goto fail_sysfs;
1648
1649 return 0;
1650
1651 fail_sysfs:
1652 platform_device_del(asus->platform_device);
1653 fail_platform_device:
1654 platform_device_put(asus->platform_device);
1655 return result;
1656 }
1657
asus_platform_exit(struct asus_laptop * asus)1658 static void asus_platform_exit(struct asus_laptop *asus)
1659 {
1660 sysfs_remove_group(&asus->platform_device->dev.kobj, &asus_attr_group);
1661 platform_device_unregister(asus->platform_device);
1662 }
1663
1664 static struct platform_driver platform_driver = {
1665 .driver = {
1666 .name = ASUS_LAPTOP_FILE,
1667 },
1668 };
1669
1670 /*
1671 * This function is used to initialize the context with right values. In this
1672 * method, we can make all the detection we want, and modify the asus_laptop
1673 * struct
1674 */
asus_laptop_get_info(struct asus_laptop * asus)1675 static int asus_laptop_get_info(struct asus_laptop *asus)
1676 {
1677 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1678 union acpi_object *model = NULL;
1679 unsigned long long bsts_result;
1680 char *string = NULL;
1681 acpi_status status;
1682
1683 /*
1684 * Get DSDT headers early enough to allow for differentiating between
1685 * models, but late enough to allow acpi_bus_register_driver() to fail
1686 * before doing anything ACPI-specific. Should we encounter a machine,
1687 * which needs special handling (i.e. its hotkey device has a different
1688 * HID), this bit will be moved.
1689 */
1690 status = acpi_get_table(ACPI_SIG_DSDT, 1, &asus->dsdt_info);
1691 if (ACPI_FAILURE(status))
1692 pr_warn("Couldn't get the DSDT table header\n");
1693
1694 /* We have to write 0 on init this far for all ASUS models */
1695 if (write_acpi_int_ret(asus->handle, "INIT", 0, &buffer)) {
1696 pr_err("Hotkey initialization failed\n");
1697 return -ENODEV;
1698 }
1699
1700 /* This needs to be called for some laptops to init properly */
1701 status =
1702 acpi_evaluate_integer(asus->handle, "BSTS", NULL, &bsts_result);
1703 if (ACPI_FAILURE(status))
1704 pr_warn("Error calling BSTS\n");
1705 else if (bsts_result)
1706 pr_notice("BSTS called, 0x%02x returned\n",
1707 (uint) bsts_result);
1708
1709 /* This too ... */
1710 if (write_acpi_int(asus->handle, "CWAP", wapf))
1711 pr_err("Error calling CWAP(%d)\n", wapf);
1712 /*
1713 * Try to match the object returned by INIT to the specific model.
1714 * Handle every possible object (or the lack of thereof) the DSDT
1715 * writers might throw at us. When in trouble, we pass NULL to
1716 * asus_model_match() and try something completely different.
1717 */
1718 if (buffer.pointer) {
1719 model = buffer.pointer;
1720 switch (model->type) {
1721 case ACPI_TYPE_STRING:
1722 string = model->string.pointer;
1723 break;
1724 case ACPI_TYPE_BUFFER:
1725 string = model->buffer.pointer;
1726 break;
1727 default:
1728 string = "";
1729 break;
1730 }
1731 }
1732 asus->name = kstrdup(string, GFP_KERNEL);
1733 if (!asus->name) {
1734 kfree(buffer.pointer);
1735 return -ENOMEM;
1736 }
1737
1738 if (string)
1739 pr_notice(" %s model detected\n", string);
1740
1741 if (!acpi_check_handle(asus->handle, METHOD_WL_STATUS, NULL))
1742 asus->have_rsts = true;
1743
1744 kfree(model);
1745
1746 return AE_OK;
1747 }
1748
asus_acpi_init(struct asus_laptop * asus)1749 static int asus_acpi_init(struct asus_laptop *asus)
1750 {
1751 int result = 0;
1752
1753 result = acpi_bus_get_status(asus->device);
1754 if (result)
1755 return result;
1756 if (!asus->device->status.present) {
1757 pr_err("Hotkey device not present, aborting\n");
1758 return -ENODEV;
1759 }
1760
1761 result = asus_laptop_get_info(asus);
1762 if (result)
1763 return result;
1764
1765 if (!strcmp(bled_type, "led"))
1766 asus->bled_type = TYPE_LED;
1767 else if (!strcmp(bled_type, "rfkill"))
1768 asus->bled_type = TYPE_RFKILL;
1769
1770 if (!strcmp(wled_type, "led"))
1771 asus->wled_type = TYPE_LED;
1772 else if (!strcmp(wled_type, "rfkill"))
1773 asus->wled_type = TYPE_RFKILL;
1774
1775 if (bluetooth_status >= 0)
1776 asus_bluetooth_set(asus, !!bluetooth_status);
1777
1778 if (wlan_status >= 0)
1779 asus_wlan_set(asus, !!wlan_status);
1780
1781 if (wimax_status >= 0)
1782 asus_wimax_set(asus, !!wimax_status);
1783
1784 if (wwan_status >= 0)
1785 asus_wwan_set(asus, !!wwan_status);
1786
1787 /* Keyboard Backlight is on by default */
1788 if (!acpi_check_handle(asus->handle, METHOD_KBD_LIGHT_SET, NULL))
1789 asus_kled_set(asus, 1);
1790
1791 /* LED display is off by default */
1792 asus->ledd_status = 0xFFF;
1793
1794 /* Set initial values of light sensor and level */
1795 asus->light_switch = !!als_status;
1796 asus->light_level = 5; /* level 5 for sensor sensitivity */
1797
1798 if (asus->is_pega_lucid) {
1799 asus_als_switch(asus, asus->light_switch);
1800 } else if (!acpi_check_handle(asus->handle, METHOD_ALS_CONTROL, NULL) &&
1801 !acpi_check_handle(asus->handle, METHOD_ALS_LEVEL, NULL)) {
1802 asus_als_switch(asus, asus->light_switch);
1803 asus_als_level(asus, asus->light_level);
1804 }
1805
1806 return result;
1807 }
1808
asus_dmi_check(void)1809 static void asus_dmi_check(void)
1810 {
1811 const char *model;
1812
1813 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1814 if (!model)
1815 return;
1816
1817 /* On L1400B WLED control the sound card, don't mess with it ... */
1818 if (strncmp(model, "L1400B", 6) == 0)
1819 wlan_status = -1;
1820 }
1821
1822 static bool asus_device_present;
1823
asus_acpi_probe(struct platform_device * pdev)1824 static int asus_acpi_probe(struct platform_device *pdev)
1825 {
1826 struct acpi_device *device;
1827 struct asus_laptop *asus;
1828 int result;
1829
1830 device = ACPI_COMPANION(&pdev->dev);
1831 if (!device)
1832 return -ENODEV;
1833
1834 pr_notice("Asus Laptop Support version %s\n",
1835 ASUS_LAPTOP_VERSION);
1836 asus = kzalloc_obj(struct asus_laptop);
1837 if (!asus)
1838 return -ENOMEM;
1839 asus->handle = device->handle;
1840 asus->device = device;
1841
1842 asus_dmi_check();
1843
1844 result = asus_acpi_init(asus);
1845 if (result)
1846 goto fail_platform;
1847
1848 platform_set_drvdata(pdev, asus);
1849
1850 /*
1851 * Need platform type detection first, then the platform
1852 * device. It is used as a parent for the sub-devices below.
1853 */
1854 asus->is_pega_lucid = asus_check_pega_lucid(asus);
1855 result = asus_platform_init(asus);
1856 if (result)
1857 goto fail_platform;
1858
1859 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1860 result = asus_backlight_init(asus);
1861 if (result)
1862 goto fail_backlight;
1863 }
1864
1865 result = asus_input_init(asus);
1866 if (result)
1867 goto fail_input;
1868
1869 result = asus_led_init(asus);
1870 if (result)
1871 goto fail_led;
1872
1873 result = asus_rfkill_init(asus);
1874 if (result && result != -ENODEV)
1875 goto fail_rfkill;
1876
1877 result = pega_accel_init(asus);
1878 if (result && result != -ENODEV)
1879 goto fail_pega_accel;
1880
1881 result = pega_rfkill_init(asus);
1882 if (result && result != -ENODEV)
1883 goto fail_pega_rfkill;
1884
1885 result = acpi_dev_install_notify_handler(device, ACPI_DEVICE_NOTIFY,
1886 asus_acpi_notify, asus);
1887 if (result)
1888 goto fail_pega_rfkill;
1889
1890 asus_device_present = true;
1891 return 0;
1892
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);
1907
1908 return result;
1909 }
1910
asus_acpi_remove(struct platform_device * pdev)1911 static void asus_acpi_remove(struct platform_device *pdev)
1912 {
1913 struct asus_laptop *asus = platform_get_drvdata(pdev);
1914
1915 acpi_dev_remove_notify_handler(asus->device, ACPI_DEVICE_NOTIFY,
1916 asus_acpi_notify);
1917 asus_backlight_exit(asus);
1918 asus_rfkill_exit(asus);
1919 asus_led_exit(asus);
1920 asus_input_exit(asus);
1921 pega_accel_exit(asus);
1922 asus_platform_exit(asus);
1923
1924 kfree(asus->name);
1925 kfree(asus);
1926 }
1927
1928 static const struct acpi_device_id asus_device_ids[] = {
1929 {"ATK0100", 0},
1930 {"ATK0101", 0},
1931 {"", 0},
1932 };
1933 MODULE_DEVICE_TABLE(acpi, asus_device_ids);
1934
1935 static struct platform_driver asus_acpi_driver = {
1936 .probe = asus_acpi_probe,
1937 .remove = asus_acpi_remove,
1938 .driver = {
1939 .name = ASUS_LAPTOP_NAME,
1940 .acpi_match_table = asus_device_ids,
1941 },
1942 };
1943
asus_laptop_init(void)1944 static int __init asus_laptop_init(void)
1945 {
1946 int result;
1947
1948 result = platform_driver_register(&platform_driver);
1949 if (result < 0)
1950 return result;
1951
1952 result = platform_driver_register(&asus_acpi_driver);
1953 if (result < 0)
1954 goto fail_acpi_driver;
1955 if (!asus_device_present) {
1956 result = -ENODEV;
1957 goto fail_no_device;
1958 }
1959 return 0;
1960
1961 fail_no_device:
1962 platform_driver_unregister(&asus_acpi_driver);
1963 fail_acpi_driver:
1964 platform_driver_unregister(&platform_driver);
1965 return result;
1966 }
1967
asus_laptop_exit(void)1968 static void __exit asus_laptop_exit(void)
1969 {
1970 platform_driver_unregister(&asus_acpi_driver);
1971 platform_driver_unregister(&platform_driver);
1972 }
1973
1974 module_init(asus_laptop_init);
1975 module_exit(asus_laptop_exit);
1976