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 cl_data->sensor_sts[i] = SENSOR_DISABLED; 241 cl_data->sensor_requested_cnt[i] = 0; 242 cl_data->cur_hid_dev = i; 243 cl_idx = cl_data->sensor_idx[i]; 244 cl_data->report_descr_sz[i] = mp2_ops->get_desc_sz(cl_idx, descr_size); 245 if (!cl_data->report_descr_sz[i]) { 246 rc = -EINVAL; 247 goto cleanup; 248 } 249 feature_report_size = mp2_ops->get_desc_sz(cl_idx, feature_size); 250 if (!feature_report_size) { 251 rc = -EINVAL; 252 goto cleanup; 253 } 254 input_report_size = mp2_ops->get_desc_sz(cl_idx, input_size); 255 if (!input_report_size) { 256 rc = -EINVAL; 257 goto cleanup; 258 } 259 cl_data->feature_report[i] = devm_kzalloc(dev, feature_report_size, GFP_KERNEL); 260 if (!cl_data->feature_report[i]) { 261 rc = -ENOMEM; 262 goto cleanup; 263 } 264 in_data->input_report[i] = devm_kzalloc(dev, input_report_size, GFP_KERNEL); 265 if (!in_data->input_report[i]) { 266 rc = -ENOMEM; 267 goto cleanup; 268 } 269 info.period = AMD_SFH_IDLE_LOOP; 270 info.sensor_idx = cl_idx; 271 info.dma_address = cl_data->sensor_dma_addr[i]; 272 273 cl_data->report_descr[i] = 274 devm_kzalloc(dev, cl_data->report_descr_sz[i], GFP_KERNEL); 275 if (!cl_data->report_descr[i]) { 276 rc = -ENOMEM; 277 goto cleanup; 278 } 279 rc = mp2_ops->get_rep_desc(cl_idx, cl_data->report_descr[i]); 280 if (rc) 281 return rc; 282 mp2_ops->start(privdata, info); 283 status = amd_sfh_wait_for_response 284 (privdata, cl_data->sensor_idx[i], SENSOR_ENABLED); 285 if (status == SENSOR_ENABLED) { 286 cl_data->sensor_sts[i] = SENSOR_ENABLED; 287 rc = amdtp_hid_probe(cl_data->cur_hid_dev, cl_data); 288 if (rc) { 289 mp2_ops->stop(privdata, cl_data->sensor_idx[i]); 290 status = amd_sfh_wait_for_response 291 (privdata, cl_data->sensor_idx[i], SENSOR_DISABLED); 292 if (status != SENSOR_ENABLED) 293 cl_data->sensor_sts[i] = SENSOR_DISABLED; 294 dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n", 295 cl_data->sensor_idx[i], 296 get_sensor_name(cl_data->sensor_idx[i]), 297 cl_data->sensor_sts[i]); 298 goto cleanup; 299 } 300 } 301 dev_dbg(dev, "sid 0x%x (%s) status 0x%x\n", 302 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 303 cl_data->sensor_sts[i]); 304 } 305 if (mp2_ops->discovery_status && mp2_ops->discovery_status(privdata) == 0) { 306 amd_sfh_hid_client_deinit(privdata); 307 for (i = 0; i < cl_data->num_hid_devices; i++) { 308 devm_kfree(dev, cl_data->feature_report[i]); 309 devm_kfree(dev, in_data->input_report[i]); 310 devm_kfree(dev, cl_data->report_descr[i]); 311 } 312 dev_warn(dev, "Failed to discover, sensors not enabled\n"); 313 return -EOPNOTSUPP; 314 } 315 schedule_delayed_work(&cl_data->work_buffer, msecs_to_jiffies(AMD_SFH_IDLE_LOOP)); 316 return 0; 317 318 cleanup: 319 for (i = 0; i < cl_data->num_hid_devices; i++) { 320 if (in_data->sensor_virt_addr[i]) { 321 dma_free_coherent(&privdata->pdev->dev, 8 * sizeof(int), 322 in_data->sensor_virt_addr[i], 323 cl_data->sensor_dma_addr[i]); 324 } 325 devm_kfree(dev, cl_data->feature_report[i]); 326 devm_kfree(dev, in_data->input_report[i]); 327 devm_kfree(dev, cl_data->report_descr[i]); 328 } 329 return rc; 330 } 331 332 int amd_sfh_hid_client_deinit(struct amd_mp2_dev *privdata) 333 { 334 struct amdtp_cl_data *cl_data = privdata->cl_data; 335 struct amd_input_data *in_data = cl_data->in_data; 336 int i, status; 337 338 for (i = 0; i < cl_data->num_hid_devices; i++) { 339 if (cl_data->sensor_sts[i] == SENSOR_ENABLED) { 340 privdata->mp2_ops->stop(privdata, cl_data->sensor_idx[i]); 341 status = amd_sfh_wait_for_response 342 (privdata, cl_data->sensor_idx[i], SENSOR_DISABLED); 343 if (status != SENSOR_ENABLED) 344 cl_data->sensor_sts[i] = SENSOR_DISABLED; 345 dev_dbg(&privdata->pdev->dev, "stopping sid 0x%x (%s) status 0x%x\n", 346 cl_data->sensor_idx[i], get_sensor_name(cl_data->sensor_idx[i]), 347 cl_data->sensor_sts[i]); 348 } 349 } 350 351 cancel_delayed_work_sync(&cl_data->work); 352 cancel_delayed_work_sync(&cl_data->work_buffer); 353 amdtp_hid_remove(cl_data); 354 355 for (i = 0; i < cl_data->num_hid_devices; i++) { 356 if (in_data->sensor_virt_addr[i]) { 357 dma_free_coherent(&privdata->pdev->dev, 8 * sizeof(int), 358 in_data->sensor_virt_addr[i], 359 cl_data->sensor_dma_addr[i]); 360 } 361 } 362 return 0; 363 } 364