• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * HP WMI hotkeys
3  *
4  * Copyright (C) 2008 Red Hat <mjg@redhat.com>
5  * Copyright (C) 2010, 2011 Anssi Hannula <anssi.hannula@iki.fi>
6  *
7  * Portions based on wistron_btns.c:
8  * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9  * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10  * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26 
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/platform_device.h>
37 #include <linux/acpi.h>
38 #include <linux/rfkill.h>
39 #include <linux/string.h>
40 
41 MODULE_AUTHOR("Matthew Garrett <mjg59@srcf.ucam.org>");
42 MODULE_DESCRIPTION("HP laptop WMI hotkeys driver");
43 MODULE_LICENSE("GPL");
44 
45 MODULE_ALIAS("wmi:95F24279-4D7B-4334-9387-ACCDC67EF61C");
46 MODULE_ALIAS("wmi:5FB7F034-2C63-45e9-BE91-3D44E2C707E4");
47 
48 #define HPWMI_EVENT_GUID "95F24279-4D7B-4334-9387-ACCDC67EF61C"
49 #define HPWMI_BIOS_GUID "5FB7F034-2C63-45e9-BE91-3D44E2C707E4"
50 
51 enum hp_wmi_radio {
52 	HPWMI_WIFI	= 0x0,
53 	HPWMI_BLUETOOTH	= 0x1,
54 	HPWMI_WWAN	= 0x2,
55 	HPWMI_GPS	= 0x3,
56 };
57 
58 enum hp_wmi_event_ids {
59 	HPWMI_DOCK_EVENT		= 0x01,
60 	HPWMI_PARK_HDD			= 0x02,
61 	HPWMI_SMART_ADAPTER		= 0x03,
62 	HPWMI_BEZEL_BUTTON		= 0x04,
63 	HPWMI_WIRELESS			= 0x05,
64 	HPWMI_CPU_BATTERY_THROTTLE	= 0x06,
65 	HPWMI_LOCK_SWITCH		= 0x07,
66 	HPWMI_LID_SWITCH		= 0x08,
67 	HPWMI_SCREEN_ROTATION		= 0x09,
68 	HPWMI_COOLSENSE_SYSTEM_MOBILE	= 0x0A,
69 	HPWMI_COOLSENSE_SYSTEM_HOT	= 0x0B,
70 	HPWMI_PROXIMITY_SENSOR		= 0x0C,
71 	HPWMI_BACKLIT_KB_BRIGHTNESS	= 0x0D,
72 	HPWMI_PEAKSHIFT_PERIOD		= 0x0F,
73 	HPWMI_BATTERY_CHARGE_PERIOD	= 0x10,
74 };
75 
76 struct bios_args {
77 	u32 signature;
78 	u32 command;
79 	u32 commandtype;
80 	u32 datasize;
81 	u8 data[128];
82 };
83 
84 enum hp_wmi_commandtype {
85 	HPWMI_DISPLAY_QUERY		= 0x01,
86 	HPWMI_HDDTEMP_QUERY		= 0x02,
87 	HPWMI_ALS_QUERY			= 0x03,
88 	HPWMI_HARDWARE_QUERY		= 0x04,
89 	HPWMI_WIRELESS_QUERY		= 0x05,
90 	HPWMI_BATTERY_QUERY		= 0x07,
91 	HPWMI_BIOS_QUERY		= 0x09,
92 	HPWMI_FEATURE_QUERY		= 0x0b,
93 	HPWMI_HOTKEY_QUERY		= 0x0c,
94 	HPWMI_FEATURE2_QUERY		= 0x0d,
95 	HPWMI_WIRELESS2_QUERY		= 0x1b,
96 	HPWMI_POSTCODEERROR_QUERY	= 0x2a,
97 };
98 
99 enum hp_wmi_command {
100 	HPWMI_READ	= 0x01,
101 	HPWMI_WRITE	= 0x02,
102 	HPWMI_ODM	= 0x03,
103 };
104 
105 enum hp_wmi_hardware_mask {
106 	HPWMI_DOCK_MASK		= 0x01,
107 	HPWMI_TABLET_MASK	= 0x04,
108 };
109 
110 struct bios_return {
111 	u32 sigpass;
112 	u32 return_code;
113 };
114 
115 enum hp_return_value {
116 	HPWMI_RET_WRONG_SIGNATURE	= 0x02,
117 	HPWMI_RET_UNKNOWN_COMMAND	= 0x03,
118 	HPWMI_RET_UNKNOWN_CMDTYPE	= 0x04,
119 	HPWMI_RET_INVALID_PARAMETERS	= 0x05,
120 };
121 
122 enum hp_wireless2_bits {
123 	HPWMI_POWER_STATE	= 0x01,
124 	HPWMI_POWER_SOFT	= 0x02,
125 	HPWMI_POWER_BIOS	= 0x04,
126 	HPWMI_POWER_HARD	= 0x08,
127 };
128 
129 #define IS_HWBLOCKED(x) ((x & (HPWMI_POWER_BIOS | HPWMI_POWER_HARD)) \
130 			 != (HPWMI_POWER_BIOS | HPWMI_POWER_HARD))
131 #define IS_SWBLOCKED(x) !(x & HPWMI_POWER_SOFT)
132 
133 struct bios_rfkill2_device_state {
134 	u8 radio_type;
135 	u8 bus_type;
136 	u16 vendor_id;
137 	u16 product_id;
138 	u16 subsys_vendor_id;
139 	u16 subsys_product_id;
140 	u8 rfkill_id;
141 	u8 power;
142 	u8 unknown[4];
143 };
144 
145 /* 7 devices fit into the 128 byte buffer */
146 #define HPWMI_MAX_RFKILL2_DEVICES	7
147 
148 struct bios_rfkill2_state {
149 	u8 unknown[7];
150 	u8 count;
151 	u8 pad[8];
152 	struct bios_rfkill2_device_state device[HPWMI_MAX_RFKILL2_DEVICES];
153 };
154 
155 static const struct key_entry hp_wmi_keymap[] = {
156 	{ KE_KEY, 0x02,   { KEY_BRIGHTNESSUP } },
157 	{ KE_KEY, 0x03,   { KEY_BRIGHTNESSDOWN } },
158 	{ KE_KEY, 0x20e6, { KEY_PROG1 } },
159 	{ KE_KEY, 0x20e8, { KEY_MEDIA } },
160 	{ KE_KEY, 0x2142, { KEY_MEDIA } },
161 	{ KE_KEY, 0x213b, { KEY_INFO } },
162 	{ KE_KEY, 0x2169, { KEY_ROTATE_DISPLAY } },
163 	{ KE_KEY, 0x216a, { KEY_SETUP } },
164 	{ KE_KEY, 0x231b, { KEY_HELP } },
165 	{ KE_END, 0 }
166 };
167 
168 static struct input_dev *hp_wmi_input_dev;
169 static struct platform_device *hp_wmi_platform_dev;
170 
171 static struct rfkill *wifi_rfkill;
172 static struct rfkill *bluetooth_rfkill;
173 static struct rfkill *wwan_rfkill;
174 
175 struct rfkill2_device {
176 	u8 id;
177 	int num;
178 	struct rfkill *rfkill;
179 };
180 
181 static int rfkill2_count;
182 static struct rfkill2_device rfkill2[HPWMI_MAX_RFKILL2_DEVICES];
183 
184 /* map output size to the corresponding WMI method id */
encode_outsize_for_pvsz(int outsize)185 static inline int encode_outsize_for_pvsz(int outsize)
186 {
187 	if (outsize > 4096)
188 		return -EINVAL;
189 	if (outsize > 1024)
190 		return 5;
191 	if (outsize > 128)
192 		return 4;
193 	if (outsize > 4)
194 		return 3;
195 	if (outsize > 0)
196 		return 2;
197 	return 1;
198 }
199 
200 /*
201  * hp_wmi_perform_query
202  *
203  * query:	The commandtype (enum hp_wmi_commandtype)
204  * write:	The command (enum hp_wmi_command)
205  * buffer:	Buffer used as input and/or output
206  * insize:	Size of input buffer
207  * outsize:	Size of output buffer
208  *
209  * returns zero on success
210  *         an HP WMI query specific error code (which is positive)
211  *         -EINVAL if the query was not successful at all
212  *         -EINVAL if the output buffer size exceeds buffersize
213  *
214  * Note: The buffersize must at least be the maximum of the input and output
215  *       size. E.g. Battery info query is defined to have 1 byte input
216  *       and 128 byte output. The caller would do:
217  *       buffer = kzalloc(128, GFP_KERNEL);
218  *       ret = hp_wmi_perform_query(HPWMI_BATTERY_QUERY, HPWMI_READ, buffer, 1, 128)
219  */
hp_wmi_perform_query(int query,enum hp_wmi_command command,void * buffer,int insize,int outsize)220 static int hp_wmi_perform_query(int query, enum hp_wmi_command command,
221 				void *buffer, int insize, int outsize)
222 {
223 	int mid;
224 	struct bios_return *bios_return;
225 	int actual_outsize;
226 	union acpi_object *obj;
227 	struct bios_args args = {
228 		.signature = 0x55434553,
229 		.command = command,
230 		.commandtype = query,
231 		.datasize = insize,
232 		.data = { 0 },
233 	};
234 	struct acpi_buffer input = { sizeof(struct bios_args), &args };
235 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
236 	int ret = 0;
237 
238 	mid = encode_outsize_for_pvsz(outsize);
239 	if (WARN_ON(mid < 0))
240 		return mid;
241 
242 	if (WARN_ON(insize > sizeof(args.data)))
243 		return -EINVAL;
244 	memcpy(&args.data[0], buffer, insize);
245 
246 	wmi_evaluate_method(HPWMI_BIOS_GUID, 0, mid, &input, &output);
247 
248 	obj = output.pointer;
249 
250 	if (!obj)
251 		return -EINVAL;
252 
253 	if (obj->type != ACPI_TYPE_BUFFER) {
254 		ret = -EINVAL;
255 		goto out_free;
256 	}
257 
258 	bios_return = (struct bios_return *)obj->buffer.pointer;
259 	ret = bios_return->return_code;
260 
261 	if (ret) {
262 		if (ret != HPWMI_RET_UNKNOWN_CMDTYPE)
263 			pr_warn("query 0x%x returned error 0x%x\n", query, ret);
264 		goto out_free;
265 	}
266 
267 	/* Ignore output data of zero size */
268 	if (!outsize)
269 		goto out_free;
270 
271 	actual_outsize = min(outsize, (int)(obj->buffer.length - sizeof(*bios_return)));
272 	memcpy(buffer, obj->buffer.pointer + sizeof(*bios_return), actual_outsize);
273 	memset(buffer + actual_outsize, 0, outsize - actual_outsize);
274 
275 out_free:
276 	kfree(obj);
277 	return ret;
278 }
279 
hp_wmi_read_int(int query)280 static int hp_wmi_read_int(int query)
281 {
282 	int val = 0, ret;
283 
284 	ret = hp_wmi_perform_query(query, HPWMI_READ, &val,
285 				   sizeof(val), sizeof(val));
286 
287 	if (ret)
288 		return ret < 0 ? ret : -EINVAL;
289 
290 	return val;
291 }
292 
hp_wmi_hw_state(int mask)293 static int hp_wmi_hw_state(int mask)
294 {
295 	int state = hp_wmi_read_int(HPWMI_HARDWARE_QUERY);
296 
297 	if (state < 0)
298 		return state;
299 
300 	return !!(state & mask);
301 }
302 
hp_wmi_bios_2008_later(void)303 static int __init hp_wmi_bios_2008_later(void)
304 {
305 	int state = 0;
306 	int ret = hp_wmi_perform_query(HPWMI_FEATURE_QUERY, HPWMI_READ, &state,
307 				       sizeof(state), sizeof(state));
308 	if (!ret)
309 		return 1;
310 
311 	return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
312 }
313 
hp_wmi_bios_2009_later(void)314 static int __init hp_wmi_bios_2009_later(void)
315 {
316 	u8 state[128];
317 	int ret = hp_wmi_perform_query(HPWMI_FEATURE2_QUERY, HPWMI_READ, &state,
318 				       sizeof(state), sizeof(state));
319 	if (!ret)
320 		return 1;
321 
322 	return (ret == HPWMI_RET_UNKNOWN_CMDTYPE) ? 0 : -ENXIO;
323 }
324 
hp_wmi_enable_hotkeys(void)325 static int __init hp_wmi_enable_hotkeys(void)
326 {
327 	int value = 0x6e;
328 	int ret = hp_wmi_perform_query(HPWMI_BIOS_QUERY, HPWMI_WRITE, &value,
329 				       sizeof(value), 0);
330 
331 	return ret <= 0 ? ret : -EINVAL;
332 }
333 
hp_wmi_set_block(void * data,bool blocked)334 static int hp_wmi_set_block(void *data, bool blocked)
335 {
336 	enum hp_wmi_radio r = (enum hp_wmi_radio) data;
337 	int query = BIT(r + 8) | ((!blocked) << r);
338 	int ret;
339 
340 	ret = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE,
341 				   &query, sizeof(query), 0);
342 
343 	return ret <= 0 ? ret : -EINVAL;
344 }
345 
346 static const struct rfkill_ops hp_wmi_rfkill_ops = {
347 	.set_block = hp_wmi_set_block,
348 };
349 
hp_wmi_get_sw_state(enum hp_wmi_radio r)350 static bool hp_wmi_get_sw_state(enum hp_wmi_radio r)
351 {
352 	int mask = 0x200 << (r * 8);
353 
354 	int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
355 
356 	/* TBD: Pass error */
357 	WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
358 
359 	return !(wireless & mask);
360 }
361 
hp_wmi_get_hw_state(enum hp_wmi_radio r)362 static bool hp_wmi_get_hw_state(enum hp_wmi_radio r)
363 {
364 	int mask = 0x800 << (r * 8);
365 
366 	int wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
367 
368 	/* TBD: Pass error */
369 	WARN_ONCE(wireless < 0, "error executing HPWMI_WIRELESS_QUERY");
370 
371 	return !(wireless & mask);
372 }
373 
hp_wmi_rfkill2_set_block(void * data,bool blocked)374 static int hp_wmi_rfkill2_set_block(void *data, bool blocked)
375 {
376 	int rfkill_id = (int)(long)data;
377 	char buffer[4] = { 0x01, 0x00, rfkill_id, !blocked };
378 	int ret;
379 
380 	ret = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_WRITE,
381 				   buffer, sizeof(buffer), 0);
382 
383 	return ret <= 0 ? ret : -EINVAL;
384 }
385 
386 static const struct rfkill_ops hp_wmi_rfkill2_ops = {
387 	.set_block = hp_wmi_rfkill2_set_block,
388 };
389 
hp_wmi_rfkill2_refresh(void)390 static int hp_wmi_rfkill2_refresh(void)
391 {
392 	struct bios_rfkill2_state state;
393 	int err, i;
394 
395 	err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
396 				   sizeof(state), sizeof(state));
397 	if (err)
398 		return err;
399 
400 	for (i = 0; i < rfkill2_count; i++) {
401 		int num = rfkill2[i].num;
402 		struct bios_rfkill2_device_state *devstate;
403 		devstate = &state.device[num];
404 
405 		if (num >= state.count ||
406 		    devstate->rfkill_id != rfkill2[i].id) {
407 			pr_warn("power configuration of the wireless devices unexpectedly changed\n");
408 			continue;
409 		}
410 
411 		rfkill_set_states(rfkill2[i].rfkill,
412 				  IS_SWBLOCKED(devstate->power),
413 				  IS_HWBLOCKED(devstate->power));
414 	}
415 
416 	return 0;
417 }
418 
display_show(struct device * dev,struct device_attribute * attr,char * buf)419 static ssize_t display_show(struct device *dev, struct device_attribute *attr,
420 			    char *buf)
421 {
422 	int value = hp_wmi_read_int(HPWMI_DISPLAY_QUERY);
423 	if (value < 0)
424 		return value;
425 	return sprintf(buf, "%d\n", value);
426 }
427 
hddtemp_show(struct device * dev,struct device_attribute * attr,char * buf)428 static ssize_t hddtemp_show(struct device *dev, struct device_attribute *attr,
429 			    char *buf)
430 {
431 	int value = hp_wmi_read_int(HPWMI_HDDTEMP_QUERY);
432 	if (value < 0)
433 		return value;
434 	return sprintf(buf, "%d\n", value);
435 }
436 
als_show(struct device * dev,struct device_attribute * attr,char * buf)437 static ssize_t als_show(struct device *dev, struct device_attribute *attr,
438 			char *buf)
439 {
440 	int value = hp_wmi_read_int(HPWMI_ALS_QUERY);
441 	if (value < 0)
442 		return value;
443 	return sprintf(buf, "%d\n", value);
444 }
445 
dock_show(struct device * dev,struct device_attribute * attr,char * buf)446 static ssize_t dock_show(struct device *dev, struct device_attribute *attr,
447 			 char *buf)
448 {
449 	int value = hp_wmi_hw_state(HPWMI_DOCK_MASK);
450 	if (value < 0)
451 		return value;
452 	return sprintf(buf, "%d\n", value);
453 }
454 
tablet_show(struct device * dev,struct device_attribute * attr,char * buf)455 static ssize_t tablet_show(struct device *dev, struct device_attribute *attr,
456 			   char *buf)
457 {
458 	int value = hp_wmi_hw_state(HPWMI_TABLET_MASK);
459 	if (value < 0)
460 		return value;
461 	return sprintf(buf, "%d\n", value);
462 }
463 
postcode_show(struct device * dev,struct device_attribute * attr,char * buf)464 static ssize_t postcode_show(struct device *dev, struct device_attribute *attr,
465 			     char *buf)
466 {
467 	/* Get the POST error code of previous boot failure. */
468 	int value = hp_wmi_read_int(HPWMI_POSTCODEERROR_QUERY);
469 	if (value < 0)
470 		return value;
471 	return sprintf(buf, "0x%x\n", value);
472 }
473 
als_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)474 static ssize_t als_store(struct device *dev, struct device_attribute *attr,
475 			 const char *buf, size_t count)
476 {
477 	u32 tmp;
478 	int ret;
479 
480 	ret = kstrtou32(buf, 10, &tmp);
481 	if (ret)
482 		return ret;
483 
484 	ret = hp_wmi_perform_query(HPWMI_ALS_QUERY, HPWMI_WRITE, &tmp,
485 				       sizeof(tmp), sizeof(tmp));
486 	if (ret)
487 		return ret < 0 ? ret : -EINVAL;
488 
489 	return count;
490 }
491 
postcode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)492 static ssize_t postcode_store(struct device *dev, struct device_attribute *attr,
493 			      const char *buf, size_t count)
494 {
495 	long unsigned int tmp2;
496 	int ret;
497 	u32 tmp;
498 
499 	ret = kstrtoul(buf, 10, &tmp2);
500 	if (!ret && tmp2 != 1)
501 		ret = -EINVAL;
502 	if (ret)
503 		goto out;
504 
505 	/* Clear the POST error code. It is kept until until cleared. */
506 	tmp = (u32) tmp2;
507 	ret = hp_wmi_perform_query(HPWMI_POSTCODEERROR_QUERY, HPWMI_WRITE, &tmp,
508 				       sizeof(tmp), sizeof(tmp));
509 
510 out:
511 	if (ret)
512 		return ret < 0 ? ret : -EINVAL;
513 
514 	return count;
515 }
516 
517 static DEVICE_ATTR_RO(display);
518 static DEVICE_ATTR_RO(hddtemp);
519 static DEVICE_ATTR_RW(als);
520 static DEVICE_ATTR_RO(dock);
521 static DEVICE_ATTR_RO(tablet);
522 static DEVICE_ATTR_RW(postcode);
523 
hp_wmi_notify(u32 value,void * context)524 static void hp_wmi_notify(u32 value, void *context)
525 {
526 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
527 	u32 event_id, event_data;
528 	union acpi_object *obj;
529 	acpi_status status;
530 	u32 *location;
531 	int key_code;
532 
533 	status = wmi_get_event_data(value, &response);
534 	if (status != AE_OK) {
535 		pr_info("bad event status 0x%x\n", status);
536 		return;
537 	}
538 
539 	obj = (union acpi_object *)response.pointer;
540 
541 	if (!obj)
542 		return;
543 	if (obj->type != ACPI_TYPE_BUFFER) {
544 		pr_info("Unknown response received %d\n", obj->type);
545 		kfree(obj);
546 		return;
547 	}
548 
549 	/*
550 	 * Depending on ACPI version the concatenation of id and event data
551 	 * inside _WED function will result in a 8 or 16 byte buffer.
552 	 */
553 	location = (u32 *)obj->buffer.pointer;
554 	if (obj->buffer.length == 8) {
555 		event_id = *location;
556 		event_data = *(location + 1);
557 	} else if (obj->buffer.length == 16) {
558 		event_id = *location;
559 		event_data = *(location + 2);
560 	} else {
561 		pr_info("Unknown buffer length %d\n", obj->buffer.length);
562 		kfree(obj);
563 		return;
564 	}
565 	kfree(obj);
566 
567 	switch (event_id) {
568 	case HPWMI_DOCK_EVENT:
569 		if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
570 			input_report_switch(hp_wmi_input_dev, SW_DOCK,
571 					    hp_wmi_hw_state(HPWMI_DOCK_MASK));
572 		if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
573 			input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
574 					    hp_wmi_hw_state(HPWMI_TABLET_MASK));
575 		input_sync(hp_wmi_input_dev);
576 		break;
577 	case HPWMI_PARK_HDD:
578 		break;
579 	case HPWMI_SMART_ADAPTER:
580 		break;
581 	case HPWMI_BEZEL_BUTTON:
582 		key_code = hp_wmi_read_int(HPWMI_HOTKEY_QUERY);
583 		if (key_code < 0)
584 			break;
585 
586 		if (!sparse_keymap_report_event(hp_wmi_input_dev,
587 						key_code, 1, true))
588 			pr_info("Unknown key code - 0x%x\n", key_code);
589 		break;
590 	case HPWMI_WIRELESS:
591 		if (rfkill2_count) {
592 			hp_wmi_rfkill2_refresh();
593 			break;
594 		}
595 
596 		if (wifi_rfkill)
597 			rfkill_set_states(wifi_rfkill,
598 					  hp_wmi_get_sw_state(HPWMI_WIFI),
599 					  hp_wmi_get_hw_state(HPWMI_WIFI));
600 		if (bluetooth_rfkill)
601 			rfkill_set_states(bluetooth_rfkill,
602 					  hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
603 					  hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
604 		if (wwan_rfkill)
605 			rfkill_set_states(wwan_rfkill,
606 					  hp_wmi_get_sw_state(HPWMI_WWAN),
607 					  hp_wmi_get_hw_state(HPWMI_WWAN));
608 		break;
609 	case HPWMI_CPU_BATTERY_THROTTLE:
610 		pr_info("Unimplemented CPU throttle because of 3 Cell battery event detected\n");
611 		break;
612 	case HPWMI_LOCK_SWITCH:
613 		break;
614 	case HPWMI_LID_SWITCH:
615 		break;
616 	case HPWMI_SCREEN_ROTATION:
617 		break;
618 	case HPWMI_COOLSENSE_SYSTEM_MOBILE:
619 		break;
620 	case HPWMI_COOLSENSE_SYSTEM_HOT:
621 		break;
622 	case HPWMI_PROXIMITY_SENSOR:
623 		break;
624 	case HPWMI_BACKLIT_KB_BRIGHTNESS:
625 		break;
626 	case HPWMI_PEAKSHIFT_PERIOD:
627 		break;
628 	case HPWMI_BATTERY_CHARGE_PERIOD:
629 		break;
630 	default:
631 		pr_info("Unknown event_id - %d - 0x%x\n", event_id, event_data);
632 		break;
633 	}
634 }
635 
hp_wmi_input_setup(void)636 static int __init hp_wmi_input_setup(void)
637 {
638 	acpi_status status;
639 	int err, val;
640 
641 	hp_wmi_input_dev = input_allocate_device();
642 	if (!hp_wmi_input_dev)
643 		return -ENOMEM;
644 
645 	hp_wmi_input_dev->name = "HP WMI hotkeys";
646 	hp_wmi_input_dev->phys = "wmi/input0";
647 	hp_wmi_input_dev->id.bustype = BUS_HOST;
648 
649 	__set_bit(EV_SW, hp_wmi_input_dev->evbit);
650 
651 	/* Dock */
652 	val = hp_wmi_hw_state(HPWMI_DOCK_MASK);
653 	if (!(val < 0)) {
654 		__set_bit(SW_DOCK, hp_wmi_input_dev->swbit);
655 		input_report_switch(hp_wmi_input_dev, SW_DOCK, val);
656 	}
657 
658 	/* Tablet mode */
659 	val = hp_wmi_hw_state(HPWMI_TABLET_MASK);
660 	if (!(val < 0)) {
661 		__set_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit);
662 		input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE, val);
663 	}
664 
665 	err = sparse_keymap_setup(hp_wmi_input_dev, hp_wmi_keymap, NULL);
666 	if (err)
667 		goto err_free_dev;
668 
669 	/* Set initial hardware state */
670 	input_sync(hp_wmi_input_dev);
671 
672 	if (!hp_wmi_bios_2009_later() && hp_wmi_bios_2008_later())
673 		hp_wmi_enable_hotkeys();
674 
675 	status = wmi_install_notify_handler(HPWMI_EVENT_GUID, hp_wmi_notify, NULL);
676 	if (ACPI_FAILURE(status)) {
677 		err = -EIO;
678 		goto err_free_dev;
679 	}
680 
681 	err = input_register_device(hp_wmi_input_dev);
682 	if (err)
683 		goto err_uninstall_notifier;
684 
685 	return 0;
686 
687  err_uninstall_notifier:
688 	wmi_remove_notify_handler(HPWMI_EVENT_GUID);
689  err_free_dev:
690 	input_free_device(hp_wmi_input_dev);
691 	return err;
692 }
693 
hp_wmi_input_destroy(void)694 static void hp_wmi_input_destroy(void)
695 {
696 	wmi_remove_notify_handler(HPWMI_EVENT_GUID);
697 	input_unregister_device(hp_wmi_input_dev);
698 }
699 
cleanup_sysfs(struct platform_device * device)700 static void cleanup_sysfs(struct platform_device *device)
701 {
702 	device_remove_file(&device->dev, &dev_attr_display);
703 	device_remove_file(&device->dev, &dev_attr_hddtemp);
704 	device_remove_file(&device->dev, &dev_attr_als);
705 	device_remove_file(&device->dev, &dev_attr_dock);
706 	device_remove_file(&device->dev, &dev_attr_tablet);
707 	device_remove_file(&device->dev, &dev_attr_postcode);
708 }
709 
hp_wmi_rfkill_setup(struct platform_device * device)710 static int __init hp_wmi_rfkill_setup(struct platform_device *device)
711 {
712 	int err, wireless;
713 
714 	wireless = hp_wmi_read_int(HPWMI_WIRELESS_QUERY);
715 	if (wireless < 0)
716 		return wireless;
717 
718 	err = hp_wmi_perform_query(HPWMI_WIRELESS_QUERY, HPWMI_WRITE, &wireless,
719 				   sizeof(wireless), 0);
720 	if (err)
721 		return err;
722 
723 	if (wireless & 0x1) {
724 		wifi_rfkill = rfkill_alloc("hp-wifi", &device->dev,
725 					   RFKILL_TYPE_WLAN,
726 					   &hp_wmi_rfkill_ops,
727 					   (void *) HPWMI_WIFI);
728 		if (!wifi_rfkill)
729 			return -ENOMEM;
730 		rfkill_init_sw_state(wifi_rfkill,
731 				     hp_wmi_get_sw_state(HPWMI_WIFI));
732 		rfkill_set_hw_state(wifi_rfkill,
733 				    hp_wmi_get_hw_state(HPWMI_WIFI));
734 		err = rfkill_register(wifi_rfkill);
735 		if (err)
736 			goto register_wifi_error;
737 	}
738 
739 	if (wireless & 0x2) {
740 		bluetooth_rfkill = rfkill_alloc("hp-bluetooth", &device->dev,
741 						RFKILL_TYPE_BLUETOOTH,
742 						&hp_wmi_rfkill_ops,
743 						(void *) HPWMI_BLUETOOTH);
744 		if (!bluetooth_rfkill) {
745 			err = -ENOMEM;
746 			goto register_bluetooth_error;
747 		}
748 		rfkill_init_sw_state(bluetooth_rfkill,
749 				     hp_wmi_get_sw_state(HPWMI_BLUETOOTH));
750 		rfkill_set_hw_state(bluetooth_rfkill,
751 				    hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
752 		err = rfkill_register(bluetooth_rfkill);
753 		if (err)
754 			goto register_bluetooth_error;
755 	}
756 
757 	if (wireless & 0x4) {
758 		wwan_rfkill = rfkill_alloc("hp-wwan", &device->dev,
759 					   RFKILL_TYPE_WWAN,
760 					   &hp_wmi_rfkill_ops,
761 					   (void *) HPWMI_WWAN);
762 		if (!wwan_rfkill) {
763 			err = -ENOMEM;
764 			goto register_wwan_error;
765 		}
766 		rfkill_init_sw_state(wwan_rfkill,
767 				     hp_wmi_get_sw_state(HPWMI_WWAN));
768 		rfkill_set_hw_state(wwan_rfkill,
769 				    hp_wmi_get_hw_state(HPWMI_WWAN));
770 		err = rfkill_register(wwan_rfkill);
771 		if (err)
772 			goto register_wwan_error;
773 	}
774 
775 	return 0;
776 
777 register_wwan_error:
778 	rfkill_destroy(wwan_rfkill);
779 	wwan_rfkill = NULL;
780 	if (bluetooth_rfkill)
781 		rfkill_unregister(bluetooth_rfkill);
782 register_bluetooth_error:
783 	rfkill_destroy(bluetooth_rfkill);
784 	bluetooth_rfkill = NULL;
785 	if (wifi_rfkill)
786 		rfkill_unregister(wifi_rfkill);
787 register_wifi_error:
788 	rfkill_destroy(wifi_rfkill);
789 	wifi_rfkill = NULL;
790 	return err;
791 }
792 
hp_wmi_rfkill2_setup(struct platform_device * device)793 static int __init hp_wmi_rfkill2_setup(struct platform_device *device)
794 {
795 	struct bios_rfkill2_state state;
796 	int err, i;
797 
798 	err = hp_wmi_perform_query(HPWMI_WIRELESS2_QUERY, HPWMI_READ, &state,
799 				   sizeof(state), sizeof(state));
800 	if (err)
801 		return err < 0 ? err : -EINVAL;
802 
803 	if (state.count > HPWMI_MAX_RFKILL2_DEVICES) {
804 		pr_warn("unable to parse 0x1b query output\n");
805 		return -EINVAL;
806 	}
807 
808 	for (i = 0; i < state.count; i++) {
809 		struct rfkill *rfkill;
810 		enum rfkill_type type;
811 		char *name;
812 		switch (state.device[i].radio_type) {
813 		case HPWMI_WIFI:
814 			type = RFKILL_TYPE_WLAN;
815 			name = "hp-wifi";
816 			break;
817 		case HPWMI_BLUETOOTH:
818 			type = RFKILL_TYPE_BLUETOOTH;
819 			name = "hp-bluetooth";
820 			break;
821 		case HPWMI_WWAN:
822 			type = RFKILL_TYPE_WWAN;
823 			name = "hp-wwan";
824 			break;
825 		case HPWMI_GPS:
826 			type = RFKILL_TYPE_GPS;
827 			name = "hp-gps";
828 			break;
829 		default:
830 			pr_warn("unknown device type 0x%x\n",
831 				state.device[i].radio_type);
832 			continue;
833 		}
834 
835 		if (!state.device[i].vendor_id) {
836 			pr_warn("zero device %d while %d reported\n",
837 				i, state.count);
838 			continue;
839 		}
840 
841 		rfkill = rfkill_alloc(name, &device->dev, type,
842 				      &hp_wmi_rfkill2_ops, (void *)(long)i);
843 		if (!rfkill) {
844 			err = -ENOMEM;
845 			goto fail;
846 		}
847 
848 		rfkill2[rfkill2_count].id = state.device[i].rfkill_id;
849 		rfkill2[rfkill2_count].num = i;
850 		rfkill2[rfkill2_count].rfkill = rfkill;
851 
852 		rfkill_init_sw_state(rfkill,
853 				     IS_SWBLOCKED(state.device[i].power));
854 		rfkill_set_hw_state(rfkill,
855 				    IS_HWBLOCKED(state.device[i].power));
856 
857 		if (!(state.device[i].power & HPWMI_POWER_BIOS))
858 			pr_info("device %s blocked by BIOS\n", name);
859 
860 		err = rfkill_register(rfkill);
861 		if (err) {
862 			rfkill_destroy(rfkill);
863 			goto fail;
864 		}
865 
866 		rfkill2_count++;
867 	}
868 
869 	return 0;
870 fail:
871 	for (; rfkill2_count > 0; rfkill2_count--) {
872 		rfkill_unregister(rfkill2[rfkill2_count - 1].rfkill);
873 		rfkill_destroy(rfkill2[rfkill2_count - 1].rfkill);
874 	}
875 	return err;
876 }
877 
hp_wmi_bios_setup(struct platform_device * device)878 static int __init hp_wmi_bios_setup(struct platform_device *device)
879 {
880 	int err;
881 
882 	/* clear detected rfkill devices */
883 	wifi_rfkill = NULL;
884 	bluetooth_rfkill = NULL;
885 	wwan_rfkill = NULL;
886 	rfkill2_count = 0;
887 
888 	if (hp_wmi_rfkill_setup(device))
889 		hp_wmi_rfkill2_setup(device);
890 
891 	err = device_create_file(&device->dev, &dev_attr_display);
892 	if (err)
893 		goto add_sysfs_error;
894 	err = device_create_file(&device->dev, &dev_attr_hddtemp);
895 	if (err)
896 		goto add_sysfs_error;
897 	err = device_create_file(&device->dev, &dev_attr_als);
898 	if (err)
899 		goto add_sysfs_error;
900 	err = device_create_file(&device->dev, &dev_attr_dock);
901 	if (err)
902 		goto add_sysfs_error;
903 	err = device_create_file(&device->dev, &dev_attr_tablet);
904 	if (err)
905 		goto add_sysfs_error;
906 	err = device_create_file(&device->dev, &dev_attr_postcode);
907 	if (err)
908 		goto add_sysfs_error;
909 	return 0;
910 
911 add_sysfs_error:
912 	cleanup_sysfs(device);
913 	return err;
914 }
915 
hp_wmi_bios_remove(struct platform_device * device)916 static int __exit hp_wmi_bios_remove(struct platform_device *device)
917 {
918 	int i;
919 	cleanup_sysfs(device);
920 
921 	for (i = 0; i < rfkill2_count; i++) {
922 		rfkill_unregister(rfkill2[i].rfkill);
923 		rfkill_destroy(rfkill2[i].rfkill);
924 	}
925 
926 	if (wifi_rfkill) {
927 		rfkill_unregister(wifi_rfkill);
928 		rfkill_destroy(wifi_rfkill);
929 	}
930 	if (bluetooth_rfkill) {
931 		rfkill_unregister(bluetooth_rfkill);
932 		rfkill_destroy(bluetooth_rfkill);
933 	}
934 	if (wwan_rfkill) {
935 		rfkill_unregister(wwan_rfkill);
936 		rfkill_destroy(wwan_rfkill);
937 	}
938 
939 	return 0;
940 }
941 
hp_wmi_resume_handler(struct device * device)942 static int hp_wmi_resume_handler(struct device *device)
943 {
944 	/*
945 	 * Hardware state may have changed while suspended, so trigger
946 	 * input events for the current state. As this is a switch,
947 	 * the input layer will only actually pass it on if the state
948 	 * changed.
949 	 */
950 	if (hp_wmi_input_dev) {
951 		if (test_bit(SW_DOCK, hp_wmi_input_dev->swbit))
952 			input_report_switch(hp_wmi_input_dev, SW_DOCK,
953 					    hp_wmi_hw_state(HPWMI_DOCK_MASK));
954 		if (test_bit(SW_TABLET_MODE, hp_wmi_input_dev->swbit))
955 			input_report_switch(hp_wmi_input_dev, SW_TABLET_MODE,
956 					    hp_wmi_hw_state(HPWMI_TABLET_MASK));
957 		input_sync(hp_wmi_input_dev);
958 	}
959 
960 	if (rfkill2_count)
961 		hp_wmi_rfkill2_refresh();
962 
963 	if (wifi_rfkill)
964 		rfkill_set_states(wifi_rfkill,
965 				  hp_wmi_get_sw_state(HPWMI_WIFI),
966 				  hp_wmi_get_hw_state(HPWMI_WIFI));
967 	if (bluetooth_rfkill)
968 		rfkill_set_states(bluetooth_rfkill,
969 				  hp_wmi_get_sw_state(HPWMI_BLUETOOTH),
970 				  hp_wmi_get_hw_state(HPWMI_BLUETOOTH));
971 	if (wwan_rfkill)
972 		rfkill_set_states(wwan_rfkill,
973 				  hp_wmi_get_sw_state(HPWMI_WWAN),
974 				  hp_wmi_get_hw_state(HPWMI_WWAN));
975 
976 	return 0;
977 }
978 
979 static const struct dev_pm_ops hp_wmi_pm_ops = {
980 	.resume  = hp_wmi_resume_handler,
981 	.restore  = hp_wmi_resume_handler,
982 };
983 
984 static struct platform_driver hp_wmi_driver = {
985 	.driver = {
986 		.name = "hp-wmi",
987 		.pm = &hp_wmi_pm_ops,
988 	},
989 	.remove = __exit_p(hp_wmi_bios_remove),
990 };
991 
hp_wmi_init(void)992 static int __init hp_wmi_init(void)
993 {
994 	int event_capable = wmi_has_guid(HPWMI_EVENT_GUID);
995 	int bios_capable = wmi_has_guid(HPWMI_BIOS_GUID);
996 	int err;
997 
998 	if (!bios_capable && !event_capable)
999 		return -ENODEV;
1000 
1001 	if (event_capable) {
1002 		err = hp_wmi_input_setup();
1003 		if (err)
1004 			return err;
1005 	}
1006 
1007 	if (bios_capable) {
1008 		hp_wmi_platform_dev =
1009 			platform_device_register_simple("hp-wmi", -1, NULL, 0);
1010 		if (IS_ERR(hp_wmi_platform_dev)) {
1011 			err = PTR_ERR(hp_wmi_platform_dev);
1012 			goto err_destroy_input;
1013 		}
1014 
1015 		err = platform_driver_probe(&hp_wmi_driver, hp_wmi_bios_setup);
1016 		if (err)
1017 			goto err_unregister_device;
1018 	}
1019 
1020 	return 0;
1021 
1022 err_unregister_device:
1023 	platform_device_unregister(hp_wmi_platform_dev);
1024 err_destroy_input:
1025 	if (event_capable)
1026 		hp_wmi_input_destroy();
1027 
1028 	return err;
1029 }
1030 module_init(hp_wmi_init);
1031 
hp_wmi_exit(void)1032 static void __exit hp_wmi_exit(void)
1033 {
1034 	if (wmi_has_guid(HPWMI_EVENT_GUID))
1035 		hp_wmi_input_destroy();
1036 
1037 	if (hp_wmi_platform_dev) {
1038 		platform_device_unregister(hp_wmi_platform_dev);
1039 		platform_driver_unregister(&hp_wmi_driver);
1040 	}
1041 }
1042 module_exit(hp_wmi_exit);
1043