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_bundle
*bundle
;
22 struct gb_connection
*connection
;
24 struct hid_device
*hid
;
25 struct gb_hid_desc_response hdesc
;
28 #define GB_HID_STARTED 0x01
29 #define GB_HID_READ_PENDING 0x04
35 static DEFINE_MUTEX(gb_hid_open_mutex
);
37 /* Routines to get controller's information over greybus */
39 /* Operations performed on greybus */
40 static int gb_hid_get_desc(struct gb_hid
*ghid
)
42 return gb_operation_sync(ghid
->connection
, GB_HID_TYPE_GET_DESC
, NULL
,
43 0, &ghid
->hdesc
, sizeof(ghid
->hdesc
));
46 static int gb_hid_get_report_desc(struct gb_hid
*ghid
, char *rdesc
)
50 ret
= gb_pm_runtime_get_sync(ghid
->bundle
);
54 ret
= gb_operation_sync(ghid
->connection
, GB_HID_TYPE_GET_REPORT_DESC
,
56 le16_to_cpu(ghid
->hdesc
.wReportDescLength
));
58 gb_pm_runtime_put_autosuspend(ghid
->bundle
);
63 static int gb_hid_set_power(struct gb_hid
*ghid
, int type
)
67 ret
= gb_pm_runtime_get_sync(ghid
->bundle
);
71 ret
= gb_operation_sync(ghid
->connection
, type
, NULL
, 0, NULL
, 0);
73 gb_pm_runtime_put_autosuspend(ghid
->bundle
);
78 static int gb_hid_get_report(struct gb_hid
*ghid
, u8 report_type
, u8 report_id
,
79 unsigned char *buf
, int len
)
81 struct gb_hid_get_report_request request
;
84 ret
= gb_pm_runtime_get_sync(ghid
->bundle
);
88 request
.report_type
= report_type
;
89 request
.report_id
= report_id
;
91 ret
= gb_operation_sync(ghid
->connection
, GB_HID_TYPE_GET_REPORT
,
92 &request
, sizeof(request
), buf
, len
);
94 gb_pm_runtime_put_autosuspend(ghid
->bundle
);
99 static int gb_hid_set_report(struct gb_hid
*ghid
, u8 report_type
, u8 report_id
,
100 unsigned char *buf
, int len
)
102 struct gb_hid_set_report_request
*request
;
103 struct gb_operation
*operation
;
104 int ret
, size
= sizeof(*request
) + len
- 1;
106 ret
= gb_pm_runtime_get_sync(ghid
->bundle
);
110 operation
= gb_operation_create(ghid
->connection
,
111 GB_HID_TYPE_SET_REPORT
, size
, 0,
114 gb_pm_runtime_put_autosuspend(ghid
->bundle
);
118 request
= operation
->request
->payload
;
119 request
->report_type
= report_type
;
120 request
->report_id
= report_id
;
121 memcpy(request
->report
, buf
, len
);
123 ret
= gb_operation_request_send_sync(operation
);
125 dev_err(&operation
->connection
->bundle
->dev
,
126 "failed to set report: %d\n", ret
);
131 gb_operation_put(operation
);
132 gb_pm_runtime_put_autosuspend(ghid
->bundle
);
137 static int gb_hid_request_handler(struct gb_operation
*op
)
139 struct gb_connection
*connection
= op
->connection
;
140 struct gb_hid
*ghid
= gb_connection_get_data(connection
);
141 struct gb_hid_input_report_request
*request
= op
->request
->payload
;
143 if (op
->type
!= GB_HID_TYPE_IRQ_EVENT
) {
144 dev_err(&connection
->bundle
->dev
,
145 "unsupported unsolicited request\n");
149 if (test_bit(GB_HID_STARTED
, &ghid
->flags
))
150 hid_input_report(ghid
->hid
, HID_INPUT_REPORT
,
151 request
->report
, op
->request
->payload_size
, 1);
156 static int gb_hid_report_len(struct hid_report
*report
)
158 return ((report
->size
- 1) >> 3) + 1 +
159 report
->device
->report_enum
[report
->type
].numbered
;
162 static void gb_hid_find_max_report(struct hid_device
*hid
, unsigned int type
,
165 struct hid_report
*report
;
168 list_for_each_entry(report
, &hid
->report_enum
[type
].report_list
, list
) {
169 size
= gb_hid_report_len(report
);
175 static void gb_hid_free_buffers(struct gb_hid
*ghid
)
182 static int gb_hid_alloc_buffers(struct gb_hid
*ghid
, size_t bufsize
)
184 ghid
->inbuf
= kzalloc(bufsize
, GFP_KERNEL
);
188 ghid
->bufsize
= bufsize
;
193 /* Routines dealing with reports */
194 static void gb_hid_init_report(struct gb_hid
*ghid
, struct hid_report
*report
)
198 size
= gb_hid_report_len(report
);
199 if (gb_hid_get_report(ghid
, report
->type
, report
->id
, ghid
->inbuf
,
204 * hid->driver_lock is held as we are in probe function,
205 * we just need to setup the input fields, so using
206 * hid_report_raw_event is safe.
208 hid_report_raw_event(ghid
->hid
, report
->type
, ghid
->inbuf
, size
, 1);
211 static void gb_hid_init_reports(struct gb_hid
*ghid
)
213 struct hid_device
*hid
= ghid
->hid
;
214 struct hid_report
*report
;
216 list_for_each_entry(report
,
217 &hid
->report_enum
[HID_INPUT_REPORT
].report_list
, list
)
218 gb_hid_init_report(ghid
, report
);
220 list_for_each_entry(report
,
221 &hid
->report_enum
[HID_FEATURE_REPORT
].report_list
, list
)
222 gb_hid_init_report(ghid
, report
);
225 static int __gb_hid_get_raw_report(struct hid_device
*hid
,
226 unsigned char report_number
, __u8
*buf
, size_t count
,
227 unsigned char report_type
)
229 struct gb_hid
*ghid
= hid
->driver_data
;
232 if (report_type
== HID_OUTPUT_REPORT
)
235 ret
= gb_hid_get_report(ghid
, report_type
, report_number
, buf
, count
);
242 static int __gb_hid_output_raw_report(struct hid_device
*hid
, __u8
*buf
,
243 size_t len
, unsigned char report_type
)
245 struct gb_hid
*ghid
= hid
->driver_data
;
246 int report_id
= buf
[0];
249 if (report_type
== HID_INPUT_REPORT
)
257 ret
= gb_hid_set_report(ghid
, report_type
, report_id
, buf
, len
);
258 if (report_id
&& ret
>= 0)
259 ret
++; /* add report_id to the number of transfered bytes */
264 static int gb_hid_raw_request(struct hid_device
*hid
, unsigned char reportnum
,
265 __u8
*buf
, size_t len
, unsigned char rtype
,
269 case HID_REQ_GET_REPORT
:
270 return __gb_hid_get_raw_report(hid
, reportnum
, buf
, len
, rtype
);
271 case HID_REQ_SET_REPORT
:
272 if (buf
[0] != reportnum
)
274 return __gb_hid_output_raw_report(hid
, buf
, len
, rtype
);
281 static int gb_hid_parse(struct hid_device
*hid
)
283 struct gb_hid
*ghid
= hid
->driver_data
;
288 rsize
= le16_to_cpu(ghid
->hdesc
.wReportDescLength
);
289 if (!rsize
|| rsize
> HID_MAX_DESCRIPTOR_SIZE
) {
290 dbg_hid("weird size of report descriptor (%u)\n", rsize
);
294 rdesc
= kzalloc(rsize
, GFP_KERNEL
);
296 dbg_hid("couldn't allocate rdesc memory\n");
300 ret
= gb_hid_get_report_desc(ghid
, rdesc
);
302 hid_err(hid
, "reading report descriptor failed\n");
306 ret
= hid_parse_report(hid
, rdesc
, rsize
);
308 dbg_hid("parsing report descriptor failed\n");
316 static int gb_hid_start(struct hid_device
*hid
)
318 struct gb_hid
*ghid
= hid
->driver_data
;
319 unsigned int bufsize
= HID_MIN_BUFFER_SIZE
;
322 gb_hid_find_max_report(hid
, HID_INPUT_REPORT
, &bufsize
);
323 gb_hid_find_max_report(hid
, HID_OUTPUT_REPORT
, &bufsize
);
324 gb_hid_find_max_report(hid
, HID_FEATURE_REPORT
, &bufsize
);
326 if (bufsize
> HID_MAX_BUFFER_SIZE
)
327 bufsize
= HID_MAX_BUFFER_SIZE
;
329 ret
= gb_hid_alloc_buffers(ghid
, bufsize
);
333 if (!(hid
->quirks
& HID_QUIRK_NO_INIT_REPORTS
))
334 gb_hid_init_reports(ghid
);
339 static void gb_hid_stop(struct hid_device
*hid
)
341 struct gb_hid
*ghid
= hid
->driver_data
;
343 gb_hid_free_buffers(ghid
);
346 static int gb_hid_open(struct hid_device
*hid
)
348 struct gb_hid
*ghid
= hid
->driver_data
;
351 mutex_lock(&gb_hid_open_mutex
);
353 ret
= gb_hid_set_power(ghid
, GB_HID_TYPE_PWR_ON
);
357 set_bit(GB_HID_STARTED
, &ghid
->flags
);
359 mutex_unlock(&gb_hid_open_mutex
);
364 static void gb_hid_close(struct hid_device
*hid
)
366 struct gb_hid
*ghid
= hid
->driver_data
;
370 * Protecting hid->open to make sure we don't restart data acquistion
371 * due to a resumption we no longer care about..
373 mutex_lock(&gb_hid_open_mutex
);
375 clear_bit(GB_HID_STARTED
, &ghid
->flags
);
377 /* Save some power */
378 ret
= gb_hid_set_power(ghid
, GB_HID_TYPE_PWR_OFF
);
380 dev_err(&ghid
->connection
->bundle
->dev
,
381 "failed to power off (%d)\n", ret
);
383 mutex_unlock(&gb_hid_open_mutex
);
386 static int gb_hid_power(struct hid_device
*hid
, int lvl
)
388 struct gb_hid
*ghid
= hid
->driver_data
;
392 return gb_hid_set_power(ghid
, GB_HID_TYPE_PWR_ON
);
394 return gb_hid_set_power(ghid
, GB_HID_TYPE_PWR_OFF
);
400 /* HID structure to pass callbacks */
401 static struct hid_ll_driver gb_hid_ll_driver
= {
402 .parse
= gb_hid_parse
,
403 .start
= gb_hid_start
,
406 .close
= gb_hid_close
,
407 .power
= gb_hid_power
,
408 .raw_request
= gb_hid_raw_request
,
411 static int gb_hid_init(struct gb_hid
*ghid
)
413 struct hid_device
*hid
= ghid
->hid
;
416 ret
= gb_hid_get_desc(ghid
);
420 hid
->version
= le16_to_cpu(ghid
->hdesc
.bcdHID
);
421 hid
->vendor
= le16_to_cpu(ghid
->hdesc
.wVendorID
);
422 hid
->product
= le16_to_cpu(ghid
->hdesc
.wProductID
);
423 hid
->country
= ghid
->hdesc
.bCountryCode
;
425 hid
->driver_data
= ghid
;
426 hid
->ll_driver
= &gb_hid_ll_driver
;
427 hid
->dev
.parent
= &ghid
->connection
->bundle
->dev
;
428 // hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
430 /* Set HID device's name */
431 snprintf(hid
->name
, sizeof(hid
->name
), "%s %04X:%04X",
432 dev_name(&ghid
->connection
->bundle
->dev
),
433 hid
->vendor
, hid
->product
);
438 static int gb_hid_probe(struct gb_bundle
*bundle
,
439 const struct greybus_bundle_id
*id
)
441 struct greybus_descriptor_cport
*cport_desc
;
442 struct gb_connection
*connection
;
443 struct hid_device
*hid
;
447 if (bundle
->num_cports
!= 1)
450 cport_desc
= &bundle
->cport_desc
[0];
451 if (cport_desc
->protocol_id
!= GREYBUS_PROTOCOL_HID
)
454 ghid
= kzalloc(sizeof(*ghid
), GFP_KERNEL
);
458 connection
= gb_connection_create(bundle
, le16_to_cpu(cport_desc
->id
),
459 gb_hid_request_handler
);
460 if (IS_ERR(connection
)) {
461 ret
= PTR_ERR(connection
);
465 gb_connection_set_data(connection
, ghid
);
466 ghid
->connection
= connection
;
468 hid
= hid_allocate_device();
471 goto err_connection_destroy
;
475 ghid
->bundle
= bundle
;
477 greybus_set_drvdata(bundle
, ghid
);
479 ret
= gb_connection_enable(connection
);
481 goto err_destroy_hid
;
483 ret
= gb_hid_init(ghid
);
485 goto err_connection_disable
;
487 ret
= hid_add_device(hid
);
489 hid_err(hid
, "can't add hid device: %d\n", ret
);
490 goto err_connection_disable
;
493 gb_pm_runtime_put_autosuspend(bundle
);
497 err_connection_disable
:
498 gb_connection_disable(connection
);
500 hid_destroy_device(hid
);
501 err_connection_destroy
:
502 gb_connection_destroy(connection
);
509 static void gb_hid_disconnect(struct gb_bundle
*bundle
)
511 struct gb_hid
*ghid
= greybus_get_drvdata(bundle
);
513 if (gb_pm_runtime_get_sync(bundle
))
514 gb_pm_runtime_get_noresume(bundle
);
516 hid_destroy_device(ghid
->hid
);
517 gb_connection_disable(ghid
->connection
);
518 gb_connection_destroy(ghid
->connection
);
522 static const struct greybus_bundle_id gb_hid_id_table
[] = {
523 { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID
) },
526 MODULE_DEVICE_TABLE(greybus
, gb_hid_id_table
);
528 static struct greybus_driver gb_hid_driver
= {
530 .probe
= gb_hid_probe
,
531 .disconnect
= gb_hid_disconnect
,
532 .id_table
= gb_hid_id_table
,
534 module_greybus_driver(gb_hid_driver
);
536 MODULE_LICENSE("GPL v2");