greybus: hid: Replace WARN_ON() with dev_err()
[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_connection            *connection;
22
23         struct hid_device               *hid;
24         struct gb_hid_desc_response     hdesc;
25
26         unsigned long                   flags;
27 #define GB_HID_STARTED                  0x01
28 #define GB_HID_READ_PENDING             0x04
29
30         unsigned int                    bufsize;
31         char                            *inbuf;
32 };
33
34 static DEFINE_MUTEX(gb_hid_open_mutex);
35
36 /* Routines to get controller's information over greybus */
37
38 /* Operations performed on greybus */
39 static int gb_hid_get_desc(struct gb_hid *ghid)
40 {
41         return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_DESC, NULL,
42                                  0, &ghid->hdesc, sizeof(ghid->hdesc));
43 }
44
45 static int gb_hid_get_report_desc(struct gb_hid *ghid, char *rdesc)
46 {
47         return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT_DESC,
48                                  NULL, 0, rdesc,
49                                  le16_to_cpu(ghid->hdesc.wReportDescLength));
50 }
51
52 static int gb_hid_set_power(struct gb_hid *ghid, int type)
53 {
54         return gb_operation_sync(ghid->connection, type, NULL, 0, NULL, 0);
55 }
56
57 static int gb_hid_get_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
58                              unsigned char *buf, int len)
59 {
60         struct gb_hid_get_report_request request;
61
62         request.report_type = report_type;
63         request.report_id = report_id;
64
65         return gb_operation_sync(ghid->connection, GB_HID_TYPE_GET_REPORT,
66                                  &request, sizeof(request), buf, len);
67 }
68
69 static int gb_hid_set_report(struct gb_hid *ghid, u8 report_type, u8 report_id,
70                              unsigned char *buf, int len)
71 {
72         struct gb_hid_set_report_request *request;
73         struct gb_operation *operation;
74         int ret, size = sizeof(*request) + len - 1;
75
76         operation = gb_operation_create(ghid->connection,
77                                         GB_HID_TYPE_SET_REPORT, size, 0,
78                                         GFP_KERNEL);
79         if (!operation)
80                 return -ENOMEM;
81
82         request = operation->request->payload;
83         request->report_type = report_type;
84         request->report_id = report_id;
85         memcpy(request->report, buf, len);
86
87         ret = gb_operation_request_send_sync(operation);
88         if (ret) {
89                 dev_err(&operation->connection->bundle->dev,
90                         "failed to set report: %d\n", ret);
91         } else {
92                 ret = len;
93         }
94
95         gb_operation_put(operation);
96
97         return ret;
98 }
99
100 static int gb_hid_request_handler(struct gb_operation *op)
101 {
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;
105
106         if (op->type != GB_HID_TYPE_IRQ_EVENT) {
107                 dev_err(&connection->bundle->dev,
108                         "unsupported unsolicited request\n");
109                 return -EINVAL;
110         }
111
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);
115
116         return 0;
117 }
118
119
120 static int gb_hid_report_len(struct hid_report *report)
121 {
122         return ((report->size - 1) >> 3) + 1 +
123                 report->device->report_enum[report->type].numbered;
124 }
125
126 static void gb_hid_find_max_report(struct hid_device *hid, unsigned int type,
127                                    unsigned int *max)
128 {
129         struct hid_report *report;
130         unsigned int size;
131
132         list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
133                 size = gb_hid_report_len(report);
134                 if (*max < size)
135                         *max = size;
136         }
137 }
138
139 static void gb_hid_free_buffers(struct gb_hid *ghid)
140 {
141         kfree(ghid->inbuf);
142         ghid->inbuf = NULL;
143         ghid->bufsize = 0;
144 }
145
146 static int gb_hid_alloc_buffers(struct gb_hid *ghid, size_t bufsize)
147 {
148         ghid->inbuf = kzalloc(bufsize, GFP_KERNEL);
149         if (!ghid->inbuf)
150                 return -ENOMEM;
151
152         ghid->bufsize = bufsize;
153
154         return 0;
155 }
156
157 /* Routines dealing with reports */
158 static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)
159 {
160         unsigned int size;
161
162         size = gb_hid_report_len(report);
163         if (gb_hid_get_report(ghid, report->type, report->id, ghid->inbuf,
164                               size))
165                 return;
166
167         /*
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.
171          */
172         hid_report_raw_event(ghid->hid, report->type, ghid->inbuf, size, 1);
173 }
174
175 static void gb_hid_init_reports(struct gb_hid *ghid)
176 {
177         struct hid_device *hid = ghid->hid;
178         struct hid_report *report;
179
180         list_for_each_entry(report,
181                 &hid->report_enum[HID_INPUT_REPORT].report_list, list)
182                 gb_hid_init_report(ghid, report);
183
184         list_for_each_entry(report,
185                 &hid->report_enum[HID_FEATURE_REPORT].report_list, list)
186                 gb_hid_init_report(ghid, report);
187 }
188
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)
192 {
193         struct gb_hid *ghid = hid->driver_data;
194         int ret;
195
196         if (report_type == HID_OUTPUT_REPORT)
197                 return -EINVAL;
198
199         ret = gb_hid_get_report(ghid, report_type, report_number, buf, count);
200         if (!ret)
201                 ret = count;
202
203         return ret;
204 }
205
206 static int __gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
207                                       size_t len, unsigned char report_type)
208 {
209         struct gb_hid *ghid = hid->driver_data;
210         int report_id = buf[0];
211         int ret;
212
213         if (report_type == HID_INPUT_REPORT)
214                 return -EINVAL;
215
216         if (report_id) {
217                 buf++;
218                 len--;
219         }
220
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 */
224
225         return 0;
226 }
227
228 static int gb_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
229                                __u8 *buf, size_t len, unsigned char rtype,
230                                int reqtype)
231 {
232         switch (reqtype) {
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)
237                         return -EINVAL;
238                 return __gb_hid_output_raw_report(hid, buf, len, rtype);
239         default:
240                 return -EIO;
241         }
242 }
243
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)
248 {
249         return gb_hid_raw_request(hid, reportnum, buf, len, rtype,
250                                   HID_REQ_GET_REPORT);
251 }
252
253 static int gb_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
254                                     size_t len, unsigned char rtype)
255 {
256         return gb_hid_raw_request(hid, buf[0], buf, len, rtype,
257                                   HID_REQ_SET_REPORT);
258 }
259 #endif
260
261 /* HID Callbacks */
262 static int gb_hid_parse(struct hid_device *hid)
263 {
264         struct gb_hid *ghid = hid->driver_data;
265         unsigned int rsize;
266         char *rdesc;
267         int ret;
268
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);
272                 return -EINVAL;
273         }
274
275         rdesc = kzalloc(rsize, GFP_KERNEL);
276         if (!rdesc) {
277                 dbg_hid("couldn't allocate rdesc memory\n");
278                 return -ENOMEM;
279         }
280
281         ret = gb_hid_get_report_desc(ghid, rdesc);
282         if (ret) {
283                 hid_err(hid, "reading report descriptor failed\n");
284                 goto free_rdesc;
285         }
286
287         ret = hid_parse_report(hid, rdesc, rsize);
288         if (ret)
289                 dbg_hid("parsing report descriptor failed\n");
290
291 free_rdesc:
292         kfree(rdesc);
293
294         return ret;
295 }
296
297 static int gb_hid_start(struct hid_device *hid)
298 {
299         struct gb_hid *ghid = hid->driver_data;
300         unsigned int bufsize = HID_MIN_BUFFER_SIZE;
301         int ret;
302
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);
306
307         if (bufsize > HID_MAX_BUFFER_SIZE)
308                 bufsize = HID_MAX_BUFFER_SIZE;
309
310         ret = gb_hid_alloc_buffers(ghid, bufsize);
311         if (ret)
312                 return ret;
313
314         if (!(hid->quirks & HID_QUIRK_NO_INIT_REPORTS))
315                 gb_hid_init_reports(ghid);
316
317         return 0;
318 }
319
320 static void gb_hid_stop(struct hid_device *hid)
321 {
322         struct gb_hid *ghid = hid->driver_data;
323
324         gb_hid_free_buffers(ghid);
325 }
326
327 static int gb_hid_open(struct hid_device *hid)
328 {
329         struct gb_hid *ghid = hid->driver_data;
330         int ret = 0;
331
332         mutex_lock(&gb_hid_open_mutex);
333         if (!hid->open++) {
334                 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
335                 if (ret < 0)
336                         hid->open--;
337                 else
338                         set_bit(GB_HID_STARTED, &ghid->flags);
339         }
340         mutex_unlock(&gb_hid_open_mutex);
341
342         return ret;
343 }
344
345 static void gb_hid_close(struct hid_device *hid)
346 {
347         struct gb_hid *ghid = hid->driver_data;
348         int ret;
349
350         /*
351          * Protecting hid->open to make sure we don't restart data acquistion
352          * due to a resumption we no longer care about..
353          */
354         mutex_lock(&gb_hid_open_mutex);
355         if (!--hid->open) {
356                 clear_bit(GB_HID_STARTED, &ghid->flags);
357
358                 /* Save some power */
359                 ret = gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
360                 if (ret)
361                         dev_err(&ghid->connection->bundle->dev,
362                                 "failed to power off (%d)\n", ret);
363         }
364         mutex_unlock(&gb_hid_open_mutex);
365 }
366
367 static int gb_hid_power(struct hid_device *hid, int lvl)
368 {
369         struct gb_hid *ghid = hid->driver_data;
370
371         switch (lvl) {
372         case PM_HINT_FULLON:
373                 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_ON);
374         case PM_HINT_NORMAL:
375                 return gb_hid_set_power(ghid, GB_HID_TYPE_PWR_OFF);
376         }
377
378         return 0;
379 }
380
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,
385         .stop = gb_hid_stop,
386         .open = gb_hid_open,
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,
391 #endif
392 };
393
394 static int gb_hid_init(struct gb_hid *ghid)
395 {
396         struct hid_device *hid = ghid->hid;
397         int ret;
398
399         ret = gb_hid_get_desc(ghid);
400         if (ret)
401                 return ret;
402
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;
407
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;
414 #endif
415 //      hid->bus = BUS_GREYBUS; /* Need a bustype for GREYBUS in <linux/input.h> */
416
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);
421
422         return 0;
423 }
424
425 static int gb_hid_probe(struct gb_bundle *bundle,
426                         const struct greybus_bundle_id *id)
427 {
428         struct greybus_descriptor_cport *cport_desc;
429         struct gb_connection *connection;
430         struct hid_device *hid;
431         struct gb_hid *ghid;
432         int ret;
433
434         if (bundle->num_cports != 1)
435                 return -ENODEV;
436
437         cport_desc = &bundle->cport_desc[0];
438         if (cport_desc->protocol_id != GREYBUS_PROTOCOL_HID)
439                 return -ENODEV;
440
441         ghid = kzalloc(sizeof(*ghid), GFP_KERNEL);
442         if (!ghid)
443                 return -ENOMEM;
444
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);
449                 goto err_free_ghid;
450         }
451
452         connection->private = ghid;
453         ghid->connection = connection;
454
455         hid = hid_allocate_device();
456         if (IS_ERR(hid)) {
457                 ret = PTR_ERR(hid);
458                 goto err_connection_destroy;
459         }
460
461         ghid->hid = hid;
462
463         greybus_set_drvdata(bundle, ghid);
464
465         ret = gb_connection_enable(connection);
466         if (ret)
467                 goto err_destroy_hid;
468
469         ret = gb_hid_init(ghid);
470         if (ret)
471                 goto err_connection_disable;
472
473         ret = hid_add_device(hid);
474         if (ret) {
475                 hid_err(hid, "can't add hid device: %d\n", ret);
476                 goto err_connection_disable;
477         }
478
479         return 0;
480
481 err_connection_disable:
482         gb_connection_disable(connection);
483 err_destroy_hid:
484         hid_destroy_device(hid);
485 err_connection_destroy:
486         gb_connection_destroy(connection);
487 err_free_ghid:
488         kfree(ghid);
489
490         return ret;
491 }
492
493 static void gb_hid_disconnect(struct gb_bundle *bundle)
494 {
495         struct gb_hid *ghid = greybus_get_drvdata(bundle);
496
497         hid_destroy_device(ghid->hid);
498         gb_connection_disable(ghid->connection);
499         gb_connection_destroy(ghid->connection);
500         kfree(ghid);
501 }
502
503 static const struct greybus_bundle_id gb_hid_id_table[] = {
504         { GREYBUS_DEVICE_CLASS(GREYBUS_CLASS_HID) },
505         { }
506 };
507 MODULE_DEVICE_TABLE(greybus, gb_hid_id_table);
508
509 static struct greybus_driver gb_hid_driver = {
510         .name           = "hid",
511         .probe          = gb_hid_probe,
512         .disconnect     = gb_hid_disconnect,
513         .id_table       = gb_hid_id_table,
514 };
515 module_greybus_driver(gb_hid_driver);
516
517 MODULE_LICENSE("GPL v2");