1 // SPDX-License-Identifier: GPL-2.0
3 * HID class driver for the Greybus.
5 * Copyright 2014 Google Inc.
6 * Copyright 2014 Linaro Ltd.
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 #include <linux/greybus.h>
17 /* Greybus HID device's structure */
19 struct gb_bundle *bundle;
20 struct gb_connection *connection;
22 struct hid_device *hid;
23 struct gb_hid_desc_response hdesc;
26 #define GB_HID_STARTED 0x01
27 #define GB_HID_READ_PENDING 0x04
33 /* Routines to get controller's information over greybus */
35 /* Operations performed on greybus */
36 static int gb_hid_get_desc(struct gb_hid *ghid)
38 return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
39 0, &ghid->hdesc, sizeof(ghid->hdesc));
42 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
46 ret = gb_pm_runtime_get_sync(ghid->bundle);
50 ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
52 le16_to_cpu(ghid->hdesc.wReportDescLength));
54 gb_pm_runtime_put_autosuspend(ghid->bundle);
59 static int gb_hid_set_power(struct gb_hid *ghid, int type)
63 ret = gb_pm_runtime_get_sync(ghid->bundle);
67 ret = gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
69 gb_pm_runtime_put_autosuspend(ghid->bundle);
74 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
75 unsigned char *buf, int len)
77 struct gb_hid_get_report_request request;
80 ret = gb_pm_runtime_get_sync(ghid->bundle);
84 request.report_type = report_type;
85 request.report_id = report_id;
87 ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
88 &request, sizeof(request), buf, len);
90 gb_pm_runtime_put_autosuspend(ghid->bundle);
95 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
96 unsigned char *buf, int len)
98 struct gb_hid_set_report_request *request;
99 struct gb_operation *operation;
100 int ret, size = sizeof(*request) + len - 1;
102 ret = gb_pm_runtime_get_sync(ghid->bundle);
106 operation = gb_operation_create(ghid->connection,
107 GB_HID_TYPE_SET_REPORT, size, 0,
110 gb_pm_runtime_put_autosuspend(ghid->bundle);
114 request = operation->request->payload;
115 request->report_type = report_type;
116 request->report_id = report_id;
117 memcpy(request->report, buf, len);
119 ret = gb_operation_request_send_sync(operation);
121 dev_err(&operation->connection->bundle->dev,
122 "failed to set report: %d\n", ret);
127 gb_operation_put(operation);
128 gb_pm_runtime_put_autosuspend(ghid->bundle);
133 static int gb_hid_request_handler(struct gb_operation *op)
135 struct gb_connection *connection = op->connection;
136 struct gb_hid *ghid = gb_connection_get_data(connection);
137 struct gb_hid_input_report_request *request = op->request->payload;
139 if (op->type != GB_HID_TYPE_IRQ_EVENT) {
140 dev_err(&connection->bundle->dev,
141 "unsupported unsolicited request\n");
145 if (test_bit(GB_HID_STARTED, &ghid->flags))
146 hid_input_report(ghid->hid, HID_INPUT_REPORT,
147 request->report, op->request->payload_size, 1);
152 static int gb_hid_report_len(struct hid_report *report)
154 return ((report->size - 1) >> 3) + 1 +
155 report->device->report_enum[report->type].numbered;
158 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
161 struct hid_report *report;
164 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
165 size = gb_hid_report_len(report);
171 static void gb_hid_free_buffers(struct gb_hid *ghid)
178 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
180 ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
184 ghid->bufsize = bufsize;
189 /* Routines dealing with reports */
190 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
194 size = gb_hid_report_len(report);
195 if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
200 * hid->driver_lock is held as we are in probe function,
201 * we just need to setup the input fields, so using
202 * hid_report_raw_event is safe.
204 hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
207 static void gb_hid_init_reports(struct gb_hid *ghid)
209 struct hid_device *hid = ghid->hid;
210 struct hid_report *report;
212 list_for_each_entry(report,
213 &hid->report_enum[HID_INPUT_REPORT].report_list,
215 gb_hid_init_report(ghid, report);
217 list_for_each_entry(report,
218 &hid->report_enum[HID_FEATURE_REPORT].report_list,
220 gb_hid_init_report(ghid, report);
223 static int __gb_hid_get_raw_report(struct hid_device *hid,
224 unsigned char report_number, __u8 *buf, size_t count,
225 unsigned char report_type)
227 struct gb_hid *ghid = hid->driver_data;
230 if (report_type == HID_OUTPUT_REPORT)
233 ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
240 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
241 size_t len, unsigned char report_type)
243 struct gb_hid *ghid = hid->driver_data;
244 int report_id = buf[0];
247 if (report_type == HID_INPUT_REPORT)
255 ret = gb_hid_set_report(ghid, report_type, report_id, buf, len);
256 if (report_id && ret >= 0)
257 ret++; /* add report_id to the number of transfered bytes */
262 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
263 __u8 *buf, size_t len, unsigned char rtype,
267 case HID_REQ_GET_REPORT:
268 return __gb_hid_get_raw_report(hid, reportnum, buf, len, rtype);
269 case HID_REQ_SET_REPORT:
270 if (buf[0] != reportnum)
272 return __gb_hid_output_raw_report(hid, buf, len, rtype);
279 static int gb_hid_parse(struct hid_device *hid)
281 struct gb_hid *ghid = hid->driver_data;
286 rsize = le16_to_cpu(ghid->hdesc.wReportDescLength);
287 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
288 dbg_hid("weird size of report descriptor (%u)\n", rsize);
292 rdesc = kzalloc(rsize, GFP_KERNEL);
297 ret = gb_hid_get_report_desc(ghid, rdesc);
299 hid_err(hid, "reading report descriptor failed\n");
303 ret = hid_parse_report(hid, rdesc, rsize);
305 dbg_hid("parsing report descriptor failed\n");
313 static int gb_hid_start(struct hid_device *hid)
315 struct gb_hid *ghid = hid->driver_data;
316 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
319 gb_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
320 gb_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
321 gb_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
323 if (bufsize > HID_MAX_BUFFER_SIZE)
324 bufsize = HID_MAX_BUFFER_SIZE;
326 ret = gb_hid_alloc_buffers(ghid, bufsize);
330 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
331 gb_hid_init_reports(ghid);
336 static void gb_hid_stop(struct hid_device *hid)
338 struct gb_hid *ghid = hid->driver_data;
340 gb_hid_free_buffers(ghid);
343 static int gb_hid_open(struct hid_device *hid)
345 struct gb_hid *ghid = hid->driver_data;
348 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
352 set_bit(GB_HID_STARTED, &ghid->flags);
356 static void gb_hid_close(struct hid_device *hid)
358 struct gb_hid *ghid = hid->driver_data;
361 clear_bit(GB_HID_STARTED, &ghid->flags);
363 /* Save some power */
364 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
366 dev_err(&ghid->connection->bundle->dev,
367 "failed to power off (%d)\n", ret);
370 static int gb_hid_power(struct hid_device *hid, int lvl)
372 struct gb_hid *ghid = hid->driver_data;
376 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
378 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
384 /* HID structure to pass callbacks */
385 static struct hid_ll_driver gb_hid_ll_driver = {
386 .parse = gb_hid_parse,
387 .start = gb_hid_start,
390 .close = gb_hid_close,
391 .power = gb_hid_power,
392 .raw_request = gb_hid_raw_request,
395 static int gb_hid_init(struct gb_hid *ghid)
397 struct hid_device *hid = ghid->hid;
400 ret = gb_hid_get_desc(ghid);
404 hid->version = le16_to_cpu(ghid->hdesc.bcdHID);
405 hid->vendor = le16_to_cpu(ghid->hdesc.wVendorID);
406 hid->product = le16_to_cpu(ghid->hdesc.wProductID);
407 hid->country = ghid->hdesc.bCountryCode;
409 hid->driver_data = ghid;
410 hid->ll_driver = &gb_hid_ll_driver;
411 hid->dev.parent = &ghid->connection->bundle->dev;
412 // hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
414 /* Set HID device's name */
415 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
416 dev_name(&ghid->connection->bundle->dev),
417 hid->vendor, hid->product);
422 static int gb_hid_probe(struct gb_bundle *bundle,
423 const struct greybus_bundle_id *id)
425 struct greybus_descriptor_cport *cport_desc;
426 struct gb_connection *connection;
427 struct hid_device *hid;
431 if (bundle->num_cports != 1)
434 cport_desc = &bundle->cport_desc[0];
435 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
438 ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
442 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
443 gb_hid_request_handler);
444 if (IS_ERR(connection)) {
445 ret = PTR_ERR(connection);
449 gb_connection_set_data(connection, ghid);
450 ghid->connection = connection;
452 hid = hid_allocate_device();
455 goto err_connection_destroy;
459 ghid->bundle = bundle;
461 greybus_set_drvdata(bundle, ghid);
463 ret = gb_connection_enable(connection);
465 goto err_destroy_hid;
467 ret = gb_hid_init(ghid);
469 goto err_connection_disable;
471 ret = hid_add_device(hid);
473 hid_err(hid, "can't add hid device: %d\n", ret);
474 goto err_connection_disable;
477 gb_pm_runtime_put_autosuspend(bundle);
481 err_connection_disable:
482 gb_connection_disable(connection);
484 hid_destroy_device(hid);
485 err_connection_destroy:
486 gb_connection_destroy(connection);
493 static void gb_hid_disconnect(struct gb_bundle *bundle)
495 struct gb_hid *ghid = greybus_get_drvdata(bundle);
497 if (gb_pm_runtime_get_sync(bundle))
498 gb_pm_runtime_get_noresume(bundle);
500 hid_destroy_device(ghid->hid);
501 gb_connection_disable(ghid->connection);
502 gb_connection_destroy(ghid->connection);
506 static const struct greybus_bundle_id gb_hid_id_table[] = {
507 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
510 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
512 static struct greybus_driver gb_hid_driver = {
514 .probe = gb_hid_probe,
515 .disconnect = gb_hid_disconnect,
516 .id_table = gb_hid_id_table,
518 module_greybus_driver(gb_hid_driver);
520 MODULE_LICENSE("GPL v2");