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