xref: /openbmc/linux/drivers/staging/greybus/hid.c (revision 3213486f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * HID class driver for the Greybus.
4  *
5  * Copyright 2014 Google Inc.
6  * Copyright 2014 Linaro Ltd.
7  */
8 
9 #include <linux/bitops.h>
10 #include <linux/hid.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 
16 #include "greybus.h"
17 
18 /* Greybus HID device's structure */
19 struct gb_hid {
20 	struct gb_bundle *bundle;
21 	struct gb_connection		*connection;
22 
23 	struct hid_device		*hid;
24 	struct gb_hid_desc_response	hdesc;
25 
26 	unsigned long			flags;
27 #define GB_HID_STARTED			0x01
28 #define GB_HID_READ_PENDING		0x04
29 
30 	unsigned int			bufsize;
31 	char				*inbuf;
32 };
33 
34 /* Routines to get controller's information over greybus */
35 
36 /* Operations performed on greybus */
37 static int gb_hid_get_desc(struct gb_hid *ghid)
38 {
39 	return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
40 				 0, &ghid->hdesc, sizeof(ghid->hdesc));
41 }
42 
43 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
44 {
45 	int ret;
46 
47 	ret = gb_pm_runtime_get_sync(ghid->bundle);
48 	if (ret)
49 		return ret;
50 
51 	ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
52 				NULL, 0, rdesc,
53 				le16_to_cpu(ghid->hdesc.wReportDescLength));
54 
55 	gb_pm_runtime_put_autosuspend(ghid->bundle);
56 
57 	return ret;
58 }
59 
60 static int gb_hid_set_power(struct gb_hid *ghid, int type)
61 {
62 	int ret;
63 
64 	ret = gb_pm_runtime_get_sync(ghid->bundle);
65 	if (ret)
66 		return ret;
67 
68 	ret = gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
69 
70 	gb_pm_runtime_put_autosuspend(ghid->bundle);
71 
72 	return ret;
73 }
74 
75 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
76 			     unsigned char *buf, int len)
77 {
78 	struct gb_hid_get_report_request request;
79 	int ret;
80 
81 	ret = gb_pm_runtime_get_sync(ghid->bundle);
82 	if (ret)
83 		return ret;
84 
85 	request.report_type = report_type;
86 	request.report_id = report_id;
87 
88 	ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
89 				&request, sizeof(request), buf, len);
90 
91 	gb_pm_runtime_put_autosuspend(ghid->bundle);
92 
93 	return ret;
94 }
95 
96 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
97 			     unsigned char *buf, int len)
98 {
99 	struct gb_hid_set_report_request *request;
100 	struct gb_operation *operation;
101 	int ret, size = sizeof(*request) + len - 1;
102 
103 	ret = gb_pm_runtime_get_sync(ghid->bundle);
104 	if (ret)
105 		return ret;
106 
107 	operation = gb_operation_create(ghid->connection,
108 					GB_HID_TYPE_SET_REPORT, size, 0,
109 					GFP_KERNEL);
110 	if (!operation) {
111 		gb_pm_runtime_put_autosuspend(ghid->bundle);
112 		return -ENOMEM;
113 	}
114 
115 	request = operation->request->payload;
116 	request->report_type = report_type;
117 	request->report_id = report_id;
118 	memcpy(request->report, buf, len);
119 
120 	ret = gb_operation_request_send_sync(operation);
121 	if (ret) {
122 		dev_err(&operation->connection->bundle->dev,
123 			"failed to set report: %d\n", ret);
124 	} else {
125 		ret = len;
126 	}
127 
128 	gb_operation_put(operation);
129 	gb_pm_runtime_put_autosuspend(ghid->bundle);
130 
131 	return ret;
132 }
133 
134 static int gb_hid_request_handler(struct gb_operation *op)
135 {
136 	struct gb_connection *connection = op->connection;
137 	struct gb_hid *ghid = gb_connection_get_data(connection);
138 	struct gb_hid_input_report_request *request = op->request->payload;
139 
140 	if (op->type != GB_HID_TYPE_IRQ_EVENT) {
141 		dev_err(&connection->bundle->dev,
142 			"unsupported unsolicited request\n");
143 		return -EINVAL;
144 	}
145 
146 	if (test_bit(GB_HID_STARTED, &ghid->flags))
147 		hid_input_report(ghid->hid, HID_INPUT_REPORT,
148 				 request->report, op->request->payload_size, 1);
149 
150 	return 0;
151 }
152 
153 static int gb_hid_report_len(struct hid_report *report)
154 {
155 	return ((report->size - 1) >> 3) + 1 +
156 		report->device->report_enum[report->type].numbered;
157 }
158 
159 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
160 				   unsigned int *max)
161 {
162 	struct hid_report *report;
163 	unsigned int size;
164 
165 	list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
166 		size = gb_hid_report_len(report);
167 		if (*max < size)
168 			*max = size;
169 	}
170 }
171 
172 static void gb_hid_free_buffers(struct gb_hid *ghid)
173 {
174 	kfree(ghid->inbuf);
175 	ghid->inbuf = NULL;
176 	ghid->bufsize = 0;
177 }
178 
179 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
180 {
181 	ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
182 	if (!ghid->inbuf)
183 		return -ENOMEM;
184 
185 	ghid->bufsize = bufsize;
186 
187 	return 0;
188 }
189 
190 /* Routines dealing with reports */
191 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
192 {
193 	unsigned int size;
194 
195 	size = gb_hid_report_len(report);
196 	if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
197 			      size))
198 		return;
199 
200 	/*
201 	 * hid->driver_lock is held as we are in probe function,
202 	 * we just need to setup the input fields, so using
203 	 * hid_report_raw_event is safe.
204 	 */
205 	hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
206 }
207 
208 static void gb_hid_init_reports(struct gb_hid *ghid)
209 {
210 	struct hid_device *hid = ghid->hid;
211 	struct hid_report *report;
212 
213 	list_for_each_entry(report,
214 			    &hid->report_enum[HID_INPUT_REPORT].report_list,
215 			    list)
216 		gb_hid_init_report(ghid, report);
217 
218 	list_for_each_entry(report,
219 			    &hid->report_enum[HID_FEATURE_REPORT].report_list,
220 			    list)
221 		gb_hid_init_report(ghid, report);
222 }
223 
224 static int __gb_hid_get_raw_report(struct hid_device *hid,
225 		unsigned char report_number, __u8 *buf, size_t count,
226 		unsigned char report_type)
227 {
228 	struct gb_hid *ghid = hid->driver_data;
229 	int ret;
230 
231 	if (report_type == HID_OUTPUT_REPORT)
232 		return -EINVAL;
233 
234 	ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
235 	if (!ret)
236 		ret = count;
237 
238 	return ret;
239 }
240 
241 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
242 				      size_t len, unsigned char report_type)
243 {
244 	struct gb_hid *ghid = hid->driver_data;
245 	int report_id = buf[0];
246 	int ret;
247 
248 	if (report_type == HID_INPUT_REPORT)
249 		return -EINVAL;
250 
251 	if (report_id) {
252 		buf++;
253 		len--;
254 	}
255 
256 	ret = gb_hid_set_report(ghid, report_type, report_id, buf, len);
257 	if (report_id && ret >= 0)
258 		ret++; /* add report_id to the number of transfered bytes */
259 
260 	return 0;
261 }
262 
263 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
264 			      __u8 *buf, size_t len, unsigned char rtype,
265 			      int reqtype)
266 {
267 	switch (reqtype) {
268 	case HID_REQ_GET_REPORT:
269 		return __gb_hid_get_raw_report(hid, reportnum, buf, len, rtype);
270 	case HID_REQ_SET_REPORT:
271 		if (buf[0] != reportnum)
272 			return -EINVAL;
273 		return __gb_hid_output_raw_report(hid, buf, len, rtype);
274 	default:
275 		return -EIO;
276 	}
277 }
278 
279 /* HID Callbacks */
280 static int gb_hid_parse(struct hid_device *hid)
281 {
282 	struct gb_hid *ghid = hid->driver_data;
283 	unsigned int rsize;
284 	char *rdesc;
285 	int ret;
286 
287 	rsize = le16_to_cpu(ghid->hdesc.wReportDescLength);
288 	if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
289 		dbg_hid("weird size of report descriptor (%u)\n", rsize);
290 		return -EINVAL;
291 	}
292 
293 	rdesc = kzalloc(rsize, GFP_KERNEL);
294 	if (!rdesc) {
295 		dbg_hid("couldn't allocate rdesc memory\n");
296 		return -ENOMEM;
297 	}
298 
299 	ret = gb_hid_get_report_desc(ghid, rdesc);
300 	if (ret) {
301 		hid_err(hid, "reading report descriptor failed\n");
302 		goto free_rdesc;
303 	}
304 
305 	ret = hid_parse_report(hid, rdesc, rsize);
306 	if (ret)
307 		dbg_hid("parsing report descriptor failed\n");
308 
309 free_rdesc:
310 	kfree(rdesc);
311 
312 	return ret;
313 }
314 
315 static int gb_hid_start(struct hid_device *hid)
316 {
317 	struct gb_hid *ghid = hid->driver_data;
318 	unsigned int bufsize = HID_MIN_BUFFER_SIZE;
319 	int ret;
320 
321 	gb_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
322 	gb_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
323 	gb_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
324 
325 	if (bufsize > HID_MAX_BUFFER_SIZE)
326 		bufsize = HID_MAX_BUFFER_SIZE;
327 
328 	ret = gb_hid_alloc_buffers(ghid, bufsize);
329 	if (ret)
330 		return ret;
331 
332 	if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
333 		gb_hid_init_reports(ghid);
334 
335 	return 0;
336 }
337 
338 static void gb_hid_stop(struct hid_device *hid)
339 {
340 	struct gb_hid *ghid = hid->driver_data;
341 
342 	gb_hid_free_buffers(ghid);
343 }
344 
345 static int gb_hid_open(struct hid_device *hid)
346 {
347 	struct gb_hid *ghid = hid->driver_data;
348 	int ret;
349 
350 	ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
351 	if (ret < 0)
352 		return ret;
353 
354 	set_bit(GB_HID_STARTED, &ghid->flags);
355 	return 0;
356 }
357 
358 static void gb_hid_close(struct hid_device *hid)
359 {
360 	struct gb_hid *ghid = hid->driver_data;
361 	int ret;
362 
363 	clear_bit(GB_HID_STARTED, &ghid->flags);
364 
365 	/* Save some power */
366 	ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
367 	if (ret)
368 		dev_err(&ghid->connection->bundle->dev,
369 			"failed to power off (%d)\n", ret);
370 }
371 
372 static int gb_hid_power(struct hid_device *hid, int lvl)
373 {
374 	struct gb_hid *ghid = hid->driver_data;
375 
376 	switch (lvl) {
377 	case PM_HINT_FULLON:
378 		return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
379 	case PM_HINT_NORMAL:
380 		return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
381 	}
382 
383 	return 0;
384 }
385 
386 /* HID structure to pass callbacks */
387 static struct hid_ll_driver gb_hid_ll_driver = {
388 	.parse = gb_hid_parse,
389 	.start = gb_hid_start,
390 	.stop = gb_hid_stop,
391 	.open = gb_hid_open,
392 	.close = gb_hid_close,
393 	.power = gb_hid_power,
394 	.raw_request = gb_hid_raw_request,
395 };
396 
397 static int gb_hid_init(struct gb_hid *ghid)
398 {
399 	struct hid_device *hid = ghid->hid;
400 	int ret;
401 
402 	ret = gb_hid_get_desc(ghid);
403 	if (ret)
404 		return ret;
405 
406 	hid->version = le16_to_cpu(ghid->hdesc.bcdHID);
407 	hid->vendor = le16_to_cpu(ghid->hdesc.wVendorID);
408 	hid->product = le16_to_cpu(ghid->hdesc.wProductID);
409 	hid->country = ghid->hdesc.bCountryCode;
410 
411 	hid->driver_data = ghid;
412 	hid->ll_driver = &gb_hid_ll_driver;
413 	hid->dev.parent = &ghid->connection->bundle->dev;
414 //	hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
415 
416 	/* Set HID device's name */
417 	snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
418 		 dev_name(&ghid->connection->bundle->dev),
419 		 hid->vendor, hid->product);
420 
421 	return 0;
422 }
423 
424 static int gb_hid_probe(struct gb_bundle *bundle,
425 			const struct greybus_bundle_id *id)
426 {
427 	struct greybus_descriptor_cport *cport_desc;
428 	struct gb_connection *connection;
429 	struct hid_device *hid;
430 	struct gb_hid *ghid;
431 	int ret;
432 
433 	if (bundle->num_cports != 1)
434 		return -ENODEV;
435 
436 	cport_desc = &bundle->cport_desc[0];
437 	if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
438 		return -ENODEV;
439 
440 	ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
441 	if (!ghid)
442 		return -ENOMEM;
443 
444 	connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
445 					  gb_hid_request_handler);
446 	if (IS_ERR(connection)) {
447 		ret = PTR_ERR(connection);
448 		goto err_free_ghid;
449 	}
450 
451 	gb_connection_set_data(connection, ghid);
452 	ghid->connection = connection;
453 
454 	hid = hid_allocate_device();
455 	if (IS_ERR(hid)) {
456 		ret = PTR_ERR(hid);
457 		goto err_connection_destroy;
458 	}
459 
460 	ghid->hid = hid;
461 	ghid->bundle = bundle;
462 
463 	greybus_set_drvdata(bundle, ghid);
464 
465 	ret = gb_connection_enable(connection);
466 	if (ret)
467 		goto err_destroy_hid;
468 
469 	ret = gb_hid_init(ghid);
470 	if (ret)
471 		goto err_connection_disable;
472 
473 	ret = hid_add_device(hid);
474 	if (ret) {
475 		hid_err(hid, "can't add hid device: %d\n", ret);
476 		goto err_connection_disable;
477 	}
478 
479 	gb_pm_runtime_put_autosuspend(bundle);
480 
481 	return 0;
482 
483 err_connection_disable:
484 	gb_connection_disable(connection);
485 err_destroy_hid:
486 	hid_destroy_device(hid);
487 err_connection_destroy:
488 	gb_connection_destroy(connection);
489 err_free_ghid:
490 	kfree(ghid);
491 
492 	return ret;
493 }
494 
495 static void gb_hid_disconnect(struct gb_bundle *bundle)
496 {
497 	struct gb_hid *ghid = greybus_get_drvdata(bundle);
498 
499 	if (gb_pm_runtime_get_sync(bundle))
500 		gb_pm_runtime_get_noresume(bundle);
501 
502 	hid_destroy_device(ghid->hid);
503 	gb_connection_disable(ghid->connection);
504 	gb_connection_destroy(ghid->connection);
505 	kfree(ghid);
506 }
507 
508 static const struct greybus_bundle_id gb_hid_id_table[] = {
509 	{ GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
510 	{ }
511 };
512 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
513 
514 static struct greybus_driver gb_hid_driver = {
515 	.name		= "hid",
516 	.probe		= gb_hid_probe,
517 	.disconnect	= gb_hid_disconnect,
518 	.id_table	= gb_hid_id_table,
519 };
520 module_greybus_driver(gb_hid_driver);
521 
522 MODULE_LICENSE("GPL v2");
523