greybus: fix a leak on error in gb_module_create()
[cascardo/linux.git] / drivers / staging / greybus / hid.c
1 /*
2  * HID class driver for the Greybus.
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
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>
16
17 #include "greybus.h"
18
19 /* Greybus HID device's structure */
20 struct gb_hid {
21         struct gb_bundle *bundle;
22         struct gb_connection            *connection;
23
24         struct hid_device               *hid;
25         struct gb_hid_desc_response     hdesc;
26
27         unsigned long                   flags;
28 #define GB_HID_STARTED                  0x01
29 #define GB_HID_READ_PENDING             0x04
30
31         unsigned int                    bufsize;
32         char                            *inbuf;
33 };
34
35 static DEFINE_MUTEX(gb_hid_open_mutex);
36
37 /* Routines to get controller's information over greybus */
38
39 /* Operations performed on greybus */
40 static int gb_hid_get_desc(struct gb_hid *ghid)
41 {
42         return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
43                                  0, &ghid->hdesc, sizeof(ghid->hdesc));
44 }
45
46 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
47 {
48         int ret;
49
50         ret = gb_pm_runtime_get_sync(ghid->bundle);
51         if (ret)
52                 return ret;
53
54         ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
55                                  NULL, 0, rdesc,
56                                  le16_to_cpu(ghid->hdesc.wReportDescLength));
57
58         gb_pm_runtime_put_autosuspend(ghid->bundle);
59
60         return ret;
61 }
62
63 static int gb_hid_set_power(struct gb_hid *ghid, int type)
64 {
65         int ret;
66
67         ret = gb_pm_runtime_get_sync(ghid->bundle);
68         if (ret)
69                 return ret;
70
71         ret = gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
72
73         gb_pm_runtime_put_autosuspend(ghid->bundle);
74
75         return ret;
76 }
77
78 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
79                              unsigned char *buf, int len)
80 {
81         struct gb_hid_get_report_request request;
82         int ret;
83
84         ret = gb_pm_runtime_get_sync(ghid->bundle);
85         if (ret)
86                 return ret;
87
88         request.report_type = report_type;
89         request.report_id = report_id;
90
91         ret = gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
92                                  &request, sizeof(request), buf, len);
93
94         gb_pm_runtime_put_autosuspend(ghid->bundle);
95
96         return ret;
97 }
98
99 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
100                              unsigned char *buf, int len)
101 {
102         struct gb_hid_set_report_request *request;
103         struct gb_operation *operation;
104         int ret, size = sizeof(*request) + len - 1;
105
106         ret = gb_pm_runtime_get_sync(ghid->bundle);
107         if (ret)
108                 return ret;
109
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;
116         }
117
118         request = operation->request->payload;
119         request->report_type = report_type;
120         request->report_id = report_id;
121         memcpy(request->report, buf, len);
122
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;
129         }
130
131         gb_operation_put(operation);
132         gb_pm_runtime_put_autosuspend(ghid->bundle);
133
134         return ret;
135 }
136
137 static int gb_hid_request_handler(struct gb_operation *op)
138 {
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;
142
143         if (op->type != GB_HID_TYPE_IRQ_EVENT) {
144                 dev_err(&connection->bundle->dev,
145                         "unsupported unsolicited request\n");
146                 return -EINVAL;
147         }
148
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);
152
153         return 0;
154 }
155
156 static int gb_hid_report_len(struct hid_report *report)
157 {
158         return ((report->size - 1) >> 3) + 1 +
159                 report->device->report_enum[report->type].numbered;
160 }
161
162 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
163                                    unsigned int *max)
164 {
165         struct hid_report *report;
166         unsigned int size;
167
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;
172         }
173 }
174
175 static void gb_hid_free_buffers(struct gb_hid *ghid)
176 {
177         kfree(ghid->inbuf);
178         ghid->inbuf = NULL;
179         ghid->bufsize = 0;
180 }
181
182 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
183 {
184         ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
185         if (!ghid->inbuf)
186                 return -ENOMEM;
187
188         ghid->bufsize = bufsize;
189
190         return 0;
191 }
192
193 /* Routines dealing with reports */
194 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
195 {
196         unsigned int size;
197
198         size = gb_hid_report_len(report);
199         if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
200                               size))
201                 return;
202
203         /*
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.
207          */
208         hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
209 }
210
211 static void gb_hid_init_reports(struct gb_hid *ghid)
212 {
213         struct hid_device *hid = ghid->hid;
214         struct hid_report *report;
215
216         list_for_each_entry(report,
217                 &hid->report_enum[HID_INPUT_REPORT].report_list, list)
218                 gb_hid_init_report(ghid, report);
219
220         list_for_each_entry(report,
221                 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
222                 gb_hid_init_report(ghid, report);
223 }
224
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)
228 {
229         struct gb_hid *ghid = hid->driver_data;
230         int ret;
231
232         if (report_type == HID_OUTPUT_REPORT)
233                 return -EINVAL;
234
235         ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
236         if (!ret)
237                 ret = count;
238
239         return ret;
240 }
241
242 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
243                                       size_t len, unsigned char report_type)
244 {
245         struct gb_hid *ghid = hid->driver_data;
246         int report_id = buf[0];
247         int ret;
248
249         if (report_type == HID_INPUT_REPORT)
250                 return -EINVAL;
251
252         if (report_id) {
253                 buf++;
254                 len--;
255         }
256
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 */
260
261         return 0;
262 }
263
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)
267 {
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;
277         }
278 }
279
280 /* HID Callbacks */
281 static int gb_hid_parse(struct hid_device *hid)
282 {
283         struct gb_hid *ghid = hid->driver_data;
284         unsigned int rsize;
285         char *rdesc;
286         int ret;
287
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;
292         }
293
294         rdesc = kzalloc(rsize, GFP_KERNEL);
295         if (!rdesc) {
296                 dbg_hid("couldn't allocate rdesc memory\n");
297                 return -ENOMEM;
298         }
299
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;
304         }
305
306         ret = hid_parse_report(hid, rdesc, rsize);
307         if (ret)
308                 dbg_hid("parsing report descriptor failed\n");
309
310 free_rdesc:
311         kfree(rdesc);
312
313         return ret;
314 }
315
316 static int gb_hid_start(struct hid_device *hid)
317 {
318         struct gb_hid *ghid = hid->driver_data;
319         unsigned int bufsize = HID_MIN_BUFFER_SIZE;
320         int ret;
321
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);
325
326         if (bufsize > HID_MAX_BUFFER_SIZE)
327                 bufsize = HID_MAX_BUFFER_SIZE;
328
329         ret = gb_hid_alloc_buffers(ghid, bufsize);
330         if (ret)
331                 return ret;
332
333         if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
334                 gb_hid_init_reports(ghid);
335
336         return 0;
337 }
338
339 static void gb_hid_stop(struct hid_device *hid)
340 {
341         struct gb_hid *ghid = hid->driver_data;
342
343         gb_hid_free_buffers(ghid);
344 }
345
346 static int gb_hid_open(struct hid_device *hid)
347 {
348         struct gb_hid *ghid = hid->driver_data;
349         int ret = 0;
350
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);
358         }
359         mutex_unlock(&gb_hid_open_mutex);
360
361         return ret;
362 }
363
364 static void gb_hid_close(struct hid_device *hid)
365 {
366         struct gb_hid *ghid = hid->driver_data;
367         int ret;
368
369         /*
370          * Protecting hid->open to make sure we don't restart data acquistion
371          * due to a resumption we no longer care about..
372          */
373         mutex_lock(&gb_hid_open_mutex);
374         if (!--hid->open) {
375                 clear_bit(GB_HID_STARTED, &ghid->flags);
376
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);
382         }
383         mutex_unlock(&gb_hid_open_mutex);
384 }
385
386 static int gb_hid_power(struct hid_device *hid, int lvl)
387 {
388         struct gb_hid *ghid = hid->driver_data;
389
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);
395         }
396
397         return 0;
398 }
399
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,
409 };
410
411 static int gb_hid_init(struct gb_hid *ghid)
412 {
413         struct hid_device *hid = ghid->hid;
414         int ret;
415
416         ret = gb_hid_get_desc(ghid);
417         if (ret)
418                 return ret;
419
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;
424
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> */
429
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);
434
435         return 0;
436 }
437
438 static int gb_hid_probe(struct gb_bundle *bundle,
439                         const struct greybus_bundle_id *id)
440 {
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;
446
447         if (bundle->num_cports != 1)
448                 return -ENODEV;
449
450         cport_desc = &bundle->cport_desc[0];
451         if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
452                 return -ENODEV;
453
454         ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
455         if (!ghid)
456                 return -ENOMEM;
457
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;
463         }
464
465         gb_connection_set_data(connection, ghid);
466         ghid->connection = connection;
467
468         hid = hid_allocate_device();
469         if (IS_ERR(hid)) {
470                 ret = PTR_ERR(hid);
471                 goto err_connection_destroy;
472         }
473
474         ghid->hid = hid;
475         ghid->bundle = bundle;
476
477         greybus_set_drvdata(bundle, ghid);
478
479         ret = gb_connection_enable(connection);
480         if (ret)
481                 goto err_destroy_hid;
482
483         ret = gb_hid_init(ghid);
484         if (ret)
485                 goto err_connection_disable;
486
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;
491         }
492
493         gb_pm_runtime_put_autosuspend(bundle);
494
495         return 0;
496
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);
505
506         return ret;
507 }
508
509 static void gb_hid_disconnect(struct gb_bundle *bundle)
510 {
511         struct gb_hid *ghid = greybus_get_drvdata(bundle);
512
513         if (gb_pm_runtime_get_sync(bundle))
514                 gb_pm_runtime_get_noresume(bundle);
515
516         hid_destroy_device(ghid->hid);
517         gb_connection_disable(ghid->connection);
518         gb_connection_destroy(ghid->connection);
519         kfree(ghid);
520 }
521
522 static const struct greybus_bundle_id gb_hid_id_table[] = {
523         { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
524         { }
525 };
526 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
527
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,
533 };
534 module_greybus_driver(gb_hid_driver);
535
536 MODULE_LICENSE("GPL v2");