• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * AMD MP2 Sensors transport driver
4  *
5  * Authors: Nehal Bakulchandra Shah <Nehal-bakulchandra.shah@amd.com>
6  *	    Sandeep Singh <sandeep.singh@amd.com>
7  */
8 #include <linux/hid.h>
9 #include <linux/wait.h>
10 #include <linux/sched.h>
11 
12 #include "amd_sfh_hid.h"
13 
14 #define AMD_SFH_RESPONSE_TIMEOUT	1500
15 
16 /**
17  * amdtp_hid_parse() - hid-core .parse() callback
18  * @hid:	hid device instance
19  *
20  * This function gets called during call to hid_add_device
21  *
22  * Return: 0 on success and non zero on error
23  */
amdtp_hid_parse(struct hid_device * hid)24 static int amdtp_hid_parse(struct hid_device *hid)
25 {
26 	struct amdtp_hid_data *hid_data = hid->driver_data;
27 	struct amdtp_cl_data *cli_data = hid_data->cli_data;
28 
29 	return hid_parse_report(hid, cli_data->report_descr[hid_data->index],
30 			      cli_data->report_descr_sz[hid_data->index]);
31 }
32 
33 /* Empty callbacks with success return code */
amdtp_hid_start(struct hid_device * hid)34 static int amdtp_hid_start(struct hid_device *hid)
35 {
36 	return 0;
37 }
38 
amdtp_hid_stop(struct hid_device * hid)39 static void amdtp_hid_stop(struct hid_device *hid)
40 {
41 }
42 
amdtp_hid_open(struct hid_device * hid)43 static int amdtp_hid_open(struct hid_device *hid)
44 {
45 	return 0;
46 }
47 
amdtp_hid_close(struct hid_device * hid)48 static void amdtp_hid_close(struct hid_device *hid)
49 {
50 }
51 
amdtp_raw_request(struct hid_device * hdev,u8 reportnum,u8 * buf,size_t len,u8 rtype,int reqtype)52 static int amdtp_raw_request(struct hid_device *hdev, u8 reportnum,
53 			     u8 *buf, size_t len, u8 rtype, int reqtype)
54 {
55 	return 0;
56 }
57 
amdtp_hid_request(struct hid_device * hid,struct hid_report * rep,int reqtype)58 static void amdtp_hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
59 {
60 	int rc;
61 
62 	switch (reqtype) {
63 	case HID_REQ_GET_REPORT:
64 		rc = amd_sfh_get_report(hid, rep->id, rep->type);
65 		if (rc)
66 			dev_err(&hid->dev, "AMDSFH  get report error\n");
67 		break;
68 	case HID_REQ_SET_REPORT:
69 		amd_sfh_set_report(hid, rep->id, reqtype);
70 		break;
71 	default:
72 		break;
73 	}
74 }
75 
amdtp_wait_for_response(struct hid_device * hid)76 static int amdtp_wait_for_response(struct hid_device *hid)
77 {
78 	struct amdtp_hid_data *hid_data =  hid->driver_data;
79 	struct amdtp_cl_data *cli_data = hid_data->cli_data;
80 	int i, ret = 0;
81 
82 	for (i = 0; i < cli_data->num_hid_devices; i++) {
83 		if (cli_data->hid_sensor_hubs[i] == hid)
84 			break;
85 	}
86 
87 	if (!cli_data->request_done[i])
88 		ret = wait_event_interruptible_timeout(hid_data->hid_wait,
89 						       cli_data->request_done[i],
90 						       msecs_to_jiffies(AMD_SFH_RESPONSE_TIMEOUT));
91 	if (ret == -ERESTARTSYS)
92 		return -ERESTARTSYS;
93 	else if (ret < 0)
94 		return -ETIMEDOUT;
95 	else
96 		return 0;
97 }
98 
amdtp_hid_wakeup(struct hid_device * hid)99 void amdtp_hid_wakeup(struct hid_device *hid)
100 {
101 	struct amdtp_hid_data *hid_data;
102 	struct amdtp_cl_data *cli_data;
103 
104 	if (hid) {
105 		hid_data = hid->driver_data;
106 		cli_data = hid_data->cli_data;
107 		cli_data->request_done[cli_data->cur_hid_dev] = true;
108 		wake_up_interruptible(&hid_data->hid_wait);
109 	}
110 }
111 
112 static struct hid_ll_driver amdtp_hid_ll_driver = {
113 	.parse	=	amdtp_hid_parse,
114 	.start	=	amdtp_hid_start,
115 	.stop	=	amdtp_hid_stop,
116 	.open	=	amdtp_hid_open,
117 	.close	=	amdtp_hid_close,
118 	.request  =	amdtp_hid_request,
119 	.wait	=	amdtp_wait_for_response,
120 	.raw_request  =	amdtp_raw_request,
121 };
122 
amdtp_hid_probe(u32 cur_hid_dev,struct amdtp_cl_data * cli_data)123 int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
124 {
125 	struct hid_device *hid;
126 	struct amdtp_hid_data *hid_data;
127 	int rc;
128 
129 	hid = hid_allocate_device();
130 	if (IS_ERR(hid))
131 		return PTR_ERR(hid);
132 
133 	hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
134 	if (!hid_data) {
135 		rc = -ENOMEM;
136 		goto err_hid_data;
137 	}
138 
139 	hid->ll_driver = &amdtp_hid_ll_driver;
140 	hid_data->index = cur_hid_dev;
141 	hid_data->cli_data = cli_data;
142 	init_waitqueue_head(&hid_data->hid_wait);
143 
144 	hid->driver_data = hid_data;
145 	cli_data->hid_sensor_hubs[cur_hid_dev] = hid;
146 	hid->bus = BUS_AMD_SFH;
147 	hid->vendor = AMD_SFH_HID_VENDOR;
148 	hid->product = AMD_SFH_HID_PRODUCT;
149 	snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-amdsfh",
150 		 hid->vendor, hid->product);
151 
152 	rc = hid_add_device(hid);
153 	if (rc)
154 		goto err_hid_device;
155 	return 0;
156 
157 err_hid_device:
158 	kfree(hid_data);
159 err_hid_data:
160 	hid_destroy_device(hid);
161 	return rc;
162 }
163 
amdtp_hid_remove(struct amdtp_cl_data * cli_data)164 void amdtp_hid_remove(struct amdtp_cl_data *cli_data)
165 {
166 	int i;
167 
168 	for (i = 0; i < cli_data->num_hid_devices; ++i) {
169 		if (cli_data->hid_sensor_hubs[i]) {
170 			kfree(cli_data->hid_sensor_hubs[i]->driver_data);
171 			hid_destroy_device(cli_data->hid_sensor_hubs[i]);
172 			cli_data->hid_sensor_hubs[i] = NULL;
173 		}
174 	}
175 }
176