xref: /linux/drivers/hid/hid-google-hammer.c (revision 52338415)
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 <wnhuang@google.com>
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/leds.h>
19 #include <linux/module.h>
20 #include <linux/platform_data/cros_ec_commands.h>
21 #include <linux/platform_data/cros_ec_proto.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_wakeup.h>
24 #include <asm/unaligned.h>
25 
26 #include "hid-ids.h"
27 
28 /*
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.
33  */
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;
39 };
40 
41 static struct cbas_ec cbas_ec;
42 static DEFINE_SPINLOCK(cbas_ec_lock);
43 static DEFINE_MUTEX(cbas_ec_reglock);
44 
45 static bool cbas_parse_base_state(const void *data)
46 {
47 	u32 switches = get_unaligned_le32(data);
48 
49 	return !!(switches & BIT(EC_MKBP_BASE_ATTACHED));
50 }
51 
52 static int cbas_ec_query_base(struct cros_ec_device *ec_dev, bool get_state,
53 				  bool *state)
54 {
55 	struct ec_params_mkbp_info *params;
56 	struct cros_ec_command *msg;
57 	int ret;
58 
59 	msg = kzalloc(sizeof(*msg) + max(sizeof(u32), sizeof(*params)),
60 		      GFP_KERNEL);
61 	if (!msg)
62 		return -ENOMEM;
63 
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;
72 
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;
82 		}
83 	}
84 
85 	kfree(msg);
86 
87 	return ret;
88 }
89 
90 static int cbas_ec_notify(struct notifier_block *nb,
91 			      unsigned long queued_during_suspend,
92 			      void *_notify)
93 {
94 	struct cros_ec_device *ec = _notify;
95 	unsigned long flags;
96 	bool base_present;
97 
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);
103 
104 		if (device_may_wakeup(cbas_ec.dev) ||
105 		    !queued_during_suspend) {
106 
107 			pm_wakeup_event(cbas_ec.dev, 0);
108 
109 			spin_lock_irqsave(&cbas_ec_lock, flags);
110 
111 			/*
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.
117 			 */
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;
124 			}
125 
126 			spin_unlock_irqrestore(&cbas_ec_lock, flags);
127 		}
128 	}
129 
130 	return NOTIFY_OK;
131 }
132 
133 static __maybe_unused int cbas_ec_resume(struct device *dev)
134 {
135 	struct cros_ec_device *ec = dev_get_drvdata(dev->parent);
136 	bool base_present;
137 	int error;
138 
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);
145 
146 		cbas_ec.base_present = base_present;
147 
148 		/*
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().
152 		 */
153 		if (!cbas_ec.base_present) {
154 			input_report_switch(cbas_ec.input, SW_TABLET_MODE, 1);
155 			input_sync(cbas_ec.input);
156 		}
157 
158 		spin_unlock_irq(&cbas_ec_lock);
159 	}
160 
161 	return 0;
162 }
163 
164 static SIMPLE_DEV_PM_OPS(cbas_ec_pm_ops, NULL, cbas_ec_resume);
165 
166 static void cbas_ec_set_input(struct input_dev *input)
167 {
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);
172 }
173 
174 static int __cbas_ec_probe(struct platform_device *pdev)
175 {
176 	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
177 	struct input_dev *input;
178 	bool base_supported;
179 	int error;
180 
181 	error = cbas_ec_query_base(ec, false, &base_supported);
182 	if (error)
183 		return error;
184 
185 	if (!base_supported)
186 		return -ENXIO;
187 
188 	input = devm_input_allocate_device(&pdev->dev);
189 	if (!input)
190 		return -ENOMEM;
191 
192 	input->name = "Whiskers Tablet Mode Switch";
193 	input->id.bustype = BUS_HOST;
194 
195 	input_set_capability(input, EV_SW, SW_TABLET_MODE);
196 
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;
202 	}
203 
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;
209 	}
210 
211 	input_report_switch(input, SW_TABLET_MODE, !cbas_ec.base_present);
212 
213 	cbas_ec_set_input(input);
214 
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;
223 	}
224 
225 	device_init_wakeup(&pdev->dev, true);
226 	return 0;
227 }
228 
229 static int cbas_ec_probe(struct platform_device *pdev)
230 {
231 	int retval;
232 
233 	mutex_lock(&cbas_ec_reglock);
234 
235 	if (cbas_ec.input) {
236 		retval = -EBUSY;
237 		goto out;
238 	}
239 
240 	retval = __cbas_ec_probe(pdev);
241 
242 out:
243 	mutex_unlock(&cbas_ec_reglock);
244 	return retval;
245 }
246 
247 static int cbas_ec_remove(struct platform_device *pdev)
248 {
249 	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
250 
251 	mutex_lock(&cbas_ec_reglock);
252 
253 	blocking_notifier_chain_unregister(&ec->event_notifier,
254 					   &cbas_ec.notifier);
255 	cbas_ec_set_input(NULL);
256 
257 	mutex_unlock(&cbas_ec_reglock);
258 	return 0;
259 }
260 
261 static const struct acpi_device_id cbas_ec_acpi_ids[] = {
262 	{ "GOOG000B", 0 },
263 	{ }
264 };
265 MODULE_DEVICE_TABLE(acpi, cbas_ec_acpi_ids);
266 
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,
274 	},
275 };
276 
277 #define MAX_BRIGHTNESS 100
278 
279 struct hammer_kbd_leds {
280 	struct led_classdev cdev;
281 	struct hid_device *hdev;
282 	u8 buf[2] ____cacheline_aligned;
283 };
284 
285 static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev,
286 		enum led_brightness br)
287 {
288 	struct hammer_kbd_leds *led = container_of(cdev,
289 						   struct hammer_kbd_leds,
290 						   cdev);
291 	int ret;
292 
293 	led->buf[0] = 0;
294 	led->buf[1] = br;
295 
296 	/*
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.
299 	 */
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;
304 	}
305 
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);
315 
316 	/* Request USB HID device back to Normal Mode. */
317 	hid_hw_power(led->hdev, PM_HINT_NORMAL);
318 
319 	return ret;
320 }
321 
322 static int hammer_register_leds(struct hid_device *hdev)
323 {
324 	struct hammer_kbd_leds *kbd_backlight;
325 
326 	kbd_backlight = devm_kzalloc(&hdev->dev,
327 				     sizeof(*kbd_backlight),
328 				     GFP_KERNEL);
329 	if (!kbd_backlight)
330 		return -ENOMEM;
331 
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;
338 
339 	/* Set backlight to 0% initially. */
340 	hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0);
341 
342 	return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev);
343 }
344 
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)
348 
349 /* HID usage for keyboard backlight (Alphanumeric display brightness) */
350 #define HID_AD_BRIGHTNESS	0x00140046
351 
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)
356 {
357 	if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
358 	    usage->hid == WHISKERS_KBD_FOLDED) {
359 		/*
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.
363 		 */
364 		return -1;
365 	}
366 
367 	return 0;
368 }
369 
370 static int hammer_event(struct hid_device *hid, struct hid_field *field,
371 			struct hid_usage *usage, __s32 value)
372 {
373 	unsigned long flags;
374 
375 	if (hid->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
376 	    usage->hid == WHISKERS_KBD_FOLDED) {
377 		spin_lock_irqsave(&cbas_ec_lock, flags);
378 
379 		hid_dbg(hid, "%s: base: %d, folded: %d\n", __func__,
380 			cbas_ec.base_present, value);
381 
382 		/*
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.
386 		 */
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);
391 		}
392 
393 		spin_unlock_irqrestore(&cbas_ec_lock, flags);
394 		return 1; /* We handled this event */
395 	}
396 
397 	return 0;
398 }
399 
400 static bool hammer_is_keyboard_interface(struct hid_device *hdev)
401 {
402 	struct hid_report_enum *re = &hdev->report_enum[HID_INPUT_REPORT];
403 	struct hid_report *report;
404 
405 	list_for_each_entry(report, &re->report_list, list)
406 		if (report->application == HID_GD_KEYBOARD)
407 			return true;
408 
409 	return false;
410 }
411 
412 static bool hammer_has_backlight_control(struct hid_device *hdev)
413 {
414 	struct hid_report_enum *re = &hdev->report_enum[HID_OUTPUT_REPORT];
415 	struct hid_report *report;
416 	int i, j;
417 
418 	list_for_each_entry(report, &re->report_list, list) {
419 		if (report->application != HID_GD_KEYBOARD)
420 			continue;
421 
422 		for (i = 0; i < report->maxfield; i++) {
423 			struct hid_field *field = report->field[i];
424 
425 			for (j = 0; j < field->maxusage; j++)
426 				if (field->usage[j].hid == HID_AD_BRIGHTNESS)
427 					return true;
428 		}
429 	}
430 
431 	return false;
432 }
433 
434 static int hammer_probe(struct hid_device *hdev,
435 			const struct hid_device_id *id)
436 {
437 	int error;
438 
439 	/*
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.
444 	 */
445 	if (hdev->product == USB_DEVICE_ID_GOOGLE_WHISKERS &&
446 			hammer_is_keyboard_interface(hdev))
447 		hdev->quirks |= HID_QUIRK_ALWAYS_POLL;
448 
449 	error = hid_parse(hdev);
450 	if (error)
451 		return error;
452 
453 	error = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
454 	if (error)
455 		return error;
456 
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);
463 	}
464 
465 	return 0;
466 }
467 
468 
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_MAGNEMITE) },
474 	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
475 		     USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_MASTERBALL) },
476 	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
477 		     USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) },
478 	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
479 		     USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) },
480 	{ HID_DEVICE(BUS_USB, HID_GROUP_GENERIC,
481 		     USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WHISKERS) },
482 	{ }
483 };
484 MODULE_DEVICE_TABLE(hid, hammer_devices);
485 
486 static struct hid_driver hammer_driver = {
487 	.name = "hammer",
488 	.id_table = hammer_devices,
489 	.probe = hammer_probe,
490 	.input_mapping = hammer_input_mapping,
491 	.event = hammer_event,
492 };
493 
494 static int __init hammer_init(void)
495 {
496 	int error;
497 
498 	error = platform_driver_register(&cbas_ec_driver);
499 	if (error)
500 		return error;
501 
502 	error = hid_register_driver(&hammer_driver);
503 	if (error) {
504 		platform_driver_unregister(&cbas_ec_driver);
505 		return error;
506 	}
507 
508 	return 0;
509 }
510 module_init(hammer_init);
511 
512 static void __exit hammer_exit(void)
513 {
514 	hid_unregister_driver(&hammer_driver);
515 	platform_driver_unregister(&cbas_ec_driver);
516 }
517 module_exit(hammer_exit);
518 
519 MODULE_LICENSE("GPL");
520