powercap: restrict energy meter to root access
[linux/fpc-iii.git] / drivers / staging / greybus / hid.c
blob730d746fc4c2a57399dec6cc26b22762a66d5aef
1 /*
2 * HID class driver for the Greybus.
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
8 */
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>
17 #include "greybus.h"
19 /* Greybus HID device's structure */
20 struct gb_hid {
21 struct gb_bundle *bundle;
22 struct gb_connection *connection;
24 struct hid_device *hid;
25 struct gb_hid_desc_response hdesc;
27 unsigned long flags;
28 #define GB_HID_STARTED 0x01
29 #define GB_HID_READ_PENDING 0x04
31 unsigned int bufsize;
32 char *inbuf;
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)
48 int ret;
50 ret = gb_pm_runtime_get_sync(ghid->bundle);
51 if (ret)
52 return ret;
54 ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
55 NULL, 0, rdesc,
56 le16_to_cpu(ghid->hdesc.wReportDescLength));
58 gb_pm_runtime_put_autosuspend(ghid->bundle);
60 return ret;
63 static int gb_hid_set_power(struct gb_hid *ghid, int type)
65 int ret;
67 ret = gb_pm_runtime_get_sync(ghid->bundle);
68 if (ret)
69 return ret;
71 ret = gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
73 gb_pm_runtime_put_autosuspend(ghid->bundle);
75 return ret;
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;
82 int ret;
84 ret = gb_pm_runtime_get_sync(ghid->bundle);
85 if (ret)
86 return ret;
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);
96 return ret;
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);
107 if (ret)
108 return ret;
110 operation = gb_operation_create(ghid->connection,
111 GB_HID_TYPE_SET_REPORT, size, 0,
112 GFP_KERNEL);
113 if (!operation) {
114 gb_pm_runtime_put_autosuspend(ghid->bundle);
115 return -ENOMEM;
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);
124 if (ret) {
125 dev_err(&operation->connection->bundle->dev,
126 "failed to set report: %d\n", ret);
127 } else {
128 ret = len;
131 gb_operation_put(operation);
132 gb_pm_runtime_put_autosuspend(ghid->bundle);
134 return ret;
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");
146 return -EINVAL;
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);
153 return 0;
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,
163 unsigned int *max)
165 struct hid_report *report;
166 unsigned int size;
168 list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
169 size = gb_hid_report_len(report);
170 if (*max < size)
171 *max = size;
175 static void gb_hid_free_buffers(struct gb_hid *ghid)
177 kfree(ghid->inbuf);
178 ghid->inbuf = NULL;
179 ghid->bufsize = 0;
182 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
184 ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
185 if (!ghid->inbuf)
186 return -ENOMEM;
188 ghid->bufsize = bufsize;
190 return 0;
193 /* Routines dealing with reports */
194 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
196 unsigned int size;
198 size = gb_hid_report_len(report);
199 if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
200 size))
201 return;
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;
230 int ret;
232 if (report_type == HID_OUTPUT_REPORT)
233 return -EINVAL;
235 ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
236 if (!ret)
237 ret = count;
239 return ret;
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];
247 int ret;
249 if (report_type == HID_INPUT_REPORT)
250 return -EINVAL;
252 if (report_id) {
253 buf++;
254 len--;
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 */
261 return 0;
264 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
265 __u8 *buf, size_t len, unsigned char rtype,
266 int reqtype)
268 switch (reqtype) {
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)
273 return -EINVAL;
274 return __gb_hid_output_raw_report(hid, buf, len, rtype);
275 default:
276 return -EIO;
280 /* HID Callbacks */
281 static int gb_hid_parse(struct hid_device *hid)
283 struct gb_hid *ghid = hid->driver_data;
284 unsigned int rsize;
285 char *rdesc;
286 int ret;
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);
291 return -EINVAL;
294 rdesc = kzalloc(rsize, GFP_KERNEL);
295 if (!rdesc) {
296 dbg_hid("couldn't allocate rdesc memory\n");
297 return -ENOMEM;
300 ret = gb_hid_get_report_desc(ghid, rdesc);
301 if (ret) {
302 hid_err(hid, "reading report descriptor failed\n");
303 goto free_rdesc;
306 ret = hid_parse_report(hid, rdesc, rsize);
307 if (ret)
308 dbg_hid("parsing report descriptor failed\n");
310 free_rdesc:
311 kfree(rdesc);
313 return ret;
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;
320 int ret;
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);
330 if (ret)
331 return ret;
333 if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
334 gb_hid_init_reports(ghid);
336 return 0;
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;
349 int ret = 0;
351 mutex_lock(&gb_hid_open_mutex);
352 if (!hid->open++) {
353 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
354 if (ret < 0)
355 hid->open--;
356 else
357 set_bit(GB_HID_STARTED, &ghid->flags);
359 mutex_unlock(&gb_hid_open_mutex);
361 return ret;
364 static void gb_hid_close(struct hid_device *hid)
366 struct gb_hid *ghid = hid->driver_data;
367 int ret;
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);
374 if (!--hid->open) {
375 clear_bit(GB_HID_STARTED, &ghid->flags);
377 /* Save some power */
378 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
379 if (ret)
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;
390 switch (lvl) {
391 case PM_HINT_FULLON:
392 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
393 case PM_HINT_NORMAL:
394 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
397 return 0;
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,
404 .stop = gb_hid_stop,
405 .open = gb_hid_open,
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;
414 int ret;
416 ret = gb_hid_get_desc(ghid);
417 if (ret)
418 return ret;
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);
435 return 0;
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;
444 struct gb_hid *ghid;
445 int ret;
447 if (bundle->num_cports != 1)
448 return -ENODEV;
450 cport_desc = &bundle->cport_desc[0];
451 if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
452 return -ENODEV;
454 ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
455 if (!ghid)
456 return -ENOMEM;
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);
462 goto err_free_ghid;
465 gb_connection_set_data(connection, ghid);
466 ghid->connection = connection;
468 hid = hid_allocate_device();
469 if (IS_ERR(hid)) {
470 ret = PTR_ERR(hid);
471 goto err_connection_destroy;
474 ghid->hid = hid;
475 ghid->bundle = bundle;
477 greybus_set_drvdata(bundle, ghid);
479 ret = gb_connection_enable(connection);
480 if (ret)
481 goto err_destroy_hid;
483 ret = gb_hid_init(ghid);
484 if (ret)
485 goto err_connection_disable;
487 ret = hid_add_device(hid);
488 if (ret) {
489 hid_err(hid, "can't add hid device: %d\n", ret);
490 goto err_connection_disable;
493 gb_pm_runtime_put_autosuspend(bundle);
495 return 0;
497 err_connection_disable:
498 gb_connection_disable(connection);
499 err_destroy_hid:
500 hid_destroy_device(hid);
501 err_connection_destroy:
502 gb_connection_destroy(connection);
503 err_free_ghid:
504 kfree(ghid);
506 return ret;
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);
519 kfree(ghid);
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 = {
529 .name = "hid",
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");