xref: /openbmc/linux/drivers/hid/amd-sfh-hid/amd_sfh_client.c (revision c0ecca6604b80e438b032578634c6e133c7028f6)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  AMD SFH Client Layer
4  *  Copyright 2020 Advanced Micro Devices, Inc.
5  *  Authors: Nehal Bakulchandra Shah <Nehal-Bakulchandra.Shah@amd.com>
6  *	     Sandeep Singh <Sandeep.singh@amd.com>
7  */
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/hid.h>
11 #include <linux/list.h>
12 #include <linux/slab.h>
13 #include <linux/workqueue.h>
14 #include <linux/errno.h>
15 
16 #include "hid_descriptor/amd_sfh_hid_desc.h"
17 #include "amd_sfh_pcie.h"
18 #include "amd_sfh_hid.h"
19 
20 #define AMD_SFH_IDLE_LOOP	200
21 
22 struct request_list {
23 	struct hid_device *hid;
24 	struct list_head list;
25 	u8 report_id;
26 	u8 sensor_idx;
27 	u8 report_type;
28 	u8 current_index;
29 };
30 
31 static struct request_list req_list;
32 
33 void amd_sfh_set_report(struct hid_device *hid, int report_id,
34 			int report_type)
35 {
36 	struct amdtp_hid_data *hid_data = hid->driver_data;
37 	struct amdtp_cl_data *cli_data = hid_data->cli_data;
38 	int i;
39 
40 	for (i = 0; i < cli_data->num_hid_devices; i++) {
41 		if (cli_data->hid_sensor_hubs[i] == hid) {
42 			cli_data->cur_hid_dev = i;
43 			break;
44 		}
45 	}
46 	amdtp_hid_wakeup(hid);
47 }
48 
49 int amd_sfh_get_report(struct hid_device *hid, int report_id, int report_type)
50 {
51 	struct amdtp_hid_data *hid_data = hid->driver_data;
52 	struct amdtp_cl_data *cli_data = hid_data->cli_data;
53 	int i;
54 
55 	for (i = 0; i < cli_data->num_hid_devices; i++) {
56 		if (cli_data->hid_sensor_hubs[i] == hid) {
57 			struct request_list *new = kzalloc(sizeof(*new), GFP_KERNEL);
58 
59 			if (!new)
60 				return -ENOMEM;
61 
62 			new->current_index = i;
63 			new->sensor_idx = cli_data->sensor_idx[i];
64 			new->hid = hid;
65 			new->report_type = report_type;
66 			new->report_id = report_id;
67 			cli_data->report_id[i] = report_id;
68 			cli_data->request_done[i] = false;
69 			list_add(&new->list, &req_list.list);
70 			break;
71 		}
72 	}
73 	schedule_delayed_work(&cli_data->work, 0);
74 	return 0;
75 }
76 
77 static void amd_sfh_work(struct work_struct *work)
78 {
79 	struct amdtp_cl_data *cli_data = container_of(work, struct amdtp_cl_data, work.work);
80 	struct request_list *req_node;
81 	u8 current_index, sensor_index;
82 	u8 report_id, node_type;
83 	u8 report_size = 0;
84 
85 	req_node = list_last_entry(&req_list.list, struct request_list, list);
86 	list_del(&req_node->list);
87 	current_index = req_node->current_index;
88 	sensor_index = req_node->sensor_idx;
89 	report_id = req_node->report_id;
90 	node_type = req_node->report_type;
91 	kfree(req_node);
92 
93 	if (node_type == HID_FEATURE_REPORT) {
94 		report_size = get_feature_report(sensor_index, report_id,
95 						 cli_data->feature_report[current_index]);
96 		if (report_size)
97 			hid_input_report(cli_data->hid_sensor_hubs[current_index],
98 					 cli_data->report_type[current_index],
99 					 cli_data->feature_report[current_index], report_size, 0);
100 		else
101 			pr_err("AMDSFH: Invalid report size\n");
102 
103 	} else if (node_type == HID_INPUT_REPORT) {
104 		report_size = get_input_report(sensor_index, report_id,
105 					       cli_data->input_report[current_index],
106 					       cli_data->sensor_virt_addr[current_index]);
107 		if (report_size)
108 			hid_input_report(cli_data->hid_sensor_hubs[current_index],
109 					 cli_data->report_type[current_index],
110 					 cli_data->input_report[current_index], report_size, 0);
111 		else
112 			pr_err("AMDSFH: Invalid report size\n");
113 	}
114 	cli_data->cur_hid_dev = current_index;
115 	cli_data->sensor_requested_cnt[current_index] = 0;
116 	amdtp_hid_wakeup(cli_data->hid_sensor_hubs[current_index]);
117 }
118 
119 static void amd_sfh_work_buffer(struct work_struct *work)
120 {
121 	struct amdtp_cl_data *cli_data = container_of(work, struct amdtp_cl_data, work_buffer.work);
122 	u8 report_size;
123 	int i;
124 
125 	for (i = 0; i < cli_data->num_hid_devices; i++) {
126 		report_size = get_input_report(cli_data->sensor_idx[i], cli_data->report_id[i],
127 					       cli_data->input_report[i],
128 					       cli_data->sensor_virt_addr[i]);
129 		hid_input_report(cli_data->hid_sensor_hubs[i], HID_INPUT_REPORT,
130 				 cli_data->input_report[i], report_size, 0);
131 	}
132 	schedule_delayed_work(&cli_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
133 }
134 
135 int amd_sfh_hid_client_init(struct amd_mp2_dev *privdata)
136 {
137 	struct amdtp_cl_data *cl_data = privdata->cl_data;
138 	struct amd_mp2_sensor_info info;
139 	struct device *dev;
140 	u32 feature_report_size;
141 	u32 input_report_size;
142 	u8 cl_idx;
143 	int rc, i;
144 
145 	dev = &privdata->pdev->dev;
146 	cl_data = devm_kzalloc(dev, sizeof(*cl_data), GFP_KERNEL);
147 	if (!cl_data)
148 		return -ENOMEM;
149 
150 	cl_data->num_hid_devices = amd_mp2_get_sensor_num(privdata, &cl_data->sensor_idx[0]);
151 
152 	INIT_DELAYED_WORK(&cl_data->work, amd_sfh_work);
153 	INIT_DELAYED_WORK(&cl_data->work_buffer, amd_sfh_work_buffer);
154 	INIT_LIST_HEAD(&req_list.list);
155 
156 	for (i = 0; i < cl_data->num_hid_devices; i++) {
157 		cl_data->sensor_virt_addr[i] = dma_alloc_coherent(dev, sizeof(int) * 8,
158 								  &cl_data->sensor_dma_addr[i],
159 								  GFP_KERNEL);
160 		cl_data->sensor_sts[i] = 0;
161 		cl_data->sensor_requested_cnt[i] = 0;
162 		cl_data->cur_hid_dev = i;
163 		cl_idx = cl_data->sensor_idx[i];
164 		cl_data->report_descr_sz[i] = get_descr_sz(cl_idx, descr_size);
165 		if (!cl_data->report_descr_sz[i]) {
166 			rc = -EINVAL;
167 			goto cleanup;
168 		}
169 		feature_report_size = get_descr_sz(cl_idx, feature_size);
170 		if (!feature_report_size) {
171 			rc = -EINVAL;
172 			goto cleanup;
173 		}
174 		input_report_size =  get_descr_sz(cl_idx, input_size);
175 		if (!input_report_size) {
176 			rc = -EINVAL;
177 			goto cleanup;
178 		}
179 		cl_data->feature_report[i] = devm_kzalloc(dev, feature_report_size, GFP_KERNEL);
180 		if (!cl_data->feature_report[i]) {
181 			rc = -ENOMEM;
182 			goto cleanup;
183 		}
184 		cl_data->input_report[i] = devm_kzalloc(dev, input_report_size, GFP_KERNEL);
185 		if (!cl_data->input_report[i]) {
186 			rc = -ENOMEM;
187 			goto cleanup;
188 		}
189 		info.period = msecs_to_jiffies(AMD_SFH_IDLE_LOOP);
190 		info.sensor_idx = cl_idx;
191 		info.dma_address = cl_data->sensor_dma_addr[i];
192 
193 		cl_data->report_descr[i] =
194 			devm_kzalloc(dev, cl_data->report_descr_sz[i], GFP_KERNEL);
195 		if (!cl_data->report_descr[i]) {
196 			rc = -ENOMEM;
197 			goto cleanup;
198 		}
199 		rc = get_report_descriptor(cl_idx, cl_data->report_descr[i]);
200 		if (rc)
201 			return rc;
202 		rc = amdtp_hid_probe(cl_data->cur_hid_dev, cl_data);
203 		if (rc)
204 			return rc;
205 		amd_start_sensor(privdata, info);
206 		cl_data->sensor_sts[i] = 1;
207 	}
208 	privdata->cl_data = cl_data;
209 	schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
210 	return 0;
211 
212 cleanup:
213 	for (i = 0; i < cl_data->num_hid_devices; i++) {
214 		if (cl_data->sensor_virt_addr[i]) {
215 			dma_free_coherent(&privdata->pdev->dev, 8 * sizeof(int),
216 					  cl_data->sensor_virt_addr[i],
217 					  cl_data->sensor_dma_addr[i]);
218 		}
219 		devm_kfree(dev, cl_data->feature_report[i]);
220 		devm_kfree(dev, cl_data->input_report[i]);
221 		devm_kfree(dev, cl_data->report_descr[i]);
222 	}
223 	devm_kfree(dev, cl_data);
224 	return rc;
225 }
226 
227 int amd_sfh_hid_client_deinit(struct amd_mp2_dev *privdata)
228 {
229 	struct amdtp_cl_data *cl_data = privdata->cl_data;
230 	int i;
231 
232 	for (i = 0; i < cl_data->num_hid_devices; i++)
233 		amd_stop_sensor(privdata, i);
234 
235 	cancel_delayed_work_sync(&cl_data->work);
236 	cancel_delayed_work_sync(&cl_data->work_buffer);
237 	amdtp_hid_remove(cl_data);
238 
239 	for (i = 0; i < cl_data->num_hid_devices; i++) {
240 		if (cl_data->sensor_virt_addr[i]) {
241 			dma_free_coherent(&privdata->pdev->dev, 8 * sizeof(int),
242 					  cl_data->sensor_virt_addr[i],
243 					  cl_data->sensor_dma_addr[i]);
244 		}
245 	}
246 	return 0;
247 }
248