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