srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 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> |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/mfd/core.h> |
| 24 | #include <linux/list.h> |
| 25 | #include <linux/hid-sensor-ids.h> |
| 26 | #include <linux/hid-sensor-hub.h> |
| 27 | #include "hid-ids.h" |
| 28 | |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 29 | #define HID_SENSOR_HUB_ENUM_QUIRK 0x01 |
| 30 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 31 | /** |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 32 | * struct sensor_hub_data - Hold a instance data for a HID hub device |
| 33 | * @hsdev: Stored hid instance for current hub device. |
| 34 | * @mutex: Mutex to serialize synchronous request. |
| 35 | * @lock: Spin lock to protect pending request structure. |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 36 | * @dyn_callback_list: Holds callback function |
| 37 | * @dyn_callback_lock: spin lock to protect callback list |
| 38 | * @hid_sensor_hub_client_devs: Stores all MFD cells for a hub instance. |
| 39 | * @hid_sensor_client_cnt: Number of MFD cells, (no of sensors attached). |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 40 | * @ref_cnt: Number of MFD clients have opened this device |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 41 | */ |
| 42 | struct sensor_hub_data { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 43 | struct mutex mutex; |
| 44 | spinlock_t lock; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 45 | struct list_head dyn_callback_list; |
| 46 | spinlock_t dyn_callback_lock; |
| 47 | struct mfd_cell *hid_sensor_hub_client_devs; |
| 48 | int hid_sensor_client_cnt; |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 49 | unsigned long quirks; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 50 | int ref_cnt; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * struct hid_sensor_hub_callbacks_list - Stores callback list |
| 55 | * @list: list head. |
| 56 | * @usage_id: usage id for a physical device. |
| 57 | * @usage_callback: Stores registered callback functions. |
| 58 | * @priv: Private data for a physical device. |
| 59 | */ |
| 60 | struct hid_sensor_hub_callbacks_list { |
| 61 | struct list_head list; |
| 62 | u32 usage_id; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 63 | struct hid_sensor_hub_device *hsdev; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 64 | struct hid_sensor_hub_callbacks *usage_callback; |
| 65 | void *priv; |
| 66 | }; |
| 67 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 68 | static struct hid_report *sensor_hub_report(int id, struct hid_device *hdev, |
| 69 | int dir) |
| 70 | { |
| 71 | struct hid_report *report; |
| 72 | |
| 73 | list_for_each_entry(report, &hdev->report_enum[dir].report_list, list) { |
| 74 | if (report->id == id) |
| 75 | return report; |
| 76 | } |
| 77 | hid_warn(hdev, "No report with id 0x%x found\n", id); |
| 78 | |
| 79 | return NULL; |
| 80 | } |
| 81 | |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 82 | static int sensor_hub_get_physical_device_count(struct hid_device *hdev) |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 83 | { |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 84 | int i; |
| 85 | int count = 0; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 86 | |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 87 | for (i = 0; i < hdev->maxcollection; ++i) { |
| 88 | struct hid_collection *collection = &hdev->collection[i]; |
| 89 | if (collection->type == HID_COLLECTION_PHYSICAL) |
| 90 | ++count; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 93 | return count; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void sensor_hub_fill_attr_info( |
| 97 | struct hid_sensor_hub_attribute_info *info, |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 98 | s32 index, s32 report_id, struct hid_field *field) |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 99 | { |
| 100 | info->index = index; |
| 101 | info->report_id = report_id; |
Srinivas Pandruvada | 9f740ff | 2013-11-27 22:19:00 +0000 | [diff] [blame] | 102 | info->units = field->unit; |
| 103 | info->unit_expo = field->unit_exponent; |
| 104 | info->size = (field->report_size * field->report_count)/8; |
| 105 | info->logical_minimum = field->logical_minimum; |
| 106 | info->logical_maximum = field->logical_maximum; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static struct hid_sensor_hub_callbacks *sensor_hub_get_callback( |
| 110 | struct hid_device *hdev, |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 111 | u32 usage_id, |
| 112 | int collection_index, |
| 113 | struct hid_sensor_hub_device **hsdev, |
| 114 | void **priv) |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 115 | { |
| 116 | struct hid_sensor_hub_callbacks_list *callback; |
| 117 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
| 118 | |
| 119 | spin_lock(&pdata->dyn_callback_lock); |
| 120 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 121 | if (callback->usage_id == usage_id && |
| 122 | (collection_index >= |
| 123 | callback->hsdev->start_collection_index) && |
| 124 | (collection_index < |
| 125 | callback->hsdev->end_collection_index)) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 126 | *priv = callback->priv; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 127 | *hsdev = callback->hsdev; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 128 | spin_unlock(&pdata->dyn_callback_lock); |
| 129 | return callback->usage_callback; |
| 130 | } |
| 131 | spin_unlock(&pdata->dyn_callback_lock); |
| 132 | |
| 133 | return NULL; |
| 134 | } |
| 135 | |
| 136 | int sensor_hub_register_callback(struct hid_sensor_hub_device *hsdev, |
| 137 | u32 usage_id, |
| 138 | struct hid_sensor_hub_callbacks *usage_callback) |
| 139 | { |
| 140 | struct hid_sensor_hub_callbacks_list *callback; |
| 141 | struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 142 | unsigned long flags; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 143 | |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 144 | spin_lock_irqsave(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 145 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 146 | if (callback->usage_id == usage_id && |
| 147 | callback->hsdev == hsdev) { |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 148 | spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 149 | return -EINVAL; |
| 150 | } |
Dan Carpenter | 2b7c4b8 | 2012-09-14 06:53:23 +0000 | [diff] [blame] | 151 | callback = kzalloc(sizeof(*callback), GFP_ATOMIC); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 152 | if (!callback) { |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 153 | spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 154 | return -ENOMEM; |
| 155 | } |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 156 | callback->hsdev = hsdev; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 157 | callback->usage_callback = usage_callback; |
| 158 | callback->usage_id = usage_id; |
| 159 | callback->priv = NULL; |
| 160 | list_add_tail(&callback->list, &pdata->dyn_callback_list); |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 161 | spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | EXPORT_SYMBOL_GPL(sensor_hub_register_callback); |
| 166 | |
| 167 | int sensor_hub_remove_callback(struct hid_sensor_hub_device *hsdev, |
| 168 | u32 usage_id) |
| 169 | { |
| 170 | struct hid_sensor_hub_callbacks_list *callback; |
| 171 | struct sensor_hub_data *pdata = hid_get_drvdata(hsdev->hdev); |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 172 | unsigned long flags; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 173 | |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 174 | spin_lock_irqsave(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 175 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 176 | if (callback->usage_id == usage_id && |
| 177 | callback->hsdev == hsdev) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 178 | list_del(&callback->list); |
| 179 | kfree(callback); |
| 180 | break; |
| 181 | } |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 182 | spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | EXPORT_SYMBOL_GPL(sensor_hub_remove_callback); |
| 187 | |
| 188 | int sensor_hub_set_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, |
| 189 | u32 field_index, s32 value) |
| 190 | { |
| 191 | struct hid_report *report; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 192 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 193 | int ret = 0; |
| 194 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 195 | mutex_lock(&data->mutex); |
| 196 | report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 197 | if (!report || (field_index >= report->maxfield)) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 198 | ret = -EINVAL; |
| 199 | goto done_proc; |
| 200 | } |
| 201 | hid_set_field(report->field[field_index], 0, value); |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 202 | hid_hw_request(hsdev->hdev, report, HID_REQ_SET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 203 | hid_hw_wait(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 204 | |
| 205 | done_proc: |
| 206 | mutex_unlock(&data->mutex); |
| 207 | |
| 208 | return ret; |
| 209 | } |
| 210 | EXPORT_SYMBOL_GPL(sensor_hub_set_feature); |
| 211 | |
| 212 | int sensor_hub_get_feature(struct hid_sensor_hub_device *hsdev, u32 report_id, |
| 213 | u32 field_index, s32 *value) |
| 214 | { |
| 215 | struct hid_report *report; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 216 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 217 | int ret = 0; |
| 218 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 219 | mutex_lock(&data->mutex); |
| 220 | report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); |
Jiri Kosina | 4e5a494 | 2013-09-06 11:59:53 +0200 | [diff] [blame] | 221 | if (!report || (field_index >= report->maxfield) || |
Kees Cook | 9e89102 | 2013-08-28 22:31:44 +0200 | [diff] [blame] | 222 | report->field[field_index]->report_count < 1) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 223 | ret = -EINVAL; |
| 224 | goto done_proc; |
| 225 | } |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 226 | hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); |
Benjamin Tissoires | b7966a4 | 2013-02-25 11:31:47 +0100 | [diff] [blame] | 227 | hid_hw_wait(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 228 | *value = report->field[field_index]->value[0]; |
| 229 | |
| 230 | done_proc: |
| 231 | mutex_unlock(&data->mutex); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | EXPORT_SYMBOL_GPL(sensor_hub_get_feature); |
| 236 | |
| 237 | |
| 238 | int sensor_hub_input_attr_get_raw_value(struct hid_sensor_hub_device *hsdev, |
| 239 | u32 usage_id, |
| 240 | u32 attr_usage_id, u32 report_id) |
| 241 | { |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 242 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 243 | unsigned long flags; |
| 244 | struct hid_report *report; |
| 245 | int ret_val = 0; |
| 246 | |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 247 | mutex_lock(&hsdev->mutex); |
| 248 | memset(&hsdev->pending, 0, sizeof(hsdev->pending)); |
| 249 | init_completion(&hsdev->pending.ready); |
| 250 | hsdev->pending.usage_id = usage_id; |
| 251 | hsdev->pending.attr_usage_id = attr_usage_id; |
| 252 | hsdev->pending.raw_size = 0; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 253 | |
| 254 | spin_lock_irqsave(&data->lock, flags); |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 255 | hsdev->pending.status = true; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 256 | spin_unlock_irqrestore(&data->lock, flags); |
Srinivas Pandruvada | f74346a | 2014-03-24 16:25:04 -0700 | [diff] [blame] | 257 | report = sensor_hub_report(report_id, hsdev->hdev, HID_INPUT_REPORT); |
| 258 | if (!report) |
| 259 | goto err_free; |
| 260 | |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 261 | mutex_lock(&data->mutex); |
Srinivas Pandruvada | f74346a | 2014-03-24 16:25:04 -0700 | [diff] [blame] | 262 | hid_hw_request(hsdev->hdev, report, HID_REQ_GET_REPORT); |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 263 | mutex_unlock(&data->mutex); |
| 264 | wait_for_completion_interruptible_timeout(&hsdev->pending.ready, HZ*5); |
| 265 | switch (hsdev->pending.raw_size) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 266 | case 1: |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 267 | ret_val = *(u8 *)hsdev->pending.raw_data; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 268 | break; |
| 269 | case 2: |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 270 | ret_val = *(u16 *)hsdev->pending.raw_data; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 271 | break; |
| 272 | case 4: |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 273 | ret_val = *(u32 *)hsdev->pending.raw_data; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 274 | break; |
| 275 | default: |
| 276 | ret_val = 0; |
| 277 | } |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 278 | kfree(hsdev->pending.raw_data); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 279 | |
| 280 | err_free: |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 281 | hsdev->pending.status = false; |
| 282 | mutex_unlock(&hsdev->mutex); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 283 | |
| 284 | return ret_val; |
| 285 | } |
| 286 | EXPORT_SYMBOL_GPL(sensor_hub_input_attr_get_raw_value); |
| 287 | |
Srinivas Pandruvada | e02cee4 | 2014-01-23 18:50:21 -0800 | [diff] [blame] | 288 | int hid_sensor_get_usage_index(struct hid_sensor_hub_device *hsdev, |
| 289 | u32 report_id, int field_index, u32 usage_id) |
| 290 | { |
| 291 | struct hid_report *report; |
| 292 | struct hid_field *field; |
| 293 | int i; |
| 294 | |
| 295 | report = sensor_hub_report(report_id, hsdev->hdev, HID_FEATURE_REPORT); |
| 296 | if (!report || (field_index >= report->maxfield)) |
| 297 | goto done_proc; |
| 298 | |
| 299 | field = report->field[field_index]; |
| 300 | for (i = 0; i < field->maxusage; ++i) { |
| 301 | if (field->usage[i].hid == usage_id) |
| 302 | return field->usage[i].usage_index; |
| 303 | } |
| 304 | |
| 305 | done_proc: |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | EXPORT_SYMBOL_GPL(hid_sensor_get_usage_index); |
| 309 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 310 | int sensor_hub_input_get_attribute_info(struct hid_sensor_hub_device *hsdev, |
| 311 | u8 type, |
| 312 | u32 usage_id, |
| 313 | u32 attr_usage_id, |
| 314 | struct hid_sensor_hub_attribute_info *info) |
| 315 | { |
| 316 | int ret = -1; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 317 | int i; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 318 | struct hid_report *report; |
| 319 | struct hid_field *field; |
| 320 | struct hid_report_enum *report_enum; |
| 321 | struct hid_device *hdev = hsdev->hdev; |
| 322 | |
| 323 | /* Initialize with defaults */ |
| 324 | info->usage_id = usage_id; |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 325 | info->attrib_id = attr_usage_id; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 326 | info->report_id = -1; |
| 327 | info->index = -1; |
| 328 | info->units = -1; |
| 329 | info->unit_expo = -1; |
| 330 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 331 | report_enum = &hdev->report_enum[type]; |
| 332 | list_for_each_entry(report, &report_enum->report_list, list) { |
| 333 | for (i = 0; i < report->maxfield; ++i) { |
| 334 | field = report->field[i]; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 335 | if (field->maxusage) { |
| 336 | if (field->physical == usage_id && |
| 337 | (field->logical == attr_usage_id || |
| 338 | field->usage[0].hid == |
| 339 | attr_usage_id) && |
| 340 | (field->usage[0].collection_index >= |
| 341 | hsdev->start_collection_index) && |
| 342 | (field->usage[0].collection_index < |
| 343 | hsdev->end_collection_index)) { |
| 344 | |
| 345 | sensor_hub_fill_attr_info(info, i, |
| 346 | report->id, |
| 347 | field); |
| 348 | ret = 0; |
| 349 | break; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 350 | } |
| 351 | } |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 352 | } |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 353 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 354 | } |
| 355 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 356 | return ret; |
| 357 | } |
| 358 | EXPORT_SYMBOL_GPL(sensor_hub_input_get_attribute_info); |
| 359 | |
| 360 | #ifdef CONFIG_PM |
| 361 | static int sensor_hub_suspend(struct hid_device *hdev, pm_message_t message) |
| 362 | { |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 363 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 364 | struct hid_sensor_hub_callbacks_list *callback; |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 365 | unsigned long flags; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 366 | |
| 367 | hid_dbg(hdev, " sensor_hub_suspend\n"); |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 368 | spin_lock_irqsave(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 369 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) { |
| 370 | if (callback->usage_callback->suspend) |
| 371 | callback->usage_callback->suspend( |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 372 | callback->hsdev, callback->priv); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 373 | } |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 374 | spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 375 | |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int sensor_hub_resume(struct hid_device *hdev) |
| 380 | { |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 381 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 382 | struct hid_sensor_hub_callbacks_list *callback; |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 383 | unsigned long flags; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 384 | |
| 385 | hid_dbg(hdev, " sensor_hub_resume\n"); |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 386 | spin_lock_irqsave(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 387 | list_for_each_entry(callback, &pdata->dyn_callback_list, list) { |
| 388 | if (callback->usage_callback->resume) |
| 389 | callback->usage_callback->resume( |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 390 | callback->hsdev, callback->priv); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 391 | } |
Jiri Kosina | 0ccf091 | 2014-06-10 11:05:43 +0200 | [diff] [blame] | 392 | spin_unlock_irqrestore(&pdata->dyn_callback_lock, flags); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int sensor_hub_reset_resume(struct hid_device *hdev) |
| 398 | { |
| 399 | return 0; |
| 400 | } |
| 401 | #endif |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 402 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 403 | /* |
| 404 | * Handle raw report as sent by device |
| 405 | */ |
| 406 | static int sensor_hub_raw_event(struct hid_device *hdev, |
| 407 | struct hid_report *report, u8 *raw_data, int size) |
| 408 | { |
| 409 | int i; |
| 410 | u8 *ptr; |
| 411 | int sz; |
| 412 | struct sensor_hub_data *pdata = hid_get_drvdata(hdev); |
| 413 | unsigned long flags; |
| 414 | struct hid_sensor_hub_callbacks *callback = NULL; |
| 415 | struct hid_collection *collection = NULL; |
| 416 | void *priv = NULL; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 417 | struct hid_sensor_hub_device *hsdev = NULL; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 418 | |
| 419 | hid_dbg(hdev, "sensor_hub_raw_event report id:0x%x size:%d type:%d\n", |
| 420 | report->id, size, report->type); |
| 421 | hid_dbg(hdev, "maxfield:%d\n", report->maxfield); |
| 422 | if (report->type != HID_INPUT_REPORT) |
| 423 | return 1; |
| 424 | |
| 425 | ptr = raw_data; |
Andy Shevchenko | 15261f6 | 2013-08-14 11:07:08 +0300 | [diff] [blame] | 426 | ptr++; /* Skip report id */ |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 427 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 428 | spin_lock_irqsave(&pdata->lock, flags); |
| 429 | |
| 430 | for (i = 0; i < report->maxfield; ++i) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 431 | hid_dbg(hdev, "%d collection_index:%x hid:%x sz:%x\n", |
| 432 | i, report->field[i]->usage->collection_index, |
| 433 | report->field[i]->usage->hid, |
Srinivas Pandruvada | d4b1bba | 2013-10-26 10:04:09 -0700 | [diff] [blame] | 434 | (report->field[i]->report_size * |
| 435 | report->field[i]->report_count)/8); |
| 436 | sz = (report->field[i]->report_size * |
| 437 | report->field[i]->report_count)/8; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 438 | collection = &hdev->collection[ |
| 439 | report->field[i]->usage->collection_index]; |
| 440 | hid_dbg(hdev, "collection->usage %x\n", |
| 441 | collection->usage); |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 442 | |
| 443 | callback = sensor_hub_get_callback(hdev, |
| 444 | report->field[i]->physical, |
| 445 | report->field[i]->usage[0].collection_index, |
| 446 | &hsdev, &priv); |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 447 | if (!callback) { |
| 448 | ptr += sz; |
| 449 | continue; |
| 450 | } |
| 451 | if (hsdev->pending.status && hsdev->pending.attr_usage_id == |
| 452 | report->field[i]->usage->hid) { |
| 453 | hid_dbg(hdev, "data was pending ...\n"); |
| 454 | hsdev->pending.raw_data = kmemdup(ptr, sz, GFP_ATOMIC); |
| 455 | if (hsdev->pending.raw_data) |
| 456 | hsdev->pending.raw_size = sz; |
| 457 | else |
| 458 | hsdev->pending.raw_size = 0; |
| 459 | complete(&hsdev->pending.ready); |
| 460 | } |
| 461 | if (callback->capture_sample) { |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 462 | if (report->field[i]->logical) |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 463 | callback->capture_sample(hsdev, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 464 | report->field[i]->logical, sz, ptr, |
| 465 | callback->pdev); |
| 466 | else |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 467 | callback->capture_sample(hsdev, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 468 | report->field[i]->usage->hid, sz, ptr, |
| 469 | callback->pdev); |
| 470 | } |
| 471 | ptr += sz; |
| 472 | } |
| 473 | if (callback && collection && callback->send_event) |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 474 | callback->send_event(hsdev, collection->usage, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 475 | callback->pdev); |
| 476 | spin_unlock_irqrestore(&pdata->lock, flags); |
| 477 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 478 | return 1; |
| 479 | } |
| 480 | |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 481 | int sensor_hub_device_open(struct hid_sensor_hub_device *hsdev) |
| 482 | { |
| 483 | int ret = 0; |
| 484 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
| 485 | |
| 486 | mutex_lock(&data->mutex); |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 487 | if (!data->ref_cnt) { |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 488 | ret = hid_hw_open(hsdev->hdev); |
| 489 | if (ret) { |
| 490 | hid_err(hsdev->hdev, "failed to open hid device\n"); |
| 491 | mutex_unlock(&data->mutex); |
| 492 | return ret; |
| 493 | } |
| 494 | } |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 495 | data->ref_cnt++; |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 496 | mutex_unlock(&data->mutex); |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(sensor_hub_device_open); |
| 501 | |
| 502 | void sensor_hub_device_close(struct hid_sensor_hub_device *hsdev) |
| 503 | { |
| 504 | struct sensor_hub_data *data = hid_get_drvdata(hsdev->hdev); |
| 505 | |
| 506 | mutex_lock(&data->mutex); |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 507 | data->ref_cnt--; |
| 508 | if (!data->ref_cnt) |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 509 | hid_hw_close(hsdev->hdev); |
| 510 | mutex_unlock(&data->mutex); |
| 511 | } |
| 512 | EXPORT_SYMBOL_GPL(sensor_hub_device_close); |
| 513 | |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 514 | static __u8 *sensor_hub_report_fixup(struct hid_device *hdev, __u8 *rdesc, |
| 515 | unsigned int *rsize) |
| 516 | { |
| 517 | int index; |
| 518 | struct sensor_hub_data *sd = hid_get_drvdata(hdev); |
| 519 | unsigned char report_block[] = { |
| 520 | 0x0a, 0x16, 0x03, 0x15, 0x00, 0x25, 0x05}; |
| 521 | unsigned char power_block[] = { |
| 522 | 0x0a, 0x19, 0x03, 0x15, 0x00, 0x25, 0x05}; |
| 523 | |
| 524 | if (!(sd->quirks & HID_SENSOR_HUB_ENUM_QUIRK)) { |
| 525 | hid_dbg(hdev, "No Enum quirks\n"); |
| 526 | return rdesc; |
| 527 | } |
| 528 | |
| 529 | /* Looks for power and report state usage id and force to 1 */ |
| 530 | for (index = 0; index < *rsize; ++index) { |
| 531 | if (((*rsize - index) > sizeof(report_block)) && |
| 532 | !memcmp(&rdesc[index], report_block, |
| 533 | sizeof(report_block))) { |
| 534 | rdesc[index + 4] = 0x01; |
| 535 | index += sizeof(report_block); |
| 536 | } |
| 537 | if (((*rsize - index) > sizeof(power_block)) && |
| 538 | !memcmp(&rdesc[index], power_block, |
| 539 | sizeof(power_block))) { |
| 540 | rdesc[index + 4] = 0x01; |
| 541 | index += sizeof(power_block); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return rdesc; |
| 546 | } |
| 547 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 548 | static int sensor_hub_probe(struct hid_device *hdev, |
| 549 | const struct hid_device_id *id) |
| 550 | { |
| 551 | int ret; |
| 552 | struct sensor_hub_data *sd; |
| 553 | int i; |
| 554 | char *name; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 555 | int dev_cnt; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 556 | struct hid_sensor_hub_device *hsdev; |
| 557 | struct hid_sensor_hub_device *last_hsdev = NULL; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 558 | |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 559 | sd = devm_kzalloc(&hdev->dev, sizeof(*sd), GFP_KERNEL); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 560 | if (!sd) { |
| 561 | hid_err(hdev, "cannot allocate Sensor data\n"); |
| 562 | return -ENOMEM; |
| 563 | } |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 564 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 565 | hid_set_drvdata(hdev, sd); |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 566 | sd->quirks = id->driver_data; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 567 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 568 | spin_lock_init(&sd->lock); |
| 569 | spin_lock_init(&sd->dyn_callback_lock); |
| 570 | mutex_init(&sd->mutex); |
| 571 | ret = hid_parse(hdev); |
| 572 | if (ret) { |
| 573 | hid_err(hdev, "parse failed\n"); |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 574 | return ret; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 575 | } |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 576 | INIT_LIST_HEAD(&hdev->inputs); |
| 577 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 578 | ret = hid_hw_start(hdev, 0); |
| 579 | if (ret) { |
| 580 | hid_err(hdev, "hw start failed\n"); |
Andy Shevchenko | 905cc19 | 2013-08-14 11:07:10 +0300 | [diff] [blame] | 581 | return ret; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 582 | } |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 583 | INIT_LIST_HEAD(&sd->dyn_callback_list); |
| 584 | sd->hid_sensor_client_cnt = 0; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 585 | |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 586 | dev_cnt = sensor_hub_get_physical_device_count(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 587 | if (dev_cnt > HID_MAX_PHY_DEVICES) { |
| 588 | hid_err(hdev, "Invalid Physical device count\n"); |
| 589 | ret = -EINVAL; |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 590 | goto err_stop_hw; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 591 | } |
Himangi Saraogi | 5be5db2 | 2014-08-03 03:41:35 +0530 | [diff] [blame] | 592 | sd->hid_sensor_hub_client_devs = devm_kzalloc(&hdev->dev, dev_cnt * |
| 593 | sizeof(struct mfd_cell), |
| 594 | GFP_KERNEL); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 595 | if (sd->hid_sensor_hub_client_devs == NULL) { |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 596 | hid_err(hdev, "Failed to allocate memory for mfd cells\n"); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 597 | ret = -ENOMEM; |
Srinivas Pandruvada | 1df3a40 | 2013-09-18 18:13:00 +0100 | [diff] [blame] | 598 | goto err_stop_hw; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 599 | } |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 600 | |
| 601 | for (i = 0; i < hdev->maxcollection; ++i) { |
| 602 | struct hid_collection *collection = &hdev->collection[i]; |
| 603 | |
| 604 | if (collection->type == HID_COLLECTION_PHYSICAL) { |
| 605 | |
Himangi Saraogi | 5be5db2 | 2014-08-03 03:41:35 +0530 | [diff] [blame] | 606 | hsdev = devm_kzalloc(&hdev->dev, sizeof(*hsdev), |
| 607 | GFP_KERNEL); |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 608 | if (!hsdev) { |
| 609 | hid_err(hdev, "cannot allocate hid_sensor_hub_device\n"); |
| 610 | ret = -ENOMEM; |
Himangi Saraogi | 5be5db2 | 2014-08-03 03:41:35 +0530 | [diff] [blame] | 611 | goto err_stop_hw; |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 612 | } |
| 613 | hsdev->hdev = hdev; |
| 614 | hsdev->vendor_id = hdev->vendor; |
| 615 | hsdev->product_id = hdev->product; |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 616 | hsdev->usage = collection->usage; |
| 617 | mutex_init(&hsdev->mutex); |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 618 | hsdev->start_collection_index = i; |
| 619 | if (last_hsdev) |
| 620 | last_hsdev->end_collection_index = i; |
| 621 | last_hsdev = hsdev; |
Himangi Saraogi | 5be5db2 | 2014-08-03 03:41:35 +0530 | [diff] [blame] | 622 | name = devm_kasprintf(&hdev->dev, GFP_KERNEL, |
| 623 | "HID-SENSOR-%x", |
| 624 | collection->usage); |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 625 | if (name == NULL) { |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 626 | hid_err(hdev, "Failed MFD device name\n"); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 627 | ret = -ENOMEM; |
Himangi Saraogi | 5be5db2 | 2014-08-03 03:41:35 +0530 | [diff] [blame] | 628 | goto err_stop_hw; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 629 | } |
| 630 | sd->hid_sensor_hub_client_devs[ |
| 631 | sd->hid_sensor_client_cnt].name = name; |
| 632 | sd->hid_sensor_hub_client_devs[ |
| 633 | sd->hid_sensor_client_cnt].platform_data = |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 634 | hsdev; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 635 | sd->hid_sensor_hub_client_devs[ |
| 636 | sd->hid_sensor_client_cnt].pdata_size = |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 637 | sizeof(*hsdev); |
| 638 | hid_dbg(hdev, "Adding %s:%d\n", name, |
| 639 | hsdev->start_collection_index); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 640 | sd->hid_sensor_client_cnt++; |
| 641 | } |
| 642 | } |
Srinivas Pandruvada | ca2ed12 | 2014-01-31 12:04:10 -0800 | [diff] [blame] | 643 | if (last_hsdev) |
| 644 | last_hsdev->end_collection_index = i; |
| 645 | |
Johan Hovold | 16b5fe2 | 2014-09-26 12:55:32 +0200 | [diff] [blame] | 646 | ret = mfd_add_hotplug_devices(&hdev->dev, |
| 647 | sd->hid_sensor_hub_client_devs, |
| 648 | sd->hid_sensor_client_cnt); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 649 | if (ret < 0) |
Himangi Saraogi | 5be5db2 | 2014-08-03 03:41:35 +0530 | [diff] [blame] | 650 | goto err_stop_hw; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 651 | |
| 652 | return ret; |
| 653 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 654 | err_stop_hw: |
| 655 | hid_hw_stop(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 656 | |
| 657 | return ret; |
| 658 | } |
| 659 | |
| 660 | static void sensor_hub_remove(struct hid_device *hdev) |
| 661 | { |
| 662 | struct sensor_hub_data *data = hid_get_drvdata(hdev); |
| 663 | unsigned long flags; |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 664 | int i; |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 665 | |
| 666 | hid_dbg(hdev, " hardware removed\n"); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 667 | hid_hw_close(hdev); |
Axel Lin | f2f13a6 | 2012-09-19 16:30:00 +0100 | [diff] [blame] | 668 | hid_hw_stop(hdev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 669 | spin_lock_irqsave(&data->lock, flags); |
Srinivas Pandruvada | e651a1d | 2015-02-19 15:31:25 -0800 | [diff] [blame^] | 670 | for (i = 0; i < data->hid_sensor_client_cnt; ++i) { |
| 671 | struct hid_sensor_hub_device *hsdev = |
| 672 | data->hid_sensor_hub_client_devs[i].platform_data; |
| 673 | if (hsdev->pending.status) |
| 674 | complete(&hsdev->pending.ready); |
| 675 | } |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 676 | spin_unlock_irqrestore(&data->lock, flags); |
| 677 | mfd_remove_devices(&hdev->dev); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 678 | hid_set_drvdata(hdev, NULL); |
| 679 | mutex_destroy(&data->mutex); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static const struct hid_device_id sensor_hub_devices[] = { |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 683 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_0, |
Stephen Chandler Paul | 30f5887 | 2014-04-03 16:27:55 -0400 | [diff] [blame] | 684 | USB_DEVICE_ID_INTEL_HID_SENSOR_0), |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 685 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
| 686 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1, |
Stephen Chandler Paul | 30f5887 | 2014-04-03 16:27:55 -0400 | [diff] [blame] | 687 | USB_DEVICE_ID_INTEL_HID_SENSOR_0), |
| 688 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
| 689 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_INTEL_1, |
| 690 | USB_DEVICE_ID_INTEL_HID_SENSOR_1), |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 691 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
Reyad Attiyat | 00478ee | 2014-05-27 02:29:10 -0500 | [diff] [blame] | 692 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT, |
| 693 | USB_DEVICE_ID_MS_SURFACE_PRO_2), |
| 694 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
| 695 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT, |
| 696 | USB_DEVICE_ID_MS_TOUCH_COVER_2), |
| 697 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
| 698 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_MICROSOFT, |
| 699 | USB_DEVICE_ID_MS_TYPE_COVER_2), |
| 700 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
Archana Patni | 218eb9e | 2014-02-03 12:44:16 +0530 | [diff] [blame] | 701 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0, |
Srinivas Pandruvada | 467669c | 2014-09-03 16:00:54 -0700 | [diff] [blame] | 702 | USB_DEVICE_ID_STM_HID_SENSOR), |
| 703 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
| 704 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_STM_0, |
Archana Patni | dde3b45 | 2014-06-02 07:40:49 -0400 | [diff] [blame] | 705 | USB_DEVICE_ID_STM_HID_SENSOR_1), |
Archana Patni | 218eb9e | 2014-02-03 12:44:16 +0530 | [diff] [blame] | 706 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
Peter F. Patel-Schneider | 825747b | 2014-05-05 15:55:32 -0700 | [diff] [blame] | 707 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, USB_VENDOR_ID_TEXAS_INSTRUMENTS, |
| 708 | USB_DEVICE_ID_TEXAS_INSTRUMENTS_LENOVO_YOGA), |
| 709 | .driver_data = HID_SENSOR_HUB_ENUM_QUIRK}, |
Mika Westerberg | 0d1e4b0 | 2013-02-11 12:31:19 +0200 | [diff] [blame] | 710 | { HID_DEVICE(HID_BUS_ANY, HID_GROUP_SENSOR_HUB, HID_ANY_ID, |
| 711 | HID_ANY_ID) }, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 712 | { } |
| 713 | }; |
| 714 | MODULE_DEVICE_TABLE(hid, sensor_hub_devices); |
| 715 | |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 716 | static struct hid_driver sensor_hub_driver = { |
| 717 | .name = "hid-sensor-hub", |
| 718 | .id_table = sensor_hub_devices, |
| 719 | .probe = sensor_hub_probe, |
| 720 | .remove = sensor_hub_remove, |
| 721 | .raw_event = sensor_hub_raw_event, |
Srinivas Pandruvada | 875e36f | 2014-01-14 09:30:20 -0800 | [diff] [blame] | 722 | .report_fixup = sensor_hub_report_fixup, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 723 | #ifdef CONFIG_PM |
| 724 | .suspend = sensor_hub_suspend, |
Andy Shevchenko | 5902fde | 2013-08-14 11:07:09 +0300 | [diff] [blame] | 725 | .resume = sensor_hub_resume, |
| 726 | .reset_resume = sensor_hub_reset_resume, |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 727 | #endif |
| 728 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 729 | module_hid_driver(sensor_hub_driver); |
srinivas pandruvada | 401ca24 | 2012-09-05 13:56:00 +0100 | [diff] [blame] | 730 | |
| 731 | MODULE_DESCRIPTION("HID Sensor Hub driver"); |
| 732 | MODULE_AUTHOR("Srinivas Pandruvada <srinivas.pandruvada@intel.com>"); |
| 733 | MODULE_LICENSE("GPL"); |