1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * AMD MP2 Sensors transport driver 4 * 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> 9 */ 10 #include <linux/hid.h> 11 #include <linux/wait.h> 12 #include <linux/sched.h> 13 14 #include "amd_sfh_hid.h" 15 #include "amd_sfh_pcie.h" 16 17 #define AMD_SFH_RESPONSE_TIMEOUT 1500 18 19 /** 20 * amdtp_hid_parse() - hid-core .parse() callback 21 * @hid: hid device instance 22 * 23 * This function gets called during call to hid_add_device 24 * 25 * Return: 0 on success and non zero on error 26 */ 27 static int amdtp_hid_parse(struct hid_device *hid) 28 { 29 struct amdtp_hid_data *hid_data = hid->driver_data; 30 struct amdtp_cl_data *cli_data = hid_data->cli_data; 31 32 return hid_parse_report(hid, cli_data->report_descr[hid_data->index], 33 cli_data->report_descr_sz[hid_data->index]); 34 } 35 36 /* Empty callbacks with success return code */ 37 static int amdtp_hid_start(struct hid_device *hid) 38 { 39 return 0; 40 } 41 42 static void amdtp_hid_stop(struct hid_device *hid) 43 { 44 } 45 46 static int amdtp_hid_open(struct hid_device *hid) 47 { 48 return 0; 49 } 50 51 static void amdtp_hid_close(struct hid_device *hid) 52 { 53 } 54 55 static int amdtp_raw_request(struct hid_device *hdev, u8 reportnum, 56 u8 *buf, size_t len, u8 rtype, int reqtype) 57 { 58 return 0; 59 } 60 61 static void amdtp_hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype) 62 { 63 int rc; 64 65 switch (reqtype) { 66 case HID_REQ_GET_REPORT: 67 rc = amd_sfh_get_report(hid, rep->id, rep->type); 68 if (rc) 69 dev_err(&hid->dev, "AMDSFH get report error\n"); 70 break; 71 case HID_REQ_SET_REPORT: 72 amd_sfh_set_report(hid, rep->id, reqtype); 73 break; 74 default: 75 break; 76 } 77 } 78 79 static int amdtp_wait_for_response(struct hid_device *hid) 80 { 81 struct amdtp_hid_data *hid_data = hid->driver_data; 82 struct amdtp_cl_data *cli_data = hid_data->cli_data; 83 int i, ret = 0; 84 85 for (i = 0; i < cli_data->num_hid_devices; i++) { 86 if (cli_data->hid_sensor_hubs[i] == hid) 87 break; 88 } 89 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) 95 return -ERESTARTSYS; 96 else if (ret < 0) 97 return -ETIMEDOUT; 98 else 99 return 0; 100 } 101 102 void amdtp_hid_wakeup(struct hid_device *hid) 103 { 104 struct amdtp_hid_data *hid_data; 105 struct amdtp_cl_data *cli_data; 106 107 if (hid) { 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); 112 } 113 } 114 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, 124 }; 125 126 int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data) 127 { 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; 132 int rc; 133 134 hid = hid_allocate_device(); 135 if (IS_ERR(hid)) 136 return PTR_ERR(hid); 137 138 hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL); 139 if (!hid_data) { 140 rc = -ENOMEM; 141 goto err_hid_data; 142 } 143 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); 148 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), 152 sizeof(hid->phys)); 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); 158 159 rc = hid_add_device(hid); 160 if (rc) 161 goto err_hid_device; 162 return 0; 163 164 err_hid_device: 165 kfree(hid_data); 166 err_hid_data: 167 hid_destroy_device(hid); 168 return rc; 169 } 170 171 void amdtp_hid_remove(struct amdtp_cl_data *cli_data) 172 { 173 int i; 174 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; 180 } 181 } 182 } 183