Wei-Ning Huang | bc774b8 | 2018-03-15 09:28:25 +0800 | [diff] [blame^] | 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/hid.h> |
| 17 | #include <linux/leds.h> |
| 18 | #include <linux/module.h> |
| 19 | |
| 20 | #include "hid-ids.h" |
| 21 | |
| 22 | #define MAX_BRIGHTNESS 100 |
| 23 | |
| 24 | /* HID usage for keyboard backlight (Alphanumeric display brightness) */ |
| 25 | #define HID_AD_BRIGHTNESS 0x00140046 |
| 26 | |
| 27 | struct hammer_kbd_leds { |
| 28 | struct led_classdev cdev; |
| 29 | struct hid_device *hdev; |
| 30 | u8 buf[2] ____cacheline_aligned; |
| 31 | }; |
| 32 | |
| 33 | static int hammer_kbd_brightness_set_blocking(struct led_classdev *cdev, |
| 34 | enum led_brightness br) |
| 35 | { |
| 36 | struct hammer_kbd_leds *led = container_of(cdev, |
| 37 | struct hammer_kbd_leds, |
| 38 | cdev); |
| 39 | int ret; |
| 40 | |
| 41 | led->buf[0] = 0; |
| 42 | led->buf[1] = br; |
| 43 | |
| 44 | ret = hid_hw_output_report(led->hdev, led->buf, sizeof(led->buf)); |
| 45 | if (ret == -ENOSYS) |
| 46 | ret = hid_hw_raw_request(led->hdev, 0, led->buf, |
| 47 | sizeof(led->buf), |
| 48 | HID_OUTPUT_REPORT, |
| 49 | HID_REQ_SET_REPORT); |
| 50 | if (ret < 0) |
| 51 | hid_err(led->hdev, "failed to set keyboard backlight: %d\n", |
| 52 | ret); |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | static int hammer_register_leds(struct hid_device *hdev) |
| 57 | { |
| 58 | struct hammer_kbd_leds *kbd_backlight; |
| 59 | |
| 60 | kbd_backlight = devm_kzalloc(&hdev->dev, |
| 61 | sizeof(*kbd_backlight), |
| 62 | GFP_KERNEL); |
| 63 | if (!kbd_backlight) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | kbd_backlight->hdev = hdev; |
| 67 | kbd_backlight->cdev.name = "hammer::kbd_backlight"; |
| 68 | kbd_backlight->cdev.max_brightness = MAX_BRIGHTNESS; |
| 69 | kbd_backlight->cdev.brightness_set_blocking = |
| 70 | hammer_kbd_brightness_set_blocking; |
| 71 | kbd_backlight->cdev.flags = LED_HW_PLUGGABLE; |
| 72 | |
| 73 | /* Set backlight to 0% initially. */ |
| 74 | hammer_kbd_brightness_set_blocking(&kbd_backlight->cdev, 0); |
| 75 | |
| 76 | return devm_led_classdev_register(&hdev->dev, &kbd_backlight->cdev); |
| 77 | } |
| 78 | |
| 79 | static int hammer_input_configured(struct hid_device *hdev, |
| 80 | struct hid_input *hi) |
| 81 | { |
| 82 | struct list_head *report_list = |
| 83 | &hdev->report_enum[HID_OUTPUT_REPORT].report_list; |
| 84 | struct hid_report *report; |
| 85 | |
| 86 | if (list_empty(report_list)) |
| 87 | return 0; |
| 88 | |
| 89 | report = list_first_entry(report_list, struct hid_report, list); |
| 90 | |
| 91 | if (report->maxfield == 1 && |
| 92 | report->field[0]->application == HID_GD_KEYBOARD && |
| 93 | report->field[0]->maxusage == 1 && |
| 94 | report->field[0]->usage[0].hid == HID_AD_BRIGHTNESS) { |
| 95 | int err = hammer_register_leds(hdev); |
| 96 | |
| 97 | if (err) |
| 98 | hid_warn(hdev, |
| 99 | "Failed to register keyboard backlight: %d\n", |
| 100 | err); |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static const struct hid_device_id hammer_devices[] = { |
| 107 | { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, |
| 108 | USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_HAMMER) }, |
| 109 | { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, |
| 110 | USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_STAFF) }, |
| 111 | { HID_DEVICE(BUS_USB, HID_GROUP_GENERIC, |
| 112 | USB_VENDOR_ID_GOOGLE, USB_DEVICE_ID_GOOGLE_WAND) }, |
| 113 | { } |
| 114 | }; |
| 115 | MODULE_DEVICE_TABLE(hid, hammer_devices); |
| 116 | |
| 117 | static struct hid_driver hammer_driver = { |
| 118 | .name = "hammer", |
| 119 | .id_table = hammer_devices, |
| 120 | .input_configured = hammer_input_configured, |
| 121 | }; |
| 122 | module_hid_driver(hammer_driver); |
| 123 | |
| 124 | MODULE_LICENSE("GPL"); |