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>
18 /* Greybus HID device's structure */
20 struct gb_bundle *bundle;
21 struct gb_connection *connection;
23 struct hid_device *hid;
24 struct gb_hid_desc_response hdesc;
27 #define GB_HID_STARTED 0x01
28 #define GB_HID_READ_PENDING 0x04
34 /* Routines to get controller's information over greybus */
36 /* Operations performed on greybus */
37 static int gb_hid_get_desc(struct gb_hid *ghid)
39 return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
40 0, &ghid->hdesc, sizeof(ghid->hdesc));
43 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
47 ret = gb_pm_runtime_get_sync(ghid->bundle);
51 ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
53 le16_to_cpu(ghid->hdesc.wReportDescLength));
55 gb_pm_runtime_put_autosuspend(ghid->bundle);
60 static int gb_hid_set_power(struct gb_hid *ghid, int type)
64 ret = gb_pm_runtime_get_sync(ghid->bundle);
68 ret = gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
70 gb_pm_runtime_put_autosuspend(ghid->bundle);
75 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
76 unsigned char *buf, int len)
78 struct gb_hid_get_report_request request;
81 ret = gb_pm_runtime_get_sync(ghid->bundle);
85 request.report_type = report_type;
86 request.report_id = report_id;
88 ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
89 &request, sizeof(request), buf, len);
91 gb_pm_runtime_put_autosuspend(ghid->bundle);
96 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
97 unsigned char *buf, int len)
99 struct gb_hid_set_report_request *request;
100 struct gb_operation *operation;
101 int ret, size = sizeof(*request) + len - 1;
103 ret = gb_pm_runtime_get_sync(ghid->bundle);
107 operation = gb_operation_create(ghid->connection,
108 GB_HID_TYPE_SET_REPORT, size, 0,
111 gb_pm_runtime_put_autosuspend(ghid->bundle);
115 request = operation->request->payload;
116 request->report_type = report_type;
117 request->report_id = report_id;
118 memcpy(request->report, buf, len);
120 ret = gb_operation_request_send_sync(operation);
122 dev_err(&operation->connection->bundle->dev,
123 "failed to set report: %d\n", ret);
128 gb_operation_put(operation);
129 gb_pm_runtime_put_autosuspend(ghid->bundle);
134 static int gb_hid_request_handler(struct gb_operation *op)
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;
140 if (op->type != GB_HID_TYPE_IRQ_EVENT) {
141 dev_err(&connection->bundle->dev,
142 "unsupported unsolicited request\n");
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);
153 static int gb_hid_report_len(struct hid_report *report)
155 return ((report->size - 1) >> 3) + 1 +
156 report->device->report_enum[report->type].numbered;
159 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
162 struct hid_report *report;
165 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
166 size = gb_hid_report_len(report);
172 static void gb_hid_free_buffers(struct gb_hid *ghid)
179 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
181 ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
185 ghid->bufsize = bufsize;
190 /* Routines dealing with reports */
191 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
195 size = gb_hid_report_len(report);
196 if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
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.
205 hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
208 static void gb_hid_init_reports(struct gb_hid *ghid)
210 struct hid_device *hid = ghid->hid;
211 struct hid_report *report;
213 list_for_each_entry(report,
214 &hid->report_enum[HID_INPUT_REPORT].report_list,
216 gb_hid_init_report(ghid, report);
218 list_for_each_entry(report,
219 &hid->report_enum[HID_FEATURE_REPORT].report_list,
221 gb_hid_init_report(ghid, report);
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)
228 struct gb_hid *ghid = hid->driver_data;
231 if (report_type == HID_OUTPUT_REPORT)
234 ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
241 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
242 size_t len, unsigned char report_type)
244 struct gb_hid *ghid = hid->driver_data;
245 int report_id = buf[0];
248 if (report_type == HID_INPUT_REPORT)
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 */
263 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
264 __u8 *buf, size_t len, unsigned char rtype,
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)
273 return __gb_hid_output_raw_report(hid, buf, len, rtype);
280 static int gb_hid_parse(struct hid_device *hid)
282 struct gb_hid *ghid = hid->driver_data;
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);
293 rdesc = kzalloc(rsize, GFP_KERNEL);
298 ret = gb_hid_get_report_desc(ghid, rdesc);
300 hid_err(hid, "reading report descriptor failed\n");
304 ret = hid_parse_report(hid, rdesc, rsize);
306 dbg_hid("parsing report descriptor failed\n");
314 static int gb_hid_start(struct hid_device *hid)
316 struct gb_hid *ghid = hid->driver_data;
317 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
320 gb_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
321 gb_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
322 gb_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
324 if (bufsize > HID_MAX_BUFFER_SIZE)
325 bufsize = HID_MAX_BUFFER_SIZE;
327 ret = gb_hid_alloc_buffers(ghid, bufsize);
331 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
332 gb_hid_init_reports(ghid);
337 static void gb_hid_stop(struct hid_device *hid)
339 struct gb_hid *ghid = hid->driver_data;
341 gb_hid_free_buffers(ghid);
344 static int gb_hid_open(struct hid_device *hid)
346 struct gb_hid *ghid = hid->driver_data;
349 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
353 set_bit(GB_HID_STARTED, &ghid->flags);
357 static void gb_hid_close(struct hid_device *hid)
359 struct gb_hid *ghid = hid->driver_data;
362 clear_bit(GB_HID_STARTED, &ghid->flags);
364 /* Save some power */
365 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
367 dev_err(&ghid->connection->bundle->dev,
368 "failed to power off (%d)\n", ret);
371 static int gb_hid_power(struct hid_device *hid, int lvl)
373 struct gb_hid *ghid = hid->driver_data;
377 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
379 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
385 /* HID structure to pass callbacks */
386 static struct hid_ll_driver gb_hid_ll_driver = {
387 .parse = gb_hid_parse,
388 .start = gb_hid_start,
391 .close = gb_hid_close,
392 .power = gb_hid_power,
393 .raw_request = gb_hid_raw_request,
396 static int gb_hid_init(struct gb_hid *ghid)
398 struct hid_device *hid = ghid->hid;
401 ret = gb_hid_get_desc(ghid);
405 hid->version = le16_to_cpu(ghid->hdesc.bcdHID);
406 hid->vendor = le16_to_cpu(ghid->hdesc.wVendorID);
407 hid->product = le16_to_cpu(ghid->hdesc.wProductID);
408 hid->country = ghid->hdesc.bCountryCode;
410 hid->driver_data = ghid;
411 hid->ll_driver = &gb_hid_ll_driver;
412 hid->dev.parent = &ghid->connection->bundle->dev;
413 // hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
415 /* Set HID device's name */
416 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
417 dev_name(&ghid->connection->bundle->dev),
418 hid->vendor, hid->product);
423 static int gb_hid_probe(struct gb_bundle *bundle,
424 const struct greybus_bundle_id *id)
426 struct greybus_descriptor_cport *cport_desc;
427 struct gb_connection *connection;
428 struct hid_device *hid;
432 if (bundle->num_cports != 1)
435 cport_desc = &bundle->cport_desc[0];
436 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
439 ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
443 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
444 gb_hid_request_handler);
445 if (IS_ERR(connection)) {
446 ret = PTR_ERR(connection);
450 gb_connection_set_data(connection, ghid);
451 ghid->connection = connection;
453 hid = hid_allocate_device();
456 goto err_connection_destroy;
460 ghid->bundle = bundle;
462 greybus_set_drvdata(bundle, ghid);
464 ret = gb_connection_enable(connection);
466 goto err_destroy_hid;
468 ret = gb_hid_init(ghid);
470 goto err_connection_disable;
472 ret = hid_add_device(hid);
474 hid_err(hid, "can't add hid device: %d\n", ret);
475 goto err_connection_disable;
478 gb_pm_runtime_put_autosuspend(bundle);
482 err_connection_disable:
483 gb_connection_disable(connection);
485 hid_destroy_device(hid);
486 err_connection_destroy:
487 gb_connection_destroy(connection);
494 static void gb_hid_disconnect(struct gb_bundle *bundle)
496 struct gb_hid *ghid = greybus_get_drvdata(bundle);
498 if (gb_pm_runtime_get_sync(bundle))
499 gb_pm_runtime_get_noresume(bundle);
501 hid_destroy_device(ghid->hid);
502 gb_connection_disable(ghid->connection);
503 gb_connection_destroy(ghid->connection);
507 static const struct greybus_bundle_id gb_hid_id_table[] = {
508 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
511 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
513 static struct greybus_driver gb_hid_driver = {
515 .probe = gb_hid_probe,
516 .disconnect = gb_hid_disconnect,
517 .id_table = gb_hid_id_table,
519 module_greybus_driver(gb_hid_driver);
521 MODULE_LICENSE("GPL v2");