Linux 5.1.15
[linux/fpc-iii.git] / drivers / hid / hid-google-hammer.c
blobee5e0bdcf078f7a05682ba45381d0f421dfde882
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * HID driver for Google Hammer device.
5 * Copyright (c) 2017 Google Inc.
6 * Author: Wei-Ning Huang <wnhuang@google.com>
7 */
9 /*
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)
13 * any later version.
16 #include <linux/acpi.h>
17 #include <linux/hid.h>
18 #include <linux/leds.h>
19 #include <linux/mfd/cros_ec.h>
20 #include <linux/mfd/cros_ec_commands.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_wakeup.h>
24 #include <asm/unaligned.h>
26 #include "hid-ids.h"
29 * C(hrome)B(ase)A(ttached)S(witch) - switch exported by Chrome EC and reporting
30 * state of the "Whiskers" base - attached or detached. Whiskers USB device also
31 * reports position of the keyboard - folded or not. Combining base state and
32 * position allows us to generate proper "Tablet mode" events.
34 struct cbas_ec {
35 struct device *dev; /* The platform device (EC) */
36 struct input_dev *input;
37 bool base_present;
38 struct notifier_block notifier;
41 static struct cbas_ec cbas_ec;
42 static DEFINE_SPINLOCK(cbas_ec_lock);
43 static DEFINE_MUTEX(cbas_ec_reglock);
45 static bool cbas_parse_base_state(const void *data)
47 u32 switches = get_unaligned_le32(data);
49 return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
52 static int cbas_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
53 bool *state)
55 struct ec_params_mkbp_info *params;
56 struct cros_ec_command *msg;
57 int ret;
59 msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)),
60 GFP_KERNEL);
61 if (!msg)
62 return -ENOMEM;
64 msg->command = EC_CMD_MKBP_INFO;
65 msg->version = 1;
66 msg->outsize = sizeof(*params);
67 msg->insize = sizeof(u32);
68 params = (struct ec_params_mkbp_info *)msg->data;
69 params->info_type = get_state ?
70 EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED;
71 params->event_type = EC_MKBP_EVENT_SWITCH;
73 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
74 if (ret >= 0) {
75 if (ret != sizeof(u32)) {
76 dev_warn(ec_dev->dev, "wrong result size: %d != %zu\n",
77 ret, sizeof(u32));
78 ret = -EPROTO;
79 } else {
80 *state = cbas_parse_base_state(msg->data);
81 ret = 0;
85 kfree(msg);
87 return ret;
90 static int cbas_ec_notify(struct notifier_block *nb,
91 unsigned long queued_during_suspend,
92 void *_notify)
94 struct cros_ec_device *ec = _notify;
95 unsigned long flags;
96 bool base_present;
98 if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) {
99 base_present = cbas_parse_base_state(
100 &ec->event_data.data.switches);
101 dev_dbg(cbas_ec.dev,
102 "%s: base: %d\n", __func__, base_present);
104 if (device_may_wakeup(cbas_ec.dev) ||
105 !queued_during_suspend) {
107 pm_wakeup_event(cbas_ec.dev, 0);
109 spin_lock_irqsave(&cbas_ec_lock, flags);
112 * While input layer dedupes the events, we do not want
113 * to disrupt the state reported by the base by
114 * overriding it with state reported by the LID. Only
115 * report changes, as we assume that on attach the base
116 * is not folded.
118 if (base_present != cbas_ec.base_present) {
119 input_report_switch(cbas_ec.input,
120 SW_TABLET_MODE,
121 !base_present);
122 input_sync(cbas_ec.input);
123 cbas_ec.base_present = base_present;
126 spin_unlock_irqrestore(&cbas_ec_lock, flags);
130 return NOTIFY_OK;
133 static __maybe_unused int cbas_ec_resume(struct device *dev)
135 struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
136 bool base_present;
137 int error;
139 error = cbas_ec_query_base(ec, true, &base_present);
140 if (error) {
141 dev_warn(dev, "failed to fetch base state on resume: %d\n",
142 error);
143 } else {
144 spin_lock_irq(&cbas_ec_lock);
146 cbas_ec.base_present = base_present;
149 * Only report if base is disconnected. If base is connected,
150 * it will resend its state on resume, and we'll update it
151 * in hammer_event().
153 if (!cbas_ec.base_present) {
154 input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1);
155 input_sync(cbas_ec.input);
158 spin_unlock_irq(&cbas_ec_lock);
161 return 0;
164 static SIMPLE_DEV_PM_OPS(cbas_ec_pm_ops, NULL, cbas_ec_resume);
166 static void cbas_ec_set_input(struct input_dev *input)
168 /* Take the lock so hammer_event() does not race with us here */
169 spin_lock_irq(&cbas_ec_lock);
170 cbas_ec.input = input;
171 spin_unlock_irq(&cbas_ec_lock);
174 static int __cbas_ec_probe(struct platform_device *pdev)
176 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
177 struct input_dev *input;
178 bool base_supported;
179 int error;
181 error = cbas_ec_query_base(ec, false, &base_supported);
182 if (error)
183 return error;
185 if (!base_supported)
186 return -ENXIO;
188 input = devm_input_allocate_device(&pdev->dev);
189 if (!input)
190 return -ENOMEM;
192 input->name = "Whiskers Tablet Mode Switch";
193 input->id.bustype = BUS_HOST;
195 input_set_capability(input, EV_SW, SW_TABLET_MODE);
197 error = input_register_device(input);
198 if (error) {
199 dev_err(&pdev->dev, "cannot register input device: %d\n",
200 error);
201 return error;
204 /* Seed the state */
205 error = cbas_ec_query_base(ec, true, &cbas_ec.base_present);
206 if (error) {
207 dev_err(&pdev->dev, "cannot query base state: %d\n", error);
208 return error;
211 input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present);
213 cbas_ec_set_input(input);
215 cbas_ec.dev = &pdev->dev;
216 cbas_ec.notifier.notifier_call = cbas_ec_notify;
217 error = blocking_notifier_chain_register(&ec->event_notifier,
218 &cbas_ec.notifier);
219 if (error) {
220 dev_err(&pdev->dev, "cannot register notifier: %d\n", error);
221 cbas_ec_set_input(NULL);
222 return error;
225 device_init_wakeup(&pdev->dev, true);
226 return 0;
229 static int cbas_ec_probe(struct platform_device *pdev)
231 int retval;
233 mutex_lock(&cbas_ec_reglock);
235 if (cbas_ec.input) {
236 retval = -EBUSY;
237 goto out;
240 retval = __cbas_ec_probe(pdev);
242 out:
243 mutex_unlock(&cbas_ec_reglock);
244 return retval;
247 static int cbas_ec_remove(struct platform_device *pdev)
249 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
251 mutex_lock(&cbas_ec_reglock);
253 blocking_notifier_chain_unregister(&ec->event_notifier,
254 &cbas_ec.notifier);
255 cbas_ec_set_input(NULL);
257 mutex_unlock(&cbas_ec_reglock);
258 return 0;
261 static const struct acpi_device_id cbas_ec_acpi_ids[] = {
262 { "GOOG000B", 0 },
265 MODULE_DEVICE_TABLE(acpi, cbas_ec_acpi_ids);
267 static struct platform_driver cbas_ec_driver = {
268 .probe = cbas_ec_probe,
269 .remove = cbas_ec_remove,
270 .driver = {
271 .name = "cbas_ec",
272 .acpi_match_table = ACPI_PTR(cbas_ec_acpi_ids),
273 .pm = &cbas_ec_pm_ops,
277 #define MAX_BRIGHTNESS 100
279 struct hammer_kbd_leds {
280 struct led_classdev cdev;
281 struct hid_device *hdev;
282 u8 buf[2] ____cacheline_aligned;
285 static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev,
286 enum led_brightness br)
288 struct hammer_kbd_leds *led = container_of(cdev,
289 struct hammer_kbd_leds,
290 cdev);
291 int ret;
293 led->buf[0] = 0;
294 led->buf[1] = br;
297 * Request USB HID device to be in Full On mode, so that sending
298 * hardware output report and hardware raw request won't fail.
300 ret = hid_hw_power(led->hdev, PM_HINT_FULLON);
301 if (ret < 0) {
302 hid_err(led->hdev, "failed: device not resumed %d\n", ret);
303 return ret;
306 ret = hid_hw_output_report(led->hdev, led->buf, sizeof(led->buf));
307 if (ret == -ENOSYS)
308 ret = hid_hw_raw_request(led->hdev, 0, led->buf,
309 sizeof(led->buf),
310 HID_OUTPUT_REPORT,
311 HID_REQ_SET_REPORT);
312 if (ret < 0)
313 hid_err(led->hdev, "failed to set keyboard backlight: %d\n",
314 ret);
316 /* Request USB HID device back to Normal Mode. */
317 hid_hw_power(led->hdev, PM_HINT_NORMAL);
319 return ret;
322 static int hammer_register_leds(struct hid_device *hdev)
324 struct hammer_kbd_leds *kbd_backlight;
326 kbd_backlight = devm_kzalloc(&hdev->dev,
327 sizeof(*kbd_backlight),
328 GFP_KERNEL);
329 if (!kbd_backlight)
330 return -ENOMEM;
332 kbd_backlight->hdev = hdev;
333 kbd_backlight->cdev.name = "hammer::kbd_backlight";
334 kbd_backlight->cdev.max_brightness = MAX_BRIGHTNESS;
335 kbd_backlight->cdev.brightness_set_blocking =
336 hammer_kbd_brightness_set_blocking;
337 kbd_backlight->cdev.flags = LED_HW_PLUGGABLE;
339 /* Set backlight to 0% initially. */
340 hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0);
342 return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
345 #define HID_UP_GOOGLEVENDOR 0xffd10000
346 #define HID_VD_KBD_FOLDED 0x00000019
347 #define WHISKERS_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED)
349 /* HID usage for keyboard backlight (Alphanumeric display brightness) */
350 #define HID_AD_BRIGHTNESS 0x00140046
352 static int hammer_input_mapping(struct hid_device *hdev, struct hid_input *hi,
353 struct hid_field *field,
354 struct hid_usage *usage,
355 unsigned long **bit, int *max)
357 if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
358 usage->hid == WHISKERS_KBD_FOLDED) {
360 * We do not want to have this usage mapped as it will get
361 * mixed in with "base attached" signal and delivered over
362 * separate input device for tablet switch mode.
364 return -1;
367 return 0;
370 static int hammer_event(struct hid_device *hid, struct hid_field *field,
371 struct hid_usage *usage, __s32 value)
373 unsigned long flags;
375 if (hid->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
376 usage->hid == WHISKERS_KBD_FOLDED) {
377 spin_lock_irqsave(&cbas_ec_lock, flags);
379 hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
380 cbas_ec.base_present, value);
383 * We should not get event if base is detached, but in case
384 * we happen to service HID and EC notifications out of order
385 * let's still check the "base present" flag.
387 if (cbas_ec.input && cbas_ec.base_present) {
388 input_report_switch(cbas_ec.input,
389 SW_TABLET_MODE, value);
390 input_sync(cbas_ec.input);
393 spin_unlock_irqrestore(&cbas_ec_lock, flags);
394 return 1; /* We handled this event */
397 return 0;
400 static bool hammer_is_keyboard_interface(struct hid_device *hdev)
402 struct hid_report_enum *re = &hdev->report_enum[HID_INPUT_REPORT];
403 struct hid_report *report;
405 list_for_each_entry(report, &re->report_list, list)
406 if (report->application == HID_GD_KEYBOARD)
407 return true;
409 return false;
412 static bool hammer_has_backlight_control(struct hid_device *hdev)
414 struct hid_report_enum *re = &hdev->report_enum[HID_OUTPUT_REPORT];
415 struct hid_report *report;
416 int i, j;
418 list_for_each_entry(report, &re->report_list, list) {
419 if (report->application != HID_GD_KEYBOARD)
420 continue;
422 for (i = 0; i < report->maxfield; i++) {
423 struct hid_field *field = report->field[i];
425 for (j = 0; j < field->maxusage; j++)
426 if (field->usage[j].hid == HID_AD_BRIGHTNESS)
427 return true;
431 return false;
434 static int hammer_probe(struct hid_device *hdev,
435 const struct hid_device_id *id)
437 int error;
440 * We always want to poll for, and handle tablet mode events from
441 * Whiskers, even when nobody has opened the input device. This also
442 * prevents the hid core from dropping early tablet mode events from
443 * the device.
445 if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
446 hammer_is_keyboard_interface(hdev))
447 hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
449 error = hid_parse(hdev);
450 if (error)
451 return error;
453 error = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
454 if (error)
455 return error;
457 if (hammer_has_backlight_control(hdev)) {
458 error = hammer_register_leds(hdev);
459 if (error)
460 hid_warn(hdev,
461 "Failed to register keyboard backlight: %d\n",
462 error);
465 return 0;
469 static const struct hid_device_id hammer_devices[] = {
470 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
471 USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_HAMMER) },
472 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
473 USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
474 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
475 USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
476 { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
477 USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
480 MODULE_DEVICE_TABLE(hid, hammer_devices);
482 static struct hid_driver hammer_driver = {
483 .name = "hammer",
484 .id_table = hammer_devices,
485 .probe = hammer_probe,
486 .input_mapping = hammer_input_mapping,
487 .event = hammer_event,
490 static int __init hammer_init(void)
492 int error;
494 error = platform_driver_register(&cbas_ec_driver);
495 if (error)
496 return error;
498 error = hid_register_driver(&hammer_driver);
499 if (error) {
500 platform_driver_unregister(&cbas_ec_driver);
501 return error;
504 return 0;
506 module_init(hammer_init);
508 static void __exit hammer_exit(void)
510 hid_unregister_driver(&hammer_driver);
511 platform_driver_unregister(&cbas_ec_driver);
513 module_exit(hammer_exit);
515 MODULE_LICENSE("GPL");