hid-sensor-hub.c 20 KB

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