hid-sensor-hub.c 21 KB

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