1 // SPDX-License-Identifier: GPL-2.0+
3 * HID driver for Google Hammer device.
5 * Copyright (c) 2017 Google Inc.
6 * Author: Wei-Ning Huang <wnhuang@google.com>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
16 #include <linux/acpi.h>
17 #include <linux/hid.h>
18 #include <linux/input/vivaldi-fmap.h>
19 #include <linux/leds.h>
20 #include <linux/module.h>
22 #include <linux/platform_data/cros_ec_commands.h>
23 #include <linux/platform_data/cros_ec_proto.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_wakeup.h>
26 #include <asm/unaligned.h>
29 #include "hid-vivaldi-common.h"
32 * C(hrome)B(ase)A(ttached)S(witch) - switch exported by Chrome EC and reporting
33 * state of the "Whiskers" base - attached or detached. Whiskers USB device also
34 * reports position of the keyboard - folded or not. Combining base state and
35 * position allows us to generate proper "Tablet mode" events.
38 struct device
*dev
; /* The platform device (EC) */
39 struct input_dev
*input
;
42 struct notifier_block notifier
;
45 static struct cbas_ec cbas_ec
;
46 static DEFINE_SPINLOCK(cbas_ec_lock
);
47 static DEFINE_MUTEX(cbas_ec_reglock
);
49 static bool cbas_parse_base_state(const void *data
)
51 u32 switches
= get_unaligned_le32(data
);
53 return !!(switches
& BIT(EC_MKBP_BASE_ATTACHED
));
56 static int cbas_ec_query_base(struct cros_ec_device
*ec_dev
, bool get_state
,
59 struct ec_params_mkbp_info
*params
;
60 struct cros_ec_command
*msg
;
63 msg
= kzalloc(struct_size(msg
, data
, max(sizeof(u32
), sizeof(*params
))),
68 msg
->command
= EC_CMD_MKBP_INFO
;
70 msg
->outsize
= sizeof(*params
);
71 msg
->insize
= sizeof(u32
);
72 params
= (struct ec_params_mkbp_info
*)msg
->data
;
73 params
->info_type
= get_state
?
74 EC_MKBP_INFO_CURRENT
: EC_MKBP_INFO_SUPPORTED
;
75 params
->event_type
= EC_MKBP_EVENT_SWITCH
;
77 ret
= cros_ec_cmd_xfer_status(ec_dev
, msg
);
79 if (ret
!= sizeof(u32
)) {
80 dev_warn(ec_dev
->dev
, "wrong result size: %d != %zu\n",
84 *state
= cbas_parse_base_state(msg
->data
);
94 static int cbas_ec_notify(struct notifier_block
*nb
,
95 unsigned long queued_during_suspend
,
98 struct cros_ec_device
*ec
= _notify
;
102 if (ec
->event_data
.event_type
== EC_MKBP_EVENT_SWITCH
) {
103 base_present
= cbas_parse_base_state(
104 &ec
->event_data
.data
.switches
);
106 "%s: base: %d\n", __func__
, base_present
);
108 if (device_may_wakeup(cbas_ec
.dev
) ||
109 !queued_during_suspend
) {
111 pm_wakeup_event(cbas_ec
.dev
, 0);
113 spin_lock_irqsave(&cbas_ec_lock
, flags
);
116 * While input layer dedupes the events, we do not want
117 * to disrupt the state reported by the base by
118 * overriding it with state reported by the LID. Only
119 * report changes, as we assume that on attach the base
122 if (base_present
!= cbas_ec
.base_present
) {
123 input_report_switch(cbas_ec
.input
,
126 input_sync(cbas_ec
.input
);
127 cbas_ec
.base_present
= base_present
;
130 spin_unlock_irqrestore(&cbas_ec_lock
, flags
);
137 static __maybe_unused
int cbas_ec_resume(struct device
*dev
)
139 struct cros_ec_device
*ec
= dev_get_drvdata(dev
->parent
);
143 error
= cbas_ec_query_base(ec
, true, &base_present
);
145 dev_warn(dev
, "failed to fetch base state on resume: %d\n",
148 spin_lock_irq(&cbas_ec_lock
);
150 cbas_ec
.base_present
= base_present
;
153 * Only report if base is disconnected. If base is connected,
154 * it will resend its state on resume, and we'll update it
157 if (!cbas_ec
.base_present
) {
158 input_report_switch(cbas_ec
.input
, SW_TABLET_MODE
, 1);
159 input_sync(cbas_ec
.input
);
162 spin_unlock_irq(&cbas_ec_lock
);
168 static SIMPLE_DEV_PM_OPS(cbas_ec_pm_ops
, NULL
, cbas_ec_resume
);
170 static void cbas_ec_set_input(struct input_dev
*input
)
172 /* Take the lock so hammer_event() does not race with us here */
173 spin_lock_irq(&cbas_ec_lock
);
174 cbas_ec
.input
= input
;
175 spin_unlock_irq(&cbas_ec_lock
);
178 static int __cbas_ec_probe(struct platform_device
*pdev
)
180 struct cros_ec_device
*ec
= dev_get_drvdata(pdev
->dev
.parent
);
181 struct input_dev
*input
;
185 error
= cbas_ec_query_base(ec
, false, &base_supported
);
192 input
= devm_input_allocate_device(&pdev
->dev
);
196 input
->name
= "Whiskers Tablet Mode Switch";
197 input
->id
.bustype
= BUS_HOST
;
199 input_set_capability(input
, EV_SW
, SW_TABLET_MODE
);
201 error
= input_register_device(input
);
203 dev_err(&pdev
->dev
, "cannot register input device: %d\n",
209 error
= cbas_ec_query_base(ec
, true, &cbas_ec
.base_present
);
211 dev_err(&pdev
->dev
, "cannot query base state: %d\n", error
);
215 if (!cbas_ec
.base_present
)
216 cbas_ec
.base_folded
= false;
218 dev_dbg(&pdev
->dev
, "%s: base: %d, folded: %d\n", __func__
,
219 cbas_ec
.base_present
, cbas_ec
.base_folded
);
221 input_report_switch(input
, SW_TABLET_MODE
,
222 !cbas_ec
.base_present
|| cbas_ec
.base_folded
);
224 cbas_ec_set_input(input
);
226 cbas_ec
.dev
= &pdev
->dev
;
227 cbas_ec
.notifier
.notifier_call
= cbas_ec_notify
;
228 error
= blocking_notifier_chain_register(&ec
->event_notifier
,
231 dev_err(&pdev
->dev
, "cannot register notifier: %d\n", error
);
232 cbas_ec_set_input(NULL
);
236 device_init_wakeup(&pdev
->dev
, true);
240 static int cbas_ec_probe(struct platform_device
*pdev
)
244 mutex_lock(&cbas_ec_reglock
);
251 retval
= __cbas_ec_probe(pdev
);
254 mutex_unlock(&cbas_ec_reglock
);
258 static void cbas_ec_remove(struct platform_device
*pdev
)
260 struct cros_ec_device
*ec
= dev_get_drvdata(pdev
->dev
.parent
);
262 mutex_lock(&cbas_ec_reglock
);
264 blocking_notifier_chain_unregister(&ec
->event_notifier
,
266 cbas_ec_set_input(NULL
);
268 mutex_unlock(&cbas_ec_reglock
);
271 static const struct acpi_device_id cbas_ec_acpi_ids
[] = {
275 MODULE_DEVICE_TABLE(acpi
, cbas_ec_acpi_ids
);
278 static const struct of_device_id cbas_ec_of_match
[] = {
279 { .compatible
= "google,cros-cbas" },
282 MODULE_DEVICE_TABLE(of
, cbas_ec_of_match
);
285 static struct platform_driver cbas_ec_driver
= {
286 .probe
= cbas_ec_probe
,
287 .remove_new
= cbas_ec_remove
,
290 .acpi_match_table
= ACPI_PTR(cbas_ec_acpi_ids
),
291 .of_match_table
= of_match_ptr(cbas_ec_of_match
),
292 .pm
= &cbas_ec_pm_ops
,
296 #define MAX_BRIGHTNESS 100
298 struct hammer_kbd_leds
{
299 struct led_classdev cdev
;
300 struct hid_device
*hdev
;
301 u8 buf
[2] ____cacheline_aligned
;
304 static int hammer_kbd_brightness_set_blocking(struct led_classdev
*cdev
,
305 enum led_brightness br
)
307 struct hammer_kbd_leds
*led
= container_of(cdev
,
308 struct hammer_kbd_leds
,
316 * Request USB HID device to be in Full On mode, so that sending
317 * hardware output report and hardware raw request won't fail.
319 ret
= hid_hw_power(led
->hdev
, PM_HINT_FULLON
);
321 hid_err(led
->hdev
, "failed: device not resumed %d\n", ret
);
325 ret
= hid_hw_output_report(led
->hdev
, led
->buf
, sizeof(led
->buf
));
327 ret
= hid_hw_raw_request(led
->hdev
, 0, led
->buf
,
332 hid_err(led
->hdev
, "failed to set keyboard backlight: %d\n",
335 /* Request USB HID device back to Normal Mode. */
336 hid_hw_power(led
->hdev
, PM_HINT_NORMAL
);
341 static int hammer_register_leds(struct hid_device
*hdev
)
343 struct hammer_kbd_leds
*kbd_backlight
;
345 kbd_backlight
= devm_kzalloc(&hdev
->dev
, sizeof(*kbd_backlight
),
350 kbd_backlight
->hdev
= hdev
;
351 kbd_backlight
->cdev
.name
= "hammer::kbd_backlight";
352 kbd_backlight
->cdev
.max_brightness
= MAX_BRIGHTNESS
;
353 kbd_backlight
->cdev
.brightness_set_blocking
=
354 hammer_kbd_brightness_set_blocking
;
355 kbd_backlight
->cdev
.flags
= LED_HW_PLUGGABLE
;
357 /* Set backlight to 0% initially. */
358 hammer_kbd_brightness_set_blocking(&kbd_backlight
->cdev
, 0);
360 return devm_led_classdev_register(&hdev
->dev
, &kbd_backlight
->cdev
);
363 #define HID_UP_GOOGLEVENDOR 0xffd10000
364 #define HID_VD_KBD_FOLDED 0x00000019
365 #define HID_USAGE_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
367 /* HID usage for keyboard backlight (Alphanumeric display brightness) */
368 #define HID_AD_BRIGHTNESS 0x00140046
370 static int hammer_input_mapping(struct hid_device
*hdev
, struct hid_input
*hi
,
371 struct hid_field
*field
,
372 struct hid_usage
*usage
,
373 unsigned long **bit
, int *max
)
375 if (usage
->hid
== HID_USAGE_KBD_FOLDED
) {
377 * We do not want to have this usage mapped as it will get
378 * mixed in with "base attached" signal and delivered over
379 * separate input device for tablet switch mode.
387 static void hammer_folded_event(struct hid_device
*hdev
, bool folded
)
391 spin_lock_irqsave(&cbas_ec_lock
, flags
);
394 * If we are getting events from Whiskers that means that it
395 * is attached to the lid.
397 cbas_ec
.base_present
= true;
398 cbas_ec
.base_folded
= folded
;
399 hid_dbg(hdev
, "%s: base: %d, folded: %d\n", __func__
,
400 cbas_ec
.base_present
, cbas_ec
.base_folded
);
403 input_report_switch(cbas_ec
.input
, SW_TABLET_MODE
, folded
);
404 input_sync(cbas_ec
.input
);
407 spin_unlock_irqrestore(&cbas_ec_lock
, flags
);
410 static int hammer_event(struct hid_device
*hid
, struct hid_field
*field
,
411 struct hid_usage
*usage
, __s32 value
)
413 if (usage
->hid
== HID_USAGE_KBD_FOLDED
) {
414 hammer_folded_event(hid
, value
);
415 return 1; /* We handled this event */
421 static bool hammer_has_usage(struct hid_device
*hdev
, unsigned int report_type
,
422 unsigned application
, unsigned usage
)
424 struct hid_report_enum
*re
= &hdev
->report_enum
[report_type
];
425 struct hid_report
*report
;
428 list_for_each_entry(report
, &re
->report_list
, list
) {
429 if (report
->application
!= application
)
432 for (i
= 0; i
< report
->maxfield
; i
++) {
433 struct hid_field
*field
= report
->field
[i
];
435 for (j
= 0; j
< field
->maxusage
; j
++)
436 if (field
->usage
[j
].hid
== usage
)
444 static bool hammer_has_folded_event(struct hid_device
*hdev
)
446 return hammer_has_usage(hdev
, HID_INPUT_REPORT
,
447 HID_GD_KEYBOARD
, HID_USAGE_KBD_FOLDED
);
450 static bool hammer_has_backlight_control(struct hid_device
*hdev
)
452 return hammer_has_usage(hdev
, HID_OUTPUT_REPORT
,
453 HID_GD_KEYBOARD
, HID_AD_BRIGHTNESS
);
456 static void hammer_get_folded_state(struct hid_device
*hdev
)
458 struct hid_report
*report
;
463 report
= hdev
->report_enum
[HID_INPUT_REPORT
].report_id_hash
[0x0];
465 if (!report
|| report
->maxfield
< 1)
468 len
= hid_report_len(report
) + 1;
470 buf
= kmalloc(len
, GFP_KERNEL
);
474 rlen
= hid_hw_raw_request(hdev
, report
->id
, buf
, len
, report
->type
, HID_REQ_GET_REPORT
);
477 hid_warn(hdev
, "Unable to read base folded state: %d (expected %d)\n", rlen
, len
);
481 for (a
= 0; a
< report
->maxfield
; a
++) {
482 struct hid_field
*field
= report
->field
[a
];
484 if (field
->usage
->hid
== HID_USAGE_KBD_FOLDED
) {
485 u32 value
= hid_field_extract(hdev
, buf
+1,
486 field
->report_offset
, field
->report_size
);
488 hammer_folded_event(hdev
, value
);
497 static void hammer_stop(void *hdev
)
502 static int hammer_probe(struct hid_device
*hdev
,
503 const struct hid_device_id
*id
)
505 struct vivaldi_data
*vdata
;
508 vdata
= devm_kzalloc(&hdev
->dev
, sizeof(*vdata
), GFP_KERNEL
);
512 hid_set_drvdata(hdev
, vdata
);
514 error
= hid_parse(hdev
);
518 error
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
522 error
= devm_add_action(&hdev
->dev
, hammer_stop
, hdev
);
527 * We always want to poll for, and handle tablet mode events from
528 * devices that have folded usage, even when nobody has opened the input
529 * device. This also prevents the hid core from dropping early tablet
530 * mode events from the device.
532 if (hammer_has_folded_event(hdev
)) {
533 hdev
->quirks
|= HID_QUIRK_ALWAYS_POLL
;
534 error
= hid_hw_open(hdev
);
538 hammer_get_folded_state(hdev
);
541 if (hammer_has_backlight_control(hdev
)) {
542 error
= hammer_register_leds(hdev
);
545 "Failed to register keyboard backlight: %d\n",
552 static void hammer_remove(struct hid_device
*hdev
)
556 if (hammer_has_folded_event(hdev
)) {
560 * If we are disconnecting then most likely Whiskers is
561 * being removed. Even if it is not removed, without proper
562 * keyboard we should not stay in clamshell mode.
564 * The reason for doing it here and not waiting for signal
565 * from EC, is that on some devices there are high leakage
566 * on Whiskers pins and we do not detect disconnect reliably,
567 * resulting in devices being stuck in clamshell mode.
569 spin_lock_irqsave(&cbas_ec_lock
, flags
);
570 if (cbas_ec
.input
&& cbas_ec
.base_present
) {
571 input_report_switch(cbas_ec
.input
, SW_TABLET_MODE
, 1);
572 input_sync(cbas_ec
.input
);
574 cbas_ec
.base_present
= false;
575 spin_unlock_irqrestore(&cbas_ec_lock
, flags
);
578 /* Unregistering LEDs and stopping the hardware is done via devm */
581 static const struct hid_device_id hammer_devices
[] = {
582 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
583 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_DON
) },
584 { HID_DEVICE(BUS_USB
, HID_GROUP_VIVALDI
,
585 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_EEL
) },
586 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
587 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_HAMMER
) },
588 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
589 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_JEWEL
) },
590 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
591 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_MAGNEMITE
) },
592 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
593 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_MASTERBALL
) },
594 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
595 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_MOONBALL
) },
596 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
597 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_STAFF
) },
598 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
599 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_WAND
) },
600 { HID_DEVICE(BUS_USB
, HID_GROUP_GENERIC
,
601 USB_VENDOR_ID_GOOGLE
, USB_DEVICE_ID_GOOGLE_WHISKERS
) },
604 MODULE_DEVICE_TABLE(hid
, hammer_devices
);
606 static struct hid_driver hammer_driver
= {
608 .id_table
= hammer_devices
,
609 .probe
= hammer_probe
,
610 .remove
= hammer_remove
,
611 .feature_mapping
= vivaldi_feature_mapping
,
612 .input_mapping
= hammer_input_mapping
,
613 .event
= hammer_event
,
615 .dev_groups
= vivaldi_attribute_groups
,
619 static int __init
hammer_init(void)
623 error
= platform_driver_register(&cbas_ec_driver
);
627 error
= hid_register_driver(&hammer_driver
);
629 platform_driver_unregister(&cbas_ec_driver
);
635 module_init(hammer_init
);
637 static void __exit
hammer_exit(void)
639 hid_unregister_driver(&hammer_driver
);
640 platform_driver_unregister(&cbas_ec_driver
);
642 module_exit(hammer_exit
);
644 MODULE_LICENSE("GPL");