HID: use hid_hw_request() instead of direct call to usbhid
[pandora-kernel.git] / drivers / hid / hid-sensor-hub.c
1 /*
2  * HID Sensors Driver
3  * Copyright (c) 2012, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  */
19 #include <linux/device.h>
20 #include <linux/hid.h>
21 #include "usbhid/usbhid.h"
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/mfd/core.h>
25 #include <linux/list.h>
26 #include <linux/hid-sensor-ids.h>
27 #include <linux/hid-sensor-hub.h>
28 #include "hid-ids.h"
29
30 /**
31  * struct sensor_hub_pending - Synchronous read pending information
32  * @status:             Pending status true/false.
33  * @ready:              Completion synchronization data.
34  * @usage_id:           Usage id for physical device, E.g. Gyro usage id.
35  * @attr_usage_id:      Usage Id of a field, E.g. X-AXIS for a gyro.
36  * @raw_size:           Response size for a read request.
37  * @raw_data:           Place holder for received response.
38  */
39 struct sensor_hub_pending {
40         bool status;
41         struct completion ready;
42         u32 usage_id;
43         u32 attr_usage_id;
44         int raw_size;
45         u8  *raw_data;
46 };
47
48 /**
49  * struct sensor_hub_data - Hold a instance data for a HID hub device
50  * @hsdev:              Stored hid instance for current hub device.
51  * @mutex:              Mutex to serialize synchronous request.
52  * @lock:               Spin lock to protect pending request structure.
53  * @pending:            Holds information of pending sync read request.
54  * @dyn_callback_list:  Holds callback function
55  * @dyn_callback_lock:  spin lock to protect callback list
56  * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance.
57  * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached).
58  */
59 struct sensor_hub_data {
60         struct hid_sensor_hub_device *hsdev;
61         struct mutex mutex;
62         spinlock_t lock;
63         struct sensor_hub_pending pending;
64         struct list_head dyn_callback_list;
65         spinlock_t dyn_callback_lock;
66         struct mfd_cell *hid_sensor_hub_client_devs;
67         int hid_sensor_client_cnt;
68 };
69
70 /**
71  * struct hid_sensor_hub_callbacks_list - Stores callback list
72  * @list:               list head.
73  * @usage_id:           usage id for a physical device.
74  * @usage_callback:     Stores registered callback functions.
75  * @priv:               Private data for a physical device.
76  */
77 struct hid_sensor_hub_callbacks_list {
78         struct list_head list;
79         u32 usage_id;
80         struct hid_sensor_hub_callbacks *usage_callback;
81         void *priv;
82 };
83
84 static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev,
85                                                 int dir)
86 {
87         struct hid_report *report;
88
89         list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) {
90                 if (report->id == id)
91                         return report;
92         }
93         hid_warn(hdev, "No report with id 0x%x found\n", id);
94
95         return NULL;
96 }
97
98 static int sensor_hub_get_physical_device_count(
99                                 struct hid_report_enum *report_enum)
100 {
101         struct hid_report *report;
102         struct hid_field *field;
103         int cnt = 0;
104
105         list_for_each_entry(report, &report_enum->report_list, list) {
106                 field = report->field[0];
107                 if (report->maxfield && field &&
108                                         field->physical)
109                         cnt++;
110         }
111
112         return cnt;
113 }
114
115 static void sensor_hub_fill_attr_info(
116                 struct hid_sensor_hub_attribute_info *info,
117                 s32 index, s32 report_id, s32 units, s32 unit_expo, s32 size)
118 {
119         info->index = index;
120         info->report_id = report_id;
121         info->units = units;
122         info->unit_expo = unit_expo;
123         info->size = size/8;
124 }
125
126 static struct hid_sensor_hub_callbacks *sensor_hub_get_callback(
127                                         struct hid_device *hdev,
128                                         u32 usage_id, void **priv)
129 {
130         struct hid_sensor_hub_callbacks_list *callback;
131         struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
132
133         spin_lock(&pdata->dyn_callback_lock);
134         list_for_each_entry(callback, &pdata->dyn_callback_list, list)
135                 if (callback->usage_id == usage_id) {
136                         *priv = callback->priv;
137                         spin_unlock(&pdata->dyn_callback_lock);
138                         return callback->usage_callback;
139                 }
140         spin_unlock(&pdata->dyn_callback_lock);
141
142         return NULL;
143 }
144
145 int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev,
146                         u32 usage_id,
147                         struct hid_sensor_hub_callbacks *usage_callback)
148 {
149         struct hid_sensor_hub_callbacks_list *callback;
150         struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
151
152         spin_lock(&pdata->dyn_callback_lock);
153         list_for_each_entry(callback, &pdata->dyn_callback_list, list)
154                 if (callback->usage_id == usage_id) {
155                         spin_unlock(&pdata->dyn_callback_lock);
156                         return -EINVAL;
157                 }
158         callback = kzalloc(sizeof(*callback), GFP_ATOMIC);
159         if (!callback) {
160                 spin_unlock(&pdata->dyn_callback_lock);
161                 return -ENOMEM;
162         }
163         callback->usage_callback = usage_callback;
164         callback->usage_id = usage_id;
165         callback->priv = NULL;
166         list_add_tail(&callback->list, &pdata->dyn_callback_list);
167         spin_unlock(&pdata->dyn_callback_lock);
168
169         return 0;
170 }
171 EXPORT_SYMBOL_GPL(sensor_hub_register_callback);
172
173 int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev,
174                                 u32 usage_id)
175 {
176         struct hid_sensor_hub_callbacks_list *callback;
177         struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev);
178
179         spin_lock(&pdata->dyn_callback_lock);
180         list_for_each_entry(callback, &pdata->dyn_callback_list, list)
181                 if (callback->usage_id == usage_id) {
182                         list_del(&callback->list);
183                         kfree(callback);
184                         break;
185                 }
186         spin_unlock(&pdata->dyn_callback_lock);
187
188         return 0;
189 }
190 EXPORT_SYMBOL_GPL(sensor_hub_remove_callback);
191
192 int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
193                                 u32 field_index, s32 value)
194 {
195         struct hid_report *report;
196         struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
197         int ret = 0;
198
199         mutex_lock(&data->mutex);
200         report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
201         if (!report || (field_index >=  report->maxfield)) {
202                 ret = -EINVAL;
203                 goto done_proc;
204         }
205         hid_set_field(report->field[field_index], 0, value);
206         hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT);
207         usbhid_wait_io(hsdev->hdev);
208
209 done_proc:
210         mutex_unlock(&data->mutex);
211
212         return ret;
213 }
214 EXPORT_SYMBOL_GPL(sensor_hub_set_feature);
215
216 int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id,
217                                 u32 field_index, s32 *value)
218 {
219         struct hid_report *report;
220         struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
221         int ret = 0;
222
223         mutex_lock(&data->mutex);
224         report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT);
225         if (!report || (field_index >=  report->maxfield)) {
226                 ret = -EINVAL;
227                 goto done_proc;
228         }
229         hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
230         usbhid_wait_io(hsdev->hdev);
231         *value = report->field[field_index]->value[0];
232
233 done_proc:
234         mutex_unlock(&data->mutex);
235
236         return ret;
237 }
238 EXPORT_SYMBOL_GPL(sensor_hub_get_feature);
239
240
241 int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev,
242                                         u32 usage_id,
243                                         u32 attr_usage_id, u32 report_id)
244 {
245         struct sensor_hub_data *data =  hid_get_drvdata(hsdev->hdev);
246         unsigned long flags;
247         struct hid_report *report;
248         int ret_val = 0;
249
250         mutex_lock(&data->mutex);
251         memset(&data->pending, 0, sizeof(data->pending));
252         init_completion(&data->pending.ready);
253         data->pending.usage_id = usage_id;
254         data->pending.attr_usage_id = attr_usage_id;
255         data->pending.raw_size = 0;
256
257         spin_lock_irqsave(&data->lock, flags);
258         data->pending.status = true;
259         report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT);
260         if (!report) {
261                 spin_unlock_irqrestore(&data->lock, flags);
262                 goto err_free;
263         }
264         hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT);
265         spin_unlock_irqrestore(&data->lock, flags);
266         wait_for_completion_interruptible_timeout(&data->pending.ready, HZ*5);
267         switch (data->pending.raw_size) {
268         case 1:
269                 ret_val = *(u8 *)data->pending.raw_data;
270                 break;
271         case 2:
272                 ret_val = *(u16 *)data->pending.raw_data;
273                 break;
274         case 4:
275                 ret_val = *(u32 *)data->pending.raw_data;
276                 break;
277         default:
278                 ret_val = 0;
279         }
280         kfree(data->pending.raw_data);
281
282 err_free:
283         data->pending.status = false;
284         mutex_unlock(&data->mutex);
285
286         return ret_val;
287 }
288 EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value);
289
290 int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev,
291                                 u8 type,
292                                 u32 usage_id,
293                                 u32 attr_usage_id,
294                                 struct hid_sensor_hub_attribute_info *info)
295 {
296         int ret = -1;
297         int i, j;
298         int collection_index = -1;
299         struct hid_report *report;
300         struct hid_field *field;
301         struct hid_report_enum *report_enum;
302         struct hid_device *hdev = hsdev->hdev;
303
304         /* Initialize with defaults */
305         info->usage_id = usage_id;
306         info->attrib_id =  attr_usage_id;
307         info->report_id = -1;
308         info->index = -1;
309         info->units = -1;
310         info->unit_expo = -1;
311
312         for (i = 0; i < hdev->maxcollection; ++i) {
313                 struct hid_collection *collection = &hdev->collection[i];
314                 if (usage_id == collection->usage) {
315                         collection_index = i;
316                         break;
317                 }
318         }
319         if (collection_index == -1)
320                 goto err_ret;
321
322         report_enum = &hdev->report_enum[type];
323         list_for_each_entry(report, &report_enum->report_list, list) {
324                 for (i = 0; i < report->maxfield; ++i) {
325                         field = report->field[i];
326                         if (field->physical == usage_id &&
327                                 field->logical == attr_usage_id) {
328                                 sensor_hub_fill_attr_info(info, i, report->id,
329                                         field->unit, field->unit_exponent,
330                                         field->report_size);
331                                 ret = 0;
332                         } else {
333                                 for (j = 0; j < field->maxusage; ++j) {
334                                         if (field->usage[j].hid ==
335                                         attr_usage_id &&
336                                         field->usage[j].collection_index ==
337                                         collection_index)  {
338                                                 sensor_hub_fill_attr_info(info,
339                                                         i, report->id,
340                                                         field->unit,
341                                                         field->unit_exponent,
342                                                         field->report_size);
343                                                 ret = 0;
344                                                 break;
345                                         }
346                                 }
347                         }
348                         if (ret == 0)
349                                 break;
350                 }
351         }
352
353 err_ret:
354         return ret;
355 }
356 EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info);
357
358 #ifdef CONFIG_PM
359 static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message)
360 {
361         struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
362         struct hid_sensor_hub_callbacks_list *callback;
363
364         hid_dbg(hdev, " sensor_hub_suspend\n");
365         spin_lock(&pdata->dyn_callback_lock);
366         list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
367                 if (callback->usage_callback->suspend)
368                         callback->usage_callback->suspend(
369                                         pdata->hsdev, callback->priv);
370         }
371         spin_unlock(&pdata->dyn_callback_lock);
372
373         return 0;
374 }
375
376 static int sensor_hub_resume(struct hid_device *hdev)
377 {
378         struct sensor_hub_data *pdata =  hid_get_drvdata(hdev);
379         struct hid_sensor_hub_callbacks_list *callback;
380
381         hid_dbg(hdev, " sensor_hub_resume\n");
382         spin_lock(&pdata->dyn_callback_lock);
383         list_for_each_entry(callback, &pdata->dyn_callback_list, list) {
384                 if (callback->usage_callback->resume)
385                         callback->usage_callback->resume(
386                                         pdata->hsdev, callback->priv);
387         }
388         spin_unlock(&pdata->dyn_callback_lock);
389
390         return 0;
391 }
392
393 static int sensor_hub_reset_resume(struct hid_device *hdev)
394 {
395         return 0;
396 }
397 #endif
398 /*
399  * Handle raw report as sent by device
400  */
401 static int sensor_hub_raw_event(struct hid_device *hdev,
402                 struct hid_report *report, u8 *raw_data, int size)
403 {
404         int i;
405         u8 *ptr;
406         int sz;
407         struct sensor_hub_data *pdata = hid_get_drvdata(hdev);
408         unsigned long flags;
409         struct hid_sensor_hub_callbacks *callback = NULL;
410         struct hid_collection *collection = NULL;
411         void *priv = NULL;
412
413         hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n",
414                          report->id, size, report->type);
415         hid_dbg(hdev, "maxfield:%d\n", report->maxfield);
416         if (report->type != HID_INPUT_REPORT)
417                 return 1;
418
419         ptr = raw_data;
420         ptr++; /*Skip report id*/
421
422         spin_lock_irqsave(&pdata->lock, flags);
423
424         for (i = 0; i < report->maxfield; ++i) {
425
426                 hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n",
427                                 i, report->field[i]->usage->collection_index,
428                                 report->field[i]->usage->hid,
429                                 report->field[i]->report_size/8);
430
431                 sz = report->field[i]->report_size/8;
432                 if (pdata->pending.status && pdata->pending.attr_usage_id ==
433                                 report->field[i]->usage->hid) {
434                         hid_dbg(hdev, "data was pending ...\n");
435                         pdata->pending.raw_data = kmalloc(sz, GFP_ATOMIC);
436                         if (pdata->pending.raw_data) {
437                                 memcpy(pdata->pending.raw_data, ptr, sz);
438                                 pdata->pending.raw_size  = sz;
439                         } else
440                                 pdata->pending.raw_size = 0;
441                         complete(&pdata->pending.ready);
442                 }
443                 collection = &hdev->collection[
444                                 report->field[i]->usage->collection_index];
445                 hid_dbg(hdev, "collection->usage %x\n",
446                                         collection->usage);
447                 callback = sensor_hub_get_callback(pdata->hsdev->hdev,
448                                                 report->field[i]->physical,
449                                                         &priv);
450                 if (callback && callback->capture_sample) {
451                         if (report->field[i]->logical)
452                                 callback->capture_sample(pdata->hsdev,
453                                         report->field[i]->logical, sz, ptr,
454                                         callback->pdev);
455                         else
456                                 callback->capture_sample(pdata->hsdev,
457                                         report->field[i]->usage->hid, sz, ptr,
458                                         callback->pdev);
459                 }
460                 ptr += sz;
461         }
462         if (callback && collection && callback->send_event)
463                 callback->send_event(pdata->hsdev, collection->usage,
464                                 callback->pdev);
465         spin_unlock_irqrestore(&pdata->lock, flags);
466
467         return 1;
468 }
469
470 static int sensor_hub_probe(struct hid_device *hdev,
471                                 const struct hid_device_id *id)
472 {
473         int ret;
474         struct sensor_hub_data *sd;
475         int i;
476         char *name;
477         struct hid_report *report;
478         struct hid_report_enum *report_enum;
479         struct hid_field *field;
480         int dev_cnt;
481
482         sd = kzalloc(sizeof(struct sensor_hub_data), GFP_KERNEL);
483         if (!sd) {
484                 hid_err(hdev, "cannot allocate Sensor data\n");
485                 return -ENOMEM;
486         }
487         sd->hsdev = kzalloc(sizeof(struct hid_sensor_hub_device), GFP_KERNEL);
488         if (!sd->hsdev) {
489                 hid_err(hdev, "cannot allocate hid_sensor_hub_device\n");
490                 ret = -ENOMEM;
491                 goto err_free_hub;
492         }
493         hid_set_drvdata(hdev, sd);
494         sd->hsdev->hdev = hdev;
495         sd->hsdev->vendor_id = hdev->vendor;
496         sd->hsdev->product_id = hdev->product;
497         spin_lock_init(&sd->lock);
498         spin_lock_init(&sd->dyn_callback_lock);
499         mutex_init(&sd->mutex);
500         ret = hid_parse(hdev);
501         if (ret) {
502                 hid_err(hdev, "parse failed\n");
503                 goto err_free;
504         }
505         INIT_LIST_HEAD(&hdev->inputs);
506
507         ret = hid_hw_start(hdev, 0);
508         if (ret) {
509                 hid_err(hdev, "hw start failed\n");
510                 goto err_free;
511         }
512         ret = hid_hw_open(hdev);
513         if (ret) {
514                 hid_err(hdev, "failed to open input interrupt pipe\n");
515                 goto err_stop_hw;
516         }
517
518         INIT_LIST_HEAD(&sd->dyn_callback_list);
519         sd->hid_sensor_client_cnt = 0;
520         report_enum = &hdev->report_enum[HID_INPUT_REPORT];
521
522         dev_cnt = sensor_hub_get_physical_device_count(report_enum);
523         if (dev_cnt > HID_MAX_PHY_DEVICES) {
524                 hid_err(hdev, "Invalid Physical device count\n");
525                 ret = -EINVAL;
526                 goto err_close;
527         }
528         sd->hid_sensor_hub_client_devs = kzalloc(dev_cnt *
529                                                 sizeof(struct mfd_cell),
530                                                 GFP_KERNEL);
531         if (sd->hid_sensor_hub_client_devs == NULL) {
532                 hid_err(hdev, "Failed to allocate memory for mfd cells\n");
533                         ret = -ENOMEM;
534                         goto err_close;
535         }
536         list_for_each_entry(report, &report_enum->report_list, list) {
537                 hid_dbg(hdev, "Report id:%x\n", report->id);
538                 field = report->field[0];
539                 if (report->maxfield && field &&
540                                         field->physical) {
541                         name = kasprintf(GFP_KERNEL, "HID-SENSOR-%x",
542                                                 field->physical);
543                         if (name  == NULL) {
544                                 hid_err(hdev, "Failed MFD device name\n");
545                                         ret = -ENOMEM;
546                                         goto err_free_names;
547                         }
548                         sd->hid_sensor_hub_client_devs[
549                                 sd->hid_sensor_client_cnt].name = name;
550                         sd->hid_sensor_hub_client_devs[
551                                 sd->hid_sensor_client_cnt].platform_data =
552                                                 sd->hsdev;
553                         sd->hid_sensor_hub_client_devs[
554                                 sd->hid_sensor_client_cnt].pdata_size =
555                                                 sizeof(*sd->hsdev);
556                         hid_dbg(hdev, "Adding %s:%p\n", name, sd);
557                         sd->hid_sensor_client_cnt++;
558                 }
559         }
560         ret = mfd_add_devices(&hdev->dev, 0, sd->hid_sensor_hub_client_devs,
561                 sd->hid_sensor_client_cnt, NULL, 0, NULL);
562         if (ret < 0)
563                 goto err_free_names;
564
565         return ret;
566
567 err_free_names:
568         for (i = 0; i < sd->hid_sensor_client_cnt ; ++i)
569                 kfree(sd->hid_sensor_hub_client_devs[i].name);
570         kfree(sd->hid_sensor_hub_client_devs);
571 err_close:
572         hid_hw_close(hdev);
573 err_stop_hw:
574         hid_hw_stop(hdev);
575 err_free:
576         kfree(sd->hsdev);
577 err_free_hub:
578         kfree(sd);
579
580         return ret;
581 }
582
583 static void sensor_hub_remove(struct hid_device *hdev)
584 {
585         struct sensor_hub_data *data = hid_get_drvdata(hdev);
586         unsigned long flags;
587         int i;
588
589         hid_dbg(hdev, " hardware removed\n");
590         hid_hw_close(hdev);
591         hid_hw_stop(hdev);
592         spin_lock_irqsave(&data->lock, flags);
593         if (data->pending.status)
594                 complete(&data->pending.ready);
595         spin_unlock_irqrestore(&data->lock, flags);
596         mfd_remove_devices(&hdev->dev);
597         for (i = 0; i < data->hid_sensor_client_cnt ; ++i)
598                 kfree(data->hid_sensor_hub_client_devs[i].name);
599         kfree(data->hid_sensor_hub_client_devs);
600         hid_set_drvdata(hdev, NULL);
601         mutex_destroy(&data->mutex);
602         kfree(data->hsdev);
603         kfree(data);
604 }
605
606 static const struct hid_device_id sensor_hub_devices[] = {
607         { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID,
608                      HID_ANY_ID) },
609         { }
610 };
611 MODULE_DEVICE_TABLE(hid, sensor_hub_devices);
612
613 static struct hid_driver sensor_hub_driver = {
614         .name = "hid-sensor-hub",
615         .id_table = sensor_hub_devices,
616         .probe = sensor_hub_probe,
617         .remove = sensor_hub_remove,
618         .raw_event = sensor_hub_raw_event,
619 #ifdef CONFIG_PM
620         .suspend = sensor_hub_suspend,
621         .resume =  sensor_hub_resume,
622         .reset_resume =  sensor_hub_reset_resume,
623 #endif
624 };
625 module_hid_driver(sensor_hub_driver);
626
627 MODULE_DESCRIPTION("HID Sensor Hub driver");
628 MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>");
629 MODULE_LICENSE("GPL");