1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AMD MP2 Sensors transport driver
5 * Copyright 2020-2021 Advanced Micro Devices, Inc.
6 * Authors: Nehal Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>
7 * Sandeep Singh <sandeep.singh@amd.com>
8 * Basavaraj Natikar <Basavaraj.Natikar@amd.com>
10 #include <linux/hid.h>
11 #include <linux/wait.h>
12 #include <linux/sched.h>
14 #include "amd_sfh_hid.h"
15 #include "amd_sfh_pcie.h"
17 #define AMD_SFH_RESPONSE_TIMEOUT 1500
20 * amdtp_hid_parse() - hid-core .parse() callback
21 * @hid: hid device instance
23 * This function gets called during call to hid_add_device
25 * Return: 0 on success and non zero on error
27 static int amdtp_hid_parse(struct hid_device *hid)
29 struct amdtp_hid_data *hid_data = hid->driver_data;
30 struct amdtp_cl_data *cli_data = hid_data->cli_data;
32 return hid_parse_report(hid, cli_data->report_descr[hid_data->index],
33 cli_data->report_descr_sz[hid_data->index]);
36 /* Empty callbacks with success return code */
37 static int amdtp_hid_start(struct hid_device *hid)
42 static void amdtp_hid_stop(struct hid_device *hid)
46 static int amdtp_hid_open(struct hid_device *hid)
51 static void amdtp_hid_close(struct hid_device *hid)
55 static int amdtp_raw_request(struct hid_device *hdev, u8 reportnum,
56 u8 *buf, size_t len, u8 rtype, int reqtype)
61 static void amdtp_hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
66 case HID_REQ_GET_REPORT:
67 rc = amd_sfh_get_report(hid, rep->id, rep->type);
69 dev_err(&hid->dev, "AMDSFH get report error\n");
71 case HID_REQ_SET_REPORT:
72 amd_sfh_set_report(hid, rep->id, reqtype);
79 static int amdtp_wait_for_response(struct hid_device *hid)
81 struct amdtp_hid_data *hid_data = hid->driver_data;
82 struct amdtp_cl_data *cli_data = hid_data->cli_data;
85 for (i = 0; i < cli_data->num_hid_devices; i++) {
86 if (cli_data->hid_sensor_hubs[i] == hid)
90 if (!cli_data->request_done[i])
91 ret = wait_event_interruptible_timeout(hid_data->hid_wait,
92 cli_data->request_done[i],
93 msecs_to_jiffies(AMD_SFH_RESPONSE_TIMEOUT));
94 if (ret == -ERESTARTSYS)
102 void amdtp_hid_wakeup(struct hid_device *hid)
104 struct amdtp_hid_data *hid_data = hid->driver_data;
105 struct amdtp_cl_data *cli_data = hid_data->cli_data;
107 cli_data->request_done[cli_data->cur_hid_dev] = true;
108 wake_up_interruptible(&hid_data->hid_wait);
111 static struct hid_ll_driver amdtp_hid_ll_driver = {
112 .parse = amdtp_hid_parse,
113 .start = amdtp_hid_start,
114 .stop = amdtp_hid_stop,
115 .open = amdtp_hid_open,
116 .close = amdtp_hid_close,
117 .request = amdtp_hid_request,
118 .wait = amdtp_wait_for_response,
119 .raw_request = amdtp_raw_request,
122 int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
124 struct amd_mp2_dev *mp2 = container_of(cli_data->in_data, struct amd_mp2_dev, in_data);
125 struct device *dev = &mp2->pdev->dev;
126 struct hid_device *hid;
127 struct amdtp_hid_data *hid_data;
130 hid = hid_allocate_device();
134 hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
140 hid->ll_driver = &amdtp_hid_ll_driver;
141 hid_data->index = cur_hid_dev;
142 hid_data->cli_data = cli_data;
143 init_waitqueue_head(&hid_data->hid_wait);
145 hid->driver_data = hid_data;
146 cli_data->hid_sensor_hubs[cur_hid_dev] = hid;
147 strscpy(hid->phys, dev->driver ? dev->driver->name : dev_name(dev),
149 hid->bus = BUS_AMD_SFH;
150 hid->vendor = AMD_SFH_HID_VENDOR;
151 hid->product = AMD_SFH_HID_PRODUCT;
152 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-amdsfh",
153 hid->vendor, hid->product);
155 rc = hid_add_device(hid);
163 hid_destroy_device(hid);
167 void amdtp_hid_remove(struct amdtp_cl_data *cli_data)
171 for (i = 0; i < cli_data->num_hid_devices; ++i) {
172 if (cli_data->hid_sensor_hubs[i]) {
173 kfree(cli_data->hid_sensor_hubs[i]->driver_data);
174 hid_destroy_device(cli_data->hid_sensor_hubs[i]);
175 cli_data->hid_sensor_hubs[i] = NULL;