12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2fcfacfd3SJiri Slaby /*
3fcfacfd3SJiri Slaby * HID driver for some chicony "special" devices
4fcfacfd3SJiri Slaby *
5fcfacfd3SJiri Slaby * Copyright (c) 1999 Andreas Gal
6fcfacfd3SJiri Slaby * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7fcfacfd3SJiri Slaby * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
8fcfacfd3SJiri Slaby * Copyright (c) 2006-2007 Jiri Kosina
9fcfacfd3SJiri Slaby * Copyright (c) 2007 Paul Walmsley
10fcfacfd3SJiri Slaby * Copyright (c) 2008 Jiri Slaby
11fcfacfd3SJiri Slaby */
12fcfacfd3SJiri Slaby
13fcfacfd3SJiri Slaby /*
14fcfacfd3SJiri Slaby */
15fcfacfd3SJiri Slaby
16fcfacfd3SJiri Slaby #include <linux/device.h>
17fcfacfd3SJiri Slaby #include <linux/input.h>
18fcfacfd3SJiri Slaby #include <linux/hid.h>
19fcfacfd3SJiri Slaby #include <linux/module.h>
209a1d78a3SНиколай Кудрявцев #include <linux/usb.h>
21fcfacfd3SJiri Slaby
22fcfacfd3SJiri Slaby #include "hid-ids.h"
23fcfacfd3SJiri Slaby
24859b510bSJian-Hong Pan #define CH_WIRELESS_CTL_REPORT_ID 0x11
25859b510bSJian-Hong Pan
ch_report_wireless(struct hid_report * report,u8 * data,int size)26859b510bSJian-Hong Pan static int ch_report_wireless(struct hid_report *report, u8 *data, int size)
27859b510bSJian-Hong Pan {
28859b510bSJian-Hong Pan struct hid_device *hdev = report->device;
29859b510bSJian-Hong Pan struct input_dev *input;
30859b510bSJian-Hong Pan
31859b510bSJian-Hong Pan if (report->id != CH_WIRELESS_CTL_REPORT_ID || report->maxfield != 1)
32859b510bSJian-Hong Pan return 0;
33859b510bSJian-Hong Pan
34859b510bSJian-Hong Pan input = report->field[0]->hidinput->input;
35859b510bSJian-Hong Pan if (!input) {
36859b510bSJian-Hong Pan hid_warn(hdev, "can't find wireless radio control's input");
37859b510bSJian-Hong Pan return 0;
38859b510bSJian-Hong Pan }
39859b510bSJian-Hong Pan
40859b510bSJian-Hong Pan input_report_key(input, KEY_RFKILL, 1);
41859b510bSJian-Hong Pan input_sync(input);
42859b510bSJian-Hong Pan input_report_key(input, KEY_RFKILL, 0);
43859b510bSJian-Hong Pan input_sync(input);
44859b510bSJian-Hong Pan
45859b510bSJian-Hong Pan return 1;
46859b510bSJian-Hong Pan }
47859b510bSJian-Hong Pan
ch_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)48859b510bSJian-Hong Pan static int ch_raw_event(struct hid_device *hdev,
49859b510bSJian-Hong Pan struct hid_report *report, u8 *data, int size)
50859b510bSJian-Hong Pan {
51859b510bSJian-Hong Pan if (report->application == HID_GD_WIRELESS_RADIO_CTLS)
52859b510bSJian-Hong Pan return ch_report_wireless(report, data, size);
53859b510bSJian-Hong Pan
54859b510bSJian-Hong Pan return 0;
55859b510bSJian-Hong Pan }
56859b510bSJian-Hong Pan
57fcfacfd3SJiri Slaby #define ch_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
58fcfacfd3SJiri Slaby EV_KEY, (c))
ch_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)59fcfacfd3SJiri Slaby static int ch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
60fcfacfd3SJiri Slaby struct hid_field *field, struct hid_usage *usage,
61fcfacfd3SJiri Slaby unsigned long **bit, int *max)
62fcfacfd3SJiri Slaby {
63fcfacfd3SJiri Slaby if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
64fcfacfd3SJiri Slaby return 0;
65fcfacfd3SJiri Slaby
66fcfacfd3SJiri Slaby set_bit(EV_REP, hi->input->evbit);
67fcfacfd3SJiri Slaby switch (usage->hid & HID_USAGE) {
68fcfacfd3SJiri Slaby case 0xff01: ch_map_key_clear(BTN_1); break;
69fcfacfd3SJiri Slaby case 0xff02: ch_map_key_clear(BTN_2); break;
70fcfacfd3SJiri Slaby case 0xff03: ch_map_key_clear(BTN_3); break;
71fcfacfd3SJiri Slaby case 0xff04: ch_map_key_clear(BTN_4); break;
72fcfacfd3SJiri Slaby case 0xff05: ch_map_key_clear(BTN_5); break;
73fcfacfd3SJiri Slaby case 0xff06: ch_map_key_clear(BTN_6); break;
74fcfacfd3SJiri Slaby case 0xff07: ch_map_key_clear(BTN_7); break;
75fcfacfd3SJiri Slaby case 0xff08: ch_map_key_clear(BTN_8); break;
76fcfacfd3SJiri Slaby case 0xff09: ch_map_key_clear(BTN_9); break;
77fcfacfd3SJiri Slaby case 0xff0a: ch_map_key_clear(BTN_A); break;
78fcfacfd3SJiri Slaby case 0xff0b: ch_map_key_clear(BTN_B); break;
793596bb92SKeng-Yu Lin case 0x00f1: ch_map_key_clear(KEY_WLAN); break;
803596bb92SKeng-Yu Lin case 0x00f2: ch_map_key_clear(KEY_BRIGHTNESSDOWN); break;
813596bb92SKeng-Yu Lin case 0x00f3: ch_map_key_clear(KEY_BRIGHTNESSUP); break;
823596bb92SKeng-Yu Lin case 0x00f4: ch_map_key_clear(KEY_DISPLAY_OFF); break;
836c30d5a5SKeng-Yu Lin case 0x00f7: ch_map_key_clear(KEY_CAMERA); break;
846c30d5a5SKeng-Yu Lin case 0x00f8: ch_map_key_clear(KEY_PROG1); break;
85fcfacfd3SJiri Slaby default:
86fcfacfd3SJiri Slaby return 0;
87fcfacfd3SJiri Slaby }
88fcfacfd3SJiri Slaby return 1;
89fcfacfd3SJiri Slaby }
90fcfacfd3SJiri Slaby
ch_switch12_report_fixup(struct hid_device * hdev,__u8 * rdesc,unsigned int * rsize)919a1d78a3SНиколай Кудрявцев static __u8 *ch_switch12_report_fixup(struct hid_device *hdev, __u8 *rdesc,
929a1d78a3SНиколай Кудрявцев unsigned int *rsize)
939a1d78a3SНиколай Кудрявцев {
949a1d78a3SНиколай Кудрявцев struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
959a1d78a3SНиколай Кудрявцев
969a1d78a3SНиколай Кудрявцев if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
979a1d78a3SНиколай Кудрявцев /* Change usage maximum and logical maximum from 0x7fff to
989a1d78a3SНиколай Кудрявцев * 0x2fff, so they don't exceed HID_MAX_USAGES */
999a1d78a3SНиколай Кудрявцев switch (hdev->product) {
1009a1d78a3SНиколай Кудрявцев case USB_DEVICE_ID_CHICONY_ACER_SWITCH12:
1019a1d78a3SНиколай Кудрявцев if (*rsize >= 128 && rdesc[64] == 0xff && rdesc[65] == 0x7f
1029a1d78a3SНиколай Кудрявцев && rdesc[69] == 0xff && rdesc[70] == 0x7f) {
1039a1d78a3SНиколай Кудрявцев hid_info(hdev, "Fixing up report descriptor\n");
1049a1d78a3SНиколай Кудрявцев rdesc[65] = rdesc[70] = 0x2f;
1059a1d78a3SНиколай Кудрявцев }
1069a1d78a3SНиколай Кудрявцев break;
1079a1d78a3SНиколай Кудрявцев }
1089a1d78a3SНиколай Кудрявцев
1099a1d78a3SНиколай Кудрявцев }
1109a1d78a3SНиколай Кудрявцев return rdesc;
1119a1d78a3SНиколай Кудрявцев }
1129a1d78a3SНиколай Кудрявцев
ch_probe(struct hid_device * hdev,const struct hid_device_id * id)113859b510bSJian-Hong Pan static int ch_probe(struct hid_device *hdev, const struct hid_device_id *id)
114859b510bSJian-Hong Pan {
115859b510bSJian-Hong Pan int ret;
116859b510bSJian-Hong Pan
117*93020953SGreg Kroah-Hartman if (!hid_is_usb(hdev))
118*93020953SGreg Kroah-Hartman return -EINVAL;
119*93020953SGreg Kroah-Hartman
120859b510bSJian-Hong Pan hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
121859b510bSJian-Hong Pan ret = hid_parse(hdev);
122859b510bSJian-Hong Pan if (ret) {
123859b510bSJian-Hong Pan hid_err(hdev, "Chicony hid parse failed: %d\n", ret);
124859b510bSJian-Hong Pan return ret;
125859b510bSJian-Hong Pan }
126859b510bSJian-Hong Pan
127859b510bSJian-Hong Pan ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
128859b510bSJian-Hong Pan if (ret) {
129859b510bSJian-Hong Pan hid_err(hdev, "Chicony hw start failed: %d\n", ret);
130859b510bSJian-Hong Pan return ret;
131859b510bSJian-Hong Pan }
132859b510bSJian-Hong Pan
133859b510bSJian-Hong Pan return 0;
134859b510bSJian-Hong Pan }
1359a1d78a3SНиколай Кудрявцев
136fcfacfd3SJiri Slaby static const struct hid_device_id ch_devices[] = {
137fcfacfd3SJiri Slaby { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_TACTICAL_PAD) },
1383596bb92SKeng-Yu Lin { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS2) },
139859b510bSJian-Hong Pan { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_WIRELESS3) },
1409a1d78a3SНиколай Кудрявцев { HID_USB_DEVICE(USB_VENDOR_ID_CHICONY, USB_DEVICE_ID_CHICONY_ACER_SWITCH12) },
141fcfacfd3SJiri Slaby { }
142fcfacfd3SJiri Slaby };
143fcfacfd3SJiri Slaby MODULE_DEVICE_TABLE(hid, ch_devices);
144fcfacfd3SJiri Slaby
145fcfacfd3SJiri Slaby static struct hid_driver ch_driver = {
146fcfacfd3SJiri Slaby .name = "chicony",
147fcfacfd3SJiri Slaby .id_table = ch_devices,
1489a1d78a3SНиколай Кудрявцев .report_fixup = ch_switch12_report_fixup,
149fcfacfd3SJiri Slaby .input_mapping = ch_input_mapping,
150859b510bSJian-Hong Pan .probe = ch_probe,
151859b510bSJian-Hong Pan .raw_event = ch_raw_event,
152fcfacfd3SJiri Slaby };
153f425458eSH Hartley Sweeten module_hid_driver(ch_driver);
154fcfacfd3SJiri Slaby
155fcfacfd3SJiri Slaby MODULE_LICENSE("GPL");
156