1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  AMD SFH Client Layer
4  *  Copyright 2020-2021 Advanced Micro Devices, Inc.
5  *  Authors: Nehal Bakulchandra Shah <Nehal-Bakulchandra.Shah@amd.com>
6  *	     Sandeep Singh <Sandeep.singh@amd.com>
7  *	     Basavaraj Natikar <Basavaraj.Natikar@amd.com>
8  */
9 
10 #include <linux/dma-mapping.h>
11 #include <linux/hid.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
14 #include <linux/workqueue.h>
15 #include <linux/errno.h>
16 
17 #include "hid_descriptor/amd_sfh_hid_desc.h"
18 #include "amd_sfh_pcie.h"
19 #include "amd_sfh_hid.h"
20 
21 void amd_sfh_set_report(struct hid_device *hid, int report_id,
22 			int report_type)
23 {
24 	struct amdtp_hid_data *hid_data = hid->driver_data;
25 	struct amdtp_cl_data *cli_data = hid_data->cli_data;
26 	int i;
27 
28 	for (i = 0; i < cli_data->num_hid_devices; i++) {
29 		if (cli_data->hid_sensor_hubs[i] == hid) {
30 			cli_data->cur_hid_dev = i;
31 			break;
32 		}
33 	}
34 	amdtp_hid_wakeup(hid);
35 }
36 
37 int amd_sfh_get_report(struct hid_device *hid, int report_id, int report_type)
38 {
39 	struct amdtp_hid_data *hid_data = hid->driver_data;
40 	struct amdtp_cl_data *cli_data = hid_data->cli_data;
41 	struct request_list *req_list = &cli_data->req_list;
42 	int i;
43 
44 	for (i = 0; i < cli_data->num_hid_devices; i++) {
45 		if (cli_data->hid_sensor_hubs[i] == hid) {
46 			struct request_list *new = kzalloc(sizeof(*new), GFP_KERNEL);
47 
48 			if (!new)
49 				return -ENOMEM;
50 
51 			new->current_index = i;
52 			new->sensor_idx = cli_data->sensor_idx[i];
53 			new->hid = hid;
54 			new->report_type = report_type;
55 			new->report_id = report_id;
56 			cli_data->report_id[i] = report_id;
57 			cli_data->request_done[i] = false;
58 			list_add(&new->list, &req_list->list);
59 			break;
60 		}
61 	}
62 	schedule_delayed_work(&cli_data->work, 0);
63 	return 0;
64 }
65 
66 void amd_sfh_work(struct work_struct *work)
67 {
68 	struct amdtp_cl_data *cli_data = container_of(work, struct amdtp_cl_data, work.work);
69 	struct request_list *req_list = &cli_data->req_list;
70 	struct amd_input_data *in_data = cli_data->in_data;
71 	struct request_list *req_node;
72 	u8 current_index, sensor_index;
73 	struct amd_mp2_ops *mp2_ops;
74 	struct amd_mp2_dev *mp2;
75 	u8 report_id, node_type;
76 	u8 report_size = 0;
77 
78 	req_node = list_last_entry(&req_list->list, struct request_list, list);
79 	list_del(&req_node->list);
80 	current_index = req_node->current_index;
81 	sensor_index = req_node->sensor_idx;
82 	report_id = req_node->report_id;
83 	node_type = req_node->report_type;
84 	kfree(req_node);
85 
86 	mp2 = container_of(in_data, struct amd_mp2_dev, in_data);
87 	mp2_ops = mp2->mp2_ops;
88 	if (node_type == HID_FEATURE_REPORT) {
89 		report_size = mp2_ops->get_feat_rep(sensor_index, report_id,
90 						    cli_data->feature_report[current_index]);
91 		if (report_size)
92 			hid_input_report(cli_data->hid_sensor_hubs[current_index],
93 					 cli_data->report_type[current_index],
94 					 cli_data->feature_report[current_index], report_size, 0);
95 		else
96 			pr_err("AMDSFH: Invalid report size\n");
97 
98 	} else if (node_type == HID_INPUT_REPORT) {
99 		report_size = mp2_ops->get_in_rep(current_index, sensor_index, report_id, in_data);
100 		if (report_size)
101 			hid_input_report(cli_data->hid_sensor_hubs[current_index],
102 					 cli_data->report_type[current_index],
103 					 in_data->input_report[current_index], report_size, 0);
104 		else
105 			pr_err("AMDSFH: Invalid report size\n");
106 	}
107 	cli_data->cur_hid_dev = current_index;
108 	cli_data->sensor_requested_cnt[current_index] = 0;
109 	amdtp_hid_wakeup(cli_data->hid_sensor_hubs[current_index]);
110 }
111 
112 void amd_sfh_work_buffer(struct work_struct *work)
113 {
114 	struct amdtp_cl_data *cli_data = container_of(work, struct amdtp_cl_data, work_buffer.work);
115 	struct amd_input_data *in_data = cli_data->in_data;
116 	struct amd_mp2_dev *mp2;
117 	u8 report_size;
118 	int i;
119 
120 	for (i = 0; i < cli_data->num_hid_devices; i++) {
121 		if (cli_data->sensor_sts[i] == SENSOR_ENABLED) {
122 			mp2 = container_of(in_data, struct amd_mp2_dev, in_data);
123 			report_size = mp2->mp2_ops->get_in_rep(i, cli_data->sensor_idx[i],
124 							       cli_data->report_id[i], in_data);
125 			hid_input_report(cli_data->hid_sensor_hubs[i], HID_INPUT_REPORT,
126 					 in_data->input_report[i], report_size, 0);
127 		}
128 	}
129 	schedule_delayed_work(&cli_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
130 }
131 
132 static u32 amd_sfh_wait_for_response(struct amd_mp2_dev *mp2, u8 sid, u32 sensor_sts)
133 {
134 	if (mp2->mp2_ops->response)
135 		sensor_sts = mp2->mp2_ops->response(mp2, sid, sensor_sts);
136 
137 	return sensor_sts;
138 }
139 
140 static const char *get_sensor_name(int idx)
141 {
142 	switch (idx) {
143 	case accel_idx:
144 		return "accelerometer";
145 	case gyro_idx:
146 		return "gyroscope";
147 	case mag_idx:
148 		return "magnetometer";
149 	case als_idx:
150 		return "ALS";
151 	case HPD_IDX:
152 		return "HPD";
153 	default:
154 		return "unknown sensor type";
155 	}
156 }
157 
158 static void amd_sfh_resume(struct amd_mp2_dev *mp2)
159 {
160 	struct amdtp_cl_data *cl_data = mp2->cl_data;
161 	struct amd_mp2_sensor_info info;
162 	int i, status;
163 
164 	for (i = 0; i < cl_data->num_hid_devices; i++) {
165 		if (cl_data->sensor_sts[i] == SENSOR_DISABLED) {
166 			info.period = AMD_SFH_IDLE_LOOP;
167 			info.sensor_idx = cl_data->sensor_idx[i];
168 			info.dma_address = cl_data->sensor_dma_addr[i];
169 			mp2->mp2_ops->start(mp2, info);
170 			status = amd_sfh_wait_for_response
171 					(mp2, cl_data->sensor_idx[i], SENSOR_ENABLED);
172 			if (status == SENSOR_ENABLED)
173 				cl_data->sensor_sts[i] = SENSOR_ENABLED;
174 			dev_dbg(&mp2->pdev->dev, "resume sid 0x%x (%s) status 0x%x\n",
175 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
176 				cl_data->sensor_sts[i]);
177 		}
178 	}
179 
180 	schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
181 	amd_sfh_clear_intr(mp2);
182 }
183 
184 static void amd_sfh_suspend(struct amd_mp2_dev *mp2)
185 {
186 	struct amdtp_cl_data *cl_data = mp2->cl_data;
187 	int i, status;
188 
189 	for (i = 0; i < cl_data->num_hid_devices; i++) {
190 		if (cl_data->sensor_idx[i] != HPD_IDX &&
191 		    cl_data->sensor_sts[i] == SENSOR_ENABLED) {
192 			mp2->mp2_ops->stop(mp2, cl_data->sensor_idx[i]);
193 			status = amd_sfh_wait_for_response
194 					(mp2, cl_data->sensor_idx[i], SENSOR_DISABLED);
195 			if (status != SENSOR_ENABLED)
196 				cl_data->sensor_sts[i] = SENSOR_DISABLED;
197 			dev_dbg(&mp2->pdev->dev, "suspend sid 0x%x (%s) status 0x%x\n",
198 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
199 				cl_data->sensor_sts[i]);
200 		}
201 	}
202 
203 	cancel_delayed_work_sync(&cl_data->work_buffer);
204 	amd_sfh_clear_intr(mp2);
205 }
206 
207 int amd_sfh_hid_client_init(struct amd_mp2_dev *privdata)
208 {
209 	struct amd_input_data *in_data = &privdata->in_data;
210 	struct amdtp_cl_data *cl_data = privdata->cl_data;
211 	struct amd_mp2_ops *mp2_ops = privdata->mp2_ops;
212 	struct amd_mp2_sensor_info info;
213 	struct request_list *req_list;
214 	struct device *dev;
215 	u32 feature_report_size;
216 	u32 input_report_size;
217 	int rc, i, status;
218 	u8 cl_idx;
219 
220 	req_list = &cl_data->req_list;
221 	dev = &privdata->pdev->dev;
222 	amd_sfh_set_desc_ops(mp2_ops);
223 
224 	mp2_ops->suspend = amd_sfh_suspend;
225 	mp2_ops->resume = amd_sfh_resume;
226 
227 	cl_data->num_hid_devices = amd_mp2_get_sensor_num(privdata, &cl_data->sensor_idx[0]);
228 	if (cl_data->num_hid_devices == 0)
229 		return -ENODEV;
230 
231 	INIT_DELAYED_WORK(&cl_data->work, amd_sfh_work);
232 	INIT_DELAYED_WORK(&cl_data->work_buffer, amd_sfh_work_buffer);
233 	INIT_LIST_HEAD(&req_list->list);
234 	cl_data->in_data = in_data;
235 
236 	for (i = 0; i < cl_data->num_hid_devices; i++) {
237 		in_data->sensor_virt_addr[i] = dma_alloc_coherent(dev, sizeof(int) * 8,
238 								  &cl_data->sensor_dma_addr[i],
239 								  GFP_KERNEL);
240 		if (!in_data->sensor_virt_addr[i]) {
241 			rc = -ENOMEM;
242 			goto cleanup;
243 		}
244 		cl_data->sensor_sts[i] = SENSOR_DISABLED;
245 		cl_data->sensor_requested_cnt[i] = 0;
246 		cl_data->cur_hid_dev = i;
247 		cl_idx = cl_data->sensor_idx[i];
248 		cl_data->report_descr_sz[i] = mp2_ops->get_desc_sz(cl_idx, descr_size);
249 		if (!cl_data->report_descr_sz[i]) {
250 			rc = -EINVAL;
251 			goto cleanup;
252 		}
253 		feature_report_size = mp2_ops->get_desc_sz(cl_idx, feature_size);
254 		if (!feature_report_size) {
255 			rc = -EINVAL;
256 			goto cleanup;
257 		}
258 		input_report_size =  mp2_ops->get_desc_sz(cl_idx, input_size);
259 		if (!input_report_size) {
260 			rc = -EINVAL;
261 			goto cleanup;
262 		}
263 		cl_data->feature_report[i] = devm_kzalloc(dev, feature_report_size, GFP_KERNEL);
264 		if (!cl_data->feature_report[i]) {
265 			rc = -ENOMEM;
266 			goto cleanup;
267 		}
268 		in_data->input_report[i] = devm_kzalloc(dev, input_report_size, GFP_KERNEL);
269 		if (!in_data->input_report[i]) {
270 			rc = -ENOMEM;
271 			goto cleanup;
272 		}
273 		info.period = AMD_SFH_IDLE_LOOP;
274 		info.sensor_idx = cl_idx;
275 		info.dma_address = cl_data->sensor_dma_addr[i];
276 
277 		cl_data->report_descr[i] =
278 			devm_kzalloc(dev, cl_data->report_descr_sz[i], GFP_KERNEL);
279 		if (!cl_data->report_descr[i]) {
280 			rc = -ENOMEM;
281 			goto cleanup;
282 		}
283 		rc = mp2_ops->get_rep_desc(cl_idx, cl_data->report_descr[i]);
284 		if (rc)
285 			return rc;
286 		mp2_ops->start(privdata, info);
287 		status = amd_sfh_wait_for_response
288 				(privdata, cl_data->sensor_idx[i], SENSOR_ENABLED);
289 		if (status == SENSOR_ENABLED) {
290 			cl_data->sensor_sts[i] = SENSOR_ENABLED;
291 			rc = amdtp_hid_probe(cl_data->cur_hid_dev, cl_data);
292 			if (rc) {
293 				mp2_ops->stop(privdata, cl_data->sensor_idx[i]);
294 				status = amd_sfh_wait_for_response
295 					(privdata, cl_data->sensor_idx[i], SENSOR_DISABLED);
296 				if (status != SENSOR_ENABLED)
297 					cl_data->sensor_sts[i] = SENSOR_DISABLED;
298 				dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n",
299 					cl_data->sensor_idx[i],
300 					get_sensor_name(cl_data->sensor_idx[i]),
301 					cl_data->sensor_sts[i]);
302 				goto cleanup;
303 			}
304 		}
305 		dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n",
306 			cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
307 			cl_data->sensor_sts[i]);
308 	}
309 	if (mp2_ops->discovery_status && mp2_ops->discovery_status(privdata) == 0) {
310 		amd_sfh_hid_client_deinit(privdata);
311 		for (i = 0; i < cl_data->num_hid_devices; i++) {
312 			devm_kfree(dev, cl_data->feature_report[i]);
313 			devm_kfree(dev, in_data->input_report[i]);
314 			devm_kfree(dev, cl_data->report_descr[i]);
315 		}
316 		dev_warn(dev, "Failed to discover, sensors not enabled\n");
317 		return -EOPNOTSUPP;
318 	}
319 	schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP));
320 	return 0;
321 
322 cleanup:
323 	for (i = 0; i < cl_data->num_hid_devices; i++) {
324 		if (in_data->sensor_virt_addr[i]) {
325 			dma_free_coherent(&privdata->pdev->dev, 8 * sizeof(int),
326 					  in_data->sensor_virt_addr[i],
327 					  cl_data->sensor_dma_addr[i]);
328 		}
329 		devm_kfree(dev, cl_data->feature_report[i]);
330 		devm_kfree(dev, in_data->input_report[i]);
331 		devm_kfree(dev, cl_data->report_descr[i]);
332 	}
333 	return rc;
334 }
335 
336 int amd_sfh_hid_client_deinit(struct amd_mp2_dev *privdata)
337 {
338 	struct amdtp_cl_data *cl_data = privdata->cl_data;
339 	struct amd_input_data *in_data = cl_data->in_data;
340 	int i, status;
341 
342 	for (i = 0; i < cl_data->num_hid_devices; i++) {
343 		if (cl_data->sensor_sts[i] == SENSOR_ENABLED) {
344 			privdata->mp2_ops->stop(privdata, cl_data->sensor_idx[i]);
345 			status = amd_sfh_wait_for_response
346 					(privdata, cl_data->sensor_idx[i], SENSOR_DISABLED);
347 			if (status != SENSOR_ENABLED)
348 				cl_data->sensor_sts[i] = SENSOR_DISABLED;
349 			dev_dbg(&privdata->pdev->dev, "stopping sid 0x%x (%s) status 0x%x\n",
350 				cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]),
351 				cl_data->sensor_sts[i]);
352 		}
353 	}
354 
355 	cancel_delayed_work_sync(&cl_data->work);
356 	cancel_delayed_work_sync(&cl_data->work_buffer);
357 	amdtp_hid_remove(cl_data);
358 
359 	for (i = 0; i < cl_data->num_hid_devices; i++) {
360 		if (in_data->sensor_virt_addr[i]) {
361 			dma_free_coherent(&privdata->pdev->dev, 8 * sizeof(int),
362 					  in_data->sensor_virt_addr[i],
363 					  cl_data->sensor_dma_addr[i]);
364 		}
365 	}
366 	return 0;
367 }
368