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