Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / hid / hid-cmedia.c
blob3296c505026492d922222cbedd4f68ee2a4c35a0
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * HID driver for CMedia CM6533 audio jack controls
5 * Copyright (C) 2015 Ben Chen <ben_chen@bizlinktech.com>
6 */
8 #include <linux/device.h>
9 #include <linux/hid.h>
10 #include <linux/module.h>
11 #include "hid-ids.h"
13 MODULE_AUTHOR("Ben Chen");
14 MODULE_DESCRIPTION("CM6533 HID jack controls");
15 MODULE_LICENSE("GPL");
17 #define CM6533_JD_TYPE_COUNT 1
18 #define CM6533_JD_RAWEV_LEN 16
19 #define CM6533_JD_SFX_OFFSET 8
23 *CM6533 audio jack HID raw events:
25 *Plug in:
26 *01000600 002083xx 080008c0 10000000
27 *about 3 seconds later...
28 *01000a00 002083xx 08000380 10000000
29 *01000600 002083xx 08000380 10000000
31 *Plug out:
32 *01000400 002083xx 080008c0 x0000000
35 static const u8 ji_sfx[] = { 0x08, 0x00, 0x08, 0xc0 };
36 static const u8 ji_in[] = { 0x01, 0x00, 0x06, 0x00 };
37 static const u8 ji_out[] = { 0x01, 0x00, 0x04, 0x00 };
39 static int jack_switch_types[CM6533_JD_TYPE_COUNT] = {
40 SW_HEADPHONE_INSERT,
43 struct cmhid {
44 struct input_dev *input_dev;
45 struct hid_device *hid;
46 unsigned short switch_map[CM6533_JD_TYPE_COUNT];
49 static void hp_ev(struct hid_device *hid, struct cmhid *cm, int value)
51 input_report_switch(cm->input_dev, SW_HEADPHONE_INSERT, value);
52 input_sync(cm->input_dev);
55 static int cmhid_raw_event(struct hid_device *hid, struct hid_report *report,
56 u8 *data, int len)
58 struct cmhid *cm = hid_get_drvdata(hid);
60 if (len != CM6533_JD_RAWEV_LEN)
61 goto out;
62 if (memcmp(data+CM6533_JD_SFX_OFFSET, ji_sfx, sizeof(ji_sfx)))
63 goto out;
65 if (!memcmp(data, ji_out, sizeof(ji_out))) {
66 hp_ev(hid, cm, 0);
67 goto out;
69 if (!memcmp(data, ji_in, sizeof(ji_in))) {
70 hp_ev(hid, cm, 1);
71 goto out;
74 out:
75 return 0;
78 static int cmhid_input_configured(struct hid_device *hid,
79 struct hid_input *hidinput)
81 struct input_dev *input_dev = hidinput->input;
82 struct cmhid *cm = hid_get_drvdata(hid);
83 int i;
85 cm->input_dev = input_dev;
86 memcpy(cm->switch_map, jack_switch_types, sizeof(cm->switch_map));
87 input_dev->evbit[0] = BIT(EV_SW);
88 for (i = 0; i < CM6533_JD_TYPE_COUNT; i++)
89 input_set_capability(cm->input_dev,
90 EV_SW, jack_switch_types[i]);
91 return 0;
94 static int cmhid_input_mapping(struct hid_device *hid,
95 struct hid_input *hi, struct hid_field *field,
96 struct hid_usage *usage, unsigned long **bit, int *max)
98 return -1;
101 static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id)
103 int ret;
104 struct cmhid *cm;
106 cm = kzalloc(sizeof(struct cmhid), GFP_KERNEL);
107 if (!cm) {
108 ret = -ENOMEM;
109 goto allocfail;
112 cm->hid = hid;
114 hid->quirks |= HID_QUIRK_HIDINPUT_FORCE;
115 hid_set_drvdata(hid, cm);
117 ret = hid_parse(hid);
118 if (ret) {
119 hid_err(hid, "parse failed\n");
120 goto fail;
123 ret = hid_hw_start(hid, HID_CONNECT_DEFAULT | HID_CONNECT_HIDDEV_FORCE);
124 if (ret) {
125 hid_err(hid, "hw start failed\n");
126 goto fail;
129 return 0;
130 fail:
131 kfree(cm);
132 allocfail:
133 return ret;
136 static void cmhid_remove(struct hid_device *hid)
138 struct cmhid *cm = hid_get_drvdata(hid);
140 hid_hw_stop(hid);
141 kfree(cm);
144 static const struct hid_device_id cmhid_devices[] = {
145 { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM6533) },
148 MODULE_DEVICE_TABLE(hid, cmhid_devices);
150 static struct hid_driver cmhid_driver = {
151 .name = "cm6533_jd",
152 .id_table = cmhid_devices,
153 .raw_event = cmhid_raw_event,
154 .input_configured = cmhid_input_configured,
155 .probe = cmhid_probe,
156 .remove = cmhid_remove,
157 .input_mapping = cmhid_input_mapping,
159 module_hid_driver(cmhid_driver);