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;
105 struct amdtp_cl_data *cli_data;
108 hid_data = hid->driver_data;
109 cli_data = hid_data->cli_data;
110 cli_data->request_done[cli_data->cur_hid_dev] = true;
111 wake_up_interruptible(&hid_data->hid_wait);
115 static const struct hid_ll_driver amdtp_hid_ll_driver = {
116 .parse = amdtp_hid_parse,
117 .start = amdtp_hid_start,
118 .stop = amdtp_hid_stop,
119 .open = amdtp_hid_open,
120 .close = amdtp_hid_close,
121 .request = amdtp_hid_request,
122 .wait = amdtp_wait_for_response,
123 .raw_request = amdtp_raw_request,
126 int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
128 struct amd_mp2_dev *mp2 = container_of(cli_data->in_data, struct amd_mp2_dev, in_data);
129 struct device *dev = &mp2->pdev->dev;
130 struct hid_device *hid;
131 struct amdtp_hid_data *hid_data;
134 hid = hid_allocate_device();
138 hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
144 hid->ll_driver = &amdtp_hid_ll_driver;
145 hid_data->index = cur_hid_dev;
146 hid_data->cli_data = cli_data;
147 init_waitqueue_head(&hid_data->hid_wait);
149 hid->driver_data = hid_data;
150 cli_data->hid_sensor_hubs[cur_hid_dev] = hid;
151 strscpy(hid->phys, dev->driver ? dev->driver->name : dev_name(dev),
153 hid->bus = BUS_AMD_SFH;
154 hid->vendor = AMD_SFH_HID_VENDOR;
155 hid->product = AMD_SFH_HID_PRODUCT;
156 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-amdsfh",
157 hid->vendor, hid->product);
159 rc = hid_add_device(hid);
167 hid_destroy_device(hid);
171 void amdtp_hid_remove(struct amdtp_cl_data *cli_data)
175 for (i = 0; i < cli_data->num_hid_devices; ++i) {
176 if (cli_data->hid_sensor_hubs[i]) {
177 kfree(cli_data->hid_sensor_hubs[i]->driver_data);
178 hid_destroy_device(cli_data->hid_sensor_hubs[i]);
179 cli_data->hid_sensor_hubs[i] = NULL;