hid-sensor-hub.c 21 KB

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