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