Skip to content

Commit a22d44c

Browse files
committed
ASoC: SDCA: register a HID device for HIDE entity
This patch registers a HID device for SDCA HIDE entity. The codec driver could call 'hid_input_report' to report events. Signed-off-by: Shuming Fan <shumingf@realtek.com>
1 parent 8d942e4 commit a22d44c

5 files changed

Lines changed: 179 additions & 0 deletions

File tree

include/sound/sdca_hid.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
2+
/*
3+
* The MIPI SDCA specification is available for public downloads at
4+
* https://www.mipi.org/mipi-sdca-v1-0-download
5+
*
6+
*/
7+
8+
#ifndef __SDCA_HID_H__
9+
#define __SDCA_HID_H__
10+
11+
#include <linux/types.h>
12+
#include <linux/hid.h>
13+
14+
#if IS_ENABLED(CONFIG_SND_SOC_SDCA_HID)
15+
int sdca_add_hid_device(struct device *dev, struct fwnode_handle *entity_node,
16+
struct sdca_entity *entity);
17+
18+
#else
19+
static inline int sdca_add_hid_device(struct device *dev, struct fwnode_handle *entity_node,
20+
struct sdca_entity *entity)
21+
{
22+
return 0;
23+
}
24+
25+
#endif
26+
27+
#endif /* __SDCA_HID_H__ */

sound/soc/sdca/Kconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ config SND_SOC_SDCA
99

1010
config SND_SOC_SDCA_OPTIONAL
1111
def_tristate SND_SOC_SDCA || !SND_SOC_SDCA
12+
13+
config SND_SOC_SDCA_HID
14+
tristate "SDCA HID support"
15+
depends on SND_SOC_SDCA && HID

sound/soc/sdca/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22

3+
snd-soc-sdca-hid-y := sdca_hid.o
4+
35
snd-soc-sdca-y := sdca_functions.o sdca_device.o sdca_regmap.o
46

7+
obj-$(CONFIG_SND_SOC_SDCA_HID) += snd-soc-sdca-hid.o
58
obj-$(CONFIG_SND_SOC_SDCA) += snd-soc-sdca.o

sound/soc/sdca/sdca_functions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/types.h>
2020
#include <sound/sdca.h>
2121
#include <sound/sdca_function.h>
22+
#include <sound/sdca_hid.h>
2223

2324
/*
2425
* Should be long enough to encompass all the MIPI DisCo properties.
@@ -1305,6 +1306,9 @@ find_sdca_entity_hide(struct device *dev, struct fwnode_handle *function_node,
13051306
return -ENOMEM;
13061307
hide->hid_report_desc = report_desc;
13071308
fwnode_property_read_u8_array(function_node, "mipi-sdca-report-descriptor", report_desc, nval);
1309+
1310+
/* add HID device */
1311+
sdca_add_hid_device(dev, entity_node, entity);
13081312
}
13091313
}
13101314

@@ -1944,3 +1948,5 @@ EXPORT_SYMBOL_NS(sdca_parse_function, "SND_SOC_SDCA");
19441948

19451949
MODULE_LICENSE("Dual BSD/GPL");
19461950
MODULE_DESCRIPTION("SDCA library");
1951+
1952+
MODULE_IMPORT_NS("SND_SOC_SDCA_HID");

