2 * HID class driver for the Greybus.
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/bitops.h>
11 #include <linux/hid.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
15 #include <linux/slab.h>
19 /* Greybus HID device's structure */
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 static DEFINE_MUTEX(gb_hid_open_mutex);
36 /* Routines to get controller's information over greybus */
38 /* Operations performed on greybus */
39 static int gb_hid_get_desc(struct gb_hid *ghid)
41 return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
42 0, &ghid->hdesc, sizeof(ghid->hdesc));
45 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
47 return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
49 le16_to_cpu(ghid->hdesc.wReportDescLength));
52 static int gb_hid_set_power(struct gb_hid *ghid, int type)
54 return gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
57 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
58 unsigned char *buf, int len)
60 struct gb_hid_get_report_request request;
62 request.report_type = report_type;
63 request.report_id = report_id;
65 return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
66 &request, sizeof(request), buf, len);
69 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
70 unsigned char *buf, int len)
72 struct gb_hid_set_report_request *request;
73 struct gb_operation *operation;
74 int ret, size = sizeof(*request) + len - 1;
76 operation = gb_operation_create(ghid->connection,
77 GB_HID_TYPE_SET_REPORT, size, 0,
82 request = operation->request->payload;
83 request->report_type = report_type;
84 request->report_id = report_id;
85 memcpy(request->report, buf, len);
87 ret = gb_operation_request_send_sync(operation);
89 dev_err(&operation->connection->bundle->dev,
90 "failed to set report: %d\n", ret);
95 gb_operation_put(operation);
100 static int gb_hid_request_handler(struct gb_operation *op)
102 struct gb_connection *connection = op->connection;
103 struct gb_hid *ghid = connection->private;
104 struct gb_hid_input_report_request *request = op->request->payload;
106 if (op->type != GB_HID_TYPE_IRQ_EVENT) {
107 dev_err(&connection->bundle->dev,
108 "unsupported unsolicited request\n");
112 if (test_bit(GB_HID_STARTED, &ghid->flags))
113 hid_input_report(ghid->hid, HID_INPUT_REPORT,
114 request->report, op->request->payload_size, 1);
120 static int gb_hid_report_len(struct hid_report *report)
122 return ((report->size - 1) >> 3) + 1 +
123 report->device->report_enum[report->type].numbered;
126 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
129 struct hid_report *report;
132 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
133 size = gb_hid_report_len(report);
139 static void gb_hid_free_buffers(struct gb_hid *ghid)
146 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
148 ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
152 ghid->bufsize = bufsize;
157 /* Routines dealing with reports */
158 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
162 size = gb_hid_report_len(report);
163 if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
168 * hid->driver_lock is held as we are in probe function,
169 * we just need to setup the input fields, so using
170 * hid_report_raw_event is safe.
172 hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
175 static void gb_hid_init_reports(struct gb_hid *ghid)
177 struct hid_device *hid = ghid->hid;
178 struct hid_report *report;
180 list_for_each_entry(report,
181 &hid->report_enum[HID_INPUT_REPORT].report_list, list)
182 gb_hid_init_report(ghid, report);
184 list_for_each_entry(report,
185 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
186 gb_hid_init_report(ghid, report);
189 static int __gb_hid_get_raw_report(struct hid_device *hid,
190 unsigned char report_number, __u8 *buf, size_t count,
191 unsigned char report_type)
193 struct gb_hid *ghid = hid->driver_data;
196 if (report_type == HID_OUTPUT_REPORT)
199 ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
206 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
207 size_t len, unsigned char report_type)
209 struct gb_hid *ghid = hid->driver_data;
210 int report_id = buf[0];
213 if (report_type == HID_INPUT_REPORT)
221 ret = gb_hid_set_report(ghid, report_type, report_id, buf, len);
222 if (report_id && ret >= 0)
223 ret++; /* add report_id to the number of transfered bytes */
228 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
229 __u8 *buf, size_t len, unsigned char rtype,
233 case HID_REQ_GET_REPORT:
234 return __gb_hid_get_raw_report(hid, reportnum, buf, len, rtype);
235 case HID_REQ_SET_REPORT:
236 if (buf[0] != reportnum)
238 return __gb_hid_output_raw_report(hid, buf, len, rtype);
244 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)
245 static int gb_hid_get_raw_report(struct hid_device *hid,
246 unsigned char reportnum, __u8 *buf,
247 size_t len, unsigned char rtype)
249 return gb_hid_raw_request(hid, reportnum, buf, len, rtype,
253 static int gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
254 size_t len, unsigned char rtype)
256 return gb_hid_raw_request(hid, buf[0], buf, len, rtype,
262 static int gb_hid_parse(struct hid_device *hid)
264 struct gb_hid *ghid = hid->driver_data;
269 rsize = le16_to_cpu(ghid->hdesc.wReportDescLength);
270 if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
271 dbg_hid("weird size of report descriptor (%u)\n", rsize);
275 rdesc = kzalloc(rsize, GFP_KERNEL);
277 dbg_hid("couldn't allocate rdesc memory\n");
281 ret = gb_hid_get_report_desc(ghid, rdesc);
283 hid_err(hid, "reading report descriptor failed\n");
287 ret = hid_parse_report(hid, rdesc, rsize);
289 dbg_hid("parsing report descriptor failed\n");
297 static int gb_hid_start(struct hid_device *hid)
299 struct gb_hid *ghid = hid->driver_data;
300 unsigned int bufsize = HID_MIN_BUFFER_SIZE;
303 gb_hid_find_max_report(hid, HID_INPUT_REPORT, &bufsize);
304 gb_hid_find_max_report(hid, HID_OUTPUT_REPORT, &bufsize);
305 gb_hid_find_max_report(hid, HID_FEATURE_REPORT, &bufsize);
307 if (bufsize > HID_MAX_BUFFER_SIZE)
308 bufsize = HID_MAX_BUFFER_SIZE;
310 ret = gb_hid_alloc_buffers(ghid, bufsize);
314 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
315 gb_hid_init_reports(ghid);
320 static void gb_hid_stop(struct hid_device *hid)
322 struct gb_hid *ghid = hid->driver_data;
324 gb_hid_free_buffers(ghid);
327 static int gb_hid_open(struct hid_device *hid)
329 struct gb_hid *ghid = hid->driver_data;
332 mutex_lock(&gb_hid_open_mutex);
334 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
338 set_bit(GB_HID_STARTED, &ghid->flags);
340 mutex_unlock(&gb_hid_open_mutex);
345 static void gb_hid_close(struct hid_device *hid)
347 struct gb_hid *ghid = hid->driver_data;
351 * Protecting hid->open to make sure we don't restart data acquistion
352 * due to a resumption we no longer care about..
354 mutex_lock(&gb_hid_open_mutex);
356 clear_bit(GB_HID_STARTED, &ghid->flags);
358 /* Save some power */
359 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
361 dev_err(&ghid->connection->bundle->dev,
362 "failed to power off (%d)\n", ret);
364 mutex_unlock(&gb_hid_open_mutex);
367 static int gb_hid_power(struct hid_device *hid, int lvl)
369 struct gb_hid *ghid = hid->driver_data;
373 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
375 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
381 /* HID structure to pass callbacks */
382 static struct hid_ll_driver gb_hid_ll_driver = {
383 .parse = gb_hid_parse,
384 .start = gb_hid_start,
387 .close = gb_hid_close,
388 .power = gb_hid_power,
389 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,15,0)
390 .raw_request = gb_hid_raw_request,
394 static int gb_hid_init(struct gb_hid *ghid)
396 struct hid_device *hid = ghid->hid;
399 ret = gb_hid_get_desc(ghid);
403 hid->version = le16_to_cpu(ghid->hdesc.bcdHID);
404 hid->vendor = le16_to_cpu(ghid->hdesc.wVendorID);
405 hid->product = le16_to_cpu(ghid->hdesc.wProductID);
406 hid->country = ghid->hdesc.bCountryCode;
408 hid->driver_data = ghid;
409 hid->ll_driver = &gb_hid_ll_driver;
410 hid->dev.parent = &ghid->connection->bundle->dev;
411 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0)
412 hid->hid_get_raw_report = gb_hid_get_raw_report;
413 hid->hid_output_raw_report = gb_hid_output_raw_report;
415 // hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
417 /* Set HID device's name */
418 snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X",
419 dev_name(&ghid->connection->bundle->dev),
420 hid->vendor, hid->product);
425 static int gb_hid_probe(struct gb_bundle *bundle,
426 const struct greybus_bundle_id *id)
428 struct greybus_descriptor_cport *cport_desc;
429 struct gb_connection *connection;
430 struct hid_device *hid;
434 if (bundle->num_cports != 1)
437 cport_desc = &bundle->cport_desc[0];
438 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
441 ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
445 connection = gb_connection_create(bundle, le16_to_cpu(cport_desc->id),
446 gb_hid_request_handler);
447 if (IS_ERR(connection)) {
448 ret = PTR_ERR(connection);
452 connection->private = ghid;
453 ghid->connection = connection;
455 hid = hid_allocate_device();
458 goto err_connection_destroy;
463 greybus_set_drvdata(bundle, ghid);
465 ret = gb_connection_enable(connection);
467 goto err_destroy_hid;
469 ret = gb_hid_init(ghid);
471 goto err_connection_disable;
473 ret = hid_add_device(hid);
475 hid_err(hid, "can't add hid device: %d\n", ret);
476 goto err_connection_disable;
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 hid_destroy_device(ghid->hid);
498 gb_connection_disable(ghid->connection);
499 gb_connection_destroy(ghid->connection);
503 static const struct greybus_bundle_id gb_hid_id_table[] = {
504 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
507 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
509 static struct greybus_driver gb_hid_driver = {
511 .probe = gb_hid_probe,
512 .disconnect = gb_hid_disconnect,
513 .id_table = gb_hid_id_table,
515 module_greybus_driver(gb_hid_driver);
517 MODULE_LICENSE("GPL v2");