Blame view
drivers/hid/hid-google-hammer.c
14.2 KB
bc774b8c1 HID: google: add ... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// SPDX-License-Identifier: GPL-2.0+ /* * HID driver for Google Hammer device. * * Copyright (c) 2017 Google Inc. * Author: Wei-Ning Huang <wnhuang@google.com> */ /* * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. */ |
eb1aac4c8 HID: google: add ... |
15 |
#include <linux/acpi.h> |
bc774b8c1 HID: google: add ... |
16 17 18 |
#include <linux/hid.h> #include <linux/leds.h> #include <linux/module.h> |
840d9f131 mfd / platform: c... |
19 20 |
#include <linux/platform_data/cros_ec_commands.h> #include <linux/platform_data/cros_ec_proto.h> |
eb1aac4c8 HID: google: add ... |
21 22 23 |
#include <linux/platform_device.h> #include <linux/pm_wakeup.h> #include <asm/unaligned.h> |
bc774b8c1 HID: google: add ... |
24 25 |
#include "hid-ids.h" |
eb1aac4c8 HID: google: add ... |
26 27 28 29 30 31 32 33 34 35 |
/* * C(hrome)B(ase)A(ttached)S(witch) - switch exported by Chrome EC and reporting * state of the "Whiskers" base - attached or detached. Whiskers USB device also * reports position of the keyboard - folded or not. Combining base state and * position allows us to generate proper "Tablet mode" events. */ struct cbas_ec { struct device *dev; /* The platform device (EC) */ struct input_dev *input; bool base_present; |
38e57f069 HID: google: whis... |
36 |
bool base_folded; |
eb1aac4c8 HID: google: add ... |
37 38 |
struct notifier_block notifier; }; |
bc774b8c1 HID: google: add ... |
39 |
|
eb1aac4c8 HID: google: add ... |
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
static struct cbas_ec cbas_ec; static DEFINE_SPINLOCK(cbas_ec_lock); static DEFINE_MUTEX(cbas_ec_reglock); static bool cbas_parse_base_state(const void *data) { u32 switches = get_unaligned_le32(data); return !!(switches & BIT(EC_MKBP_BASE_ATTACHED)); } static int cbas_ec_query_base(struct cros_ec_device *ec_dev, bool get_state, bool *state) { struct ec_params_mkbp_info *params; struct cros_ec_command *msg; int ret; msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)), GFP_KERNEL); if (!msg) return -ENOMEM; msg->command = EC_CMD_MKBP_INFO; msg->version = 1; msg->outsize = sizeof(*params); msg->insize = sizeof(u32); params = (struct ec_params_mkbp_info *)msg->data; params->info_type = get_state ? EC_MKBP_INFO_CURRENT : EC_MKBP_INFO_SUPPORTED; params->event_type = EC_MKBP_EVENT_SWITCH; ret = cros_ec_cmd_xfer_status(ec_dev, msg); if (ret >= 0) { if (ret != sizeof(u32)) { dev_warn(ec_dev->dev, "wrong result size: %d != %zu ", ret, sizeof(u32)); ret = -EPROTO; } else { *state = cbas_parse_base_state(msg->data); ret = 0; } } kfree(msg); return ret; } static int cbas_ec_notify(struct notifier_block *nb, unsigned long queued_during_suspend, void *_notify) { struct cros_ec_device *ec = _notify; unsigned long flags; bool base_present; if (ec->event_data.event_type == EC_MKBP_EVENT_SWITCH) { base_present = cbas_parse_base_state( &ec->event_data.data.switches); dev_dbg(cbas_ec.dev, "%s: base: %d ", __func__, base_present); if (device_may_wakeup(cbas_ec.dev) || !queued_during_suspend) { pm_wakeup_event(cbas_ec.dev, 0); spin_lock_irqsave(&cbas_ec_lock, flags); /* * While input layer dedupes the events, we do not want * to disrupt the state reported by the base by * overriding it with state reported by the LID. Only * report changes, as we assume that on attach the base * is not folded. */ if (base_present != cbas_ec.base_present) { input_report_switch(cbas_ec.input, SW_TABLET_MODE, !base_present); input_sync(cbas_ec.input); cbas_ec.base_present = base_present; } spin_unlock_irqrestore(&cbas_ec_lock, flags); } } return NOTIFY_OK; } static __maybe_unused int cbas_ec_resume(struct device *dev) { struct cros_ec_device *ec = dev_get_drvdata(dev->parent); bool base_present; int error; error = cbas_ec_query_base(ec, true, &base_present); if (error) { dev_warn(dev, "failed to fetch base state on resume: %d ", error); } else { spin_lock_irq(&cbas_ec_lock); cbas_ec.base_present = base_present; /* * Only report if base is disconnected. If base is connected, * it will resend its state on resume, and we'll update it * in hammer_event(). */ if (!cbas_ec.base_present) { input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1); input_sync(cbas_ec.input); } spin_unlock_irq(&cbas_ec_lock); } return 0; } |
8f35260e0 HID: google: drop... |
165 |
static SIMPLE_DEV_PM_OPS(cbas_ec_pm_ops, NULL, cbas_ec_resume); |
eb1aac4c8 HID: google: add ... |
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
static void cbas_ec_set_input(struct input_dev *input) { /* Take the lock so hammer_event() does not race with us here */ spin_lock_irq(&cbas_ec_lock); cbas_ec.input = input; spin_unlock_irq(&cbas_ec_lock); } static int __cbas_ec_probe(struct platform_device *pdev) { struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); struct input_dev *input; bool base_supported; int error; error = cbas_ec_query_base(ec, false, &base_supported); if (error) return error; if (!base_supported) return -ENXIO; input = devm_input_allocate_device(&pdev->dev); if (!input) return -ENOMEM; input->name = "Whiskers Tablet Mode Switch"; input->id.bustype = BUS_HOST; input_set_capability(input, EV_SW, SW_TABLET_MODE); error = input_register_device(input); if (error) { dev_err(&pdev->dev, "cannot register input device: %d ", error); return error; } /* Seed the state */ error = cbas_ec_query_base(ec, true, &cbas_ec.base_present); if (error) { dev_err(&pdev->dev, "cannot query base state: %d ", error); return error; } |
38e57f069 HID: google: whis... |
213 214 215 216 217 218 219 220 221 |
if (!cbas_ec.base_present) cbas_ec.base_folded = false; dev_dbg(&pdev->dev, "%s: base: %d, folded: %d ", __func__, cbas_ec.base_present, cbas_ec.base_folded); input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present || cbas_ec.base_folded); |
eb1aac4c8 HID: google: add ... |
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
cbas_ec_set_input(input); cbas_ec.dev = &pdev->dev; cbas_ec.notifier.notifier_call = cbas_ec_notify; error = blocking_notifier_chain_register(&ec->event_notifier, &cbas_ec.notifier); if (error) { dev_err(&pdev->dev, "cannot register notifier: %d ", error); cbas_ec_set_input(NULL); return error; } device_init_wakeup(&pdev->dev, true); return 0; } static int cbas_ec_probe(struct platform_device *pdev) { int retval; mutex_lock(&cbas_ec_reglock); if (cbas_ec.input) { retval = -EBUSY; goto out; } retval = __cbas_ec_probe(pdev); out: mutex_unlock(&cbas_ec_reglock); return retval; } static int cbas_ec_remove(struct platform_device *pdev) { struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent); mutex_lock(&cbas_ec_reglock); blocking_notifier_chain_unregister(&ec->event_notifier, &cbas_ec.notifier); cbas_ec_set_input(NULL); mutex_unlock(&cbas_ec_reglock); return 0; } static const struct acpi_device_id cbas_ec_acpi_ids[] = { { "GOOG000B", 0 }, { } }; MODULE_DEVICE_TABLE(acpi, cbas_ec_acpi_ids); static struct platform_driver cbas_ec_driver = { .probe = cbas_ec_probe, .remove = cbas_ec_remove, .driver = { .name = "cbas_ec", .acpi_match_table = ACPI_PTR(cbas_ec_acpi_ids), .pm = &cbas_ec_pm_ops, }, }; #define MAX_BRIGHTNESS 100 |
bc774b8c1 HID: google: add ... |
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
struct hammer_kbd_leds { struct led_classdev cdev; struct hid_device *hdev; u8 buf[2] ____cacheline_aligned; }; static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev, enum led_brightness br) { struct hammer_kbd_leds *led = container_of(cdev, struct hammer_kbd_leds, cdev); int ret; led->buf[0] = 0; led->buf[1] = br; |
7d3d88401 HID: google: Enab... |
306 307 308 309 310 311 312 313 314 315 |
/* * Request USB HID device to be in Full On mode, so that sending * hardware output report and hardware raw request won't fail. */ ret = hid_hw_power(led->hdev, PM_HINT_FULLON); if (ret < 0) { hid_err(led->hdev, "failed: device not resumed %d ", ret); return ret; } |
bc774b8c1 HID: google: add ... |
316 317 318 319 320 321 322 323 324 325 |
ret = hid_hw_output_report(led->hdev, led->buf, sizeof(led->buf)); if (ret == -ENOSYS) ret = hid_hw_raw_request(led->hdev, 0, led->buf, sizeof(led->buf), HID_OUTPUT_REPORT, HID_REQ_SET_REPORT); if (ret < 0) hid_err(led->hdev, "failed to set keyboard backlight: %d ", ret); |
7d3d88401 HID: google: Enab... |
326 327 328 |
/* Request USB HID device back to Normal Mode. */ hid_hw_power(led->hdev, PM_HINT_NORMAL); |
bc774b8c1 HID: google: add ... |
329 330 331 332 333 334 |
return ret; } static int hammer_register_leds(struct hid_device *hdev) { struct hammer_kbd_leds *kbd_backlight; |
38e57f069 HID: google: whis... |
335 |
int error; |
bc774b8c1 HID: google: add ... |
336 |
|
38e57f069 HID: google: whis... |
337 |
kbd_backlight = kzalloc(sizeof(*kbd_backlight), GFP_KERNEL); |
bc774b8c1 HID: google: add ... |
338 339 340 341 342 343 344 345 346 347 348 349 |
if (!kbd_backlight) return -ENOMEM; kbd_backlight->hdev = hdev; kbd_backlight->cdev.name = "hammer::kbd_backlight"; kbd_backlight->cdev.max_brightness = MAX_BRIGHTNESS; kbd_backlight->cdev.brightness_set_blocking = hammer_kbd_brightness_set_blocking; kbd_backlight->cdev.flags = LED_HW_PLUGGABLE; /* Set backlight to 0% initially. */ hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0); |
38e57f069 HID: google: whis... |
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
error = led_classdev_register(&hdev->dev, &kbd_backlight->cdev); if (error) goto err_free_mem; hid_set_drvdata(hdev, kbd_backlight); return 0; err_free_mem: kfree(kbd_backlight); return error; } static void hammer_unregister_leds(struct hid_device *hdev) { struct hammer_kbd_leds *kbd_backlight = hid_get_drvdata(hdev); if (kbd_backlight) { led_classdev_unregister(&kbd_backlight->cdev); kfree(kbd_backlight); } |
bc774b8c1 HID: google: add ... |
370 |
} |
eb1aac4c8 HID: google: add ... |
371 372 |
#define HID_UP_GOOGLEVENDOR 0xffd10000 #define HID_VD_KBD_FOLDED 0x00000019 |
20c55f250 HID: google: Dete... |
373 |
#define HID_USAGE_KBD_FOLDED (HID_UP_GOOGLEVENDOR | HID_VD_KBD_FOLDED) |
eb1aac4c8 HID: google: add ... |
374 375 376 377 378 379 380 381 |
/* HID usage for keyboard backlight (Alphanumeric display brightness) */ #define HID_AD_BRIGHTNESS 0x00140046 static int hammer_input_mapping(struct hid_device *hdev, struct hid_input *hi, struct hid_field *field, struct hid_usage *usage, unsigned long **bit, int *max) |
bc774b8c1 HID: google: add ... |
382 |
{ |
20c55f250 HID: google: Dete... |
383 |
if (usage->hid == HID_USAGE_KBD_FOLDED) { |
eb1aac4c8 HID: google: add ... |
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 |
/* * We do not want to have this usage mapped as it will get * mixed in with "base attached" signal and delivered over * separate input device for tablet switch mode. */ return -1; } return 0; } static int hammer_event(struct hid_device *hid, struct hid_field *field, struct hid_usage *usage, __s32 value) { unsigned long flags; |
20c55f250 HID: google: Dete... |
399 |
if (usage->hid == HID_USAGE_KBD_FOLDED) { |
eb1aac4c8 HID: google: add ... |
400 |
spin_lock_irqsave(&cbas_ec_lock, flags); |
eb1aac4c8 HID: google: add ... |
401 |
/* |
b543db46b HID: google: whis... |
402 403 |
* If we are getting events from Whiskers that means that it * is attached to the lid. |
eb1aac4c8 HID: google: add ... |
404 |
*/ |
b543db46b HID: google: whis... |
405 |
cbas_ec.base_present = true; |
38e57f069 HID: google: whis... |
406 |
cbas_ec.base_folded = value; |
eb1aac4c8 HID: google: add ... |
407 408 |
hid_dbg(hid, "%s: base: %d, folded: %d ", __func__, |
38e57f069 HID: google: whis... |
409 |
cbas_ec.base_present, cbas_ec.base_folded); |
eb1aac4c8 HID: google: add ... |
410 |
|
b543db46b HID: google: whis... |
411 |
if (cbas_ec.input) { |
eb1aac4c8 HID: google: add ... |
412 413 414 415 416 417 418 419 420 421 422 |
input_report_switch(cbas_ec.input, SW_TABLET_MODE, value); input_sync(cbas_ec.input); } spin_unlock_irqrestore(&cbas_ec_lock, flags); return 1; /* We handled this event */ } return 0; } |
20c55f250 HID: google: Dete... |
423 424 |
static bool hammer_has_usage(struct hid_device *hdev, unsigned int report_type, unsigned application, unsigned usage) |
eb1aac4c8 HID: google: add ... |
425 |
{ |
20c55f250 HID: google: Dete... |
426 |
struct hid_report_enum *re = &hdev->report_enum[report_type]; |
eb1aac4c8 HID: google: add ... |
427 428 |
struct hid_report *report; int i, j; |
bc774b8c1 HID: google: add ... |
429 |
|
eb1aac4c8 HID: google: add ... |
430 |
list_for_each_entry(report, &re->report_list, list) { |
20c55f250 HID: google: Dete... |
431 |
if (report->application != application) |
eb1aac4c8 HID: google: add ... |
432 |
continue; |
bc774b8c1 HID: google: add ... |
433 |
|
eb1aac4c8 HID: google: add ... |
434 435 436 437 |
for (i = 0; i < report->maxfield; i++) { struct hid_field *field = report->field[i]; for (j = 0; j < field->maxusage; j++) |
20c55f250 HID: google: Dete... |
438 |
if (field->usage[j].hid == usage) |
eb1aac4c8 HID: google: add ... |
439 440 441 442 443 444 |
return true; } } return false; } |
20c55f250 HID: google: Dete... |
445 446 447 448 449 450 451 452 453 454 455 |
static bool hammer_has_folded_event(struct hid_device *hdev) { return hammer_has_usage(hdev, HID_INPUT_REPORT, HID_GD_KEYBOARD, HID_USAGE_KBD_FOLDED); } static bool hammer_has_backlight_control(struct hid_device *hdev) { return hammer_has_usage(hdev, HID_OUTPUT_REPORT, HID_GD_KEYBOARD, HID_AD_BRIGHTNESS); } |
eb1aac4c8 HID: google: add ... |
456 457 458 459 |
static int hammer_probe(struct hid_device *hdev, const struct hid_device_id *id) { int error; |
eb1aac4c8 HID: google: add ... |
460 461 462 463 464 465 466 |
error = hid_parse(hdev); if (error) return error; error = hid_hw_start(hdev, HID_CONNECT_DEFAULT); if (error) return error; |
eb1aac4c8 HID: google: add ... |
467 468 |
/* * We always want to poll for, and handle tablet mode events from |
20c55f250 HID: google: Dete... |
469 470 471 |
* devices that have folded usage, even when nobody has opened the input * device. This also prevents the hid core from dropping early tablet * mode events from the device. |
eb1aac4c8 HID: google: add ... |
472 |
*/ |
20c55f250 HID: google: Dete... |
473 |
if (hammer_has_folded_event(hdev)) { |
eb1aac4c8 HID: google: add ... |
474 |
hdev->quirks |= HID_QUIRK_ALWAYS_POLL; |
38e57f069 HID: google: whis... |
475 476 477 478 |
error = hid_hw_open(hdev); if (error) return error; } |
eb1aac4c8 HID: google: add ... |
479 480 481 482 |
if (hammer_has_backlight_control(hdev)) { error = hammer_register_leds(hdev); if (error) |
bc774b8c1 HID: google: add ... |
483 484 485 |
hid_warn(hdev, "Failed to register keyboard backlight: %d ", |
eb1aac4c8 HID: google: add ... |
486 |
error); |
bc774b8c1 HID: google: add ... |
487 488 489 490 |
} return 0; } |
38e57f069 HID: google: whis... |
491 492 |
static void hammer_remove(struct hid_device *hdev) { |
79085c7dd HID: google: whis... |
493 |
unsigned long flags; |
20c55f250 HID: google: Dete... |
494 |
if (hammer_has_folded_event(hdev)) { |
38e57f069 HID: google: whis... |
495 |
hid_hw_close(hdev); |
79085c7dd HID: google: whis... |
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
/* * If we are disconnecting then most likely Whiskers is * being removed. Even if it is not removed, without proper * keyboard we should not stay in clamshell mode. * * The reason for doing it here and not waiting for signal * from EC, is that on some devices there are high leakage * on Whiskers pins and we do not detect disconnect reliably, * resulting in devices being stuck in clamshell mode. */ spin_lock_irqsave(&cbas_ec_lock, flags); if (cbas_ec.input && cbas_ec.base_present) { input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1); input_sync(cbas_ec.input); } cbas_ec.base_present = false; spin_unlock_irqrestore(&cbas_ec_lock, flags); } |
38e57f069 HID: google: whis... |
514 |
hammer_unregister_leds(hdev); |
79085c7dd HID: google: whis... |
515 |
|
38e57f069 HID: google: whis... |
516 517 |
hid_hw_stop(hdev); } |
eb1aac4c8 HID: google: add ... |
518 |
|
bc774b8c1 HID: google: add ... |
519 520 521 522 |
static const struct hid_device_id hammer_devices[] = { { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_HAMMER) }, { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, |
9e4dbc464 HID: google: add ... |
523 524 525 526 |
USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MAGNEMITE) }, { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MASTERBALL) }, { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, |
58322a159 HID: google: add ... |
527 528 |
USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MOONBALL) }, { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, |
bc774b8c1 HID: google: add ... |
529 530 531 |
USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) }, { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) }, |
3e84c7651 HID: google: Add ... |
532 533 |
{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) }, |
bc774b8c1 HID: google: add ... |
534 535 536 537 538 539 540 |
{ } }; MODULE_DEVICE_TABLE(hid, hammer_devices); static struct hid_driver hammer_driver = { .name = "hammer", .id_table = hammer_devices, |
eb1aac4c8 HID: google: add ... |
541 |
.probe = hammer_probe, |
38e57f069 HID: google: whis... |
542 |
.remove = hammer_remove, |
eb1aac4c8 HID: google: add ... |
543 544 |
.input_mapping = hammer_input_mapping, .event = hammer_event, |
bc774b8c1 HID: google: add ... |
545 |
}; |
eb1aac4c8 HID: google: add ... |
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
static int __init hammer_init(void) { int error; error = platform_driver_register(&cbas_ec_driver); if (error) return error; error = hid_register_driver(&hammer_driver); if (error) { platform_driver_unregister(&cbas_ec_driver); return error; } return 0; } module_init(hammer_init); static void __exit hammer_exit(void) { hid_unregister_driver(&hammer_driver); platform_driver_unregister(&cbas_ec_driver); } module_exit(hammer_exit); |
bc774b8c1 HID: google: add ... |
571 572 |
MODULE_LICENSE("GPL"); |