sound/soc/sdca/sdca_hid.c

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2+
3+
/*
4+
* The MIPI SDCA specification is available for public downloads at
5+
* https://www.mipi.org/mipi-sdca-v1-0-download
6+
*/
7+
8+
#include <linux/acpi.h>
9+
#include <linux/byteorder/generic.h>
10+
#include <linux/cleanup.h>
11+
#include <linux/device.h>
12+
#include <linux/dev_printk.h>
13+
#include <linux/module.h>
14+
#include <linux/property.h>
15+
#include <linux/soundwire/sdw.h>
16+
#include <linux/types.h>
17+
#include <sound/sdca.h>
18+
#include <sound/sdca_function.h>
19+
#include <sound/sdca_hid.h>
20+
21+
static int sdwhid_parse(struct hid_device *hid)
22+
{
23+
struct sdca_entity *entity = hid->driver_data;
24+
unsigned int rsize = 0;
25+
char *rdesc;
26+
int ret;
27+
28+
rsize = entity->hide.hid_desc.desc[0].wDescriptorLength;
29+
30+
if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
31+
dev_err(&hid->dev, "invalid size of report descriptor (%u)\n", rsize);
32+
return -EINVAL;
33+
}
34+
35+
rdesc = kmalloc(rsize, GFP_KERNEL);
36+
if (!rdesc)
37+
return -ENOMEM;
38+
39+
memcpy(rdesc, entity->hide.hid_report_desc, rsize);
40+
41+
ret = hid_parse_report(hid, rdesc, rsize);
42+
kfree(rdesc);
43+
44+
if (!ret)
45+
return 0;
46+
47+
dev_err(&hid->dev, "parsing report descriptor failed\n");
48+
return ret;
49+
}
50+
51+
static int sdwhid_start(struct hid_device *hid)
52+
{
53+
return 0;
54+
}
55+
56+
static void sdwhid_stop(struct hid_device *hid)
57+
{
58+
59+
}
60+
61+
static int sdwhid_raw_request(struct hid_device *hid, unsigned char reportnum,
62+
__u8 *buf, size_t len, unsigned char rtype, int reqtype)
63+
{
64+
switch (reqtype) {
65+
case HID_REQ_GET_REPORT:
66+
/* not implemented yet */
67+
//return usbhid_get_raw_report(hid, reportnum, buf, len, rtype);
68+
printk("%s: HID_REQ_GET_REPORT not implemented\n", __func__);
69+
return 0;
70+
case HID_REQ_SET_REPORT:
71+
/* not implemented yet */
72+
//return usbhid_set_raw_report(hid, reportnum, buf, len, rtype);
73+
printk("%s: HID_REQ_SET_REPORT not implemented\n", __func__);
74+
return 0;
75+
default:
76+
return -EIO;
77+
}
78+
}
79+
80+
static int sdwhid_open(struct hid_device *hid)
81+
{
82+
return 0;
83+
}
84+
85+
static void sdwhid_close(struct hid_device *hid)
86+
{
87+
88+
}
89+
90+
static const struct hid_ll_driver sdw_hid_driver = {
91+
.parse = sdwhid_parse,
92+
.start = sdwhid_start,
93+
.stop = sdwhid_stop,
94+
.open = sdwhid_open,
95+
.close = sdwhid_close,
96+
.raw_request = sdwhid_raw_request,
97+
};
98+
99+
int sdca_add_hid_device(struct device *dev, struct fwnode_handle *entity_node,
100+
struct sdca_entity *entity)
101+
{
102+
struct sdw_bus *bus;
103+
struct hid_device *hid;
104+
struct sdw_slave *slave = dev_to_sdw_dev(dev);
105+
int ret;
106+
107+
bus = slave->bus;
108+
109+
hid = hid_allocate_device();
110+
if (IS_ERR(hid))
111+
return PTR_ERR(hid);
112+
113+
hid->ll_driver = &sdw_hid_driver;
114+
115+
hid->dev.parent = dev;
116+
117+
hid->name[0] = 0;
118+
if (!strlen(hid->name))
119+
snprintf(hid->name, sizeof(hid->name), "HID sdw:%01x:%01x:%04x:%04x:%02x",
120+
bus->controller_id, bus->link_id, slave->id.mfg_id, slave->id.part_id,
121+
slave->id.class_id);
122+
printk("HIDE: hid name = %s\n", hid->name);
123+
124+
hid->driver_data = entity;
125+
126+
ret = hid_add_device(hid);
127+
if (ret) {
128+
if (ret != -ENODEV)
129+
dev_err(dev, "can't add hid device: %d\n", ret);
130+
}
131+
132+
entity->hide.hid = hid;
133+
134+
return 0;
135+
}
136+
EXPORT_SYMBOL_NS(sdca_add_hid_device, "SND_SOC_SDCA_HID");
137+
138+
MODULE_LICENSE("Dual BSD/GPL");
139+
MODULE_DESCRIPTION("SDCA HID library");

0 commit comments

Comments
 (0)